Go Down

Topic: Programming Style (Read 2686 times) previous topic - next topic

hsteve

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 ?

Coding Badly

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.

Quote
Are there any real advantages to using the first style ?


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

Quote
Why does it seem to be the standard these days ?


Because this guy made the decision.

lloyddean

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.

modeller


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.

PeterH


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.)
I only provide help via the forum - please do not contact me for private consultancy.

Nick Gammon

Personally I use this style:

Code: [Select]
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. :)

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.).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

modeller

... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).


That's a good idea.

hsteve

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

Code: [Select]


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

  {
      function def;
  }


lloyddean

... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).


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.

Nick Gammon

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. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

johncc


Nick Gammon

This is a good one from that link:

Quote

8. Heisenbug


A computer bug that disappears or alters its characteristics when an attempt is made to study it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty


@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.  ;)

Quote

(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

johncc


hsteve

Well stuff K&R , I'll stick with

{

}

:0

Go Up