Feb 7
Naming
icon1 Darrell Mozingo | icon2 Musings | icon4 February 7th, 2010| icon3No Comments »

Alright people, we’re not on 8088’s with 20M hard drives anymore. It’s OK, and in fact recommended, to stretch out your variable and method names so they make sense. We don’t need variables with names like x, col, or cAcctNum anymore. Use Intellisense and don’t be afraid to type a bit when you first enter the names. Meaningful names like employeeCount are good, as is getting rid of stupid one-off abbreviations and instead opting for naming like column and clientAccountNumber.

By using a shortened variable or method name, you’re forcing some other developer in the future (who often ends up being yourself) to mentally substitute that shortened version in their head each time they see it while reading through your code. Lets face it, our code is usually hard enough to understand long after we’ve written it just on its own, so strive to do everything possible to more quickly and easily figure it out in the future. Those small substation times accumulate pretty quick when you’re trying to find a bug in foreign code.

Queue the contrived example:

public int AddEmp(string fname, string lname, decimal rate, Dept dept)
{
	var emp = EmpFact.CreateEmp(fname, lname, rate, dept);
 
	this.Employess.Add(emp);
 
	var x = 0;
 
	foreach(var emp in this.Employees)
	{
		x++;
	}
 
	return x;
}

Yea yea, like I said, contrived. Anyway, while you can probably tell somewhat easily what’s going on there, you can see there’s a lot of common and not so common abbreviations and shortened names being used. Dept? x? What they hell do they do? Lets try this again without being quite so lazy:

public int AddEmployee(string firstName, string lastName, decimal payRate, Department department)
{
	var newEmployee = EmployeeFactory.CreateEmployee(firstName, lastName, payRate, department);
 
	this.Employess.Add(newEmployee);
 
	var newEmployeeCount = 0;
 
	foreach(var employee in this.Employees)
	{
		newEmployeeCount++;
	}
 
	return newEmployeeCount;
}

A bit better, I’d say. On your first read through this code it’s easier to see what’s going on and what each piece plays in the puzzle. You don’t have to stop for a split second to think “first name” when you see “fname”. You just read “first name”.

I know a lot of people scoff at longer names when they first see them (like FindAllCustomersInStateNotUsingDiscountWhenAvailable), but that’s cool. It is weird at first, but you get used to it. Even more foreign to most developers is using underlines for spacing: Find_all_customers_in_state_not_using_discount_when_available. I personally find that version even more readable as you don’t have to mentally parse the words, but it’s a hard pill for a lot of developers to swallow.

Regardless of how you do it, the next time you find yourself using a quick abbreviation or some meaningless variable/method name, please do your future self and other developers a favor by putting some meaning into it.