The importance of naming variables

It cannot be mentioned enough: do name your variables properly!!!

Last example of stupidity:

you want your methods to be portable, so you never use globals inside this method,

thus you pass values by reference, using pointers, not making all kinds of copies

but if you forget that the pointers are pointers and change one of the variables a little bit in a hierarchy of methods,

this variable is changed everywhere!!!

The variable names were ballPosition and ballSpeed,

The methods were a sequence of: nameOfTheMethod ( ballPosition, ballSpeed ) in a complex algorithm using a ball with width, so having to take care of the distance of the bouncing wall,

some methods for bouncing against a side, others for boucing against a point, all together steering this bouncing ball in an irregular mesh of triangles….

Something went wrong somewhere, having in one method these lines:

  • ballPosition.x += .1 * ballSpeed.x;
  • ballPosition.y += .1 * ballSpeed.y;

Thinking this changed the variable ballPosition only in this method…


If I had named the variables: ballPositionPointer and ballSpeedPointer, I would have avoided this mistake and/or found out sooner where and why the variable was changed ……..

Lesson: always indicate in the name the kind or type of the variable.

This mistake happened in XCODE, objective C, where you can only define “Pointers” to your own datatypes. This not being able to use “normal” variables made me fall asleep, thinking no mistake would be possible.

So rename your variables immediately, add Pointer or Ptr to pointers!!!

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: