Monday, March 10, 2014

Python Style: Why is it So Important to Follow Conventions?

Overview
Conventions are in there for a reason. They make things easy to scale over time by providing easy-to remember guidelines for people to follow. In python, you don't have to follow most of the guidelines. However, you should try to follow all of the guidelines when possible. It makes it easier for people to read your code as well as helps yourself search through your code faster and remember where things are.

Readability
Ever read something that had horrendous grammar? Maybe a post on my site? Then you know how hard it is to read something that doesn't follow grammer. When you don't follow conventions, you are basically writing code without following python's grammar. Sure, you can write something without grammar, but you and other people won't be able to read it/maintain it later. Writing code that is maintainable is one of the most important things in programming, because even if your program's functionality should never change, the system its running on certainly will (Like the change from python2.7 to python 3.2).

Searching Your Code
Ever have to grep in your terminal in order to find a method or variable in your program? By following conventions, you can find things faster and easier. Say you're trying to find an instance variable. If you did it right, you'll search "_var_name". This will help you weed out all of the things that have "var_name" in them, like method names. Say you want to find a method that starts with "foo". All you have to search is "def foo" and you'll be taking to all the methods that start with "foo".

Remembering Where Things Are
By Following conventions, you can remember where things are put in your program. Say you have a class file with a bunch of methods, helper methods, variables and other things. If you scatter all of these across the file, it will be harder for you and others to find methods or variables of interest to you/them. If you instead put all of the variables together, then helper methods together, then regular methods together, you'll be able to find things a lot quicker later.

Conclusion
Study and Follow conventions. You'll be able to read your code and other code faster with more comprehension.


NOTE: Please note that I am not an expert at this topic and this info can certainly be improved. If you have questions, comments or suggestions for this blog post, please comment! Also, this guide is currently in rough draft form. If you would like it to be more in depth, I will be extremely happy to improve on this, all you need to do is ask in the comments and I will do it asap (I just don't want to spend forever on something no one reads and/or cares about).

No comments :

Post a Comment