How much time do you spend thinking of good identifier names?

5 points by gsaga ↗ HN
Sometimes I find myself getting stuck on a project just because I can't think of a good name for a new class which implements some new feature. How do you deal with this problem? One way I can think of is assigning template names('NewFeature1'). It isn't that big of a problem with modern ide's which provide one click renames.

6 comments

[ 0.23 ms ] story [ 23.4 ms ] thread
Template names or code names are the way I do it too. Otherwise I spend much too long thinking of identifiers.
When I am having difficulty naming things its usually because I am trying to be too concise. In my experience its always better to be more verbose and accurate than to spend time trying to be clever and end up with something I don't remember what it does but its called newSlide.
Honestly, I don't have a good way. It doesn't happen often but sometimes I just end up with a 20+ character identifier. Seems to fit in fine in Java land though. And like you, if I want to do a rename, my IDE will help me do it better than I ever could alone.

I will note that I usually find a better name after it sits there for a while and starts to not make sense. Also after hearing complaints from other programmers about "why is this called x when it should be y :)".

You can often improve your identifiers, but they should come pretty easy if you understand your task and the approach you're taking to it.

Nondescriptive "template names" (your "NewFeature1") are a huge red flag signaling unpreparedness.

While there is pressure to "just code it" that comes from methodologies like Agile or from just having an intense workload, the spontaneity should be happening in the context of professional expertise and good insight.

Do you have a sense of what algorithms are relevant? What traditional patterns? What data is being represented? What transforms are being applied? What libraries and services are being utilized? You don't need an 40 page architecture to start coding, but you'll usually have answers to all those questions somewhere in mind. And your identifiers should usually be pretty obvious when you do.

Can't think of a good name? So think of a bad name for it. You will automatically think about what's wrong with the bad name and a better one will occur to you, etc.

Also, worrying about the perfect name at the start is extremely premature optimization - often/usually the purpose of things changes, or you realize it wasn't what you thought it was, or you come to understand it better, or it gets split or combined etc.

Take time to think about the way/pattern you will use to name identifiers across the whole project. Camel-cased? Lowercased with underscores? 2-4 words in each?

After that things will be easier and faster.