Click to See Complete Forum and Search --> : Bad programming?
armando86
10-15-2000, 06:14 PM
I've been reading this old C book and it uses a different syntax for functions than newer books. ie
int foo(a,b)
char*a
double b;
{
code
}
whereas in the newer books the syntax is
int foo(char *a, double b) {
code
{
To me the older syntax is easier to use and read but I was worried that if I used that it would be considered bad programming. Will it?
Unruly
10-15-2000, 06:17 PM
most likely, no... simply because it's the same thing, just squished together... as long as you follow some sort of structure (ie syntax) then it really woulnd't make a difference to the computer...
------------------
Nathan
Q: How many existentialists does it take to screw in a lightbulb?
A: Two. One to screw it in and one to observe how the lightbulb itself symbolizes a single incandescent beacon of subjective reality in a netherworld of endless absurdity reaching out toward a maudlin cosmos of nothingness.
armando86
10-15-2000, 06:36 PM
Cool, I was worried the book's syntax was too old, after all it was printed in 1989.
------------------
majorpayne201@yahoo.com
"In a world without walls who needs windows?"
Craig McPherson
10-15-2000, 06:54 PM
http://www.tuxedo.org/~esr/jargon/html/entry/indent-style.html
Keith M
10-15-2000, 07:16 PM
Watch you were seeing was K&R style code. When C was originally written, the parameters were specified outside of the () in the function definition. This may still be supported by some compilers, but I think most people consider it bad style and I would advise against using it.
As far as indenting goes, well there's a lot of different views on that. I prefer:
void foo()
{
code;
}
which I have found to be used most often at the various places I've worked. Some prefer:
void foo()
{
code;
}
which in truth is a more accurate representation for how it is interpreted based on the grammer, however most people I know don't feel it is as readable.
Putting the opening brace on the same line as the function definition is possibly done to keep the code tighter. I see this often done in books, but rarely done in the real world. I'd recommend going with the indenting you see in the older book, but don't declare the functions that way.
armando86
10-16-2000, 12:33 AM
I think I'll continue to use K&R style, for some reason is seems more readble to me, gcc doesn't seem to care either way. Anyway thanks for all the help.
------------------
majorpayne201@yahoo.com
"In a world without walls who needs windows?"
Originally posted by Craig McPherson:
http://www.tuxedo.org/~esr/jargon/html/entry/indent-style.html
It's funny he says K&R has gone out, that and the 2nd one are all I see. The others look silly.
Dru Lee Parsec
10-16-2000, 01:29 PM
If you used the K&R syntax on Borland's compilers as long as 7 years ago it would fire off a messages saying something like:
Warning, possible archaic method declaration.
I'm not sure if the K&R way is supported with the newest spec of C++. So if you ever want to move to C++ you may find yourself with some bad habits. In any case, I've never seen people code with the K&R style in the real world. They always use the new style (Which I feel is vastly clearer anyway).
actually, I just meant the indenting style; placing the opening brace at the end of the line, instead of on its own line...
from Craig's link:Surveys have shown the Allman and Whitesmiths styles to be the most common, with about equal mind shares. K&R/1TBS used to be nearly universal, but is now much less common in C (the opening brace tends to get lost against the right paren of the guard part in an if or while, which is a Bad Thing). Defenders of 1TBS argue that any putative gain in readability is less important than their style's relative economy with vertical space, which enables one to see more code on one's screen at once.
The "second" style I was referring to in my original post was "Allman", where the brace is on the next line...
personally, I'm a "defender" of K&R style for the exact reason mentioned above, and I think ESR may be pushing a biased opinion here. Maybe not.
actually, I feel there is no gain in readability in Allman over K&R, because you just go up to the last line that had the same indent. Indeed, K&R is more readable, since more is present. Ug. That irritates me; sure he's some pro-hacker or whatever, but I see no enlightenment in those lines. But what do I know? I'm a dope and a half on crack, and drunk with fatigue to boot.
Dru Lee Parsec
10-16-2000, 05:10 PM
OH!
Yes, placing the bracket at the end of a line is called the "Line Saver" version of bracketing. I use it exclusivly.
Remember, C, C++, Java, and probably most other similar languages ignore extra white space. So whether the return is before or after the bracket makes no difference to the compiler. I think it makes a difference in the readability of the code and I like the open bracket at the end of the line.