Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Style guide question

 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am busy to 'make' a style guide for C++
and therefore looking at 'our' styleguide here.
From Linux Thorwald: http://www.cs.umd.edu/users/cml/cstyle/LinuxKernelCodingStyle.txt
excerpt gotton from ^^^^
-----------excerpt--------------
The other issue that always comes up in C styling is the placement of
braces. Unlike the indent size, there are few technical reasons to
choose one placement strategy over the other, but the preferred way, as
shown to us by the prophets Kernighan and Ritchie, is to put the opening
brace last on the line, and put the closing brace first, thusly:
if (x is true) {
we do y
}
However, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:
int function(int x)
{
body of function
}
Heretic people all over the world have claimed that this inconsistency
is ... well ... inconsistent, but all right-thinking people know that
(a) K&R are _right_ and (b) K&R are right. Besides, functions are
special anyway (you can't nest them in C).
Note that the closing brace is empty on a line of its own, _except_ in
the cases where it is followed by a continuation of the same statement,
ie a "while" in a do-statement or an "else" in an if-statement, like
this:
do {
body of do-loop
} while (condition);
and
if (x == y) {
..
} else if (x > y) {
...
} else {
....
}

Rationale: K&R.
Also, note that this brace-placement also minimizes the number of empty
(or almost empty) lines, without any loss of readability. Thus, as the
supply of new-lines on your screen is not a renewable resource (think
25-line terminal screens here), you have more empty lines to put
comments on.
-------------------end excerpt------
I would like to here reasons why our stylguide
differs from K&R, especially the start
of a "{" after an if-statement.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all the reasons why the JavaRanch style guide is the way it is, you might have to ask higher powers...
In my opinion though, having both braces on the same line and indenting the content within the braces makes it much easier to locate blocks and sub-blocks of code.
It's balanced and easier for the eye to find the pattern (and if the brain isn't working so fast that can be very helpful ).
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun Microsystems uses the K&R style braces too, but in my opinion the JavaRanch Style Guide causes code to be more readable.

"think 25-line terminal screens here" When is the last time you were limited to a 25 line screen?

Also, the focus is on saving precious bytes in the source file. When is the last time you were worried about wasting one byte in this age of gigabytes/terabytes?

In my opinion it's easier to find missing braces in your code (debugging).

You probably already read this comment "Reasoning: .... Plus, less editing is involved if lines of code are added or removed." in the Style Guide, right?

But, since I didn't write the Style Guide, I've asked the author, Paul Wheaton, to come and comment on his reasons for choosing the pascal-style.
 
paul wheaton
Trailboss
Pie
Posts: 21741
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks from both camps will claim "it is easier to read".
In the old days, doing it that way was preferred because it would reduce the amount of disk and screen real estate used. Nowadays, I would have to say that the importance of that is a small fraction of what it used to be.
I did C and C++ for eleven years before becoming a java junkie and I always used the pascal style brace placement.
Now, with Java, I have surveyed dozens of java people that know nothing of my style guide and in this small sample of engineers, pascal style beats K&R style 2 to 1.
The most important thing? When in Rome ..... I'm currently working on a project where the first guy here used K&R - so I use K&R. A professional will always comform. When a new guy comes on and insists we change the style, I start thinking about getting rid of the guy: the productivity to headache ratio is going to not be worth it.
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most important thing? When in Rome ..... I'm currently working on a project where the first guy here used K&R - so I use K&R. A professional will always comform. When a new guy comes on and insists we change the style, I start thinking about getting rid of the guy: the productivity to headache ratio is going to not be worth it.
Well said Paul !!
The most important thing is that a style guide is used and enforced. There is nothing worse than having to maintain a system where every programmer uses/ed his/her own style. Cant tell you how frustrating it is when you are trying to find a problem in a system at 03:00 in the middle of winter, when the programs you have to work through to find the bug all use different styles even multiple styles in one program.
Quite a few Dutch colleagues of mine learnt a few typical South-African swear words when I got hold of them the next morning I promise you
Personally I also prefer the way "we" do braces over here
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
I will propagate 'our'(=Cattledrive) style and by the way, my son, busy with C++ programming professionally does { under } too it is another reason isn't it .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic