Why is so much code like this?

Keith Watford
Moderator
32153

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
  }

ydrol
513
ydrol  

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.

Ian Venner
2400
Ian Venner  

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

Ubzen
5300
Ubzen  

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.
Ian Venner
2400
Ian Venner  

I do it like this:

if(.............................)
{if(.............................)
 {if(.............................)
  {if(.............................)
   {if(.............................)
    {if(.............................)
     {if(.............................)
      {if(.............................)
       {if(.............................)
        {if(.............................)
}}}}}}}}}
Ex Ovo Omnia
3118
Ex Ovo Omnia  

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;
   }  
}
Simon Gniadkowski
17410
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
Ubzen
5300
Ubzen  

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

Simon Gniadkowski
17410
SDC:

i do if else like this

Personally I find that about as clear as mud
Ubzen
5300
Ubzen  
RaptorUK: Personally I find that about as clear as mud
It just hurts my eyes :{
Ahmad Mehdiyev
473
Ahmad Mehdiyev  
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.