Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > More on the cosmetics (if-statements)

Reply
Thread Tools

More on the cosmetics (if-statements)

 
 
desktop
Guest
Posts: n/a
 
      05-31-2007
Which would you prefer:

/* A */

int k = 402;
if ( k == 40) {
std::cout << "40\n";
} else {
std::cout << "Not 40\n";
}


/* B */
if ( k == 40) std::cout << "40\n";
else std::cout << "Not 40\n";


I know it depends on taste but I am just curious and would like to hear
your pro's and con's.
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      05-31-2007
desktop a écrit :
> Which would you prefer:
>
> /* A */
>
> int k = 402;
> if ( k == 40) {
> std::cout << "40\n";
> } else {
> std::cout << "Not 40\n";
> }
>
>
> /* B */
> if ( k == 40) std::cout << "40\n";
> else std::cout << "Not 40\n";
>
>
> I know it depends on taste but I am just curious and would like to hear
> your pro's and con's.


/* C */
// ...
const int magic_k = 402;
// ...
const int special_k = 40;
//k is ...
int k = magic_k;
//
if ( special_k == k )
{
std::cout << "40\n";
}
else
{
std::cout << "Not 40\n";
}

As for proposition B, it was explicitely forbidden in internal norm in
the last two compagny I've been.

Michael
 
Reply With Quote
 
 
 
 
terminator
Guest
Posts: n/a
 
      05-31-2007
On May 31, 3:13 pm, desktop <(E-Mail Removed)> wrote:
> Which would you prefer:
>
> /* A */
>
> int k = 402;
> if ( k == 40) {
> std::cout << "40\n";
> } else {
> std::cout << "Not 40\n";
> }
>
> /* B */
> if ( k == 40) std::cout << "40\n";
> else std::cout << "Not 40\n";
>
> I know it depends on taste but I am just curious and would like to hear
> your pro's and con's.


for the sake of maintanance and later correction/improvement 'A' is
better ;it prevents algorithmic errors due to later editing and
forggeting braces.

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      05-31-2007
desktop wrote :
> Which would you prefer:
>
> /* A */
>
> int k = 402;
> if ( k == 40) {
> std::cout << "40\n";
> } else {
> std::cout << "Not 40\n";
> }
>
>
> /* B */
> if ( k == 40) std::cout << "40\n";
> else std::cout << "Not 40\n";
>
>
> I know it depends on taste but I am just curious and would like to hear your
> pro's and con's.


/* C */
if (k == 40)
std::cout << "40\n";
else
std::cout << "Not 40\n";

I'd like to have braces on separate lines, but for compound statements
with only a single inner statement that produces too much clutter, so I
won't use braces for those.

Pro: it looks cleaner (to me at least)
Con: you'll have to add the braces when extending the statements, which
can be forgotten

- Sylvester


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      05-31-2007
terminator wrote:
\
> for the sake of maintanance and later correction/improvement 'A' is
> better ;it prevents algorithmic errors due to later editing and
> forggeting braces.
>

Further it makes use of debuggers a lot easier. Most of the
popular ones I've used (Micrsoft, gdb, Sun) all deal with
"lines" as their resolution (unless you switch to instructions).
By putting things all on one line, it's hard to break/step on
the if condition test separately from the attached statement.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      05-31-2007
desktop <(E-Mail Removed)> wrote:

> Which would you prefer:
>
> /* A */
>
> int k = 402;
> if ( k == 40) {
> std::cout << "40\n";
> } else {
> std::cout << "Not 40\n";
> }
>
>
> /* B */
> if ( k == 40) std::cout << "40\n";
> else std::cout << "Not 40\n";
>
>
> I know it depends on taste but I am just curious and would like to hear
> your pro's and con's.


If those are the only two options I have to choose from, then A.
 
Reply With Quote
 
desktop
Guest
Posts: n/a
 
      05-31-2007
terminator wrote:
> On May 31, 3:13 pm, desktop <(E-Mail Removed)> wrote:
>> Which would you prefer:
>>
>> /* A */
>>
>> int k = 402;
>> if ( k == 40) {
>> std::cout << "40\n";
>> } else {
>> std::cout << "Not 40\n";
>> }
>>
>> /* B */
>> if ( k == 40) std::cout << "40\n";
>> else std::cout << "Not 40\n";
>>
>> I know it depends on taste but I am just curious and would like to hear
>> your pro's and con's.

>
> for the sake of maintanance and later correction/improvement 'A' is
> better ;it prevents algorithmic errors due to later editing and
> forggeting braces.
>


Ok but what about this (assuming that there is no such thing as a switch):


int k = 40;

/* Case 1. */
if (k == 10) {
std::cout << "1\n";
} else
/* Case 2. */
if (k == 20) {
std::cout << "2\n";
} else
/* Case 3. */
if (k == 30) {
std::cout << "3\n";
} else
/* Case 4. */
if (k == 40) {
std::cout << "4\n";
}



/* INSTEAD OF:*/

/* Case 1: Only 1 element in the list. */
if (k == 10) {
std::cout << "1\n";
} else {
if (k == 20) {
std::cout << "2\n";
} else {
if (k == 30) {
std::cout << "3\n";
} else {
if (k == 40) {
std::cout << "4\n";
}
}
}
}

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      05-31-2007
desktop <(E-Mail Removed)> wrote in news:f3mgvl$med$(E-Mail Removed)-c.dk:

>
> Ok but what about this (assuming that there is no such thing as a
> switch):
>
>
> int k = 40;
>
> /* Case 1. */
> if (k == 10) {
> std::cout << "1\n";
> } else
> /* Case 2. */
> if (k == 20) {
> std::cout << "2\n";
> } else
> /* Case 3. */
> if (k == 30) {
> std::cout << "3\n";
> } else
> /* Case 4. */
> if (k == 40) {
> std::cout << "4\n";
> }
>
>
>
> /* INSTEAD OF:*/
>
> /* Case 1: Only 1 element in the list. */
> if (k == 10) {
> std::cout << "1\n";
> } else {
> if (k == 20) {
> std::cout << "2\n";
> } else {
> if (k == 30) {
> std::cout << "3\n";
> } else {
> if (k == 40) {
> std::cout << "4\n";
> }
> }
> }
> }


Neither:

if (k == 10)
{
std::cout << "1\n";
}
else if (k == 20)
{
std::cout << "2\n";
}
else if (k == 30)
{
std::cout << "3\n";
}
else if (k == 40)
{
std::cout << "4\n";
}





 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      05-31-2007
desktop wrote:
> Which would you prefer:
>
> /* A */
>
> int k = 402;
> if ( k == 40) {
> std::cout << "40\n";
> } else {
> std::cout << "Not 40\n";
> }
>
>
> /* B */
> if ( k == 40) std::cout << "40\n";
> else std::cout << "Not 40\n";
>
>
> I know it depends on taste but I am just curious and would like to hear
> your pro's and con's.


Guess I'm the only one who'd go for /*B*/. Some people dislike
it, because they tend to forget to add braces when they need to
extend the 'if' or 'else' to more statements. To me, a single
'if' or 'else' line rings an alarm bell that beeps "add the braces"
whenever I modify it.

- J.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-31-2007
Andre Kostur wrote:
> desktop <(E-Mail Removed)> wrote in news:f3mgvl$med$(E-Mail Removed)-c.dk:
>
>>
>> Ok but what about this (assuming that there is no such thing as a
>> switch):
>>
>>
>> int k = 40;
>>
>> /* Case 1. */
>> if (k == 10) {
>> std::cout << "1\n";
>> } else
>> /* Case 2. */
>> if (k == 20) {
>> std::cout << "2\n";
>> } else
>> /* Case 3. */
>> if (k == 30) {
>> std::cout << "3\n";
>> } else
>> /* Case 4. */
>> if (k == 40) {
>> std::cout << "4\n";
>> }
>>
>>
>>
>> /* INSTEAD OF:*/
>>
>> /* Case 1: Only 1 element in the list. */
>> if (k == 10) {
>> std::cout << "1\n";
>> } else {
>> if (k == 20) {
>> std::cout << "2\n";
>> } else {
>> if (k == 30) {
>> std::cout << "3\n";
>> } else {
>> if (k == 40) {
>> std::cout << "4\n";
>> }
>> }
>> }
>> }

>
> Neither:
>
> if (k == 10)
> {
> std::cout << "1\n";
> }
> else if (k == 20)
> {
> std::cout << "2\n";
> }
> else if (k == 30)
> {
> std::cout << "3\n";
> }
> else if (k == 40)
> {
> std::cout << "4\n";
> }


For unsigned 'k':

std::cout << "01234"[k / 10 % 5] << "\n";



V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
Mary Kay Cosmetics buck Computer Support 5 11-30-2005 08:18 PM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
How to collaborate with another developer who is only concerned with cosmetics? Paul E ASP General 3 08-19-2004 12:54 AM
Re: With More Flash More Lumix: using an external flash unit with the FZ1 and other digicams Hans-Georg Michna Digital Photography 4 08-24-2003 06:05 PM



Advertisments