My code, My Pride — Part 1
Write Clean Code and you can sleep well at night!
This article is very suitable for those who are willing to prove themselves as a CLEAN CODER and they are in very beginning stage of practicing clean code
WRITING IN PYTHON 🐍 ASPECTS
Yes, here I am going to explain the way I am practicing clean code and proud myself.
Four things make your coding practice, very clean
- Use a coding standard
- Auto documentation
- File & Folder structure
Practice #1: Use a coding standard
All programming languages has its own coding style and standard.
Why coding standard?
When you are working with a large team (for startups 1+ team), your code must be in standard structure, so that, anyone can understand the code logic easily.
If you are a solo programmer for the project, even after a long time, if you review your own code it will give you clear idea about the logic
How to apply coding standard?
In Python world there are two standards are very popular which gives you the clean guidance on writing code in very standard manner. They are
- PEP8 - https://www.python.org/dev/peps/pep-0008/
- Google Style guide — https://google.github.io/styleguide/pyguide.html
There are some differences between PEP8 and Google style guide. PEP8 is the old one exclusive for Python development, most of the open source projects using PEP8 style guide. (I personally prefer PEP8 which is simple & easy to adopt than Google style guide)
How style guide help you?
The style guides explain you
Wrong approach of writing of each cases.
Tab or Space is a commonly asking questions by Python developers. This style guide will give you the answer for that simple question.
Here is the answer from PEP8 style guide
Practice #2: Auto documentation
The next big thing in your Clean code practice is Documentation.
Documentation is a Love letter that you write to your future self
As Python is using for both Webapp development and Backend development I am using two different Documentation
- API Documentation
- Module Documentation
Why auto documentation?
Most of the developer don’t write a documentation separately because of the time and effort to maintain document is more than development. So, auto documentation will help the developer to create the document as the
comment on their code base. The auto document tool will create the beautiful HTML based document.
API Auto Documentation
There is a popular tool called Swagger which is the commonly using interface tool for delivering any API documentation.
Here is a detailed article about Swagger Flask integration
Automated Swagger API Doc with Flask Restful
Before diving into the blog, lets clear the air and talk about some of the terms that we are going to use in the blog…
How to create API Auto documentation?
It is so simple, to create the documentation for your API.
Here is my example
Execute a pipeline
- in: header
description: Bearer <token>
- in: body
description: Get config request body
logger.info("Started Triggering pipeline")
There are three main sections in the documentation
- Responses (optional)
In the description part, we can add the information about the REST endpoint. The Tags are the used to show the endpoint in a collection.
Parameters are used to, how the user/developer can pass the input to the endpoint. There are four places, we can capture the parameters
- Header — used to pass the credentials and JWT tokens
- Body — used to pass the large number of requests in JSON/XML
- Path (url path) — URL Parameter to pass the short things like ID
- FormData — used to send files
There are some Swagger tools which can read these documents from your code and create the HTML based beautiful Swagger UI.
Try these libraries
A Swagger 2.0 spec extractor for Flask Install: pip install flask-swagger Flask-swagger provides a method (swagger)…
Simple Flask blueprint for adding Swagger UI to your flask application. Included Swagger UI version: 3.36.0.
In Part 2, I’ll write more on Folder structure & Testing.