Ask HN: How to become good at naming things in code?

8 points by dennisy ↗ HN
I find myself so often staring at my screen, thinking of a name or names for various things, variables, files, functions etc.

I also am constantly changing names.

This seems to be a huge amount of time wasted, is there a way to become "good" at naming things in software?

20 comments

[ 2.2 ms ] story [ 48.3 ms ] thread
To be honest: experience.

You are already doing it right: You actually think about naming.

What helps me is to name variables very simple and clearly wrong, code a little bit to see where it brings me and then it becomes much easier and much clearer to name them right.

One thing that might help you is just to make a quick decision first. It might be a 'wrong' name but if, like you say, you end up changing them anyway you have just saved time.
Try having a naming system, I usually name things based on what they do and typically follow the same ordering format across the board so everything builds on each other and matches up.

I'll also add some Star Wars and other movie/pop culture references to keep things interesting. And hopefully bring a smile to someone editing the code in the future.

Kamino is one of my favorites for anything cloning or duplicating related for example.

You do need to be decisive and quick so it doesn't hold you up, with a system you will be, and realize that sometimes a name just isn't that critical.

Avoid being cryptic though, think about what name would make sense to someone looking at your project for the first time.

Thanks! Could you provide a few examples from your system?
Ah so there are people out there like this, who think random pop culture references will 'bring a smile' to someone editing the code.

Please don't. 'Avoid being cryptic' - but please, throw in a reference no-one might get or something unrelated to the purpose of a variable. Give me a break.

IMHO adding a Star Wars or movie / pop culture reference fits the definition of being cryptic.
”Kamino is one of my favorites for anything cloning or duplicating related for example.”

That seems almost straight out of https://github.com/Droogans/unmaintainable-code (which has good anti-hints on naming)

If you can’t resist the urge to be a bit playful, use xerox for such a thing. Way more people would get that.

Two useful google searches:

- class responsibility collaborator

- John Osterhout a Philosophy of Software Design

> This seems to be a huge amount of time wasted

Yes, that's how learning works. You're doing it right :)

If the name isn't obvious from it's function in the domain, there is something wrong with the architecture you're using
Could you elaborate a little on this please, maybe with an example?
I do agree with this. For example, if you can't easily name a function, then it is likely it does too many things and should be split or something.
1) Look at the language style guide. To see the best practices if relevant; or how naming variables in said guide. 2) Name based on a variation of hungarian notation based on data. bCat for boolean for if cat or not; x for item of list; xs for a list of items. Here are more examples https://en.wikipedia.org/wiki/Hungarian_notation#Examples (PS i know hungarian notation has fallen out of favor; modify for the language you are using; and keep it simple) 3) This blog post from Joel Spolsky might be useful https://www.joelonsoftware.com/2005/05/11/making-wrong-code-...
When I get stuck, sometimes I ask a co-worker. See, I don't just need a name that communicates to me; I need a name that communicates to them.

By the way, this implies that, if there are group conventions, use them.

I always name things according to what I'm doing.

$dbname might be for the name in the database. $dbpass for password. $dbipddr logging of ip address. $dbdark might be a setting for light or dark theme.

In the databasae, it's just named as Name, Pass, IPAddr, DarkTheme or whatever. I've very rarely ever had to change names. But before I even start a project, I outline not only what the project is going to do, but how the database will look and function as well.

(I hope you actually don't store passwords in clear text in your database :) )
Security and privacy is something I strive to have... I would want the same for anyone who uses anything I create.
Try to make the beginning part of the name unique. If you follow the Hungarian notation, put that at the end of the name not the beginning. Bad example strname, straddress, strphone. Better: namestr, addressstr, phonestr.
Arguably both of those are terrible. It's just fluff for no real benefit. Why do you not know what data type those things are?

What's wrong with 'address' or 'phone_number' or something to that effect?

key phrase was 'IF'. I typically do not apply type info, as I care about its logical purpose when debugging. Most modern IDE will tell you the type when you hover over them.