The Problem
Recently I was reviewing some code at work that was written by a senior developer that had left the organization. I saw something along these lines that set off a huge red flag in my head:
Address newAddress = customer.Address;
newAddress.LineOne = "122 SongbirdLane"; // more changes etc...
customer.Address = newAddress; // RED FLAG!
Although the above code technically works, I took it as a warning sign indicating that the developer probably didn’t understand how references work. Sadly, my suspicions were confirmed after I dug through some more code. Even worse, the application was littered with hacks to fix areas where this misunderstanding manifested problems.
Yes ladies and gentlemen, there are people out there that have been doing C# development since .NET 1.0 that don’t have a functional mental model of how the language works. I wish I could say this was the first time I’ve run into this. Sadly, I’ve run into it several times over the years.
The Solution
It is very easy to weed these people out during the interview process by asking a very simple interview question. That question is:
What does the following program output to the command line?
public class Person
{
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person joe = new Person();
joe.Name = "Joe";
Person tim = joe;
tim.Name = "tim";
Console.WriteLine(joe.Name);
Console.WriteLine(tim.Name);
int myNumber = 666;
DoSomething(myNumber);
Console.WriteLine(myNumber);
}
private static void DoSomething(int someNumber)
{
someNumber = 777;
}
}
The trick is that you have to ask it to every developer, no matter how many years they have under their belt.
0 comments:
Post a Comment