How to enforce coding style conventions

  • Thread starter Thread starter Ron Burd
  • Start date Start date
Agreed, and duly noted :)
I just cannot stand to try and read code like that. Again,
period.
 
Anyway, one day I was interested in submitting code to a particular
open-source project. K&R bracing style was used throughout the project.
I decided that my dislike for the style wouldn't get in the way of my
desire to add to the project, so I grinned and beared it.

My experience is the exact opposite. Learning C in the early 80's, I
used the K&R style, because everyone did --- it was just the way things were
done.

A few years later, a changed companies, and the new one mandated the
other code style. I resisted for about 2 days. Then I realized that the
code was so much easier to follow then you didn't have to pause to mentally
unwrap it.

(I had a similar revelation a bit later, when I discovered it's far easier
to read an if() statement, if you don't include an assignment in it, that
is,
prefer:
x = GetValue();
if (x!=0)
{....
over:
if ((x=GetValue())!=0)
and by far over:
if (x=GetValue())


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Jon Skeet said:
Well, one of the reasons I believe K&R became popular was because it
was used in books - and in books, vertical space *is* a relatively
precious commodity. People copy what's written in books, and thus a
tradition is born...

The problem with the "save a line" argument is that it just don't hold
up.

Given the code:
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

it could still be written as:
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}

In exactly the same number of lines, and you still get the advantage of the
braces line up.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
James,

read your post again -- it does hold the save a line, and does it quite well

this is the "wasted-line" example
if (x==y)
{
ThisFunc();
thatFunc();
OtherFunc();
}

these two show the "save-a-line" :

1)
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}
2)
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

I am not at all a fan or advocate of style 1 - the world does not need
another style to further split the community.

For what it worth - Visual Studio 2003 editor preferences has an option in
Text Editor | C# | Formatting | Indentation that reads : "Leave open braces
on same line as construct", effectively helping enforce style 2 of above.
My guess is that appeared because of either:
a) someone on the MS VS/C# team wanted that option
b) significant customer requests for the feature


regards
roy fine
 
James said:
The problem with the "save a line" argument is that it just don't hold
up.

Given the code:
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

it could still be written as:
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}

In exactly the same number of lines, and you still get the advantage of the
braces line up.

I am a newbie and from my point of view, the latter (2nd) option looks
more pleasing...

Regards
Skc
 
Wow!!! I can't believe how this thread has evolved!

Anyway, thank you guys for the help!
 
Roy said:
The later wastes space - one line with ONE character on it!

Are you referring to the "{" or the "}"? If you want to at least be
consistent, you should propose:

if (something) {
DoStuff ();
Do MoreStuff(); }

Hilton
 
Hilton said:
Are you referring to the "{" or the "}"? If you want to at least be
consistent, you should propose:

if (something) {
DoStuff ();
Do MoreStuff(); }

So I guess that VB programmers aren't being consistent unless they write:

If Something Then
DoSomething End If

Right? ;-)
 

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

Back
Top