Programming Style

The currently accepted style for a block definition (function, if..else) seems to be

void function_name(parameters){

function definition ;
}

This seems to be an untidy way of defining a function and makes the code hard to read imo

I have always used the following where the braces line up and you can immediately see the start and end of the block

void function_name(parameters)
{
function definition ;
}

Although it's a personal preference, code that I download usually use the first style.

Are there any real advantages to using the first style ? Why does it seem to be the standard these days ?

hsteve:
I have always used the following where the braces line up and you can immediately see the start and end of the block

That's the one I use.

Are there any real advantages to using the first style ?

Your boss will stop riding your ass when you use it.

Why does it seem to be the standard these days ?

Because this guy made the decision.

Personal preferences and myths is all we really have to go by.

I prefer the the second and with nothing but myth to go on believe the former came about to reduce the volume of paper needed in both publishing language references, articles and such as well as programming print outs pre-portable computer days.

hsteve:
I have always used the following where the braces line up and you can immediately see the start and end of the block

void function_name(parameters)
{
function definition ;
}

Although it's a personal preference, code that I download usually use the first style.

That's the way I've always done it. If you look in the classic -

"The C Programming Language"

By Kernighan & Ritchie, it 's done that way. If it's good enough for them it's good enough for me.

hsteve:
Are there any real advantages to using the first style ? Why does it seem to be the standard these days ?

It reduces the number of lines that a given piece of code takes up. Which used to be important, when we wrote programs on teletype terminals. Nowadays, not so much. I strongly prefer to see the matching braces lined up so that the intended structure of the code is obvious. The only reason I can see these days for using the other style is if it's what you're used to. (I personally don't see that as a good reason to burden another generation of programmers with the same style, but when you're used to something it gets hard to change.)

Personally I use this style:

void setup ()
  {
  Serial.begin (115200);

  }  // end of setup

void loop ()
  {

  }  // end of loop

I think it is logical for the start and end braces to have their own lines, it is just asymmetrical for them not to be like that.

And "things within the braces" should be indented, as are the braces themselves, in my own style universe. :slight_smile:

I also have a space between a function name and the bracket, and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).

[quote author=Nick Gammon link=topic=151874.msg1140391#msg1140391 date=1362272372]... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).
[/quote]

That's a good idea.

If there are a lot of parameters to a function I find this helps readability

function foo (
                      int parameter1,
                      int parameter2,
                      .
                      int parameter n
                   )

  {
      function def;
  }

[quote author=Nick Gammon link=topic=151874.msg1140391#msg1140391 date=1362272372]... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).
[/quote]

No offense Nick but so far as I'm concerned only adds noise. But then again my editor highlights, and can collapse, the code between braces so it very easy to see what belongs to what.

That's fine, but my code is often these days designed to be posted on a forum, where you don't have the benefit of fancy editors.

Plus, I got into the habit years ago. :slight_smile:

@OP

I too don't care for Egyptian Style. #3: http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html

K&R for me...

This is a good one from that link:

8. Heisenbug

A computer bug that disappears or alters its characteristics when an attempt is made to study it.

johncc:
@OP

I too don't care for Egyptian Style. #3: http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html

K&R for me...

LOL, K&R is the Egyptian Style, so you do care for it after all. :wink:

(This style of brackets is used in Kernighan and Ritchie's book The C Programming Language, so it's known by many as K&R style.)

Lefty

@Lefty:

Oops :slight_smile:

Well stuff K&R , I'll stick with

{

}

:0

hsteve:
Well stuff K&R , I'll stick with

{

}

:0

Me too, I got your six mate. :wink:

Lefty

void function_name(parameters)
{
   function definition ;
}

To me, this is the way I code, and i'd refuse to do it any other way.

It makes it neater, easier to read... most other languages work in the same way

Procedure Function_Name(Params...);
Begin

End;

It seems the most easiest and most logical way to code and always will... if i ever had a boss "ride me" until i stopped, i'd probably end up seriously
hurting him one day....

cjdelphi,

I don't know if you meant to or not, but I like to place a space between the code and the closing semi-colon.

Like this:

   function definition ;

rather than this:

   function definition;

I don't mean to be trolling...

If I saw that , I would get rid of it as a typo .

I personally dislike the

int some_function() {
    // calculate something
}

style rather intensely. And it seems quite a lot of other people don't favour it either.
The explanations I have seen for why this style is "better" seem to be rather unconvincing, but
along the lines that with the opening curly brace at the end of the line with the function name,
you can tell that it really is the function definition and not just a declaration. Which is
an unconvincing reason to me. But then, people who write computer code as an end in itself
rather than as a means to an end, often have an interest in writing obsure code.

If I really want to save space, I sometimes do this

int some_function()
{ // start the calculation on this line so the line is not wasted
// and this is the second line of the calculation
}

This avoids one of the (almost) empty lines and the curly braces still line up.