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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.