Dialog #28: contracts, promises, and mere Semantics

Source: Internet
Author: User
Tags coding standards
Dialog #28: contracts, promises, and mere Semantics

Like most days, I started my work that day-with fresh coffee in my square room, I was receiving morning emails before I started writing code. Very strange. On this day, guru did not suddenly appear behind me. In fact, I accidentally heard it happen to another person.

I was sitting in front of my desk quietly working, and heard the time from my colleagues nearby to press the keys.

"It's time," guru said. "Let's talk about something ."

I jumped up habitually. I looked around, but guru didn't stand behind me. Then I heard the Chair scream from the next room-it's Kerry, and we're a clever and annoying new guy who jumped up from his chair. She is with him. I doubt how long it will last; Guru is famous for scaring away a vast majority of new people in the first few months and cannot wait until he becomes right. Kerry has early symptoms of crash. I guess he will have to stick to it for a few more weeks.

I listened with a little guilt:

"Bob told me that there is a problem in my new code ......" I heard Kerry start to resist.

"Are you, my child ?" I was able to figure out how she raised her eyebrows and smiled at me.

"Well ...... Yes. It's about coding standards. I will handle it myself ......"

I heard the sound of closed books. Guru must have closed the brick she brought ". I am too reluctant to guess which one she brought today. "Tell me, young," I heard guru say to Kerry. "What do we say about our coding specifications? Let's talk about it ."

"Well ...... They are here ." While the Kerry was hitting the key, I also quietly called out the same file and found the location where he was reciting it. This is part of it:

When declaring the parameter type, it is best to follow the traditional C ++ Interface Usage:

LIf the real parameter needs to be changed, pass the pointer.

LIf the real parameter is not changed, pass the value.

"I didn't do this, so Bob pointed out it. That's all. This is correct. This is correct; I will modify it; really, "Kerry said, and it sounds very nervous.

There was a pause and I was guessing that guru was smiling or frowning. Finally, she said, "Do what he says ...... Now. Update the code, but do not use Chek in. Instead, he gave Bob the updated code, asked him to view it, and asked him to use it by himself, and then waited ."

"Waiting ?...... Why ?" Kerry asked.

This time I'm sure Guru is smiling. "Just wait, apprentice," she said. Then I heard the sand and sound of the paper and the footsteps of the retreat.

The performance was over. I thought, so I went back to work. But the most exciting part is always staged ......

A few hours later, when I was writing code, I heard Kerry be called to Bob's office desk. Although it was a little far away, it was not so far that I could not hear Bob crazy about Kerry.

After a while, Bob seemed to have enough training. Kerry was put back and went back to his room alone (I think. When he sat down, the Chair rang, and then it became silent ...... But it does not last long.

About five minutes later, the guru sound was coming again, and the Kerry chair squashed again (or from Kerry? He is used to making a buzz when guru appears.) Another conversation:

"About why C language is hot," guru suddenly said, "And does pig have wings ." I laughed at the next sentence, knowing that she might be talking about Bob.

(SQUEAKING) "I ...... I told Bob ......"

"You did, kid, I can hear it all over the building," said guru. There was another sound of close books. "And what did he say ?"

"My code must be wrong; it makes his code unable to work," Kerry students. "But I don't know what I did wrong. I did what he said! I did according to the encoding rules !"

"Ah,"-Guru smiled sharply in front of me-"but they are the same thing. Because Bob is one of the people who develop this coding specification ."

"…… He ...... He is ?"

"Oh, yes. He is indeed an apprentice. This is why the encoding specification is incorrect ."

"Error ?"

"That is wrong for C ++ usage," she continued.

"Although, it is correct for C languages without 'quota ."

"...... If it is wrong, why don't they update it ?"

"Our new manager, Pete Williams, is too trusting Bob to see what he actually needs," she sighed. "His predecessor did not. Therefore, the encoding specification is incorrect. The usage of C ++ is more like this ......" I heard the sound of a writing pen on the whiteboard, which I later saw on the whiteboard:

When declaring the parameter type, it is best to follow the traditional C ++ Interface Usage unless it is not appropriate:

LIf the real parameter needs to be changed, pass a non-const pointer or reference.

LIf the real parameter is not changed, pass the reference of the const, or pass the value when the copy overhead is very small.

"Such a statement is close to the fact," guru concluded. "However, apprentice, you must pay attention to the two words" best "and" unless. This is just a usage. Not a dogma. It is not without exceptions ."

"Well ...... Yes. No problem. Bob says it applies to everything he has tried, ......"

"Auto_ptr object," guru nodded and said for him. I heard her continue to write on the whiteboard.

Kerry was surprised. "How do you know! Bob told you? What you saw ?"

"I just know. I don't need to see it ." She continued to write on the whiteboard, which I saw a few minutes later:

Template <typename T>

Void mutate (T * bypointer );

Template <typename T>

Void mutate (T & byreference );

Template <typename T>

Void leavealone (const T * bypointertoconst );

Template <typename T>

Void leavealone (const T & byreferencetoconst );

Template <typename T>

Void leavealone (T byvalue );

"Let's take a look at this example and then answer: for which type of T, the name of the function is a real name? Apprentice !" She said.

I don't know what I'm talking about until I hear a different click, and an eraser bounce from my head. Then I quickly responded and rubbed my head and joined them.

"Well? What ?" I muttered a sentence. "I am busy with my work ." "In fact, you are focusing on your ears ." Guru squinted for a while and pretended to be worried. Then I saw her blinking eyes. After she put down her pen and writing, she opened her thick book and hung her hair behind her ears. Then she smiled and walked away. "My apprentice will explain, my child ......" Her voice floated from her shoulder, just as the moment she turned and disappeared.

I shook my head and grinned. What behavior! But Kerry looks at me strangely. I suspect that he hasn't begun to put guru and I together in the "magic man" list. I don't know. If he does this, it gives me an interesting inspiration.

"Young man," I said with a smile. I laughed even more when I realized what I just said. "What will happen when you pass an auto_ptr ?"

"Well ...... Oh !" Clearly, Kerry understands. "Ownership passed! Is that what she meant ?"

"Yes," I agree. "You already understand. The same thing also happens in all classes that will transfer ownership-by the way, its typical feature is to use non-const references on the form parameters of the copy constructor. This is the answer you need: passing a value usually means that you will not touch the source object, except auto_ptr, which has the opposite meaning-and worse, because not only the source auto_ptr is modified, it is also set to null. Passing a pointer often means preparing to modify the source object, except for auto_ptr, which has the opposite meaning-you are avoiding modifying it. For auto_ptr, traditional usage is doing bad things. This is exactly the reason why the container cannot work with auto_ptr because of the common usage code, such as the assumption of the objects they contain. This is not only true for auto_ptr, but also for all classes that pass ownership during copy. Always pay special attention to the non-const reference parameters in the copy constructor; that is what you usually miss out on ."

"Now," as I walked, my voice floated behind me. "I have to go back and continue my meditation ." I reached the end of the aisle and turned to a bend. As soon as I disappeared from his view of vision, I couldn't help laughing.

[References]

[1] H. Suter and J. hyslop. "Conversations: A Midsummer Night's madness ,"C/C ++ users Journal C ++ Experts Forum, August 2002.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.