tangaroa ([personal profile] tangaroa) wrote2017-02-06 03:41 pm
Entry tags:

A lesson in meaningful variable naming

A variable representing a link to a website might be called $url or $link or $site. Take this project I'm working on. At one point in the code the variable might be called $url. At another point it is $link. Further on, it is $site. I want to reach back across time to my younger self and slap him the face and scream at him and impart a lesson on the value of maintaining a consistent naming convention.

So let's say $site represents a link to a website except when it doesn't because I am also using $site as a unique key to represent a website in the general sense that "this website contains web pages". For this purpose I send $site through a filter that strips out the protocol and leading www. and the ending index.html if it exists. This means that $site is not the same thing that it used to be. So $site and $link actually are two different things with two different meanings. They should be two different variables.

I fixed a few bugs just by noticing that the meaning of these variables was not consistent.


In the same program I have two variables $sites_visited and $destination_count that seem to have a similar purpose.

$sites_visited is:

  • set to 1 when process_site() is run
  • checked when recursing into new sites
  • was sent to process_site_results() in an earlier version, but this is commented out

$destination_count is:

  • increased by 1 when process_site() is run (this is a bug)
  • increased by 1 when a site's list of links is finalized
  • used by process_site_results() to determine whether the site should be included in output.

So both of them are set to true when a site is processed and were intended to be used to tell if a site is worth processing. However, they mean different things.

  • $sites_visited is true when a site has been fetched and processed.
  • $destination_count counts the number of times a site has been linked from another site. It is nonzero (true) before the site is fetched or processed.

So they remain two different variables because they account for different concepts.