Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Another style question

Reply
Thread Tools

Another style question

 
 
Ed Morton
Guest
Posts: n/a
 
      02-04-2004


Joona I Palaste wrote:

> Ed Morton <(E-Mail Removed)> scribbled the following:
>
>>If you're going for consistency, why not just run all your code through
>>a C beautifier and just accept whatever it spits out? As long as all the
>>code's consistent in style, the actual style selected doesn't matter a
>>whole lot.

>
>
>>FWIW the UNIX tool "cb -s" (K&R mode) spits out your boss's preferred
>>style for case statements and the final style above for if...else.
>>Without the "-s" it still prefers your boss's case style but doesn't
>>appear to do anything useful with "if...else".

>
>
> Can the C beautifier also beautify Java?
>


Maybe, but I wouldn't trust cb with Java or even C++. There are plenty
of beautifiers for those and other languages out there though, e.g. take
a look at
http://ldp.homegrownhost.net/HOWTO/C...or_others.html

Ed.

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-04-2004
Ed Morton wrote:

> Joona I Palaste wrote:
>
>> Can the C beautifier also beautify Java?

>
> Maybe, but I wouldn't trust cb with Java or even C++.
> There are plenty of beautifiers
> for those and other languages out there though, e.g. take a look at


> http://ldp.homegrownhost.net/HOWTO/C...or_others.html


BEWARE!

Code "beautifiers" are not necessarily [style] reformatters.
Most beautifiers insert text and embedded printer control sequences
that your compiler will *not* accept.

You also need to verify that
reformatting did not change the meaning of your code.
Always compile both the original and the reformatted versions
of your code and verify that the resulting objects are identical.

 
Reply With Quote
 
 
 
 
Thomas Stegen CES2000
Guest
Posts: n/a
 
      02-04-2004
Joona I Palaste wrote:

> Can you please ask whoever came up with that rule what they were
> smoking? =)
>
> Personally I'm a bit of a style Nazi myself. Whenever I have to edit
> code someone else wrote, I take time to format it to "readable" style
> first. Which means:
> - Indents are 2 spaces
> - Braces K&R style: opening brace on the same line, closing on its own
> line, one space before the opening brace
> - Always 1 space after every comma and every semicolon, otherwise
> 1 space around every "important" operator or no spaces at all if it's
> not "important"
> - Always 1 space between *keywords* (if, for, while, etc) and the
> opening paren, never any space between a function or a macro name and
> the opening paren
> - Two blank lines between each function, one blank line separating
> conceptual groups of statements
> That's pretty much the important stuff.


Say what you will, but the above are not very important elements in
determining whether a style is good or bad. Some are more important
than other. I would say spaces around most operators is a good thing.

But in general, as long as layout style is consistent and somewhat
reasonable, naming and commenting conventions are more important.
As are conventions for when to use and not to use macros.

If you find code much harder to read because the opening brace of
a block is on its own line instead of K&R style, then it is not
the code that has a problem.

1. while(condition) {
condition = important();
}

2. while ( condition )
{
condition = important();
}

Point is, 1 vs 2 should be hardly an issue. If you are someone who uses
1 and must work for some people who demand 2 then I don't see that you
have a problem at all.

I use neither of the above, but if someone gave me some reason for why
I should use 1 or 2 I would not have a problem doing that.

I think I would object if someone told me to code like this though:
a=b+c*3;
I think a = b + c * 3; is much better. There is even a physiological
reason for this. I think it is called crowding. It basically means that
human eyes have an easier time distuinguishing objects which are further
apart than close together. So there, I have a non-emotional,
non-subjective reason for this particular style choice. Which means I
win


--
Thomas.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      02-04-2004
Thomas Stegen CES2000 wrote:

> Joona I Palaste wrote:
>
> I think I would object if someone told me to code like this though:
> a=b+c*3;
> I think a = b + c * 3; is much better.


And I prefer

a = c * 3 + b;

YMMV.

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-04-2004
Christopher Benson-Manica wrote:
> Joona I Palaste <(E-Mail Removed)> spoke thus:
>
> > Make those if( cond ) thingies if (cond), and you've got my style
> > pretty much spot-on.

>
> That's a house rule. If I were a style Nazi I would have committed
> seppuku my first week


My argument is that if is not a function, and thus should have a
following blank. If you want blanks surrounding "cond" be my
guest. I want to easily distinguish function calls from other
constructs. Richard Heathfield disagrees.

So, which is the house rule?

BTW, a useful style rule is that the else clause is the longer
one, other things being equal. This makes finding the controlling
statements easier. Contrast:

if (foo) {
....
....
}
else {
/* notmuch */
bar();
}

with
if (notfoo) {
/* notmuch */
bar();
}
else {
....
....
}

finding a controlling statment for something in the else clause
involves first finding the else, and then continuing north. The
second phase is much easier with the short then clause. In the
extreme, when applied to else if clauses, you get:

if (foo) foof();
else if (bar) barf();
else if (goo) goof();
else {
moo();
gou();
gai();
pan();
}

All of this is all very well when you are developing your style.
However once you have something, be consistent, at least through
any single source file. Try things out and see what minimizes the
errors and maximizes the readability.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-04-2004
Joona I Palaste wrote:
>

.... snip ...
>
> Personally I'm a bit of a style Nazi myself. Whenever I have to edit
> code someone else wrote, I take time to format it to "readable" style
> first. Which means:
> - Indents are 2 spaces
> - Braces K&R style: opening brace on the same line, closing on its
> own line, one space before the opening brace
> - Always 1 space after every comma and every semicolon, otherwise
> 1 space around every "important" operator or no spaces at all if
> it's not "important"
> - Always 1 space between *keywords* (if, for, while, etc) and the
> opening paren, never any space between a function or a macro name
> and the opening paren
> - Two blank lines between each function, one blank line separating
> conceptual groups of statements
> That's pretty much the important stuff.


I think we are pretty close to agreement. Try the following for
indent.pro. It is not perfect, but comes as close as I have found
possible to my style.

-kr -l66 -i3 -bad -di16 -lc66 -nce -ncs -cbi0 -bbo -pmt -psl -ts1

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-04-2004
"E. Robert Tisdale" wrote:
> Christopher Benson-Manica wrote:
>
> > How about your if/else if/else constructs?
> > Being [anal] like any good C programmer,

^^^^^^
This is another of Trollsdales gratuitious alterations of quotes.
He is sneaky, very sneaky.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-05-2004
Richard Heathfield wrote:
> Thomas Stegen CES2000 wrote:
> > Joona I Palaste wrote:
> >
> > I think I would object if someone told me to code like this though:
> > a=b+c*3;
> > I think a = b + c * 3; is much better.

>
> And I prefer
>
> a = c * 3 + b;
>
> YMMV.


Definitely MV. I can even find cases where I want different
emphasis, and would write:

a = 3*c + b;
or
dsq = b*b - 4*a*c;

although normally I would not suppress the blanks.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-05-2004
CBFalconer <(E-Mail Removed)> spoke thus:

> So, which is the house rule?


Both - the if( and the ( cond ). Our editor even auto-formats like
this.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      02-05-2004
CBFalconer <(E-Mail Removed)> scribbled the following:
> "E. Robert Tisdale" wrote:
>> Christopher Benson-Manica wrote:
>> > How about your if/else if/else constructs?
>> > Being [anal] like any good C programmer,

> ^^^^^^
> This is another of Trollsdales gratuitious alterations of quotes.
> He is sneaky, very sneaky.


Christopher originally wrote "nitpicky", not "anal". It's fun to see
what Trollsdale will alter *my* message to. =)

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I will never display my bum in public again."
- Homer Simpson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another style/readability question... jackassplus@gmail.com C Programming 3 03-17-2010 10:04 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Yet another nitpicky style question Christopher Benson-Manica C Programming 17 02-20-2004 09:03 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments