Jump to content

Code formatting


bundyxc

Recommended Posts

I'm curious, how to you guys put your brackets?

 

I'm torn between

if (x)
{
    //code
}

else
{
    //code
}

 

 

and

if (x) {
    //code
}

else {
    //code
}

 

The top is easier to read (in my opinion), but it looks like it drags out the code, and gets annoying. What do you guys use, and why?

Link to comment
https://forums.phpfreaks.com/topic/168992-code-formatting/
Share on other sites

Fewest lines taken up by structure and is readable when using proper indentation -

if (x) {
    //code
} else {
    //code
}

 

See, that just looks confusing to me, especially when you start overlapping loops and such.

 

if (w) {
    //code
} else {
    if (y) {
        //code
    } elseif  (z) {
        //code
    } else {
        //code
    }
} elseif  (x) {
//code
}

 

if (w)
{
    //code
}

else
{
    if (y)
    {
        //code
    }

    elseif  (z)
    {
        //code
    }

    else
    {
        //code
    }
}

elseif  (x)
{
    //code
}

Link to comment
https://forums.phpfreaks.com/topic/168992-code-formatting/#findComment-891640
Share on other sites

if (w) {
    //code
} 
else {
    if (y) {
        //code
    } 
    elseif  (z) {
        //code
    }
    else {
        //code
    }
} 
elseif  (x) {
//code
}

there much better. Looks fine to me. BTW this code is invalid =P. cant have an else before an elseif

Link to comment
https://forums.phpfreaks.com/topic/168992-code-formatting/#findComment-891644
Share on other sites

See, that just looks confusing to me, especially when you start overlapping loops and such.

 

So why are you asking? Unless your code has to follow a particular format for business reasons, go with whatever logical format works for you.

 

According to the zend framework standards, which some subscribe to, functions and classes have the brackets on thier own line, but loops and other constructs include them in-line with other code. Personally I prefer to use the more verbose method (your first example) as it makes readability easier - at least for me - especially when going back to code I've written months/years earlier.

Link to comment
https://forums.phpfreaks.com/topic/168992-code-formatting/#findComment-891647
Share on other sites

I'm solely asking because code that is 'easier' to read, looks very empty, and I see more experienced coders using different methods.

 

What you are asking is completely relative to the person coding. 

 

I was just curious if the 'easier' code was syntactically incorrect, etc.

 

This has nothing to do with syntax.  You can put all of your code on a single line if you want to and not receive syntax errors.

Link to comment
https://forums.phpfreaks.com/topic/168992-code-formatting/#findComment-891657
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.