Click here to Skip to main content
15,887,135 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: if else Style Pin
trønderen26-Jan-24 8:29
trønderen26-Jan-24 8:29 
GeneralRe: if else Style Pin
0x01AA26-Jan-24 9:32
mve0x01AA26-Jan-24 9:32 
GeneralRe: if else Style Pin
Gary R. Wheeler26-Jan-24 13:12
Gary R. Wheeler26-Jan-24 13:12 
GeneralRe: if else Style Pin
BernardIE531726-Jan-24 13:34
BernardIE531726-Jan-24 13:34 
AnswerRe: if else Style Pin
Ravi Bhavnani26-Jan-24 20:53
professionalRavi Bhavnani26-Jan-24 20:53 
GeneralRe: if else Style Pin
trønderen27-Jan-24 9:05
trønderen27-Jan-24 9:05 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 10:24
BernardIE531727-Jan-24 10:24 
GeneralRe: if else Style Pin
trønderen27-Jan-24 11:44
trønderen27-Jan-24 11:44 
BernardIE5317 wrote:
I find it helpful for braces to align.
It provides a 'graphic consistency', but it breaks with the logic of braces and indents representing the same structural property. For the closing brace, you are not yet out of the block, yet you undent it.

Also, the first line, with the 'if' terminates abruptly without explanation. Putting the brace at the end of the line explains what is happening: You will now see an indented block that is the 'if' clause. As I illustrated, I accept a single short statement after the if-condition; then you will see a line terminated with a semicolon, and you know that the if-clause is complete, and no indented if-clause follows.

Another reason why I do not want the opening brace on a new line: It visually clutters the 'if'. With the 'if' having blank space underneath it, it stands out as the kind of statement owning the block; you can see from a far distance that is isn't a e.g. while or for loop. Similar for the closing brace: It clutters up what will happen after the conditional block.

I do not understand the 2nd discussion.
Consider it a somewhat stylized, simplified example.

First, each of the tests may be far more complex than a simple comparison, and the total combined expression may be lengthy, maybe not fitting on a line.

More important: If the tests are conceptually independent, testing three separate aspects, that should be reflected by writing them as three separate tests. They are three different reasons for not performing the operation (or for returning from the method), not a single complex reason. A test such as 'if (ch1 >= 'a' && ch1 <= 'z')' is one conceptually homogenous test that belongs in one single if-condition. Three different tests on three distinct variables is rarely similarly homogenous.

Third: In many cases, a test that leads to the operation not being performed may lead to e.g. a status value being set. Each test may lead to a different status value. If you mold together the three tests, you cannot fit such handling into your layout rules.

Also, note that in my final example, which is my preferred one, the three conditions are clearly separated from the operation code, so that you see what is the condition, what is the operation. (I should have inserted a blank line after the conditions in the example with the 'break;' at separate lines - I always do in my code, and leaving it out here is at the level of a typo Smile | :) .)

Yet another reason for not combining several conditions in a single if: I also abhor excessive use of or need for parentheses. Having to mentally break up and group the pieces of a complex logical expression is not good for readability. In my last example, you can immediately see that there are three distinct conditions, you see which elements are included in each, and the 'break' is an explicit indication that this condition may cause an operation to be skipped - analogous to an early return from a method.

For the record: I have looked at the binary code generated by the compiler for complex expressions compared to a sequence of simpler conditions. I have never, with a modern high quality compiler, seen any case where I could 'help' the compiler to created better code by lumping together as much as possible into large complex expressions. Even in cases where the code may be slightly different, timing shows that the more readable source code compiles to code executing just as fast.

Religious freedom is the freedom to say that two plus two make five.

GeneralRe: if else Style Pin
BernardIE531727-Jan-24 12:16
BernardIE531727-Jan-24 12:16 
GeneralRe: if else Style Pin
trønderen27-Jan-24 14:32
trønderen27-Jan-24 14:32 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 14:42
BernardIE531727-Jan-24 14:42 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 19:16
BernardIE531727-Jan-24 19:16 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 11:59
BernardIE531727-Jan-24 11:59 
GeneralRe: if else Style Pin
Ravi Bhavnani28-Jan-24 16:52
professionalRavi Bhavnani28-Jan-24 16:52 
GeneralRe: if else Style Pin
Iacopo Vettori28-Jan-24 23:47
Iacopo Vettori28-Jan-24 23:47 
GeneralRe: if else Style Pin
Peter Kelley 202128-Jan-24 23:58
Peter Kelley 202128-Jan-24 23:58 
GeneralRe: if else Style Pin
John Wellbelove29-Jan-24 1:29
John Wellbelove29-Jan-24 1:29 
GeneralRe: if else Style Pin
MikeCO1029-Jan-24 1:42
MikeCO1029-Jan-24 1:42 
GeneralRe: if else Style Pin
Matt Bond29-Jan-24 2:49
Matt Bond29-Jan-24 2:49 
GeneralRe: if else Style Pin
jschell29-Jan-24 5:22
jschell29-Jan-24 5:22 
GeneralRe: if else Style Pin
Alister Morton30-Jan-24 23:46
Alister Morton30-Jan-24 23:46 
GeneralRe: if else Style Pin
Stacy Dudovitz29-Jan-24 13:56
professionalStacy Dudovitz29-Jan-24 13:56 
GeneralRe: if else Style Pin
jschell30-Jan-24 4:40
jschell30-Jan-24 4:40 
GeneralRe: if else Style Pin
Stacy Dudovitz30-Jan-24 10:38
professionalStacy Dudovitz30-Jan-24 10:38 
GeneralRe: if else Style Pin
jschell31-Jan-24 4:53
jschell31-Jan-24 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.