The world's evilest code formatting style

N

Nobody

coders with "poor reading skills", the average coder takes more pride
in their work and in making sure they can be understood. Otherwise we

No, actually, most coders (including myself) don't give a bucket of canadian
moose piss about that and only care that they can maintain the code (to
protect their jobs in times of layoff). Being "the only one who can rapidly
work on this code" saved my ass in the last round of layoffs at my company
and the CEO himself even told me so himself. Its not that I write poor code,
in fact, I take time to VERY nicely format and comment code... when its
something *I* care about... like my personal projects. Going hogwild with
stylization and comments and formatting at work? Why bother? Selfish
attitude? of course... same attitude as every company has towards its
employees. Am I still employed while more concientious workers were let go?
Yup...

And anyone here who tells me they dont have this attitude is full of it...
someone at my work who EVERYBODY there considers the most concientious
worker recently confessed to me that he just gives that appereance, but
doesn't give a crap about the company or the product as long as he is
working on something interesting...
 
N

Nobody

Noah Roberts said:
Personally I feel comments should be rarely required. Your code should
be self documenting and easily understood.

Granted, certain special cases make that difficult and the concepts
will need to be explained...but really, it is rather rare that you get
to do anything 'difficult' with code so most of the time comments
should be redundant.

True... I don't comment every line, and am definitely guilty of using
useless comments as a code formatting crutch... even in my personal comments
I have code like

// clear the menu flags

pDLL->bMenuFlag = FALSE;

// do something else

.....

While everybody in this thread will think thats an insanely dumb comment
(and it is)... it allows me to format the code to my liking.
 
N

Noah Roberts

Axter said:
I'm the second highest ranking Expert in the C++ topic area for the
Experts-Exchange:
http://www.experts-exchange.com/Cplusplus
The Experts-Exchange is a well know controlled forum. (User name Axter)

Oh jeesh...here we go again.

Ok, Mr. Expert...you have no excuse for sending people to that hackery
you call code in dynamic_2d_array then. I then have to assume you are
doing it to purposfully mislead those that are trying to learn C++.

'Cause that crap is crap. No two ways about it. It's riddled with bad
design, bad practice, and basic bugs. These numerous problems have
been pointed out to you several times and you've had plenty of time to
fix it (in fact you had time to make it worse) yet you continue to use
it as an example and hand it off to beginners trying to learn.

Frankly, the fact that a bunch of the poor suckers actually think
you've helped them is sad to say the least.

You know, I have also been published many times. My articles have been
translated to numerous languages including Chinese. But that is
completely irrelivant to whether I know wtf I'm talking about and it is
no different with you. Frankly, it doesn't take much to be published
anymore...especially on some free help website.

How is someone supposed to take you seriously when you call code like
your d2 array 'examples'? I've looked at your smart pointer a small
amount and it didn't show the basic flaws your d2 array does...I didn't
review it all but it definately isn't something I could chew up and
spit out like your d2 array...but you don't seem to be handing that one
out saying, "Here, this is how it is done." What you _are_ handing out
is just garbage and I for one hate seeing it being used for teaching
beginners....fix it already....stop giving out bad advice.
 
N

Noah Roberts

Ian said:
Better yet, just read thought the unit tests.

Unit testing and TDD are not something I was taught in school or have
been mentored into. I have grabbed some books on the subject and am
delighted at how useful I am finding it. I have been thinking of
spending the 2 grand on one of the object mentor courses...especially
if I can get my employer to help. Anyone been to one?
 
I

Ian Collins

Noah said:
Unit testing and TDD are not something I was taught in school or have
been mentored into. I have grabbed some books on the subject and am
delighted at how useful I am finding it. I have been thinking of
spending the 2 grand on one of the object mentor courses...especially
if I can get my employer to help. Anyone been to one?
Good to see someone with an open mind.

I don't have any experience with Object Mentor, but they do have a good
reputation.

Try asking on one of the XP or TDD yahoo lists.

Good luck,
 
N

Noah Roberts

Ian said:
Good to see someone with an open mind

Yes well, I learned long ago that a closed mind is malfunctional and
try to remain as open as I can. Besides, it's hard to argue with
success.
I don't have any experience with Object Mentor, but they do have a good
reputation.

They certainly write good books.

Actually, I was conversing with them on Friday before I went home for
the weekend. If I could get 10 people in my area interested I could
probably get them up here and in a classroom. I'm in Lacey, WA. I get
the 10 I get my ride for free but would likely end up spending it on
the venue.

I'm interested in their TDD course that includes unit testing and
refactoring. It's 3 days and costs 2 grand. Anyone else interested?
Can't promise anything but if I can get enough people saying they want
to go that will in fact sign up I would be more than happy to continue
trying to get it happening.
 
P

Phlip

Nobody said:
No, actually, most coders (including myself) don't give a bucket of
canadian moose piss about that and only care that they can maintain the
code (to protect their jobs in times of layoff). Being "the only one who
can rapidly work on this code" saved my ass in the last round of layoffs
at my company and the CEO himself even told me so himself.

Let me get this straight. The company rewarded heroism by dismissing the
team and keeping the hero. And now the boss's boss knows his next target.

Your only hope here is to make sure your bosses never learn that code can be
easily shared among programmers, particularily if it has short methods and
extensive unit tests.

Start at the top and work your way to the bottom of this checklist.

http://c2.com/cgi/wiki?JobSecurity
Its not that I write poor code, in fact, I take time to VERY nicely format
and comment code...

Ah, so it has short lines and methods, very little duplication, and eeeeunit
tests?
 
P

Phlip

Noah said:
Unit testing and TDD are not something I was taught in school or have
been mentored into. I have grabbed some books on the subject and am
delighted at how useful I am finding it. I have been thinking of
spending the 2 grand on one of the object mentor courses...especially
if I can get my employer to help. Anyone been to one?

If you are near Southern California I can save you 1 grand. ;-)

http://www.zeroplayer.com/cgi-bin/wiki?TestFirstUserInterfaces
 
R

Ron House

Phlip said:
Kai-Uwe Bux wrote:




Because our last best hope for clarity among nested blocks is the ability to
see { in the same column as }.

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

The source of this style, afaik, was a paper by AJ Sale proposing a
similar style for Pascal. The reason is to align the block structure of
the language's logical structure with the line-oriented layout of a text
document. Each language structure looks like:

starting template
...indented contents...
ending template.

So, your starting template is:

for (...;...;...) {

All of it is compulsory because it is a cookie-cutter template. That
means no 'naked' statements inside loops unprotected by curlies. To take
your example, another style might write:

for (int i = 0; i < 100; i++)
{
printf("whatever\n");
}

But of course, no one does, precisely because of two wasted lines. That
matters: too low an information density on a screen or page lowers
comprehension in the same way as too high a density. So people write:

for (int i = 0; i < 100; i++)
printf("whatever\n");

Then they remember they forgot a line, so they insert it:

for (int i = 0; i < 100; i++)
k = 2;
printf("whatever\n");

and then they have a bug. I have used Sale's style, adapted for C and
C++, for decades, taught it to classes, and seen with my own eyes how it
lowers the bug level. It puts a good information density on a page,
aligns block structure with line structure, and allows spotting errors a
line at a time. Any line starting with 'for' and not ending with '{' is
wrong.
 
N

Noah Roberts

Ron said:
for (int i = 0; i < 100; i++)
{
printf("whatever\n");
}

But of course, no one does, precisely because of two wasted lines. That
matters: too low an information density on a screen or page lowers
comprehension in the same way as too high a density.

This is precicely what I do with one liners (except the braces go
either on the first column or have one indent and the code another).
I've never seen any case where this actually lowers readability.
 
P

Phlip

Ron said:
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.
 
J

Jim Langston

Jim Langston said:
I have two, one easy to fix.

Some code I work with has no indentation whatsoever, everything is left
justified. This is easy enough to fix in my IDE though, I just select
everthing and hit a few keys to auto indent. Takes me a few seconds.

The second is code someone has me work with sometimes. Not only does it
not have any indents, it has absolutely no whitespace, and many statements
on one line. This takes a while to format so I can read it.

Oh, I forgot about something else this 2nd person does. He puts functions
in .h files. So he only has one .cpp file and everything else is included
in .h including functions, etc... Very bad, but I couldn't convince him of
that.
 
S

Steve Pope

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.

In the original for the above code snippet, was there white space
at the beginning of each line, or were the lines (particularly
the one starting with "inherits") unindented?

If the latter, it is probably a continuation of an old C
formatting style wherein the definitions of each function
had the function name always the beginning of a line. This
makes it easy to find the function definition (as opposed
to function calls) with a text editor search.

Such formatting is obsolesced by modern tools, but was useful
in its day, and I used it myself (when working in C) up to about
five years ago.

Steve
 
P

Phlip

Steve said:
If the latter, it is probably a continuation of an old C
formatting style wherein the definitions of each function
had the function name always the beginning of a line. This
makes it easy to find the function definition (as opposed
to function calls) with a text editor search.

My favorite:

void
kozmik()
{
}

That is beyond reproach. It puts the most important thing in the first
column, and the lessor thing.

Now replace 'void' with some huge type from hell, and that style gets even
better.
Such formatting is obsolesced by modern tools, but was useful
in its day, and I used it myself (when working in C) up to about
five years ago.

void
Foo::kozmik()
{
}

But the code I got that sample from was post-modern. It used Boost::Spirit,
and it used multiply nested namespaces. I'm beginning to think most of those
things are nothing but cures in search of diseases...
 
N

Noah Roberts

Phlip said:
My favorite:

void
kozmik()
{
}

That is beyond reproach. It puts the most important thing in the first
column, and the lessor thing.

How about this:

void f()
{
if (preconfail)
goto end;

while (something)
{
... do stuff
if (whatever)
goto end;
}

if (more stuff)
{
do X;
}
else
{
goto end;
}

end:
return;
}

Yes, I in fact ran into that in the REAL world.
 
S

Steve Pope

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

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

Certainly is. It should be:

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

The idea is that to find the left brace for a given right
brace, go upwards until you find a line less indented than
the right brace.

The exception being the braces for the function itself,
which are both un-indented.

(Not that there aren't many other acceptable ways of
doing it...)

Steve
 
J

John L Fjellstad

Phlip said:
That's not "preferred" - it's an artifact of writing a book. The "rationale"
is only to save vertical space and make room for your wondrous prose.

Then why, for functions, they put brackets on a new line. If it was just to
save vertical space, why not be consistent?

And what's wrong with saving vertical space again?
 
I

I V

How about this:

void f()
{
if (preconfail)
goto end;
....

end:
return;
}

Yes, I in fact ran into that in the REAL world.

Well, everyone knows that structured code should only have one return
statement. If you have to use gotos to make sure you have structured
code, that's a sacrifice you'll just have to make.
 
N

Nobody

Steve Pope said:
Certainly is. It should be:

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

The idea is that to find the left brace for a given right
brace, go upwards until you find a line less indented than
the right brace.

The exception being the braces for the function itself,
which are both un-indented.

(Not that there aren't many other acceptable ways of
doing it...)

Steve

Thats even stupider... not only are you being inconsistent in your use of
braces... but your comment of "look at the indents to find the matching
brace" smells of .... well... lets be nice... but thats dumb... why would
you look at indents to match a brace when you could look at the MATCHING GOD
DAMN BRACE?!?!?!

Correct formatting is:

void Whatever()
{
for (int i = 0; i < 100; i++)
{
printf("whatever\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("whatever\n");
}

Have a nice day you stinkin' right bracer! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top