4 ms·
One word: autocompletion. You only need to type unsafe_file_name_from_user once. After that, even the simplest, language-agnostics autocompletion of identifie
by nine_k 10y ago
One word: autocompletion.
You only need to type unsafe_file_name_from_user once. After that, even the simplest, language-agnostics autocompletion of identifiers from the current buffer goes a.long way. A proper IDE makes it even simpler.
- mikekchar 10y agoI have found that when I start reaching for long variable names it's because there is a design problem. Ideally the use of the variable should be obvious from the context. As you find yourself adding more and more context to the name, you should realise that it's because you lack context in your design.
- lloeki 10y ago> I have found that when I start reaching for long variable names it's because there is a design problem. I've come across that rule (Gerrand's rule?) which sums up my personal experience pretty well: > The greater the distance between a name's declaration and its uses, the longer the name should be. [0]: https://talks.golang.org/2014/names.slide#5 https://talks.golang.org/2014/names.slide#5
- marcosdumay 10y agoI do really like that rule that states that name_length is proportional to log(scope_size).
- randallsquared 10y agoDeriving meaning from the context requires understanding the context. That's fine for the original author or the refactoring dev, but the troubleshooter swooping in to fix that problem that's obvious in the UI needs to be able to understand the variable names with as little context as possible. "Well, if you'd just spent a few hours to truly grok the intent of the code, ..." is not an acceptable answer when it's possible to make the solution transparent as easily as making more descriptive names.
- kryptiskt 10y agoLong names that are constantly repeated makes the code very tough to read, there are many Java codebases that show that effect.
- nine_k 10y agoIn Java, you just have to repeat types twice very often: DescriptivelyNamedFoo = new DescriptivelyNamedFoo(); In a language with even limited type inference, e.g. C#, this repetition is gone.
- natch 10y agoVery well put. I wish everybody could get this concept. With this kind of attitude, you are going to write code that is easier to read, is easier to update and debug, and has fewer bugs in the first place.
- barrkel 10y agoCode with long identifiers is harder to read, not easier to read. It's harder to see the flow of data and the flow of control, because there is invariably a soup of common prefixes throughout, performing the contextualization.
- natch 10y agoThe identifiers don't all have to be long. Just long enough to be clear.
- skybrian 10y agoDescriptive names are great when a symbol is used in many scattered places. Context is okay when the definition is right in front of you. When a function is short enough, context for a parameter should mean reading the function's documentation written immediately above it. Or if the scope of a local variable is three lines, its initializer often explains what it is, and it's right there.
- 10y ago
- jghn 10y agoAgreed although I've found that come code review time there'll always be someone demanding a more descriptive name regardless
- 50CNT 10y agoBut then your code easily gets really wide, and you start committing heresy against the holy rule of 82.
- nine_k 10y agoIf your functions are short, and logic straight, you usually don't end up with overly long lines and deep indentation. It's kind of similar to enforcing 8-space tabs, only with space consumed by a more expressive approach. I often notice that I can remove a comment line after renaming a few identifiers in the code below it, because the code starts to read self-explanatory enough.
- noobiemcfoob 10y ago> I often notice that I can remove a comment line after renaming a few identifiers in the code below it, because the code starts to read self-explanatory enough. Careful with that. I'm of the mind that the code author is incapable of describing how readable any codebase is. It's too easy to mistake your innate understanding of your own creation for readability.
- chrisweekly 10y agoAlso, while good names might obviate the need for "what" comments, they can't replace often-invaluable "why" comments.
- nine_k 10y agoGood comments are usually about "why" and "what", while the code is about the "how". Code with descriptive identifiers often make code read like plain English.
- barrkel 10y agoIdentifiers have the same problem that comments have: the thing identified can grow different responsibilities than its name. You don't get magically updating identifiers any more than you get magically updating comments. I've found that people who believe too much in the ability for identifiers to tell the story of the code factor too much stuff into separate non-reused functions. The result being that you can't take the name at face value; you need to push the current context onto your mental stack, drill into the identifier being called, and if necessary continue drilling, until the unstated side-effects and extra bits and bobs that always accrete in continuously maintained code become clear. This code is ironically harder to read than inlined code.
- barrkel 10y agoLong variable names are a smell: they mean multiple concerns have been mixed in a single body of code / scope, and the long names are required to disambiguate them. They're a sign that something has been designed incorrectly.