473,756 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The world's evilest code formatting style

C++ers:

Feast your eyes:

void Home::
inherits (IdentifierPtr const& id)
{
...
}

For those of you who haven't figured it out yet, that's the method
Home::inherits( ).

I suppose the theory is that Home:: is an enclosing type, so it should
pretend to wrap the method, like a namespace.

Now that's not evil just because it's so contrary to common styles and hence
hard to read. It's totally evil when it makes Home::inherits impossible to
search for. Searching, for big projects, is kind'a important!

Can anyone think of an eviler style, seen in published code? ;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 4 '06
93 3884
Nobody wrote:
Correct formatting is:

void Whatever()
{
for (int i = 0; i < 100; i++)
{
printf("whateve r\n");
}
}

And technically, its impolite to brace single lines (single SOURCE lines,
not single code lines... ie... if your single line of source wraps multiple
lines, it should be braced), but in the above case... CORRECT & POLITE
formatting is:

void Whatever()
{
for (int i = 0; i < 100; i++)
printf("whateve r\n");
}


Except for when your project's official style guide says, "Thou *SHALT*
enclose if statements and for loops in braces."
Jun 5 '06 #81
Alf P. Steinbach wrote:
The final revelation which made me stop was that the style does not
support inline blocks, which in turn means it doesn't support the
important guideline

* in C++, declare variables close to their first usage, and limit
their scopes as much as practically possible.

E.g., the code

for( int i = 0; i < 100; ++i )
{
std::cout << "whatever\n ";
{
double const x = f( i );
// Do things with x
}
{
double const y = f( i );
// Do things with y
}
}

becomes less than readable with the indented braces style,

for( int i = 0; i < 100; ++i )
{
std::cout << "whatever\n ";
{
double const x = f( i );
// Do things with x
}
{
double const y = f( i );
// Do things with y
}
}

Not that this kind of thing occurs very often in practice, but it's the
principle.


I agree with your principle, certainly, but in multithreaded
programming, local blocks like that are not so rare if a ScopedLock
pattern is used (see, e.g., Boost.Threads' boost::mutex::s coped_lock
which is demonstrated here
http://www.ddj.com/showArticle.jhtml...5kempf&pgno=5).

Cheers! --M

Jun 5 '06 #82
Noah Roberts <ro**********@g mail.com> wrote:

JustBoo wrote:
On Sun, 4 Jun 2006 00:54:24 -0700, "Nobody" <no****@cox.net > wrote:
>Although that sucks... its not as bad as people who "right brace"... so
>called "right bracers"... ie:
>
>void Whatever() {
> for (int i = 0; i < 100; i++) {
> printf("whateve r\n");
> }
>}
>
>Thats just dumb beyond dumb. A guy I worked with actually said he does it
>like that because "putting it on the next line just wastes a line". I'm
>thinking the fact that he has a brain is just a waste of precious brain
>cells that others could better utilize.
So Kernighan and Ritchie - the two guys who *invented C* - are just
"dumb beyond dumb" eh?

You do relaize that style is called the Kernighan and Ritchie (K&R)
Style right?


If the style can't stand on its own without an appeal to authority then
it can't stand on its own.

[...] I'll leave you to your hero worship and remain...unconv inced.


If the style can't stand on its own without an ad hominem argument then
it can't stand on its own either.

I can see benefits and disadvantages to both styles.

The most important thing IMHO is that the style has a valid rationale,
and that it is applied consistently to a body of code. As I see it,
both styles have valid rationales (though with slightly different aims).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 5 '06 #83

Marcus Kwok wrote:
The most important thing IMHO is that the style has a valid rationale,
and that it is applied consistently to a body of code.


I don't see that it even needs to be applied consistently. I run into
lots of code with a miriad of brace styles and I hardly even
notice...except I don't like the right bracing. But either way I can
read it and even when a function has two or more brace styles in it I
haven't found it any harder to read.

Jun 5 '06 #84
On 5 Jun 2006 08:38:48 -0700, "Noah Roberts" <ro**********@g mail.com>
wrote:
You do relaize that style is called the Kernighan and Ritchie (K&R)
Style right?


What does that matter?
Answer: not a damn thing.

If the style can't stand on its own without an appeal to authority then
it can't stand on its own. I'm not ready to bow down and worship K&R
or Bjarne.


Umm, well what can you say to that. Man.... Perhaps someone has
messianic issues?

Hint: You are NOT the chosen one.

He was the sort of person who stood on mountaintops during
thunderstorms in wet copper armour shouting "All the Gods are
bastards." - Terry Pratchett
Jun 5 '06 #85

JustBoo wrote:
On 5 Jun 2006 08:38:48 -0700, "Noah Roberts" <ro**********@g mail.com>
wrote:
You do relaize that style is called the Kernighan and Ritchie (K&R)
Style right?


What does that matter?
Answer: not a damn thing.

If the style can't stand on its own without an appeal to authority then
it can't stand on its own. I'm not ready to bow down and worship K&R
or Bjarne.


Umm, well what can you say to that. Man.... Perhaps someone has
messianic issues?

Hint: You are NOT the chosen one.


I'll just say that is quite a leap and leave it at that. You're way
too invested in this silly issue.

Jun 5 '06 #86
On 5 Jun 2006 14:48:05 -0700, "Noah Roberts" <ro**********@g mail.com>
wrote:
JustBoo wrote:
On 5 Jun 2006 08:38:48 -0700, "Noah Roberts" <ro**********@g mail.com>
wrote:
>> You do relaize that style is called the Kernighan and Ritchie (K&R)
>> Style right?
>
>What does that matter?
>Answer: not a damn thing.
>
>If the style can't stand on its own without an appeal to authority then
>it can't stand on its own. I'm not ready to bow down and worship K&R
>or Bjarne.


Umm, well what can you say to that. Man.... Perhaps someone has
messianic issues?

Hint: You are NOT the chosen one.


I'll just say that is quite a leap and leave it at that. You're way
too invested in this silly issue.


Now you're indulging in Psychological Projection. The very behavior
you're accusing me of is actually your behavior. Let's review shall
we.

Who just went on an emotional rant that had little to do with
programming and everything to do with "hero" worship.

Let's see, it was not me, so that only leaves... you. I hope you can
figure out the rest on your own. Well, probably not….

Men occasionally stumble over the truth, but most
of them pick themselves up and hurry off as if
nothing had happened. - Winston Churchill
Jun 5 '06 #87
Phlip wrote:
Ron House wrote:

Only if you are in the habit of inconsistency in using braces.

I'm sorry, but despite you have taught classes, you seem to have avoided
teaching me exactly what style you are talking about.

Please just write your favorite kind of if{} block here, without lectures or
alternatives. I promise not to bust on it.


if (...) {
...
} else if (...) {
...
} else if (...) {
...
} else {
...
}

There are four line-oriented cookie-cutter templates here:

start if
else if
else
end if

Some languages define special keywords for these, but with this
line-oriented discipline, it is safe and readable. The only statement
allowed after else without a protecting set of braces is another if, and
that too only if it is written using the line-oriented template. So,

if (...) {
...
} else
if (...) {
...
}

is not allowed.

--
Ron House ho***@usq.edu.a u
http://www.sci.usq.edu.au/staff/house
Ethics website: http://www.sci.usq.edu.au/staff/house/goodness
Jun 6 '06 #88
Nobody wrote:
"Steve Pope" <sp*****@speedy mail.org> wrote in message
news:e6******** **@blue.rahul.n et...
<Vkwgg.178431 $bm6.26661@fed1 read04>, Nobody <no****@cox.net > wrote:

... its not as bad as people who "right brace"... so
called "right bracers"... ie:

void Whatever() {
for (int i = 0; i < 100; i++) {
printf("whateve r\n");
}
}
Thats just dumb beyond dumb.

... And technically, its impolite to brace single lines (single SOURCE lines,
not single code lines... ie... if your single line of source wraps multiple
lines, it should be braced), but in the above case... CORRECT & POLITE
formatting is:

void Whatever()
{
for (int i = 0; i < 100; i++)
printf("whateve r\n");
}


And here we have the source of countless runtime errors. Needless to
say, anyone who thinks the safest style is "dumb beyond dumb" (not to
mention USING CAPS) will zero in infallibly on one of the most
error-prone and inconsistent styles possible.

--
Ron House ho***@usq.edu.a u
http://www.sci.usq.edu.au/staff/house
Ethics website: http://www.sci.usq.edu.au/staff/house/goodness
Jun 6 '06 #89

Well, since we can't agree on bracing, and it is soooooo important....I
say we stop using them:

int main()
<%
int x;
if (x = 1) <% cerr << "OH SHIT!!\n"; %>

return 0;
%>

Jun 6 '06 #90

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.