ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Coding Practices Question: Curly brackets on a new line for else statement?

(1/3) > >>

Deozaan:
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 ---function myFunction() {  // multiple lines of code here}
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 ---function myFunction(){  // multiple lines of code here}
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 ---if (statement){  // multiple lines of code here} else {  // multiple lines of code here}
But if I'm trying to stay consistent with putting opening brackets on a new line, should I do it like this?


--- Code: ActionScript ---if (statement){  // multiple lines of code here} else{  // multiple lines of code here}
But then that looks weird, so should it be


--- Code: ActionScript ---if (statement){  // multiple lines of code here}else{  // multiple lines of code here}
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?

mouser:
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.org/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:
As mouser said, there is no one correct way.  Personally, I use the Allman/ANSI style...as in your last example.

app103:
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 ---if (statement)  {    // multiple lines of code here  }else  {    // multiple lines of code here  }

jgpaiva:

--- Code: C ---if (statement){// multiple lines of code here} else {// multiple lines of code here}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 ---if (statement){// multiple lines of code here} else {// multiple lines of code here}if (another statement){//lines here}
or you'd have to write the previous code like this to respect the form you mentioned:

--- Code: C ---if (statement){// multiple lines of code here} else {// multiple lines of code here} if (another statement){//lines here}That just looks weird :)

Navigation

[0] Message Index

[#] Next page

Go to full version