Comment On Self-Documenting Comments

In yesterday's post (Doing What You Say, Saying What You Do), we all learned how important it is to be explicit when naming functions; preferably, one should encapsulate the entire function's logic into that function's name. Although I didn't cover commenting in yesterday's post, the same rule should apply: be as explicit as possible. The ideal comments are free of any non-programming languages (English, Dutch, etc) and should be as verbatim to the code as possible. Nick F demonstrates this perfectly with code from (what I would imagine is) the set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false() function ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Self-Documenting Comments

2005-11-01 13:46 • by John
FIRST!

Re: Self-Documenting Comments

2005-11-01 13:47 • by WTFer
I just hope that is automatically generated code. Although I don't think computers can be that stupid by themselves.

Re: Self-Documenting Comments

2005-11-01 13:50 • by kipthegreat
Alex Papadimoulis:

In yesterday's post (Doing What You Say, Saying What You Do),
we all learned how important it is to be explicit when naming
functions; preferably, one should encapsulate the entire function's
logic into that function's name. Although I didn't cover commenting in
yesterday's post, the same rule should apply: be as explicit as
possible. The ideal comments are free of any non-programming languages
(English, Dutch, etc) and should be as verbatim to the code as
possible. Nick F demonstrates this perfectly with code from
(what I would imagine is) the
set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false()
function ...


// if hasSiblings, actionType == cont; else actionType == end.

if ($hasSiblings)
{
$actionType = "cont";
}
else
{
$actionType = "end";
}




Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...

Re: Self-Documenting Comments

2005-11-01 13:51 • by Mung Kee
48969 in reply to 48964
Anonymous:
FIRST!




"Yay, I got mail!"

Re: Self-Documenting Comments

2005-11-01 13:51 • by iAmNotACantalope
const int CONSTANT_WITH_VALUE_ZERO = (int)0;
const int CONSTANT_WITH_VALUE_ONE_GREATER_THAN_ZERO = (int)1 + (int)CONSTANT_WITH_VALUE_ZERO;

// if_verbosity == tedious then exit_program else exit_program_scarcastically
if_if_verbosity_is_tedious_exit_program_else_exit_program_scarcastically( bool true_or_false_value_of_verbosity )
{
    if( true_or_false_value_of_verbosity == true )
    {
  exit_program_and_stop_executing_program_and_return_control_to_operating_system_____tech_savvy_people_might_call_it_an_os_comma_you _should_be_aware_of_that_fact( CONSTANT_WITH_VALUE_ZERO );
    }
    else
    {
    std::cout << "Ha! That's right, verbosity isn't tedious!" << std::endl;
exit_program_and_stop_executing_program_and_return_control_to_operating_system_____tech_savvy_people_might_call_it_an_os_comma_you
_should_be_aware_of_that_fact( CONSTANT_WITH_VALUE_ONE_GREATER_THAN_ZERO );

    }
}

Oh, I hope this formats!

Re: Self-Documenting Comments

2005-11-01 13:52 • by Mung Kee
48971 in reply to 48968
kipthegreat:




Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...




Nope, I have had a love affair with the ternary for quite some time.

Re: Self-Documenting Comments

2005-11-01 13:55 • by Apoch
48972 in reply to 48971

The ternary operator is a beautiful thing. Heck, it even has a cool name.

Re: Self-Documenting Comments

2005-11-01 13:56 • by Anonymous
48973 in reply to 48965
WTFer:
I just hope that is automatically generated code.
Although I don't think computers can be that stupid by themselves.




I wish.  Here's a small snippet from a project I got sucked into.  Every line of code is commented like this.



    ' Instantiate the file system object

    Set lfsoFileSystem = CreateObject(FILE_SYSTEM)

   

    If lfsoFileSystem.FileExists(rstrFile) Then

        ' Retrieve the file

        Set lfilFile = lfsoFileSystem.GetFile(rstrFile)

        ' Open the file as a stream

        Set mtsActiveStream = lfilFile.OpenAsTextStream(rlngMode)

        ' Hold the file

        Set mfilActiveFile = lfilFile

        ' Release the file

        Set lfilFile = Nothing

        ' Hold that the operation was successful

        llngSuccess = SUCCESS

    Else



Re: Self-Documenting Comments

2005-11-01 13:59 • by procyon112
48975 in reply to 48968
tertiary is not neccissarily bad.  I use it in lots of situations,
but not generally for the case you site.  I use it whenever I'm
doing the same thing with a slightly different parameter.  ie:



function_call( var1, var2, is_blah() ? varX : varY);



instead of:



if(is_blah())

  function_call(var1, var2, varX);

else

  function_call(var1, var2, varY);



Re: Self-Documenting Comments

2005-11-01 14:00 • by Ytram
48976 in reply to 48964
Anonymous:
FIRST!




TENTH!!!



Seriously, we need to implement something similar to Fark.com, someone
that posts "First Post!" gets the text of their comment changed to
"Boobies!" and the time stamp on it changes to 12 hours into the future.

Re: Self-Documenting Comments

2005-11-01 14:03 • by Ytram
48977 in reply to 48968
kipthegreat:




Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...




Really?  I hadn't ever heard that the ternary operator is bad
practice.  I wonder what the justification for that blanket
statement is?  Anyone know?



I know it was confusing the first time I saw one, but once I figured it out I was like "Neato!" and "First Post!!!!1111".

Re: Self-Documenting Comments

2005-11-01 14:08 • by Madge O'Reene
48978 in reply to 48973
Meh, when I write pseudocode like that and go back later to make it into real code. I often leave the comments.

Re: Self-Documenting Comments

2005-11-01 14:10 • by Schol-R-LEA
48979 in reply to 48977
New programmers are often warned against using the ternary operator because they often don't get it - they think of it as a shortcut for 'if', not as an operator returning a value. Also, it does have some potential for obfuscation if used carelessly - but then, what doesn't?

Needless to say, I like it myself, but I try to be careful where and when I use it.

Re: Self-Documenting Comments

2005-11-01 14:17 • by John Smallberries
48980 in reply to 48968
kipthegreat:




$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...


You forgot the comment:

//$actionType = $hasSiblings ? "cont" : "end";

$actionType = $hasSiblings ? "cont" : "end";

Re: Self-Documenting Comments

2005-11-01 14:18 • by ferrengi
48981 in reply to 48978
Anonymous:
Meh, when I write pseudocode like that and go
back later to make it into real code. I often leave the
comments.




You need to get one of those "The Cheat" pictures as your avatar.

Re: Self-Documenting Comments

2005-11-01 14:21 • by A Wizard A True Star
48982 in reply to 48979

Oh, come on. Get off your lazy asses and type an if/then/else like a normal human being. No one wants to maintain an if block that's all smashed together on one line just because you thought it was 'leet.


 

Re: Self-Documenting Comments

2005-11-01 14:27 • by ferrengi
Alex Papadimoulis:

In yesterday's post (Doing What You Say, Saying What You Do),
we all learned how important it is to be explicit when naming
functions; preferably, one should encapsulate the entire function's
logic into that function's name. Although I didn't cover commenting in
yesterday's post, the same rule should apply: be as explicit as
possible. The ideal comments are free of any non-programming languages
(English, Dutch, etc) and should be as verbatim to the code as
possible. Nick F demonstrates this perfectly with code from
(what I would imagine is) the
set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false()
function ...


// if hasSiblings, actionType == cont; else actionType == end.

if ($hasSiblings)
{
$actionType = "cont";
}
else
{
$actionType = "end";
}




So what is so bad about this? Granted, the comment does nothing to
explain what the coding is doing but it's no worse than people who do
not write any comments at all. The comments in the code can be pretty
funny or strange at times but it's a little bit of stretch to spend an
entire post making fun of one useless comment.

This programmer probably has some interesting code. Couldn't the submitter have found something funnier than this?



- Dan

Re: Self-Documenting Comments

2005-11-01 14:30 • by ferrengi
48984 in reply to 48968
kipthegreat:


Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...




There is nothing bad about it. It is really a matter of taste. I happen
not to like it. I imagine it would be more difficult for me to follow
than the standard if statement once it gets to be a nested if.



- Dan

Re: Self-Documenting Comments

2005-11-01 14:33 • by fregas
48985 in reply to 48983

Not really a WTF at all...just kind of stupid and pointless.


Oh, and I like ternary for squishing an if statement all on one line.

Re: Self-Documenting Comments

2005-11-01 14:33 • by Manni
48987 in reply to 48968
kipthegreat:
Alex Papadimoulis:

In yesterday's post (Doing What You Say, Saying What You Do), we all learned how important it is to be explicit when naming functions; preferably, one should encapsulate the entire function's logic into that function's name. Although I didn't cover commenting in yesterday's post, the same rule should apply: be as explicit as possible. The ideal comments are free of any non-programming languages (English, Dutch, etc) and should be as verbatim to the code as possible. Nick F demonstrates this perfectly with code from (what I would imagine is) the set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false() function ...



// if hasSiblings, actionType == cont; else actionType == end.

if ($hasSiblings)
{
$actionType = "cont";
}
else
{
$actionType = "end";
}



Not really related to the WTF at all, but something I've been wondering:  Am I the only one who likes the ternary ?: operator?  It is usually grouped with global variables and goto statements as "bad no matter what."

$actionType = $hasSiblings ? "cont" : "end";

That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...


I haven't tried the ternary operator this way in C-based languages, but in VB it's dangerous. You can use the IIf( ) function, which truly is a function and not an operator.


answer = IIf(condition, truepart, falsepart)


If you put function names in the truepart and falsepart, it will execute both functions regardless of the condition, and then try to take the return value of the appropriate one and apply it to "answer". So don't think that it's a direct replacement for:


If condition
 answer = truepart
Else
 answer = falsepart
End If

Re: Self-Documenting Comments

2005-11-01 14:34 • by Cloak

Turnary operators can be pretty useful though.


printf("There is %d %s of foo\n", cnt, ((cnt == 1) ? "instance" : "instances"));


Without:


if (cnt==1)


   printf("There is 1 instance of foo\n");


else


   printf("There is %d instances of foo\n", cnt);


 


Is that really that unmaintainable?

Re: Self-Documenting Comments

2005-11-01 14:34 • by NewbieCoder
48989 in reply to 48983
I'm suprised nobody has pointed out the syntax error in the comments
themselves...it's using the boolean operator "==" rather then the
assignment one...the comment SHOULD be:



//if hasSiblings actionType = cont, else ActionType = end.



I also removed the extra "," have hasSiblings and the obviously
incorrect semi-colon.  Don't you know your comments have to
compile!?



(Note:  I do realize that they may have used the boolean operator
on purpose (as in "then actionType == cont is true"), if this is the
case it's obviously someone being silly, or strung out on a caffeine
binge)

Re: Self-Documenting Comments

2005-11-01 14:38 • by Satanicpuppy
48991 in reply to 48968
kipthegreat:
Alex Papadimoulis:

In yesterday's post (Doing What You Say, Saying What You Do),
we all learned how important it is to be explicit when naming
functions; preferably, one should encapsulate the entire function's
logic into that function's name. Although I didn't cover commenting in
yesterday's post, the same rule should apply: be as explicit as
possible. The ideal comments are free of any non-programming languages
(English, Dutch, etc) and should be as verbatim to the code as
possible. Nick F demonstrates this perfectly with code from
(what I would imagine is) the
set_actionType_to_cont_or_end_depending_on_whether_hasSiblings_is_true_or_false()
function ...


// if hasSiblings, actionType == cont; else actionType == end.

if ($hasSiblings)
{
$actionType = "cont";
}
else
{
$actionType = "end";
}




Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...




I usually try and stay away from it because people who come along later
always make the same grunting puzzled sound when they see it
"Uhhaarooo?" and I have to explain it's just another way of writing a
conditional, but the little crease between their eyebrows never goes
away, and I know as soon as I'm not looking, that line will be replaced
with if-then-else.



So I just save a step, and leave it out.



Re: Self-Documenting Comments

2005-11-01 14:39 • by KingKillerBigWheelaCapPeeler
48992 in reply to 48982
terniary operator's rule.



 I specially like them nested

at least 5 deep.



 buy hey, howcome this code is here?

 is wtf out of code?

 I have some real wtfery I could post.

 cause this is boring.



lets get some stuff with some meat up in here..

 I want something to REALLY laugh about.

 

 its good bonding for the team when we all gather around one station and poke fun at the code provided here.



 but this is just no fun at all.



 bool flag = value?(value>1?(value>2?(value>3?(value>4?(value>5?true:false):false):true):false):true):false;



... lol something like that anyhow.

Re: Self-Documenting Comments

2005-11-01 14:41 • by dabocla
48993 in reply to 48968
That's exactly what I was thinking.  Ternary.  :)  This
whole function is not even necessary.  just turn the function name
into a comment, and turn the underscores into spaces.  If you even
need to do that much.



JC

Re: Self-Documenting Comments

2005-11-01 14:43 • by Satanicpuppy
48994 in reply to 48976
Ytram:
Anonymous:
FIRST!




TENTH!!!



Seriously, we need to implement something similar to Fark.com, someone
that posts "First Post!" gets the text of their comment changed to
"Boobies!" and the time stamp on it changes to 12 hours into the future.




It'd be hard to enforce...people start typing other things to get past the filter.



The real reason Fark doesn't have the FP moron crowd is because the
TFers always get the first ten posts, and they pay for it, and thus
feel no need to grandstand.



Re: Self-Documenting Comments

2005-11-01 14:48 • by Kiss me, I'm Polish
48997 in reply to 48992
Boobies!

Re: Self-Documenting Comments

2005-11-01 14:50 • by Manni
I'm always skeptical when I see a variable named like "actionType" and it's a string. Later on down in the code there's probably a line that says "if ($actionType == 'cont')". Yeesh. In cases like this, I'm inclined to use.. I dunno, enumerated values? Constants (defined as integers maybe?) Something with which I can minimize case-sensitive string errors n' such. Should we get into a discussion about the time difference it takes to compare two integers versus two strings? Nah, I don't want to be one of THOSE types of post whores.

Re: Self-Documenting Comments

2005-11-01 14:56 • by kipthegreat
48999 in reply to 48992
Anonymous:
terniary operator's rule.



 I specially like them nested

at least 5 deep.



 buy hey, howcome this code is here?

 is wtf out of code?

 I have some real wtfery I could post.

 cause this is boring.



lets get some stuff with some meat up in here..

 I want something to REALLY laugh about.

 

 its good bonding for the team when we all gather around one station and poke fun at the code provided here.



 but this is just no fun at all.



 bool flag = value?(value>1?(value>2?(value>3?(value>4?(value>5?true:false):false):true):false):true):false;



... lol something like that anyhow.




Usually if they're kinda hard to read I'll break the two conditions out onto two lines, with : under ?, like this-



return  some_condition ? a_kinda_long_statement_might_go_here

                      
: another_sorta_long_statement_maybe;




Re: Self-Documenting Comments

2005-11-01 14:57 • by Mung Kee
49000 in reply to 48994
Satanicpuppy:
Ytram:
Anonymous:
FIRST!




TENTH!!!



Seriously, we need to implement something similar to Fark.com, someone
that posts "First Post!" gets the text of their comment changed to
"Boobies!" and the time stamp on it changes to 12 hours into the future.




It'd be hard to enforce...people start typing other things to get past the filter.



The real reason Fark doesn't have the FP moron crowd is because the
TFers always get the first ten posts, and they pay for it, and thus
feel no need to grandstand.






I'm surprised no one has gotten (or commented on) the Crank Yankers
"Special Ed" reference.  I figured it was more politically correct
than just calling the first poster a retard.



Mung Kee:


"Yay, I got mail!"



Re: Self-Documenting Comments

2005-11-01 15:03 • by rbrendler
49001 in reply to 48979

Ternary is indeed a wonderful thing (although there is a special corner of hell for people who nest ternary ops), but one thing that I have seen bite the unwary is order of operations.  For example, in C++:



std::cout << 1 ? 2 : 3 ;


will output "1"!  Order of operations means that the << operator will be evaluated before the ?:, so this is the same as saying:



(std::cout << 1) ? 2 : 3 ;


Counterintuitive at best...

Re: Self-Documenting Comments

2005-11-01 15:05 • by rogthefrog
49003 in reply to 48968
kipthegreat:


Not really related to the WTF at all, but something I've been
wondering:  Am I the only one who likes the ternary ?:
operator?  It is usually grouped with global variables and goto
statements as "bad no matter what."



$actionType = $hasSiblings ? "cont" : "end";



That is much easier and doesn't waste so much space, and I don't see how it would be hard to understand...




I use it all the time if the elements in it aren't too long. Great stuff.

Re: Self-Documenting Comments

2005-11-01 15:06 • by UncleMidriff
49004 in reply to 48976
Ytram:
Anonymous:
FIRST!




TENTH!!!



Seriously, we need to implement something similar to Fark.com, someone
that posts "First Post!" gets the text of their comment changed to
"Boobies!" and the time stamp on it changes to 12 hours into the future.




Except, with this forum software, the text would get changed to



[thsetiugaggib] ddbvg  jzhfdgkuasdhg
zdsfkgkjb lbzsddfgsdrhf
Boobidghjsrthdfsh&&esrysjfysj[/thsetiugaggib]qqoxcvnlnjllttg h
llzflgklu




and the timestamp would be changed to 1 month in the past, if the first
poster used the word first -anywhere- in his/her post and happened to
be using Firefox.

Re: Self-Documenting Comments

2005-11-01 15:06 • by rogthefrog
49005 in reply to 48982
A Wizard A True Star:

Oh, come on. Get off your lazy
asses and type an if/then/else like a normal human being. No one wants
to maintain an if block that's all smashed together on one line
just because you thought it was 'leet.


 




Oh, come on. Get off your lazy ass and learn to maintain real code. Sheesh.


Re: Self-Documenting Comments

2005-11-01 15:08 • by rogthefrog
49006 in reply to 48988
Anonymous:

Turnary operators can be pretty useful though.


printf("There is %d %s of foo\n", cnt, ((cnt == 1) ? "instance" : "instances"));


Without:


if (cnt==1)


   printf("There is 1 instance of foo\n");


else


   printf("There is %d instances of foo\n", cnt);


 


Is that really that unmaintainable?





I'd factor everything out that can be factored out, though, like so:



printf("There is %d instance%s of foo\n", cnt, ((cnt == 1) ? "" : "s"));





Re: Self-Documenting Comments

2005-11-01 15:15 • by Mung Kee
49007 in reply to 49006
What's with the mutilation of the spelling of "ternary". 



turnary

terniary

Re: Self-Documenting Comments

2005-11-01 15:27 • by Boojum
Chained ternary ops aren't so bad as long as their done correctly. The example above is perfectly readable if rewritten as:


bool flag = ( !value ? false :
value<=1 ? true :
value<=2 ? false :
value<=3 ? true :
value<=4 ? false :
value>5 );


It's just like how most programmers wouldn't chain if statements together in the then clause (if-if-if-if-else-else-else-else), but instead would do it in the else clause to make it if-else if-else if-else if-else, etc.

Sheesh. Haven't you people ever done anything in functional languages?

Re: Self-Documenting Comments

2005-11-01 15:28 • by rogthefrog
49009 in reply to 49006
rogthefrog:
Anonymous:

Turnary operators can be pretty useful though.


printf("There is %d %s of foo\n", cnt, ((cnt == 1) ? "instance" : "instances"));


Without:


if (cnt==1)


   printf("There is 1 instance of foo\n");


else


   printf("There is %d instances of foo\n", cnt);


 


Is that really that unmaintainable?





I'd factor everything out that can be factored out, though, like so:



printf("There is %d instance%s of foo\n", cnt, ((cnt == 1) ? "" : "s"));








Actually, if we're going down that road:

printf("There %s %d instance%s of foo\n", (cnt == 1 ? "is" : "are"), cnt, (cnt == 1 ? "" : "s"));


Which means we're better off with an if statement here. Oh well.






Re: Self-Documenting Comments

2005-11-01 15:28 • by Anonymous Coward
49010 in reply to 49006
rogthefrog:


I'd factor everything out that can be factored out, though, like so: printf("There is %d instance%s of foo\n", cnt, ((cnt == 1) ? "" : "s"));




I'm not an english-speaking person, but.. wouldn't it be: "there ARE %d instanceS of foo\n", for cnt>1 ?

Re: Self-Documenting Comments

2005-11-01 15:32 • by Boojum
Sigh.  That was supposed to be:





bool flag = ( !value ? false :



              value<=1 ? true :



              value<=2 ? false :



              value<=3 ? true :



              value<=4 ? false :



              value>5 );



I hate this forum software.

Re: Self-Documenting Comments

2005-11-01 15:32 • by Anonymous
49012 in reply to 49006
Surely that should be

printf("There %s %d instance%s of foo\n", ((cnt == 1) ? "is" : "are"), cnt, ((cnt == 1) ? "" : "s"));

Re: Self-Documenting Comments

2005-11-01 15:34 • by Anonymous
49013 in reply to 49012
Darn, looks like I got beaten on that correction.

Re: Self-Documenting Comments

2005-11-01 15:45 • by a name
49015 in reply to 49009
Which means we're better off with an if statement here. Oh well.


Why? I still like the the ? better. I'd rather keep it as one statement to visually scan over instead of an if.


Ternary is important in some languages

2005-11-01 15:49 • by Will
49016 in reply to 49013
Another thing: there are things you can't do without the ternary operator in some languages (like, say, C++):



   const int someVar = someCondition ? value1 : value2;



Try and do that using ifs.  Maybe with some truly wretched casting
it would be possible, but at that point, you're very clearly much better off just using the ternary operator.

Re: Self-Documenting Comments

2005-11-01 15:50 • by UncleMidriff
49017 in reply to 49015
Anonymous:
Which means we're better off with an if statement here. Oh well.


Why? I still like the the ? better. I'd rather keep it as one statement to visually scan over instead of an if.





I'd venture to guess the reason is because with an if statement, you
don't have to do "cnt == 1" twice.  I've no idea if that'd make
any difference in anything whatsoever, but it seems a wee bit silly to
do two comparisons when you could get away with just one.

Re: Self-Documenting Comments

2005-11-01 15:50 • by emptyset
49018 in reply to 49012

Anonymous:
Surely that should be
printf("There %s %d instance%s of foo\n", ((cnt == 1) ? "is" : "are"), cnt, ((cnt == 1) ? "" : "s"));


oh, how rotten.  baby, please.  let's refactor these puppies:


const char *get_conjugation(const char *verb, unsigned int count) ;


const char *get_plural_suffix(const char *noun, unsigned int count) ;


the implementation is left as an exercise to the reader.

Re: Self-Documenting Comments

2005-11-01 15:52 • by Mung Kee
49019 in reply to 49018
emptyset:

Anonymous:
Surely that should be
printf("There %s %d instance%s of foo\n", ((cnt == 1) ? "is" : "are"), cnt, ((cnt == 1) ? "" : "s"));


oh, how rotten.  baby, please.  let's refactor these puppies:


const char *get_conjugation(const char *verb, unsigned int count) ;


const char *get_plural_suffix(const char *noun, unsigned int count) ;


the implementation is left as an exercise to the reader.





I'm sick of you and your homework.

Re: Self-Documenting Comments

2005-11-01 15:55 • by Meddler
49020 in reply to 49015
Anonymous:
Which means we're better off with an if statement here. Oh well.


Why? I still like the the ? better. I'd rather keep it as one statement to visually scan over instead of an if.





Agreed.  You use and understand ? ? You are probably skilled and
efficient : You are a novice programmer or have to show your code to
novices.

Re: Ternary is important in some languages

2005-11-01 15:57 • by Maurits
49021 in reply to 49016
Anonymous:
Another thing: there are things you can't do without the ternary operator in some languages (like, say, C++):



   const int someVar = someCondition ? value1 : value2;



Try and do that using ifs.  Maybe with some truly wretched casting
it would be possible, but at that point, you're very clearly much better off just using the ternary operator.


int tempVar = 0;

if (someCondition) { tempVar = value1; } else { tempVar = value2; }

const int someVar = tempVar;

Re: Self-Documenting Comments

2005-11-01 15:58 • by a name
49022 in reply to 49017
I'd venture to guess the reason is because with an if statement, you don't have to do "cnt == 1" twice. I've no idea if that'd make any difference in anything whatsoever, but it seems a wee bit silly to do two comparisons when you could get away with just one.


Have you ever looked at the code needed to implement printf? A double comparison of integers is the least of your worries.



Not to mention we are either talking file IO (glaciers move faster), or interactive (the user will never notice the 2 processor cycles you saved)


« PrevPage 1 | Page 2 | Page 3Next »

Add Comment