&gt; However, declaring variables close to where they use is a different kind of &quot;cohesion&quot;, since it&#x27;s keeping related things together<p>Cohesion is about keeping unrelated things apart (to avoid accidental complexity).<p>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.<p>If you feel a function is doing unrelated things (because the data is not very &quot;cohesive&quot;) but can&#x27;t tell why, why not being honest about it and still host all the variables at the top.<p>Being disciplined in this way has found me appropriate code factorings, and many bugs in previously ill-factored code, both syntactic and semantic.<p>[1] in John Carmack&#x27;s words &quot;what&#x27;s syntactically allowed will eventually end up in your codebase&quot;