Good names help — can ‘Foo’ hurt?

Naming is the most important part of design. Concise yet meaningful names are hugely important.

One of my top recommendations to improve legacy code, is to improve & clarify naming.  Names should be short and sweet, yet meaningful. For example — Customer.Name.

For parameters or variables, I prefer a degree of conciseness. These names may be concise phrases from the problem domain or math names — customer/cust, name, customerName/custName, id, value, val, x, str, text, obj.

One practice that interests me is how sometimes people use ‘foo’ or ‘bar’. Interestingly, I never use these. I can’t personally tell if ‘foo’ is a name, an ID, a value, or an object? As a symbol ‘foo’ is scarcely any shorter, but almost deliberately uninformative.

When I want to allow an arbitrary value, I use ‘val’ or ‘value’ which are more meaningful. Or ‘x’ and ‘y’ from the language of math. Or perhaps ‘name’, ‘text’ or ‘str’ if it’s a string (they all imply semantics).

‘Value’, ‘name’, ‘customer’, ‘id’, ‘x’ or ‘amount’ will all inform readers of your code better what they should understand and expect of a variable. Can that be a bad thing?

Eclipse IDE pro tip: I use ‘Rename in File’ as a fast all-purpose code improver. It can rename parameters, variables and methods lightning quick — by only updating the current file.

You can access it with Ctrl-2, R — but I use it so heavily I rebind it to Ctrl-~ (tilde key as found on the top-left of your keyboard).

This shortcut is gaining popularity due to its speed, and is probably my favourite Eclipse shortcut.

Leave a Reply

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