Why do we need to follow the best software engineering practices? (Part 1)
These days most of us know how to code in a programming language but many of us don’t know how to write production-level codes efficiently. This will be a series of articles that will focus on different software engineering practices. I will go through some of the important practices being followed in the software engineering industry. Let’s explore them one by one.
Writing clean and modular code
When you are writing a code you have to post that code to the production server. For example the code which was running on Google, amazon was production level code. The code that is used in production should pass the number of criteria such as reliability and efficiency before it becomes public. So, how can we achieve this reliability and efficiency? To make the code more reliable and efficient we should always write our code clean. The code is clean when it’s readable, simple, and clean. I would like to explain this with plain English examples.
“One could observe that your shirt has been sullied due to the orange color on your shirt that appears to be similar to the color of a certain kind of juice.”
Oh my god!! See how complex this sentence is. Even my head is spinning when reading this sentence. This is the best example of an unclean sentence. How can we express this sentence that anyone could understand? This could be rewritten as follows:
“It looks like you spilled orange juice on your shirt”
This sentence is much more concise, readable, and simple. This makes the code clean. Cheers now everybody can understand what you are trying to say.
I want to explain this in the programming way too.
Compare these bad.py & better.py. Which one do you think nicer and easier to understand? Obviously the second one. When it comes to clean code naming the variable meaningful plays an important role.
Tip: Use meaningful names
· Be descriptive and imply type — E.g. for Booleans, you can prefix with is_ or has_ to make it clear it is a condition. You can also use part of speech to imply types, like verbs for functions and nouns for variables.
· Be consistent but clearly differentiate — E.g. age_list and age is easier to differentiate than ages and age.
· Avoid abbreviations and especially single letters — (Exception: counters and common math variables) Choosing when these exceptions can be made can be determined based on the audience for your code. If you work with other data scientists, certain variables may be common knowledge. While if you work with full-stack engineers, it might be necessary to provide more descriptive names in these cases as well.
· Long names != descriptive names — You should be descriptive, but only with relevant information. E.g. good functions names describe what they do well without including details about implementation or highly specific uses.
Try testing how effective your names are by asking a fellow programmer to guess the purpose of a function or variable based on its name, without looking at your code. Coming up with meaningful names often requires effort to get right.
Writing Clean Code: Nice Whitespace
Tip: Use whitespace properly
Organize your code with consistent indentation — the standard is to use 4 spaces for each indent. You can make this a default in your text editor.
Separate sections with blank lines to keep your code well organized and readable.
Try to limit your lines to around 79 characters, which is the guideline given in the PEP 8 style guide. In many good text editors, there is a setting to display a subtle line that indicates where the 79 character limit is.
For more guidelines, check out the code layout section of PEP 8 in the notes below.
Writing Clean Code: Nice Whitespace
PEP 8 is for python. Here they recommend some guidelines to follow. PEP 8 guidelines for code layout:
PEP 8 -- Style Guide for Python Code
The official home of the Python Programming Language
Take a look at this sample code:
Writing clean code is very important in the industry. Most of the time you have to collaborate with other programmers in the industry. So it’s very important that others could also able to understand your code.
When it comes to programming, clean code is not enough. We should know how to modularize them. To achieve this modularization we have to break the program logically into functions and modules. In programming, the module is just a file. We can write many functions and encapsulate them into a module and reuse it wherever we need it by importing them in that specific module. In programming encapsulation is an object-oriented technique that made the code doesn’t access by the outside classes. You can simply think of this as a pill containing your medicine. Let me explain this with a simple real-world scenario example.
Everyone has a drawer right? Yeah, I hope so. It’s similar to arrange your clothes in the correct section of the drawer. For example you have to keep the shirts in the shirt section of the drawer and other items will be kept in their own section as mentioned in the picture. What good can be achieved from this? If you are getting ready for your work, you will dress up in a rush. You won’t have any time to look for your clothes. If you arrange them in a proper place it will be very easier for you to identify them. It will reduce the time consumption and you can get to work on time. I hope you guys understand what I want to explain through this example.
If you can able to follow the same habit in the programming you could be able to avoid repeating your code and reuse them wherever you want. Programming in a way that your team could understand is very crucial in production.
Let’s meet in another interesting topic in software engineering practices. Stay tuned with me everyone. Thank You.