4 ms·
The 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
by jstimpfle 11y ago
The 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"