Why is so much code like this?

 

I see this a lot when people post their code.

if(a==b){
Dothis;
Dothis;
etc;
}

Now, this is not a problem when reading short pieces of other people's code.

But when their is a lot of code, it can make it almost impossible to follow.

I would think that many would have difficulties finding errors in their own code.

Why not this?

if(a==b)
{
Dothis
Dothis
etc
}

//Or

if(a==b)
  {
  Dothis
  Dothis
  etc
  }

 

There is nothing wrong with the former style ( K & R style ) (as long as it is indented). It's used a lot in languages with this type of syntax (C, Java, Perl) .

It is, for example, the Java Standard and quite readable IMO (as long as indented correctly). [ As mql4 borrows heavily from the C style syntax I think mentioning these other languages is relevant ]

It should be indented though :


if(a==b){
   Dothis;
   Dothis;
   etc;
}


The 2nd example ( Allman style ) is also prefered by some, and is generally agreed to be more readable (again with correct indenting )


if(a==b)
{
   Dothis;
   Dothis;
   etc;
}

but the method you seem to dismiss is quitet popular elsewhere (for reasons of readability)

Your latter example I dont like at all.

 

allman style is also easier to identify missing braces especially in complex if else trees.

 

Brace style is a programmers preference, there's really no right or wrong method Imo. The K&R style is the best ;-). Why take-up an extra line for an opening brace .... what I really care about is which if{statement} it belongs to. Example what looks better and easier to understand.

if(.............................)
    {
    if(.............................)
        {
        if(.............................)
            {
            if(.............................)
                {
                if(.............................)
                    {
                    if(.............................)
                        {
                        if(.............................)
                            {
                            if(.............................)
                                {
                                if(.............................)
                                    {
                                    if(.............................)
                                        {
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

Or this version:

if(.............................){
    if(.............................){
        if(.............................){
            if(.............................){
                if(.............................){
                    if(.............................){
                        if(.............................){
                            if(.............................){
                                if(.............................){
                                    if(.............................){
}   }   }   }   }   }   }   }   }   }

And even better yet.

if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
if( !.............................) return
Imo, the last version feels more natural to how i write in real-life. Line by line.
 

I do it like this:

if(.............................)
{if(.............................)
 {if(.............................)
  {if(.............................)
   {if(.............................)
    {if(.............................)
     {if(.............................)
      {if(.............................)
       {if(.............................)
        {if(.............................)
}}}}}}}}}
 

I like the discussion.

And what about the preference in coding the else statement. I prefer the first example, though some editors cannot fold/unfold such blocks. The second one is a compromise. The third one is even more consistent, but unreadable.

if (cond) {
   command;
} else if (cond) {
   command;
} else {
   command;
}

or

if (cond) {
   command;
}
else if (cond) {
   command;
} 
else {
   command;
}

or

if (cond) {
   command;
} 
else {
   if (cond) {
      command;
   }   
   else {
      command;
   }  
}
 
To some degree it doesn't matter, the main thing that matters is consistency of applying the chosen style . . . we all have our own preferences of course
 

I don't use else much anymore. There was one problem where I was forced to use else a while ago (cannot remember the problem). When I did use else, it went something like this.

if (cond) { command; } else 
if (cond) { command; } else {
   command;
}

Now however my first taught on coding something like above would be.

if( cond ){ command; return; }
if( cond ){ command; return; }
command;

Yes, I will turn a three_line code into a function 8)))

 
SDC:

i do if else like this

Personally I find that about as clear as mud
 
RaptorUK: Personally I find that about as clear as mud
It just hurts my eyes :{
 
RaptorUK:
To some degree it doesn't matter, the main thing that matters is consistency of applying the chosen style . . . we all have our own preferences of course

Agree with Raptor. Topic about nothing. I get a lot more frustrated when people post their code here without using SRC. How they code their program is up to them, unless they are going to ask for help.
Reason: