4 ms·
>A function name, and especially local ones, should be short. I have more problems reading code with short names rather than long. C code with one or two lette
by tonyedgecombe 6y ago
>A function name, and especially local ones, should be short.
I have more problems reading code with short names rather than long. C code with one or two letter variable names often looks impenetrable to me.
- TheChaplain 6y agoI 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. :/
- rbanffy 6y agoA 1000+ line function should be a syntax error. Heck. I start to itch breaking Python modules when they cross the 1000 line barrier.
- syshum 6y agoYou have heard of Single Page Applications, well now we need Single Function Applications ;)
- Ancapistani 6y agoThat would be anything ever written in a Lisp, if I’m not mistaken :)
- williamdclt 6y agoSome 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
- Thiez 6y agoDoesn't a cyclomatic complexity limit of 5 basically rule out the switch statement?
- williamdclt 6y agoGood 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
- magicalhippo 6y agoI 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.
- globular-toast 6y agoNaming is difficult. Naming requires intelligence. There is no simple set of rules to follow to do it right for much the same reason there is no simple set of rules for writing a novel or any other creative work. Being good at it is partly due to experience and partly talent. Is a single-letter name for a function ever correct? Absolutely. Is it always correct? Obviously not. Knowing when to use shorter names versus longer names is a big part of being a good programmer.
- bawolff 6y agoThere's a happy medium between function names being 2 letters and function names being a full damn sentence. I personally prefer common things to have short names, less common to have long names. That's how natural languages work, and i think it makes a lot of sense. If everything has a long name, it ends up being hard to pick out the important bits. If everything has a short name, it also becomes a bit impeneterable.
- ngcc_hk 6y agoCannot stop laughing of the limit of early basic with only 2 characters.
- LeonM 6y ago> I have more problems reading code with short names rather than long. C code with one or two letter variable names often looks impenetrable to me. I agree. Short names don't make code readable, they make typing less work. In my opinion this means the author used the wrong tool. If you don't want to type long names, use an IDE with auto-complete. Don't make the reader suffer from your laziness.