Want to improve your Python Code Quality? Here’s a simple thread:

🧵👇
2/20 If you look at the official PEP8 page, it says:
One of Guido's key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code.
3/20 It also quotes that “A Foolish Consistency is the Hobgoblin of Little Minds”. So writing consistent and readable code is always important to maintain the code quality.
4/20 So, how to enable these PEP8 rules? Most of the IDEs are equipped with PEP8 or other linters and all you have to do is enable them. Once you enable them, they will point out the logical and styling errors in your code.
5/20 There are various other linters which can do the similar checking for you. Some of the popular tools include PyLint(for logical errors, coding standards, code smells etc), PyFlakes(for logical errors only and not styling), Flake8(PEP8+PyFlakes+CircularComplexity).
7/20 Now that we have seen Linters, let’s see a code formatter(Uncompromised) called “Black” for Python. If you have used @golang anytime before, you would have come across a tool called “gofmt” which auto-formats the go code and thereby avoiding all the code styling wars.
8/20 Here’s what @rob_pike said about gofmt: “Gofmt’s style is no one’s favourite, yet gofmt is everyone’s favourite.” So “black” is like the “gofmt” but for Python code.
9/20 From the black repository’s Readme: “By using Black, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting". See: https://github.com/psf/black  for more details on black.
10/20 Google being one of the heavy users of Python, have published their Python style guide. You can see it here: https://google.github.io/styleguide/pyguide.html
You can also follow these guidelines for maintaining better code.
11/20 Now that we have seen linters and formatters, one important thing that most of us ignore is documenting the code. Python uses something called “docstrings” to document code. A "docstring" is a string that is the first statement in a package, module, class or function.
12/20 PEP 257 talks about the "docstrings" and you can read more about it here: https://www.python.org/dev/peps/pep-0257/
You can write docstrings in various formats like reStructured text (reST) format, Google format or the NumPy documentation format.
13/20 If you are working on a large code bases and have documented the code, you can also generate documentation(for the entire codebase) from "docstrings" using tools like Sphinx. You can read more about Sphinx here: https://www.sphinx-doc.org/en/master/ 
14/20 Next thing that we are going to look at is Type Hinting. PEP 484 talks about type hints in Python. You can read more about it here: https://www.python.org/dev/peps/pep-0484/. Type hinting is a way to statically indicate the type of a value within your Python code.
15/20 Python being a dynamically typed language, knowing the type of an object can be difficult at times. So type hinting comes to the rescue here, when you add type hints to your code, your IDE can can point out the type errors if there’s a mismatch. See the attached screenshot:
16/20 If you want to refer to all the available type hints, see: https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html. This brings us to the last tool that we are going to look at, which is MyPy. MyPy is a static type checker for Python 3 and Python 2.7 written by @JukkaLeh
17/20 You can run MyPy against a python file and it will point out all the type errors in the code. See the attached screenshot.
18/20 Thread Summary:
1. Enable PEP8 rules on your code base for consistency and improving the overall code readability. Use tools like PyLint, Flake8 etc.
2. Use “Black” for formatting your code.
3. Document your code and add type hints. Use MyPy for static code analysis.
19/20 If you use “git” to version control your Python code, I will be writing a thread very soon on how to automate these checks on your code. For now, Thank you for reading this thread till the end and please like/retweet this thread if you think it can help others.
20/20 Follow me for more threads on Programming Languages and Software Engineering.
#Python #Programming
You can follow @RaviTezu.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: