Poor Bool

zhaozj2021-02-17  50

Poor Bool

Author: Jim Hyslop and Herb Sutter translation: oversense <17:33 2002-10-21> Source: http://www.cuj.com/experts/2011/hyslop.htm?topic=experts

Oh ... Today's live is more cool! I wrote a broken program a few days ago, and I will get it today. Hey, really sleepy! Drink some coffee, leaning on my small chair, look at my code ...

Magical, this is it? Void f () {TextHandler T; T.sendtext ("Hello, World", True); // ...}

What is the TRUE? Turn over the definition:

Class TextHandler {public: void sendtext (const st: string & msg, bool sendnewline); // ...};

Drink some Coffee, I remembered, True said that the Sendtext function automatically adds a carriage return, how do I forget? Is it stupid? I caught confused meditation ...

boom! ! ! A loud noise, I panic, the coffee in my mouth is almost sprayed out, and I am tight. It must be Guru to close her big book. I turned to her and squeezed a little smile. Her hands holding a booklet for less than 100 pages. Magical, such a small book makes so much, how did she get it?

Obviously, she knows everything.

"My baby! If you have forgotten the meaning of the parameters so soon, when other programs first look at your code, he understands what you want to express?"

"Well, yeah" I "I 咕道" but in the IDE, he just wants to move the mouse to the function, he can see the parameter! "

"Some IDE is so, not all, even not most! I said many times, the main use of the source code is used to exchange, the intention of the intention. I am in my hands, the old, distinguished volume elaborate The art of communication. Here, it wrote, 'uses clear, detailed, specific language' [1], the BOOL in your code is not able to pass any useful information to readers. "

"No, once he knows what this bool means, it is easy to remember!" I said that I am inadequate.

Guru stared at me with her beautiful and firm blue-eyed. I pounced in my heart.

"How long have you been written before?" Guru said very gentle - that kind of gentleness I like.

"Well, good, um, how to correct it?" I cleverly avoid her problems.

"You can't find another way to express your intent?" She didn't answer me directly, we would seem to be in the fried egg.

"I can do not have the second parameter, let the user add '/ n' good." I said while I wrote:

{T.sendtext ("Hello, World / N");

"If it is passed to sendText is a variable?" Guru asked.

"That's so good"

{T.sendtext (variable); T.sendtext ("/ n");}

I looked up and saw that Guru's face was not good, and said:

"That's this, I offer two functions" Void SendText (const st :: string &); void sendtextwithnewline (const std :: string&);

"Is there no other way?" When Guru thought, I watched slightly.

Hey, it seems that I don't want to easily walk today. I am so impressed ... I didn't want to come out, I surrendered, "Just like this!"

"About you, you have to know a little" Guru wrote:

Void Displaytext (Const std :: string &, bool applyitalics, bool applybold);

Void f () {Displaytext ("this is bold but not italic", true, false;

"If a programmer wants to display text with italics, it makes the wrong parameter order, then these texts are displayed with bold, and it is clear that the compiler cannot discover this error."

"If Bob gets this code, change the order of parameters, a technology called 'permute and baffle' [2]. Will it?"

"Obviously, there are a lot of displaytext can't get 'clear, detailed, specific' parameters."

"Now you look at your problem, your first program, because Displaytext requires other parameters and not use. Your second program, you can work, but if Displaytext requires a lot of information, such as color, font, etc., you are Isn't it necessary to provide so many functions? "

"So, we can use enumerated."

"ENUMERATIONS?" I strangely said.

"Yes, Enumerations can get a good application here," Class TextHandler {public: enum newlinedisposition {sendnewline, nonewline}; void sendtext (const st: string &, newlinedisposition);

Void f () {TextHandler T; T.Sendtext ("Hello,", TextHandler :: Nonewline); T.sendtext ("World", TextHandler :: SendNewline);

"This kind of writing is very good, this code is now self-docuumenting, no comments, intent and results are clear. It doesn't need to find the definition of functions."

"And, this kind of writing is good, if you need to add a carriage return, you only need to add PrependNewline in enumeration, and existing code does not need any changes."

"Don't abandon you the first program, baby. In some cases, he is the best." Guru turned, re-opened the book, elegant and light, disappeared at the corner.

Note: [1] William Strunk Jr. And E.B. White. The Elements of Style (Macillan Publishing Co. Ltd, 1979).

[2] from ooedy green's "how to write unmainable code,"

. (Primarily Aimed At Java Program), IT STILL HAS

Lots of Relevance for C Programmers.)

转载请注明原文地址:https://www.9cbs.com/read-30502.html

New Post(0)