The importance of meaningful variable names

I was having my usual browse around Stack Overflow today (I really want that Fanatic badge) when I came across this question about fatal errors.  The coder in question was getting the following fatal error:

Fatal error: Call to a member function Createuser() on string

The code section in question was

$u = $_POST['username'];
$p = $_POST['password'];
$e = $_POST['email'];

// attempt to create user
$reg = $u->Createuser($u, $p, $e);

They went on to comment that the variable $u had been created as an instance of the class User earlier in their code, so for the sake of argument, their code was essentially:

$u = new User();

// some code here....

$u = $_POST['username'];
$p = $_POST['password'];
$e = $_POST['email'];

// attempt to create user
$reg = $u->Createuser($u, $p, $e);

The error message did tell them everything they needed to know, but they couldn’t see how this would happen.  For those not into code, they had created $u as a User object early in their code, and then had set $u (the user object) to be something different, which stopped it being what they thought it was.  This could have been avoided if they had used sensible naming of the variables.  it would have made the whole thing a lot more readable, and probably would have prevented the issue at hand.  Compare the previous code snippet with the one below to see what would have needed to be changed.  It’s not a lot, but makes it a lot easier:

$user = new User();

// some code here...

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

// attempt to create user
$registration = $user->Createuser($username, $password, $email);

The overall difference is around 60 bytes.  Ok, this will multiply with the size of the code base, but it’s not a bit issue when it improves readability and will help avoid the error which was presented.  It’s not likely you’re going to confuse $user with $username.  Both are descriptive for what they need to be, and what they actually represent.

That being said we’re a hypocritical bunch, coders.  We want nice and simple variable names for things, but we’re always happy to do loops using non-descriptive variable names, such as:

for ($i = 0; $i < $arbitrary_amount; $i++)
{
    for ($j = 0; $j < $other_upper_limit; $j++)
    {
        // some code here
    }
}

As soon as we start with the highly useful for-loop, we forget that we like nice variable names and make things “easier” for us at the time.  What exactly are $i and $j? Who knows.  They are numbers, but they mean nothing.  It’s the way we have been taught, and the way tutorials are written.  Getting people to change to name $i and $j to $outer_loop_count and $inner_loop_count probably isn’t going to happen as, ultimately, they still don’t improve the meaning of them.  Sometimes we’ll just have to live with useless variable names; but live in hope that other variables will be usefully name when we come to maintain code.