6 ms·
Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits. At least these are not uni
by yitchelle 6y ago
Oh man, multiple declaration on the single of code is nasty. Passing this kind of advice causes the beginners to develop bad habits.
At least these are not uninitialised variables.
// Shorthands for multiple declarations:
int i1 = 1, i2 = 2;
float f1 = 1.0, f2 = 2.0;
int b, c;
b = c = 0;
Edited:..
After 1hr of posting my original comment, I noticed that there many opinions here.
I should have added why I think this is bad. Readability and maintenance. I would rather it have one line per declaration like the following..
int i1=2;
int i2=2;
Each variable easier to be changed, and reading it is clearer as there are no ambiguity to its type.
A common error I have see from "the multiple declarations in the single line" construct is that changing the type impacts all the variables whereas the intention was only for the first variable.
- flohofwoe 6y agoThis is also bad advice for C: void function_1(); Most people coming from other languages (like C++) will probably think that this is identical with "void function_1(void)", but without the explicit void argument list, the compiler will not warn if the function is accidentally called with parameters.
- okl 6y ago-Wstrict-prototypes "Warn if a function is declared or defined without specifying the argument types."
- junon 6y agoThis isn't the case nowadays.
- flohofwoe 6y agoHuh? Of course it is: https://godbolt.org/z/8qYeK89rh https://godbolt.org/z/8qYeK89rh It's only an error in C++, but not in C, and the warning for this isn't included in the usual "-Wall -Wextra" set.
- shoto_io 6y agoWhat is the “correct” version of this?
- flohofwoe 6y agoJust split them into separate variable definitions: int i1 = 1; int i2 = 2; The "usual footgun" is: int i1, i2, i3 = 0; Only i3 will be initialized in this case. And since one needs to write out the assignments anyway, adding the type for each variable doesn't hurt either. (today's compilers usually have uninitialized-warnings for this though, but sometimes people forget to add a -Wall to their compiler options, especially beginners, and then get stuck on such trivial problems).
- thrwaeasddsaf 6y agoSorry I can't read your comment, could you split your words into separate paragraphs?
- sltkr 6y agoThere is nothing incorrect about multiple declarations, but the syntax can become confusing when declaring pointers or arrays: char *s = "foo", *t = "bar"; A beginner might forget to add the asterisk in `*t` (especially if the first declaration is written `char*` with no space on the left of the asterisk). Similarly with multiple types: char *p = "foo", **q = &p, *z[2] = {"foo", "bar"}, *(*f)(const char*) = strdup; This is correct and an intermediate C programmer won't have too much trouble parsing these, but in the interest of clarity, it makes sense to put each declaration on a separate line. If you find yourself needing a lot of local variables, that's often a bad sign anyway, since it implies that the function has a lot of state to manage. In that case, you might want to rethink your implementation to make it less complex, or restructure the code to make it more managable.
- thrwaeasddsaf 6y agoThere is nothing wrong with multiple declarations.
- okl 6y agoIt's more difficult to read and edit.
- thrwaeasddsaf 6y agoI wish we could talk about style in objective terms, not just opinion. Opinions are kinda stupid. Here's my opinion: https://news.ycombinator.com/item?id=26790701 https://news.ycombinator.com/item?id=26790701 (actually that's just a faux opinion to make a point, but you get the point) And if there are no objective facts, then maybe we could just stop calling other peoples' preferred style nasty and "bad habits" as if it were objective.
- okl 6y agoYour "opinion" is particular "stupid" (your words) since you introduce a false analogy between program code structure and natural language sentences. Let's see what CMU's SEI has to say: https://wiki.sei.cmu.edu/confluence/display/c/DCL04-C.+Do+not+declare+more+than+one+variable+per+declaration https://wiki.sei.cmu.edu/confluence/display/c/DCL04-C.+Do+no...
- thrwaeasddsaf 6y agoI know SEI well. I have worked on projects where linters enforce that style, and I hate it. You can read their justification: a programmer or code reviewer might mistakenly believe that the two variables src and c are declared as char *. "A programmer might not know the syntax" is as stupid a justification for enforcing these rules as "a reader might not be able to read long sentences correctly" is for enforcing a stupid rule over text written in a natural language. I'm sorry that you failed to see the analogy. They also just drop a statement like "declaring no more than one variable per declaration can make code easier to read and eliminate confusion." I can drop my opinion too: "declaring more than one variable per declaration can make code easier to read and eliminate confusion." It can also make the code shorter and easier to scan & edit. And I think analogies involving natural language are more relevant than you think. I would rather read "U, V, and W are phases" than "U is a phase. V is a phase. W is a phase." It's the same goddamn thing, we're declaring things (and optionally initializing them).
- ajross 6y agoJust don't. This kind of prescriptive nonsense isn't nearly as helpful as you think it is. People value very different things when reading code, and vertical whitespace is not nearly the universal good you seem to think it is. For myself, I much prefer to trade slightly more complicated intra-line semantics for the ability to see more of the code on the screen at one time. C has been around long enough to develop multiple competing paradigms for how to format source code, each with copious existence proofs that "Good Code" can be written in them. Conform to what you're maintaining, don't start fights.