if (expression) check

K

Keith Thompson

Eric Laberge said:
Keith said:
I recently read a very interesting essay by Ken Arnold, in the Joel
Spolsky's book "The Best Software Writing I". He advocates making
coding style part of the syntax of the programming language. (He's
talking about programming languages in general, not just C.) [example]
Programmers would quickly adapt, simply because they'd have to. You
don't hear complaints about C keywords being in lower case, because if
you try to use "IF" rather than "if" your code won't compile.
Likewise, if your code would no longer compile if you used a
non-standard layout, you'd quickly learn to conform.

The result: Not only would code be more consistent and easier to read,
but we'd stop wasting time arguing about where the braces go.
[etc.]

I think this is indeed a good idea, but I believe the *computer* should be
in charge of the coding style.

What I have in mind is something like the unix "indent" tool, with more
power/customization/a pretty configuration GUI and integrated in the design
environment or ran stand-alone before the code is uploaded to the revision
control system/repository/whatever and after it is downloaded from it.

This way, every coder can have his own coding style, and it will all look
the same for each other. This seems to me like it would be easy, just a
matter of parsing the code (keeping the comments) and rewriting it using
pre-defined or custom guidelines.

Of course, there would still be the need for comments and code documentation
standards, but this could eliminate style wars like
if (a == 0)
versus
if (0 == a)
or
if (condition) do_stuff();
versus
if (condition)
{
do_stuff();
}

Now feel free to totally crush this idea :^)

Ok. :cool:}

Sorry, but if we were to impose style as part of the syntax, I don't
see much benefit in allowing each programmer to write in a different
style. Just define a single style and enforce it. Smart text editors
can help a lot for programmers who want to use them; the programmer
can type his parentheses and braces anywhere he likes, and they'll
*immediately* be put in the right place. Personally, I don't use a
syntax-aware editor (beyond auto-indent); I'll just write the code
correctly in the first place.

This is as much about the programmer's mindset as he's writing the
code as it is about anything else. The point is to end the debate and
spend our time on more important things.

Of course, nobody is going to stop anybody from creating whatever
tools they like, as long as the end result is syntactically correct
source code. But *if* this idea were to catch on, there would be no
more reason to let each programmer have his own coding style than to
let each programmer have his own idea of how reserved words should be
spelled. It's easy enough to create a syntax-aware editor that
transforms "IF" to "if" and "ELSE" to "} else {", but nobody has
bothered to do so.

Surely placement of punctuation marks isn't the best way you can think
of to express your individuality and creativity.
 
C

CBFalconer

Keith said:
.... snip ...

See also Henry Spencer's 8th Commandment:

Thou shalt make thy program's purpose and structure clear to thy
fellow man by using the One True Brace Style, even if thou likest
it not, for thy creativity is better used in solving problems than
in creating beautiful new impediments to understanding.

<http://www.lysator.liu.se/c/ten-commandments.html>

Perhaps some particular style really is more efficient than the
"standard" one -- but it's vanishingly unlikely that the increase in
efficiency outweighs the countless person-hours wasted arguing about
it.

At least for C it is worthwhile to keep a standard configuration
for indent available. The following (one line) is the one I use,
which results in something very close to my preferred style with
indent 2.2.9.

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

Christian Bau

Keith Thompson said:
Sorry, but if we were to impose style as part of the syntax, I don't
see much benefit in allowing each programmer to write in a different
style. Just define a single style and enforce it.

On the other hand, your average programmer uses a programming editor
that leaves a few billion operations per second unused, and that is
plenty of time to display code and let me edit code in the style that
_I_ like, and lets you see code and edit code in the style that _you_
like.

Not trivial, but not really difficult either. Probably works best if a
source code control system defines a style X and translates everything
from and to style X when some programmer edits a file.
 
R

Richard Bos

Keith Thompson said:
For example, if it were decided that if statements should be written
like this:

if (condition) {
...
}

then the following:

if( condition )
{
...
}

would be a syntax error.
The result: Not only would code be more consistent and easier to read,
but we'd stop wasting time arguing about where the braces go.

Drawbacks: None that I can think of (except that it's not likely to
happen in real life).

Drawbacks: see Python.

Richard
 
D

Default User

gooch said:

[blither blather]
Your idea of overly restrictive probably does not coincide with
everyone elses idea of overly restrictive so how do you then say this
is too much and that is not.


It's useless to try to have a discussion with Trollsdale. He's not
interested in rationally discussing anything, he posts his nonsense to
get a rise out of people. I don't believe for a minute that JPL lets
people program willy-nilly, he's working to a code style guide like
everyone else.




Brian
 
K

Keith Thompson

Drawbacks: see Python.

Hmm. Point taken. Personally, I like the way Python uses indentation
to denote structure. But I understand that a lot of people don't feel
that way. C, on the other hand, uses braces to denote structure, and
programmers are expected to use braces to communicate to the compiler
*and* indentation to communicate to human readers; great confusion
ensues if they get out of sync.

Anyway, this discussion would probably be more appropriate in
comp.lang.misc.
 
E

E. Robert Tisdale

Keith said:
I recently read a very interesting essay by Ken Arnold
in the Joel Spolsky's book "The Best Software Writing I".
He advocates making coding style part of the syntax of the programming language.

That's just plain silly.
(He's talking about programming languages in general, not just C.)

For example,
if it were decided that if statements should be written like this:

if (condition) {
...
}

then the following:

if( condition )
{
...
}

would be a syntax error.
> cat snippet.cc
if( condition )
{
...
}
> indent -br snippet.c
> cat snippet.c
if (condition) {
...}
> astyle snippet.c
> cat snippet.c
if (condition) {
...
}

Or you could use a code reformatter to convert the code
to your preferred format for easy maintenance
then convert it back into a format
acceptable to your overly picky code reviewer.

There is practically no reason to have or enforce style guidelines.
That's a job for a code reformatter -- not a programmer.
You probably have serious management problems
if you have team members obsessing about coding style.

Allow yourself and the programmers that you work with
to mature and to grow emotionally and intellectually.
Forget about coding style and concentrate on issues
that are really important to good design and engineering.
 
M

Malcolm

E. Robert Tisdale said:
Allow yourself and the programmers that you work with
to mature and to grow emotionally and intellectually.
Forget about coding style and concentrate on issues
that are really important to good design and engineering.
Communication with the human programmer is really important to good design
and engineering.
Compileable gibberish is asking for bugs.
 
W

websnarf

What is the BIG difference between checking the "if(expression)" in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?

[instead of if(a==0) ... snipped]

Fire the guy that thinks your style is a problem. They have an
extremely weak grasp of elementary algebra and there is a very real
possibility that they are a serious danger to your programming
environment. Obviously your method also has the well known property of
being resistant to the typical single = instead of == typo.
 
W

websnarf

Keith said:
I recently read a very interesting essay by Ken Arnold, in the Joel
Spolsky's book "The Best Software Writing I". He advocates making
coding style part of the syntax of the programming language. (He's
talking about programming languages in general, not just C.)

This idea comes from the Python programming language, not Joel Spolsky.
I don't know if he acknowledges it, but certainly I do for a lot of
eye opening programming ideas the language has given me (even when
programming in C.)

(It really drives a nail in the coffin of the LISP guys who constantly
claim that other programming languages are really just trying to
emulate things they invented ages ago. Python ain't no Lisp rip off.)

Anyhow, the point is that there is only one style for programming
Python, and the interpretor will not really accept much variation other
than the amount of whitespace.
 
F

Flash Gordon

What is the BIG difference between checking the "if(expression)" in A
and B ? I'm used to with style A, "if(0==a)", but my peer reviewer
likes style B, how can I defend myself to stay with style A ?


[instead of if(a==0) ... snipped]

Fire the guy that thinks your style is a problem.

Very unhelpful since most people do not have the option to fire
colleagues, QA engineers working for a different boss, people senior to
then, or anyone else.
> They have an
extremely weak grasp of elementary algebra

What makes you think that? If I'm working somewhere where the standard
practice is to express it as
if (variable == test_value)
then at a review I would probably ask for code to be changed to that way
round. I say that as someone who has an above average knowledge of
algebra (based on comparison of my knowledge against the other
developers I have worked with).
> and there is a very real
possibility that they are a serious danger to your programming
environment.

Complete nonsense. Or rather, no more try than it is of any other
programmer.
> Obviously your method also has the well known property of
being resistant to the typical single = instead of == typo.

Yes, but has been discussed here many times before a number of people
find the
if (test_value == variable)
form jarring and harder to read even though they fully understand that
as far as both maths and C are concerned they have identical meanings.
Even some people who prefer this style (and they are entitled to their
opinion) have accepted that it can take a bit of getting used to.

Personally, I prefer
if (variable == test_value)
I recognise that is personal preference based on what I, personally,
find easier to read. Unless you are able to get inside my mind there is
no reasonable justification you can have for saying I am wrong in my
claim to find it easier to read.
 
K

Keith Thompson

This idea comes from the Python programming language, not Joel Spolsky.
I don't know if he acknowledges it, but certainly I do for a lot of
eye opening programming ideas the language has given me (even when
programming in C.)

The essay was by Ken Arnold, not Joel Spolsky; Joel was the editor of
the book in which it appeared.
Anyhow, the point is that there is only one style for programming
Python, and the interpretor will not really accept much variation other
than the amount of whitespace.

What Ken Arnold advocates goes far beyond what Python does. Python,
compared to, say, C, nails down one minor matter of style: the use of
indentation to denote structure. Everything else in the Python
grammar is as free-form as any other modern language. For example,
the following is legal Python code:

cond=1

if cond:
print "One"
if cond :
print "Two"
if cond:print"Three"

Arnold advocates far stricter rules than that. For example, in C,
this:
if (cond) {
printf("Hello\n");
}
would be valid, but this:
if(cond) {
printf("Hello\n");
}
would be a syntax error. (The only difference is the lack of a space
after the "if" keyword).

You can read the essay at
<http://www.artima.com/weblogs/viewpost.jsp?thread=74230>. Since the
blog has comments enabled, you can also comment on it there -- which
you should do in preference to commenting on it here unless you have a
point that's relevant to C. As I mentioned earlier, comp.lang.misc
would also be a good place to discuss this.
 
C

Clark S. Cox III

wrong; for me it is
if( variable==test_value )
or if(variable==test_value)

What is "wrong" about the quoted line? And how are your two
alternatives any better (or even relevant to the issue being discussed).
 
C

Charlie Gordon

cs said:
wrong; for me it is

if( variable==test_value )
or
if(variable==test_value)

You are not bold enough to dare use this alternative:

if (variable=\
=test_value)

much faster to read : less eye movement.
 
K

Keith Thompson

cs said:
it is common, all you and all your bosses use that
but for me it is wrong

What do you mean by "wrong"? I prefer to reserve the word "wrong" for
things that are unambiguously incorrect, not for things that I just
don't like.

People seldom change their minds about style issues, but consider this.

Spacing can be used to make similar things look similar, and different
things look different. if and while are not functions, and they
shouldn't look like them. If I write this:

func(variable==test_value);
if(variable==test_value) {
...
}
return(some_value);

everything looks like a function call. On the other hand, if I write:

func(variable==test_value);
if (variable==test_value) {
...
}
return some_value;

it's more obvious at a glance that only the first line is a function
call.

The compiler doesn't care, of course, and any competent C programmer
should be able to read either form, but there are valid reasons for
the more commonly accepted style.
 
C

cs

What do you mean by "wrong"? I prefer to reserve the word "wrong" for
things that are unambiguously incorrect, not for things that I just
don't like.

it is a "style issues" but for me the separators are ' ' and point
characters "+ - * != == ( ) [ ] etc "
for you seems only spaces
People seldom change their minds about style issues, but consider this.

Spacing can be used to make similar things look similar, and different
things look different. if and while are not functions, and they
shouldn't look like them. If I write this:

func(variable==test_value);
if(variable==test_value) {
...
}
return(some_value);

everything looks like a function call. On the other hand, if I write:

func(variable==test_value);
if (variable==test_value) {
...
}
return some_value;

for this i don't like {} position
it's more obvious at a glance that only the first line is a function
call.

it is possible you have right in this but i would write it

func( variable==test_value );
if( variable==test_value )
{ ...
...
}
return some_value;

but i never have written something like
"func( variable==test_value );" (== in a function)
 

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,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top