• 1. Best Python Practices

 

Back to the Table Of Contents

1.1 Introduction


Good code is not all that hard to write. You just have to know what you are doing and how to explain it to others. Here on CheckiO we encourage everyone to share their solutions with their fellow programmers and critique each other in an effort to learn new methods and improve our overall skills at writing code. Here in this article is a set of “Best Practices” to consider when writing your code and publishing it for the world to see. The Three Main Ideas When writing your code there are three main ideas you need to think about. These ideas are as follows:

  1. Is the solution simple and elegant?

  2. Is my code easy to read?

  3. Have I explained my code through comments so others may understand?

These three ideas are important in programming because they create good solid code that others can pick up, understand and implement or improve themselves.

1.2 Is the solution simple and elegant?


Guido Van Rossum, a.k.a the Python Foundations Benevolent Dictator for Life, once wrote that:

Small is beautiful. Given Python's hefty charges for bytecode instructions and variable look-up, it rarely pays off to add extra tests to save a little bit of work.

Python is specially designed to create solutions that are elegant and simple. If you find your code becoming complicated, large, slow and unwieldy, there is probably a better way to solve the problem. If you’re having trouble making your code simple and elegant, don’t worry! There are thousands of resources to be found on the internet that can help you with this. One resource we suggest is part of Googles own documentation for their Python developers. It contains a list of dos and don’ts for various elements of Python.

1.3 Is my code easy to read?


Because CheckiO is a place that encourages sharing your code, it’s necessary to make sure the code is easy to read. We’ve seen elegant solutions crammed into single lines and massive solutions with no breaks between different segments. That’s fine and all, but it makes it difficult to read and critique. It helps to clean up your code before publishing it, so your peers can review it and give you proper feedback.

1.4 Have I explained my code through comments so others may understand?


Along with readability comes comments. Professional coders use comments to explain their code so that others who are working on the same code can readily understand and work on it as well. Microsoft does this, Google does this, if you expect to work with others, you should do this too.

Check out following articles to get more information on commenting system:

  1. https://www.python.org/dev/peps/pep-0008/#comments
  2. http://www.programiz.com/python-programming/statement-indentation-comments
  3. https://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Comments
40