Click here to Skip to main content
15,886,919 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
honey the codewitch26-Jan-24 7:21
mvahoney the codewitch26-Jan-24 7:21 
GeneralRe: if else Style Pin
0x01AA26-Jan-24 8:09
mve0x01AA26-Jan-24 8:09 
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 
I make a sharp distinction between how I personally would like to format the code, and how I code when someone else will/might read it. I see so many errors caused by deep nesting and misinterpretation of indentation levels that I want a double check to detect mistakes. Therefore:

1) No indentation that is not supported by braces
2) No braces that do not adhere to the indentation level.

So if I see anything like
C#
if (somecondtion) 
   xval = 1;
nextstatement;
I immediately ask for permission to add the "redundant" braces. Most programmers accept this.

The second point is far less accepted: As long as you are not past the closing brace, you are still in the indented block. The brace is what causes the undent. Code is read in forward direction, so the last character position before the brace is still indented. Also, the closing brace visually blurs what is the next statement. I would really want to edit the above code to
C#
if (somecondition) { 
   xval = 1;
   }
nextstatement;
- but few other programmers agree to the indentation of the closing brace. I have to tolerate what I consider a logical inconsistency, in non-private code. I also consider as inconsistent the style suggested by some:
C#
if (somecondition) 
   { 
   xval = 1;
   }
nextstatement;
- you don't know that an indented block follows until after the opening brace. Inserting indenting space before it cannot be logically justified.

I honestly abhor deep block nesting, in particular when it ends up in a sequence of '} } } } }'. I try to avoid that whenever possible. Get out of a given nesting level as soon as possible! So I code like
C#
FailureCode SomeMethod(int argX, float argY, sometype Z) {

   if (argX > maxXvalue) {
      return ErrorIllegalXArgument;
      }
   if (argZ == null) {
      return ErrorUnspecifiedZ;
      }

   statement;
   statement;
   return Success;
   }
Today, this is accepted by most other programmers, at the method level, although the majority would like to add 'else's, including for the successful part. Sequences of 'else if' are by most not considered a new nesting level and usually accepted as a series of alternatives, although they strictly are not, by the grammar definition, of most C class languages.

A similar situation can occur in the middle of a method body: Several conditions must be satisfied for some operation to be performed. The C class of languages doesn't allow you to define arbitrary blocks (and certainly not to name them for the purpose of skipping out), so we have to 'abuse' e.g. a do statement:
C#
do {
   if (argX > MaxXvalue) {
      break;
      }
   if (argY < 0.0) {
      break;
      }
   if (argZ == null {
      break;
      }
   statement;
   statement;
   }
while (false);
I really would like to drop the 'while (false)'; (and preferably the introducing 'do' as well), but the compiler won't let me.

The 'classical' style, argued by some who consider themselves structural purists; insist that there shall be a single exit from every method; returning as soon as you discover a fatal error is messy coding, and the proper layout is like:
C#
do {
  if (argX <= MaxXvalue) {
      if (argY >= 0.0) {
         if (argZ != null) {
            statement;
            statement;
            }
         }
   } 
while (false);
What really upsets people is if I want to get rid of the two first indentations as well, formatting it like
C#
do {
   if (argX > MaxXvalue) break;
   if (argY < 0) break;
   if (argZ == null) break;

   statement;
   statement;
   } 
while (false);
It is my honest opinion that this code is far more readable than the fully structured purist code. I know of programmers who 'admit' that they agree about the readability. Yet, they will not accept this coding style; it is not 'proper'. So I reserve it for my private code that will never be handled by others.

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

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

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.