More on the cosmetics (if-statements)

D

desktop

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.
 
M

Michael DOUBEZ

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
 
T

terminator

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.
 
S

Sylvester Hesp

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
 
R

Ron Natalie

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.
 
D

Daniel T.

desktop said:
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.
 
D

desktop

terminator said:
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";
}
}
}
}
 
A

Andre Kostur

desktop said:
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";
}
 
J

Jacek Dziedzic

desktop said:
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.
 
V

Victor Bazarov

Andre said:
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
 
S

Sylvester Hesp

Victor Bazarov wrote :
Andre said:
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

Which is not really the same if 'k' is not either one of the values in
the if statements ;)

- Sylvester
 
P

Puppet_Sock

/* 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";

Whoops! There's your magic number back again. You want
something resembling the following.

std::cout << special_k << "\n";

Or some such.
}
else
{
std::cout << "Not 40\n";

Same kind of comment.
Socks
 
P

Puppet_Sock

Which would you prefer:
[brackets or no brackets for single line of if]

As Michael said, option C.

It may simply be the kind of engineering projects I've
been involved in over the last few years. But one of the
principles that gets drilled on pretty harshly is, for
every task you must define the start and end of the task
before you actually start the task.

So I alwasy type things in the following way. When I'm
going to put in an "if" statement, first I type this.

if()
{
}

That is, I define the scaffold that provides the bounds.
Then I fill in the condition inside the if() part.
Then I put the lines of code between the { and },
and I do this even if I expect there to be only one
line of code.

I do much the same thing for functions. Say I'm going
to have a function called myFunc. First I type this.

myFunc()
{
}

Then I fill in the return type, then the argument list,
then I start on the code. Same for while, for, etc.
Socks
 
J

James Kanze

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

Only up to a certain point: B is not acceptable anywhere.

But different places have different conventions regarding
braces, and while I tend to use something like your A, the exact
placement of braces really is a question of personal taste.
 
J

James Kanze

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";
}

Why the extra lines?
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";
}
/* 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";
}
}
}
}

And why all the extra nesting. "else if" is pretty much a
recognized concept. Languages like Modula or Ada, which
force bracing the controled statements, almost always have an
elsif concept. C++ doesn't have it because it doesn't need
anything special to simulate the same construction.
 
J

Jerry Coffin

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";

Like most, I prefer option C (or somewhere around option J, based on the
number of "C" options already presented):

std::cout << (k == 40 ? "40\n" : "Not 40\n");

or using a bit of boolean math:

std::string outputs[] = {"Not 40\n", "40\n"};

std::cout << outputs[k==40];

or getting tricky with the boolean math:

std::cout << "Not 40\n"+4*(k==40);

Though I'm honsetly not sure I'd consider this one advisable...

Between the two you presented, I'd consider A reasonable, and B
unacceptable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top