Thursday, 16 May 2013

The art of naming

There has been numerous debates about naming things. In general, I like most of you find it difficult to name things correctly. It has been said that this is the hardest thing to do in our profession.

I have never really gave it much thought until I met one of my bosses. One of his pet peeves was things that were named incorrectly. For some time I thought what is the big deal, it's just a name and his response to that was if you can't name it you probably don't understand the problem. That statement got me thinking however not enough to really care about it. I really did wish we spent more time at the code level as I knew he had a wealth of experience.

Today I had a very interesting conversation. It all started around a code review of a feature that I was working on. This feature was around a user which had a mobile phone number and we wanted to validate that number by sending a unique code to the phone. Once they received it they would just enter that code. Easy enough.

We already had a User model so I decided to introduce two modules that would be mixed in to the User  model. These two modules were CodeGenerator and MessageSender. I thought this is great and it conveys the message quite well. During this code review in a very humble way it was brought to my attention that I have obviously forgot about "Object Thinking" (I blame this on DevOps and writing all those release management scripts :))

These two great articles really drove the message:

  1. One of the Best Bits of Programming Advice I ever Got
  2. Your coding conventions are hurting you
I really urge you all to read these great posts. It basically outlines trying to avoid the following in your names:
  1. The -er suffix
  2. The -able suffix
  3. The -Object suffix
  4. The I- prefix
So know with my "Object Thinking" cap on how would I approach the problem that I stated above. So my thoughts are the we could have a class called UniqueCode which could have the methods generate, validate. For sending we could have a class called SmsOutBox with a method send. All these classes could be composed with the User class to give it life. There could be better ways.

This is really fascinating and I thought since I have come across this twice, my life was trying to tell me something. Like my dad said to me growing up you will eventually understand what I was trying to tell you and I think this is one of those moments.