Re:Greppability is an underrated code metric
by sestep on Tuesday September 03, 2024 @08:31AM
(#41434325)
Not true at the top-level.
[ Parent
| on HN ]
Re:Greppability is an underrated code metric
by wruza on Tuesday September 03, 2024 @10:25AM
(#41435300)
Not sure what you find not true about it. All named “function”s get hoisted just like “var”s, I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere. You’re probably thinking about
const foo = function (){}
without its own name before (). These behave like expressions and cannot be hoisted.
Re:Greppability is an underrated code metric
by MetaWhirledPeas on Tuesday September 03, 2024 @10:55AM
(#41435559)
> I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere
I haven't figured out if people consider this a best practice, but I love doing it. To me the list of called functions is a high-level explanation of the code, and listing all the definitions first just buries the high-level logic "below the fold". Immediately diving into function contents outside of their broader context is confusing to me.
Re:Greppability is an underrated code metric
by wruza on Tuesday September 03, 2024 @11:29AM
(#41435910)
I don’t monitor “best” practices, so beware. But in languages like C and Pascal I also had a habit of simply declaring all interfaces at the top and then grouping implementations reasonably. It also created a nice “index” of what’s in the file.
Hoisting also enables cross-imports without helper unit extraction headaches. Many hate js/ts at the “kids hate == and null” level but in reality these languages have a very practical design that wins so many rounds irl.