473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

More on the cosmetics (if-statements)

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.
May 31 '07 #1
15 1454
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
May 31 '07 #2
On May 31, 3:13 pm, desktop <f...@sss.comwrote:
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.

May 31 '07 #3
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
May 31 '07 #4
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.
May 31 '07 #5
desktop <ff*@sss.comwrote:
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.
May 31 '07 #6
terminator wrote:
On May 31, 3:13 pm, desktop <f...@sss.comwrote:
>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";
}
}
}
}

May 31 '07 #7
desktop <ff*@sss.comwrote in news:f3**********@news.net.uni-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";
}

May 31 '07 #8
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.
May 31 '07 #9
Andre Kostur wrote:
desktop <ff*@sss.comwrote in news:f3**********@news.net.uni-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
May 31 '07 #10
Victor Bazarov wrote :
Andre Kostur wrote:
>desktop <ff*@sss.comwrote in news:f3**********@news.net.uni-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
Which is not really the same if 'k' is not either one of the values in
the if statements ;)

- Sylvester
May 31 '07 #11
On May 31, 7:52 am, Michael DOUBEZ <michael.dou...@free.frwrote:
[snip]
/* 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

May 31 '07 #12
On May 31, 7:13 am, desktop <f...@sss.comwrote:
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

May 31 '07 #13
On May 31, 1:13 pm, desktop <f...@sss.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 1 '07 #14
On May 31, 3:01 pm, desktop <f...@sss.comwrote:
terminator wrote:
On May 31, 3:13 pm, desktop <f...@sss.comwrote:
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";
}
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 1 '07 #15
In article <f3**********@news.net.uni-c.dk>, ff*@sss.com says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 3 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NotGiven | last post by:
I am researching the best place to put pictures. I have heard form both sides and I'd like to know why one is better than the other. Many thanks!
11
by: Gobo Borz | last post by:
Hi everyone, I have a python cgi program that uses print statements to write html. The program has grown, and for reasons I won't bore you with, I need to build the page in a string and "print"...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
3
by: A.V.C. | last post by:
Hello, I want to store two information (so 2 columns) for 2/3rd of the rows that will be in a table and only one information (1 column is suffecient) for 1/3rd of the rows of the table. ex:...
51
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on...
15
by: sparks | last post by:
We get more and more data done in excel and then they want it imported into access. The data is just stupid....values of 1 to 5 we get a lot of 0's ok that alright but 1-jan ? we get colums...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.