Camel Case
10 March 19
When learning coding a junior developer sometimes finds it difficult to understand a single line of code, let alone provide a reason why a whole chunk of 10 lines of code is there and why it is the best way to do something.
Often learning resources for coding don’t give line-by-line explanations and the reason why it is the best way to do something - Stack Overflow is amazing, but does not always provide the most educationally sound explanation about the code you’re pasting into your project.1
The following two versions of the same explanation about naming variables and camel case shows the difference between an annotated example and a less detailed example.
Example One: Not So Detailed
/*
* Why is it important to use a naming
* convention for variables? Because
* when you work on a dev team, or when
* you do pair programming. You need to
* be on the same page as your colleagues.
* Keywords: camel-case, variable, declaration
*/
// DON'T:
const p = 3.14;
// CONSIDER:
const pi = 3.14;
const valueOfPi = 3.14; // This is better!
// DON'T:
const theValueOfPiToTwoDecimalPlaces = 3.14;
/*
* The moral of the story is: please use camel case.
* It's good practice and makes your variable declarations
* more readable and easily understood.
*/
Even if you aren’t a developer you could understand the reasoning behind some of the above. It’s easy to see why theValueOfPiToTwoDecimalPlaces
is a silly, difficult to read name.
But what about the contrast between pi
and valueOfPi
… why is valueOfPi
better? Why can’t I use a single letter as a variable name? Isn’t that what we did in Year 9 Algebra?2
Example Two: Explanation every step of the way
/*
* Why is it important to use a naming
* convention for variables? Because
* when you work on a dev team, or when
* you do pair programming. You need to
* be on the same page as your colleagues.
* Keywords: camel-case, variable, declaration
*/
// DON'T: Use single letters as variable names
const p = 3.14; // because it is unclear at
// a glance what they are for.
// CONSIDER, Giving some context to
// help others understand:
const pi = 3.14; // is not as clear as:
const valueOfPi = 3.14; // Use camel case
// DON'T: However, do this, it's an abomination and abuse
// of camel case:
// If you are unsure about length...try to remember that
// typically, Camels only have a maximum of 2 humps!
const theValueOfPiToTwoDecimalPlaces = 3.14;
// How many capital letters (humps) does this variable name have?
/*
* The moral of the story is: please use camel case.
* It's good practice and makes your variable declarations
* more readable and easily understood.
*/
A very effective way that teachers and mentors can help to build understanding is to provide annotated examples. If you are teaching something, you will be able to do this with a bit of practice, and if it’s your first time teaching something (we’ve all been there), annotating an example is the best way you can be a few steps ahead, and see the gaps in your own knowledge.
Camels do in fact have two humps.3 But there are two different kinds, and I did not know this before embarking on writing this thing.
-
You shouldn’t be pasting code from Stack Overflow into your project if you don’t know what it does. ↩︎
-
Thankfully, coding gives some form and purpose to the abstraction, but if you would like to wallow in the misery of Year 9 r/teenagers has some contemporary misery on the topic. We didn’t all have a teacher like Eddie. SOB. ↩︎