DEV Community

Cover image for Checking your python code?
leslysandra for Codiga

Posted on

 

Checking your python code?

Hello world! Let's talk about python 🐍
Whether you are a beginner or an advanced python developer, it is a good habit to check your code frequently (while writing, deploying or refactoring). You might wonder, where to start or how to start, here some ideas to check your python code 🐍💻

But first, recall about good code! 🤓 High-quality code should do what's meant to do (functionality), should not have problems or other errors, and should be easy to read and maintain.

✅ your python code should be easy to read for others (First checkpoint!)

There are different tools, that can help you check your python code. I'll mention a few of them, that I've been trying:

  • Pylint supporting with Coding standards, Error detection, Refactoring help, among others.

  • Pytest helps you write better programs

Although, both can help you write good coding, also check these general remarks that will guide you in your regular checkings:

  1. Check spaces ✅
    Coding on python? spaces are super important! Remember to use whitespaces after a colon, semicolon, or comma, as well as when using operators (it is a good practice!) to have a whitespace ' ' before and after an operator (=, <, >, etc).

  2. Booleans ✅
    Avoid repetitions... if you are writing this:

var flag = TRUE
# something something

if flag == TRUE
   #something
Enter fullscreen mode Exit fullscreen mode

Consider in better writing this:

var flag = TRUE
# something something

if flag
   #something
Enter fullscreen mode Exit fullscreen mode
  1. Handling errors ✅ If writing this...
try:
    #something something
except: 
Enter fullscreen mode Exit fullscreen mode

Consider writing this...

try:
    #something something
except TypeError:
Enter fullscreen mode Exit fullscreen mode

We can go on and on, talking about recommendations to have a high-quality python code! I recommend checking out pytest and/or pylint and see how much better you are getting at coding in python 🤓 and also if you would like to automate and learn more about common errors and best practices, check Code Inspector helping to build code better, faster.

Alt Text

Top comments (1)

Collapse
 
gsandec profile image
Gustavo Silva

Great article Code Inspector!
Here's an article about python static analysis tools we hope it adds value to your readers.
Keep on with great articles!

🤔 Did you know?

🌚 You can turn on dark mode in Settings