3 ms·
> and because the greybeards at RedHat thinks it looks nicer with all the variables declared at the top. Why do people always assume newer = better? Get the ba
by jstimpfle 11y ago
> and because the greybeards at RedHat thinks it looks nicer with all the variables declared at the top.
Why do people always assume newer = better? Get the basics right. Yes, declaring your variables at the top does usually look nicer. It requires more code discipline, it often forces you to better decompose your code, but usually results in better code structure and readabilty.
C is not a language to program at scripting speed in the first place.
- umanwizard 11y ago[citation needed]. This is pure personal taste (and has nothing to do with C vs. any other language, unless by "C" you mean K&R era)
- jstimpfle 11y agoThe notion of cohesion is actually fairly common. Declaring variables at the top helps cohesion. It makes obvious when too much is done in one procedure. Among other small refinements, C99 is a set of features on top of C90, such as declarations anywhere, restrict pointers, and complex numbers. If you don't need these features, restricting to C90 is a very effective way to have your code auto-linted.
- umanwizard 11y agoYes, I agree with the upsides to declaring variables at the top. However, declaring variables close to where they use is a different kind of "cohesion", since it's keeping related things together -- just related in a different way. I see your point, but it's still a matter of taste.
- jstimpfle 11y ago> However, declaring variables close to where they use is a different kind of "cohesion", since it's keeping related things together Cohesion is about keeping unrelated things apart (to avoid accidental complexity). If two things are semantically unrelated, you should be able to name both (yes, it is hard). If you want the separation statically asserted keep them syntactically separated [1]. There you go -- make two smaller functions, each with a clear scope and a descriptive name. If you feel a function is doing unrelated things (because the data is not very "cohesive") but can't tell why, why not being honest about it and still host all the variables at the top. Being disciplined in this way has found me appropriate code factorings, and many bugs in previously ill-factored code, both syntactic and semantic. [1] in John Carmack's words "what's syntactically allowed will eventually end up in your codebase"
- sangnoir 11y ago>Why do people always assume newer = better I can't help but to notice the irony of your statement in a thread about GTK3 (since they are ripping out functionality in the pursuit of newness)