
Ask HN: How to become good at naming things in code? - dennisy
I find myself so often staring at my screen, thinking of a name or names for various things, variables, files, functions etc.<p>I also am constantly changing names.<p>This seems to be a huge amount of time wasted, is there a way to become &quot;good&quot; at naming things in software?
======
fullito
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](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-...](https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-
wrong/)

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

~~~
itm
(I hope you actually don't store passwords in clear text in your database :) )

~~~
mattbgates
Security and privacy is something I strive to have... I would want the same
for anyone who uses anything I create.

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

~~~
dennisy
Could you elaborate a little on this please, maybe with an example?

~~~
itm
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.

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

~~~
tcbasche
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?

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

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

~~~
tcbasche
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.

