News for hackers. Stuff that matters.

Ask HN: 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?

( 20 comments | 8 points | on HN )
Re:Ask HN: How to become good at naming things in code? by fullito on Saturday April 04, 2020 @10:10AM (#22778305)
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.

Re:Ask HN: How to become good at naming things in code? by maps7 on Saturday April 04, 2020 @10:14AM (#22778326)
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.
Re:Ask HN: How to become good at naming things in code? by martinmartinez on Saturday April 04, 2020 @05:54PM (#22781856)
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-...
Re:Ask HN: How to become good at naming things in code? by AnimalMuppet on Saturday April 04, 2020 @09:20PM (#22782970)
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.

Re:Ask HN: How to become good at naming things in code? by mattbgates on Saturday April 04, 2020 @09:26PM (#22782994)
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.

Re:Ask HN: How to become good at naming things in code? by itm on Sunday April 05, 2020 @05:07AM (#22784623)
(I hope you actually don't store passwords in clear text in your database :) )
Re:Ask HN: How to become good at naming things in code? by mattbgates on Monday April 06, 2020 @12:30AM (#22790914)
Security and privacy is something I strive to have... I would want the same for anyone who uses anything I create.
Re:Ask HN: How to become good at naming things in code? by thedevindevops on Saturday April 04, 2020 @04:51PM (#22781424)
If the name isn't obvious from it's function in the domain, there is something wrong with the architecture you're using
Re:Ask HN: How to become good at naming things in code? by dennisy on Sunday April 05, 2020 @04:56AM (#22784584)
Could you elaborate a little on this please, maybe with an example?
Re:Ask HN: How to become good at naming things in code? by itm on Sunday April 05, 2020 @05:09AM (#22784631)
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.
Re:Ask HN: How to become good at naming things in code? by kleer001 on Saturday April 04, 2020 @01:55PM (#22780040)
> This seems to be a huge amount of time wasted

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

Re:Ask HN: How to become good at naming things in code? by sloaken on Sunday April 05, 2020 @08:27PM (#22789912)
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.
Re:Ask HN: How to become good at naming things in code? by tcbasche on Monday April 06, 2020 @01:50AM (#22791190)
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?

Re:Ask HN: How to become good at naming things in code? by sloaken on Monday April 06, 2020 @10:53AM (#22794249)
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.
Re:Ask HN: How to become good at naming things in code? by afarrell on Saturday April 04, 2020 @01:12PM (#22779661)
Two useful google searches:

- class responsibility collaborator

- John Osterhout a Philosophy of Software Design

Re:Ask HN: How to become good at naming things in code? by saluki on Saturday April 04, 2020 @12:37PM (#22779359)
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.

Re:Ask HN: How to become good at naming things in code? by tcbasche on Sunday April 05, 2020 @07:30PM (#22789626)
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.

Re:Ask HN: How to become good at naming things in code? by Someone on Monday April 06, 2020 @03:28AM (#22791559)
”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.

Re:Ask HN: How to become good at naming things in code? by sloaken on Sunday April 05, 2020 @08:23PM (#22789891)
IMHO adding a Star Wars or movie / pop culture reference fits the definition of being cryptic.
Re:Ask HN: How to become good at naming things in code? by dennisy on Sunday April 05, 2020 @04:57AM (#22784590)
Thanks! Could you provide a few examples from your system?