comment 0

Using Better Naming to Clarify Code

I’m a big fan of good naming in code, here’s a recent example:

Suppose you have a unique index in a database table and you’re trusting that index to enforce no more than one record with the key.

So you’re using an insert ignore into…on duplicate key update statement.

So you end up calling something like DataAccess.InsertRecord(data) or DataAccess.AddRecord(data). Looking at such code it’s very unclear that what really happens is an insert/update and you’re only left with one record.

You can go the way of making your code explicit my moving the logic into your app and doing something like

var record=DataAccess.GetRecord(key);
if(record == null)
  DataAccess.InsertRecord(data);

But then you’ll be losing the power of using the database do that for you.

So what I’m suggesting is just making your naming better, for example: DataAccess.ReplaceRecord(data).

Leave a Reply

Your email address will not be published. Required fields are marked *