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
[ 2.2 ms ] story [ 48.3 ms ] threadYou 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.
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.
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.
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.
- class responsibility collaborator
- John Osterhout a Philosophy of Software Design
Yes, that's how learning works. You're doing it right :)
By the way, this implies that, if there are group conventions, use them.
$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.
What's wrong with 'address' or 'phone_number' or something to that effect?