HACKER Q&A
📣 dennisy

How to become good at naming things in code?


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?


  👤 fullito Accepted Answer ✓
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.


👤 maps7
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.

👤 martinmartinez
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-...

👤 AnimalMuppet
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.


👤 mattbgates
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.


👤 thedevindevops
If the name isn't obvious from it's function in the domain, there is something wrong with the architecture you're using

👤 kleer001
> This seems to be a huge amount of time wasted

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


👤 sloaken
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.

👤 afarrell
Two useful google searches:

- class responsibility collaborator

- John Osterhout a Philosophy of Software Design


👤 saluki
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.