DEV Community

Jason Omondi
Jason Omondi

Posted on

Python for Everyone: Mastering python the right way.

Mastering anything takes time and adequate practice. Like any other programming language, for you to fully understand python as a programming language, you need to practice a lot.
Learning how to program is different from actually understanding what is going on and why you do what you do on different scenarios.
Before we start off I would like to give you a little secret that will help you grow in programming. When you are comfortable with the syntax of a language and you have gone through all the basics of the language, start on a serious application from scratch (not templates) and try to consolidate all the knowledge you have acquired and develop the application.
With this approach there are a lot of things that you will learn as you develop which may not be covered in the basics. By the time you are done with the application you will be bubbling experience enough to secure you a job.
We are going to tackle a few fields just to enlighten you on some advanced topics.
• Naming conventions
• Error handling exceptions
• Documenting code
• Unit testing
• Refactoring

 1. Naming conventions 
Enter fullscreen mode Exit fullscreen mode

These are rules that a programmer follows when naming the objects and variables when programming.
You need to choose one naming convention and use it in the entire application. It is highly discouraged to use different conventions because of confusions.
They make the code readable to the programmer and also the reviews or people who will manage the code later when you are gone.
Examples:
- Camel case – this is where the first letter of the word starts with capital a letter and in case of other words in the name they also start with a capital letter e.g. StudentName = ‘Alvin’ or Student_Name.
- Pascal notation - the first word starts with a small letter and if there are words they start with capital letters e.g. studentName = ‘Alvin’ or student_Name.

You can also come up with your own convention which is suitable for you but make sure it is consistent throughout your code.

 2. Error handling exceptions
Enter fullscreen mode Exit fullscreen mode

Error handling is just taking care of errors that might occur during usage of the application.
There are a lot or exceptions that can occur and cause the application to crush if not dealt with appropriately.
One thing a programmer should know and understand well is writing code in a way that most of the errors will never occur in the application. For example if a user is required to enter an integer as input, you first ascertain that it’s the right data type then proceed else provide a meaningful error message to the user.
As you proceed you will learn on how to navigate through and make sure that your application is very stable and reliable.
Exceptions are basically logical errors that occur during runtime of the application. Examples of the exceptions are:
• ImportError – occurs when the imported module is not found.
• IndexError – occurs when index is out of range mostly in arrays
• SyntaxError – occurs when a syntax error is encountered in the code.
• OverFlowError – occurs when a result of an arithmetic operation is too large to be represented given the data type.
These are just but a few exceptions. When developing an application try to understand as many exceptions as you can and find ways to mitigate them and the end results will be fantabulous.

 3. Documenting code
Enter fullscreen mode Exit fullscreen mode

When you develop an application there is always that part where you have to write very complex code to achieve a certain goal.
A writer once said that the code you write today may be as clear as mad tomorrow even to you.
To make sure that we write code and understand it in 20 years to come, I will highly recommend that you document you code when necessary.
By documenting I mean this, write comments above lines of code that you think needs some explanation. This will come in handy when you are not available and someone else is managing your code. They will tend to understand it more quickly and make their life easy.
Commenting out a line you start with a ‘#’ sign and the compiler will ignore that line during execution.
Example: # This piece of code is computing the net salary of an employee by getting the total tax, deducting NHIF, NSSF from the gross salary.
In days to come, with the first glance of the code you will be already understanding it and maintaining it with no itches at all.

 4. Unit testing 
Enter fullscreen mode Exit fullscreen mode

This is a process where small portions or functions are tested individually in order to ascertain that they give legitimate results with different set of values.
Any application that is developed and is supposed to operate in the business world needs a lot of testing. Tests expose short comings of an application before it reaches production stage.
In order to achieve unit testing, a programmer should embrace the separation of concerns. This is where each class or function does one thing only. With this it’s easier to test each function and with all the possible set of results, approve that the function works correctly.
Python has a module (unittest) which helps in testing.

 5. Refactoring 
Enter fullscreen mode Exit fullscreen mode

This is where a programmer goes through the code and try to make changes based on newly learned concepts or reducing code in either way.
When you are writing code most if not all upcoming programmers tend to use very long ways to achieve something. It is accepted and it is not a sin. When now you sit down later and try to read your own code after some exposure you will release that there are approaches that you made which are relatively long.
The act of changing code to simpler versions or just changing the logic is called refactoring.
The changes can be made increase performance of maybe some queries, stability of the application and also to reduce complexity or lines of code.
The main benefit of code refactoring is to help clean up dirty code so as to reduce the technical debt.
A technical debt is a basically the rework on an application that was already in production due to instability or low performance.
When you write code try to take it to a reviewer who has higher experience who will point out shortcomings and help you reduce the technical debt.
These is just but a few things that one should look out after learning python programming. This will help develop industry grade applications with minimal or no technical debt.

Top comments (0)