Pages: [1] 2 3   Go Down
Author Topic: Programming Style  (Read 2295 times)
0 Members and 1 Guest are viewing this topic.
Orange, NSW, Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 208
Posts: 12940
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

TX
Offline Offline
Jr. Member
**
Karma: 2
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18816
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I use this style:

Code:
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. smiley

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.).
Logged


TX
Offline Offline
Jr. Member
**
Karma: 2
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

That's a good idea.
Logged

Orange, NSW, Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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

  {
      function def;
  }
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... 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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18816
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley
Logged


Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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...
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18816
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.  smiley-wink

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
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Lefty:

Oops smiley
Logged

Orange, NSW, Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well stuff K&R , I'll stick with

 {

 }

 smiley-mad
Logged

Pages: [1] 2 3   Go Up
Jump to: