Hi! And welcome to this lesson. Today, we’re going to look at good programming practices.
Why adopt good programming practices?
Coding is not just about writing lines of codes, one after the other. It’s complex and challenging, and requires a certain degree of skill.
I’m going to introduce a few important good practices that you should always keep in mind while coding.
Do not optimize first
First, do not optimize first!
As we’ll see in the coming weeks, coding is often a case of finding the best compromise between correctness of the code and speed. The problem is that achieving a good speed boils down to exploiting adapted data structures, using elaborate algorithms, and sometimes even using low-level tricks to accelerate computations. In many cases, these optimizations have an impact on the readability of the code, which can make it more difficult to see what it does.
You should always have a correct program before optimizing, and there’s no need to consider speed at this point.
Optimizations can be implemented later. It’s always much simpler to debug a code that was built to be correct rather than a code that was optimized in terms of speed.
Structure the code
It’s no secret that coding is difficult. And why’s this? Because programming languages are not meant to be easily understood by humans, but to be easily processed by machines.
Writing code means that you can explicitly describe the details of a given method in a way that is unambiguous.
Most of the time, a programmer doesn’t think about the low-level programming language itself, but uses abstract language layers.
Coding is all about stacking up more and more abstract layers, until your program can be written with the help of just a few functions.
This process is called structuring the code. And a code should always be structured.
As a rule of thumb, you should always try to code functions that are no more than 15 lines long. If it contains more than that, then you should probably try to split your function into multiple subfunctions.
Factorize the code
This process is very important, because one day you might want to modify your code, update the code to take into account new inputs, or optimize part of the code. It’s much simpler when parts of the code to be updated are at one location.
No mysterious constants
For example, you might want to use the number « infinity » in your code, but such a thing doesn’t exist in computer memory.
Remark (outside of transcripts): This is an example for illustrating the message. In practice, Python has a pretty good way of handling « standard » numbers (infinity, pi, etc.), that you should favor rather than setting constants. You may want to have a quick Google search about this, or just check
Explicit inputs/outputs and comments
Finally, you should always write your code thinking that it will be read by someone else.
This is obviously the case if you’re working in a team, but even if you’re not, adding comments is a good way to be sure that what you’re doing is correct.
A good way of using comments is to write them first, and then add code sparingly between comments.
If you don’t know what comments to write, begin with an explicit description of a function before you write the corresponding code.
Coding is not and should never be a question of the number of lines of codes written per minute.
If it takes one hour to write one line, then so be it. Maybe that single line will be used by hundreds or thousands of programmers, so it can be worth every moment.
If you follow all these principles, then you have all the keys to becoming a great programmer.
Thanks for watching this lesson. I how you will now adopt good programming practices. See you next time!