Live data from Hacker News

How to name things in programming

slideshare.net

21–30 of 177 posts

Re: How to name things in programming

#21
post #14

I agree with everything except abbreviations, but to be honest, I think my worst habit it just trying to use words with the same number of characters for different variables so they align well with monospace font. e.g. int num = 42; int acc = 0; instead of; int n = 42; int acc = 0; and it gets worse when things get complicated; vector dist; // stands for distances vector excs; // stands for excesses Does anyone else…

int n = 42; int acc = 0; Fixed Edit: Install http://wbond.net/sublime_packages/alignment and add this key binding `{ "keys": ["ctrl+shift+a"], "command": "alignment" }`.

Which, when you add a third variable "stuff", will require you to go back and add a space for both n and acc, pollutting both the diffs themselves (making reviews harder) and the git-blame (making pinpointing bugs harder).

Re: How to name things in programming

#22
Nice write-up. But there's not a single slide about usability and how it affects naming.

If your library exposes 'getCurrentRuntimeContext' which is going to be used in every other line of user's code, name it 'ctx'.

It's ambiguous, but it saves the user 3 seconds per 2 lines of code.

As for the readability, when the reader encounteres 'ctx' for the third time (at line 6 of the source file) they will already know what it is supposed to mean by heart.

Re: How to name things in programming

#23
post #14

I agree with everything except abbreviations, but to be honest, I think my worst habit it just trying to use words with the same number of characters for different variables so they align well with monospace font. e.g. int num = 42; int acc = 0; instead of; int n = 42; int acc = 0; and it gets worse when things get complicated; vector dist; // stands for distances vector excs; // stands for excesses Does anyone else…

int n = 42; int acc = 0; Fixed Edit: Install http://wbond.net/sublime_packages/alignment and add this key binding `{ "keys": ["ctrl+shift+a"], "command": "alignment" }`.

That's actually right against the style guideline of many projects which tell you explicitly not to do this but to simply put the = sign and value right after the variable name and be done with it. Lining them up serves no purpose and does not in fact make the code easier to read.

Re: How to name things in programming

#24
post #7

When structuring code, elegance and efficiency always take top priority. However, when naming, my philosophy is always clarity over elegance. for example (in python): def reverseListAndAddOneToEachElement(input_list): Ugly, but clear. I don't even need to write out the full definition you already know exactly what it does.

I like the logic of this, but it goes against all books that i've read to keep it short. Why do we need to keep it short?

We need to expend effort to keep names short, while it is easy to let them become long. 'Keep it short' is shorthand for 'do not forget to expend effort towards keeping names from becoming long'. It's easy to unthinkingly come up with 'company_people'. It takes effort to reduce it to 'employees'.

It's actually not just 'short', but 'short and simple'.

Re: How to name things in programming

#26

I agree with everything except abbreviations, but to be honest, I think my worst habit it just trying to use words with the same number of characters for different variables so they align well with monospace font. e.g. int num = 42; int acc = 0; instead of; int n = 42; int acc = 0; and it gets worse when things get complicated; vector dist; // stands for distances vector excs; // stands for excesses Does anyone else…

I try to pick words with different lengths. They're more visually distinguishable.

And try to pick words that are lexicographically distant from each other to minimize the chance of single letter typos changing the meaning of the code.

Re: How to name things in programming

#27
post #9
post #7

Earlier quoted context omitted.

I like the logic of this, but it goes against all books that i've read to keep it short. Why do we need to keep it short?

You shouldn't try to make it short on purpose because that will obscure the meaning of the variable. But long name by itself could be systematic error in abstraction (BananaSingletonFactoryFactoryCacheFactory comes to mind). At the very least, reading long name will also take a lot more mental power to understand.

Of course, if the meaning makes no sense it shouldn't be long.

But if the longer name provide meaning, shouldn't we use it if it's a complex thing and the words bring value?

Re: How to name things in programming

#28
An important rule is missing here: variables should be named with their scope in mind. So if a variable is longer lived and has larger scope its name should be that much more descriptive because when you're looking at it the only thing that will tie the value of the variable to the context within which it can be used is its name.

So 'i' is fine for a loop control variable with a scope of five lines but totally inadequate for something expressing a larger and longer lived concept.

Ditto for function names and parameters to functions, if the function and the parameters are named properly understanding the function is trivial.

So if you write a chunk of code that exports one or more functions that is where your effort should go, that's the public interface. The reduced scope of the rest of the code should make any naming issues much more limited.

This is also why it is good to assign one person on a team to defining the interfaces between the code. That way you get consistency in naming which is a great thing to have in a codebase.

Anecdote time:

I once worked for a game programming company. One of the programmers there would name all his functions and variables for fruit and vegetables. It was his way of ensuring job security. Guess who got saddled with untangling the salad when he left the company.

cucumber(cherry, strawberry, orange);

Good luck with that...

Re: How to name things in programming

#29
post #22

Nice write-up. But there's not a single slide about usability and how it affects naming. If your library exposes 'getCurrentRuntimeContext' which is going to be used in every other line of user's code, name it 'ctx'. It's ambiguous, but it saves the user 3 seconds per 2 lines of code. As for the readability, when the reader encounteres 'ctx' for the third time (at line 6 of the source file) they will already know wha…

I'm not so sure that is good advice, and I come from C programming so we love to do that.

With autocompletion in most code editors saving the user typing time is not a valid argument anymore, and I would rather prefer comprehensible if slightly more verbose code than accronyms everywhere.

What is obviously ctx -> getCurrentRuntimeContext for you is completely foreign to the next guy. And sometimes you don't spend a large amount of time in a specific part of a code base, such as when debugging and then figuring out what ctx means is a real PITA.

Re: How to name things in programming

#30
post #14

Earlier quoted context omitted.

int n = 42; int acc = 0; Fixed Edit: Install http://wbond.net/sublime_packages/alignment and add this key binding `{ "keys": ["ctrl+shift+a"], "command": "alignment" }`.

Which, when you add a third variable "stuff", will require you to go back and add a space for both n and acc, pollutting both the diffs themselves (making reviews harder) and the git-blame (making pinpointing bugs harder).

There is a plugin in sublime text that I just select the lines and hit ctrl+shift+a and alignes everything in one hit.
Post reply on HN