Write the RIGHT Code

I am writing this blog as an awareness for the developers to write good quality code.

As a human being we all take care of good hygiene, cleanliness, good looks etc etc etc. So that we live longer healthier in a good way. So why do we write code so messed up that  the next person will not even willing to read the file one more time. I really believe we should take care of the code maintenance more than the logic. So that the code is loosely coupled, properly modules, properly commented and well maintained.

Starting from the basics I would suggest prepare a coding guideline and go through PEP8 in detail. That will not only keep the readability of your code but also keep it healthy for future upgrade and debugging. Use a library named Flake8 that scans python files for coding standards and gives errors/suggestions to correct them. I use sublime text for coding and there is a add on for flake8 available in it. I will suggest tightly follow and after 1 month you do not need any coding standards to make your code better. Bellow are few major suggestion I want to give that developers often forget/ignore, but in long run those are the most crucial things to do.

  • Always try to make your code/apps/microservices/module/utilities modular/loosely coupled. Meaning i can take some part of the code out of the complete application or add a new thing to it at any point of time without breaking it. Sometimes it needs some configuration changes some times code changes. Try to make it less dependent on those. The lesser the better.
  • Always write Doc-strings properly. There are apps which can generate high quality documentation from the doc-strings without even bothering you. A well structured doc-string not only helps future developer, but also helps making the life span of the code longer. even after that i will suggest write doc-strings properly. follow Flake8 for styles and add @param, @raises and @returns correctly. If you have doubts about it google it  or ask me by dropping a comment below 🙂
  • Follow the spacing, naming standards, commenting rules and other small things very strictly. You might think this is unnecessary but in long run if you follow these strictly you code will be more readable, easier to debug and easy to upgrade.
  • Always keep upgrade in mind while coding if you want to make your code survive over decades. The world of technology is getting evolved second, keep pace with it.
  • Always remember python is an object oriented programming language and the theories that you know about coding with it is actually implemented within itself. So if you want to write proper pythonic code follow the object oriented concepts with your code. Like make the function names start with _ if you don’t want to expose those functions to everyone. try to make some important variable start with __ so they will be accessed with the module. Try not to import with *, if you are using _ import with * will not harm you etc etc.
  • You should always give extra attention to imports. even if you have written the imports properly without *, in alphabetic order, etc etc still you may not be the only one working on that code and hence those will become crappy very easily. MAINTAIN it.
  • People often says import within the code should not be done and should be done at the top of the file. I agree, but sometimes we need to write a lot of functions for one use within the file and we know it will always be used once only in that file. In that case why to preload everything at the beginning and make the process heavy. Just do it within the function and your execution will be faster i bet.
  • Developer hate to write test-cases, even i hate too but they are pretty helpful man. We all should write them. There is a library TOX, i will suggest learn about it and it will definitely help you in maintaining your code.

There are uncountable rules for making your code better, obviously we can’t go through each of those. You should be the one who can find out these and make your code better. I am offering free code checks and suggestions, you can drop a comment or contact me for a free code check. Below i am attaching a screenshot of a sample code i have written to generate a csv report with the style i like. This is not my best code but something that i could find at this moment of time to show you. This type of code is not only readable but also very easy to debug and use.

Capture

Leave a comment