In my previous article, I discussed the benefits of plain language for a developer working in a team. I also mentioned some Tenets of good code.
In this article, we’ll continue to explore what good code is by discussing a tenet that I am sure, almost, everyone who has written code has faced. Structure.
Noooooo!!!! That’s not how you do it
Over the last year, I have learned that computers are very very stupid, they can only do exactly what you tell them to do. They are a stellar example of the following statement:
“Every system is perfectly designed to get the results it gets. – The W. Edwards Deming Institute”
As programmers, our job is to give these computers the right instructions in order to get the right output. Every decision we make as we build our code is vital, as nothing should be left to chance.
How then do you decide what to name your variable or function? What case to use and how many spaces to leave.
Stick around and I will try my best to demystify the decision-making process!
Get Some Class
Learning about the language you use and how it works is completely vital in utilizing it to the best of your abilities. I code mostly in python so this part of the guide will be more relevant to OOP (Object Oriented Programming).
A class is a blueprint for an object and an object is the instance of a class. We have all heard that and I know most people understand it but, when do you decide to use it rather than just a function? My rule of thumb is rather simple. If it’s a thing and not a process you should probably use a class and if it’s a process you should probably be using a function.
For example, if we are building a product catalog system, the product would be a class and each individual product would be an object. On the flip side if our catalog system needs to arrange, add, delete, etc we could write functions that are performed by each of these functions.
Does length really matter?
When deciding on the names of functions and variables, a good question to ask is: does this name accurately depict what is being performed without any further insight? A few practical examples, for instance, we are building a product catalog system and we create a function to sort products into their appropriate classes whats a good name for this:
def sort_products():
pass
def sort_products_into_class():
pass
def class_sorting():
pass
def products_sorting_functions():
pass
Try and pick an answer before moving on…
Going through the options we have…
- Option 1 is bad because of its ambiguity you can sort 100 items based on numerous features. Someone looking at the code for the first time wouldn’t be able to tell
- Option 3 class_sorting!!!! Now that’s just bad.
- Option 4 is more like it but you have a function in the name. In python, it reads now has function products sorting function. Plus it has the same ambiguity as option 1
- Option 2 is my preferred choice simply because of its simplicity and readability (if you can think of better drop it in the comments below, eager to learn!!)
Variables follow the same principle. Having the right variable and function names can improve the ease and speed at which your code. Leaving your brain some room to focus on much harder tasks.
Keep it simple and readable
Lastly, let’s address consistency, in naming and formatting. Please please and please for your sake and that of every other person who is going to read your code, be very consistent in the naming and typing conventions you choose I.e if you are using snake case for a variable name (this_is_my_variable) you should stick with it throughout your process and if you are using camel case, stick with it. You can use one for certain purposes. I personally prefer naming functions in snake case and variables in camel case
Examples code below
def bad_product_example():
isAvailable = True
sale_price = 90000
def good_product_example():
isAvailable = True
salePrice = 90000