Hacker News new | ask | show | jobs
by TheChaplain 2024 days ago
I actually prefer single letter if they are declared and used within 10 rows for a single purpose.

But I've seen the opposite... Horrors with globals and other re-used all over 1000+ line functions. :/

2 comments

I prefer single letter or short names if they don't have a specific purpose. Like for just another simple for loop, I'll use "i" instead of "elementIndex".

But if there's more to it, I prefer a proper name, even if it's used only on the line right below it.

The point is to make the code as self-documenting and readable as possible. It's always trade-offs. Sure 5000 column lines are not good. But strict 80 columns can also reduce readability. Similarly superlong or supershort identifiers aren't always good, but can be.

A 1000+ line function should be a syntax error.

Heck. I start to itch breaking Python modules when they cross the 1000 line barrier.

Some linters (eg ESLint) have rules you can enforce for cyclomatic complexity (which is often the problem with long functions, rather than the length itself). In the service I maintain, we enforce a cyclomatic complexity of 5, which is pretty low but it yielded good results
Doesn't a cyclomatic complexity limit of 5 basically rule out the switch statement?
Good question, I suppose it would? On the other hand we do have chunky switch statements (as it allows exhaustive type checking in Typescript), so not so sure now that I think about it, I'd have to check
You have heard of Single Page Applications, well now we need Single Function Applications ;)
That would be anything ever written in a Lisp, if I’m not mistaken :)