topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday April 16, 2024, 10:14 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Coding Practices Question: Curly brackets on a new line for else statement?  (Read 13878 times)

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,748
    • View Profile
    • Read more about this member.
    • Donate to Member
Okay, so when I first was learning programming, I learned to create code blocks with the curly bracket on the starting line, like so:

Code: ActionScript [Select]
  1. function myFunction() {
  2.   // multiple lines of code here
  3. }

But since I've been using Torque Game Builder and coding in TorqueScript, the standard practice is to open the block of code on a new line so that the opening and closing brackets are lined up:

Code: ActionScript [Select]
  1. function myFunction()
  2. {
  3.   // multiple lines of code here
  4. }

That's fine and all, and I've successfully made the transition in coding style, and this topic isn't really about the personal preference of where to put the opening bracket of a code block. The question really is, how does this style work when immediately opening up another block of code, for example from an else statement? This is how it seems natural to me, coming from my background putting the bracket on the same line as the code that indicates I'm about to open a block of code (see line 4 below).

Code: ActionScript [Select]
  1. if (statement)
  2. {
  3.   // multiple lines of code here
  4. } else {
  5.   // multiple lines of code here
  6. }

But if I'm trying to stay consistent with putting opening brackets on a new line, should I do it like this?

Code: ActionScript [Select]
  1. if (statement)
  2. {
  3.   // multiple lines of code here
  4. } else
  5. {
  6.   // multiple lines of code here
  7. }

But then that looks weird, so should it be

Code: ActionScript [Select]
  1. if (statement)
  2. {
  3.   // multiple lines of code here
  4. }
  5. else
  6. {
  7.   // multiple lines of code here
  8. }

But then the else looks strange all by its self.

I'm confused... Any tips and reasons on the "proper" or "best" way to handle this?
« Last Edit: August 27, 2009, 11:12 PM by Deozaan »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,900
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
there is no one best way -- there are lots of different styles, some with names.

i'm a fan of Whitesmiths style, which basically is closest to your last one.  But thats not nearly so common nowadays..

see here for more:
http://en.wikipedia....rg/wiki/Indent_style

i guess my advice is: always prefer clarity over all else -- id rather have 10 lines than 5 if the the 10 lines make clearer what's going on.
putting brackets on the same line as the if else stuff makes it harder to line up with its partner, which is why i dislike it.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
As mouser said, there is no one correct way.  Personally, I use the Allman/ANSI style...as in your last example.

app103

  • That scary taskbar girl
  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 5,884
    • View Profile
    • Donate to Member
Personally, I prefer this style, because if you have multiple nested statements, it's a lot clearer to me what goes with what, if your indenting lines them up like this:

Code: ActionScript [Select]
  1. if (statement)
  2.   {
  3.     // multiple lines of code here
  4.   }
  5. else
  6.   {
  7.     // multiple lines of code here
  8.   }

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Code: C [Select]
  1. if (statement){
  2. // multiple lines of code here
  3. } else {
  4. // multiple lines of code here
  5. }
That's the form I prefer. I do change the style depending on the language/environment I'm coding for,though. I think it's important not to get too attached to a coding style. It's important to respect the code style other expect to see, being stubborn won't help anyone.

On what you asked, I think the correct form would be

Code: C [Select]
  1. if (statement)
  2. {
  3. // multiple lines of code here
  4. }
  5. else
  6. {
  7. // multiple lines of code here
  8. }
  9. if (another statement)
  10. {
  11. //lines here
  12. }

or you'd have to write the previous code like this to respect the form you mentioned:
Code: C [Select]
  1. if (statement)
  2. {
  3. // multiple lines of code here
  4. } else
  5. {
  6. // multiple lines of code here
  7. } if (another statement)
  8. {
  9. //lines here
  10. }
That just looks weird :)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,646
    • View Profile
    • Donate to Member
Ironically I like the first example/style for the same reason app103 gave. However I (created a habit for myself) tend to indent the else line by one space like so:
Code: ActionScript [Select]
  1. function myFunction() {
  2.   // multiple lines of code here
  3.  }else{
  4.   // multiple lines of code here
  5. }

While I chalk it up to bad habits caused by self teaching ... and (defensively) agree (to cover my sins) that it's a personal preference thing. I think Deozaan was asking in the context of which style is more universally accepted as proper & good. e.g. Which style would best serve one if they were including code samples with their resume. ...Which given the direction the economy has been going I'd like to know too.

fenixproductions

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,186
    • View Profile
    • Donate to Member
2Deozaan
Your last sample may be strange but this is what I am using :)

The most important thing is: is it readable to you? Can someone else look into it and get an idea right away? If yes, format it whatever you like.
And if you don't want to lok on foreign code because of its look, you can find some tools over Internet to make all syntax up to your liking.

BTW I've started with K&R in the past but moved into current style because it is easier to me to read such code after longer absence. My brain gets overloaded if there is too much stuff in one line ;)
« Last Edit: August 28, 2009, 05:56 AM by fenixproductions »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,900
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
The style I use, which most closely matches whitesmiths is:

Code: C++ [Select]
  1. if (statement)
  2.     {
  3.     // multiple lines of code here
  4.     }
  5. else
  6.     {
  7.     // multiple lines of code here
  8.     }

Lashiec

  • Member
  • Joined in 2006
  • **
  • Posts: 2,374
    • View Profile
    • Donate to Member
Unless you're collaborating in a project which has a defined standard, or working in a company with is own coding style guidebook (the only one I can remember now is Google), it's a matter of personal preference, thus there is no objective "best" way to do it. In the case you ask, the correct way to do it would the one João proposes, to maintain consistency, although I have seen many people mixing various of the styles shown here in the same block of code, something like this:

Code: C++ [Select]
  1. function myFunction()
  2. {
  3.     // multiple lines of code
  4.     if (statement)
  5.         {
  6.             // multiple lines of code
  7.         }
  8.         else
  9.             // single line of code
  10. }

or like this:

Code: C++ [Select]
  1. function myFunction()
  2. {
  3.     if (statement)
  4.         // single line of code
  5.     else {
  6.             // multiple lines of code
  7.          }
  8. }

The examples above were extracted from real code, and both functions were contiguous to each other and written by the same person (not me, though :P)
« Last Edit: August 28, 2009, 07:17 AM by Lashiec »

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Single-line if statements without brackets are just evil..
The only way I can stand those is in this form:
Code: C [Select]
  1. if (statement)  //single line of code

being that if it has an else case, I go back to
Code: C [Select]
  1. if (statement){
  2.   // single line
  3. } else {
  4.   //single line
  5. }

Actually, to be fair I don't really do this, but eclipse corrects it for me! It's really useful because it makes it easier to see errors (if something got the wrong formating, I did something wrong :P).

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
ps: I really like how eclipse formats code down to spaces in function calls, between the 'if' and the condition, between the left and right side of an attribution, etc etc... It's great to have all your code respecting the same norm, makes it much easier to understand :D

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,009
    • View Profile
    • Donate to Member
Ahh, the religious war finally arrives on the DC shores  :o

In my experience working with many developers, the K&R style is almost universally derided, while the vast majority of developers prefer the Allman style.

However, more recently a couple of guys have convinced me of the philosophical superiority of the Whitesmith style that Mouser referenced. There's no practical difference relative to Allman. But in the context of the BNF for the grammar of C or C++ (see http://www.nongnu.org/hcb/ ), looking at the definition of statement and compound-statement, you can see that what you're indenting is, in any case, some kind of statement. The Allman approach would have you indenting the opening of a compound-statement at the same level as its controlling selection-statement. The Whitesmith style is entirely consistent, though.

From a practical perspective, either of these is superior to K&R because they allow you too comment out things like the controlling selection clause and still get a program that is syntactically correct. But with K&R-style, the opening curlybracket would have been commented out as well, breaking the code.

Let me add this to the controversy: with my style, single-statement blocks are never legal. Any block, even if it's just a single statement, must be enclosed in curly brackets. I've seen too many bugs that are the result of adding a second statement to what you'd like to have done in a conditional -- but since there were no curlies, that second statement is executed in all cases. Simply using curlies all the time prevents this from ever happening. (and it's also one reason why python's whitespace-based blocking is better)

And another thing: whenever possible, when doing comparisons, always try to have the lefthand side of the "==" comparison operator be an rvalue rather than an lvalue (that is, it's an expression that can't legally be assigned to). This prevents accidental assignment if one of the equals signs is inadvertently omitted.

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,748
    • View Profile
    • Read more about this member.
    • Donate to Member
Apparently when I started practicing indent styles, I used the 1TBS.

But since I've been working with Torque, it appears I've started using the Allman/ANSI style.

It wasn't my intent to start an indenting style religious war. This topic wasn't asking for a discussion on the superior indenting style. Now that I know these things have names, I think I can more adequately phrase my question:

How would one correctly vertically space the brackets of an if/else statement using Allman style? It appears to me that the answer is my last example in the original post.

Thanks for the feedback and information.

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
I don't think anyone in this topic entered a religious war... I only see constructive criticism ;)