Improve a piece of code by rewriting a couple of variable names so that they become more readable and more descriptive.
You are working in a big e-learning company as an intern and have been given a very easy task. That is, you have been provided with a piece of code from a senior developer, who's a nerd at solving coding problems but very bad at naming variables.
He has been working on the application, that is to be released by the company very soon as part of their e-learning motive.
Your task is to improve the naming of variables in a given segment of code which is part of the application, where a course's details are showed to the user, such as its name, description, total number of chapters and so on.
Here's the code:
<?php // Name of the course. $cn = 'PHP' // Description of the course. $cde = 'A web server scripting language' // Difficulty level of the course. // A number in the range 1 - 10 $cd = 7 // The number of modules in the course. $cm = 15 // The number of chapters in the course. $cc = 70 // The number of quizzes in the course. $cq = 20 // The number of exercises in the course. $ce = 35 // The number of projects in the course. $cp = 5
Above each variable, is the description of what it would hold later on. Currently, each variable holds some placeholder value. This would obviously be changed later on; but that's a different story.
For now, we just need to focus on the naming aspect.
Along with the code, you've been given a set of rules to follow.
- You must follow the snake casing convention.
- Don't abbreviate names with their initials.
With all this in hand, you have to come up with a better piece of naming, if not the best.
After all, variable naming is not a theoretical matter, whereby a given set of rules could be applied to arrive at a clean name — it's an art that one learns with time and experience.
Inside the directory you created for this course on PHP, create a new folder called Exercise-2-Clean-Naming and put the .php solution files for this exercise within it.
The name of the course could simply be written fully as
$course_name. It's short and descriptive and abides by both the rules above. Perfect!
The description could be denoted as
$course_description or even as
$course_desc. What you choose over here is totally preferential; but often times, the word 'description' can be shortened down to just 'desc', so in that way, we'd go with
The difficulty level of the course has quite a few options for naming. We could call it
$course_diff, and so on and so forth. The word
$course_diff misses on the word 'level', likewise we just put it aside for now. Of the two choices,
$course_diff_level, we go with the latter, since it's shorter and still meaningful. But you could also go with the former — it's just a matter of preference.
To represent the total number of modules in the course, we could say
$course_modules and so on.
$course_total_modules is long without any sort of use, hence we'll keep it aside. So is
$course_mods is a bit vague — it could even mean 'course modes'. The thing is that 'modules' isn't a very commonly occuring word, hence we couldn't just call it 'mods'. What we're left with at least in our list of options is
$course_modules; which is both short and descriptive. Perfect!
In the same way, we name all the variables that follow. That is, the name starts with 'course_' and ends with the word that is represented by the variable.
The only exception is the variable for the total number of chapters. We could've called it
$course_chapters, just like we did with all the following variables. However, there is one important thing to note over here. The word 'chapters' can easily be shortened down to 'chaps'; yet it still is recognisable. Many course books use the word 'chap' commonly to denote the word 'chapter'. Likewise, we use the variable name
$course_chaps for this case.
And with this, we are done!
<?php // Name of the course. $course_name = 'PHP' // Description of the course. $course_desc = 'A web server scripting language' // Difficulty level of the course. // A number in the range 1 - 10 $course_diff_level = 7 // The number of modules in the course. $course_modules = 15 // The number of chapters in the course. $course_chaps = 70 // The number of quizzes in the course. $course_quizzes = 20 // The number of exercises in the course. $course_exercises = 35 // The number of projects in the course. $course_projects = 5
One thing to keep in mind at this point is that this renaming of variables that we've done doesn't need to be only this. You could use other names as well, as long as you are confident that those names are short and meaningful.
This exercise was just meant to give you a subtle sense of how to approach variable naming in programming.
Being able to name variables in a meaningful way is an extremely paramount skill developers must have. This helps other people, who are reading your code, to quickly grasp on what is going on in there.
Well, frankly speaking, it helps you as well, when you read your code after a long long time!