Code Style

Hopefully, over the course of these tutorials, it has become clear that there is no single “right” way to do any programming task. Generally, we feel that the best way to do something is the way that is clearest to you. And as you write more and more code, you’ll start to develop a distinctive coding style, which reflects the way you logically tackle problems. We have no recommendations as to what this logical style should look like. Admittedly, chances are good it will look pretty similar to one of ours, given that the ways we do things tend to reflect the path of least resistance (unless, like me, you are an adherent of the cowboy way).

We do, however, have some recommendations as to the actual cosmetic appearance of your code. That is, we have suggestions about naming conventions, formatting, and the like. This probably seems like a meaningless thing to be writing about, but it actually ends up being pretty important when you’re either coming back to a piece of code, or when you’re sharing code with someone else. If you follow the conventions, the intent of your code is baked into the names (much like our earlier discussion on self-documenting code). This makes code much, much faster to write, read, share, debug, etc. and has the added benefit of making errors less common in the first place, since pernicious, subtle issues are handle automatically by the style conventions. The usual way these kinds of things are codified is in a style guide. Google has one, this person has one, the Python organization even has one. You will here software developers talking about “PEP 8”, this is what they are referring to.

The nice thing, and one of the things that makes Python such a shareable language, is that most of the key points in all of these style guides are shared. Our suggestion is to default to the Python organization guide, but if you’ve got strong opinions on this or want to write you own, we’re open to proposals.

Next: Further Reading
Previous: Writing for the Future

Got questions? Ask them on the McCoy Group Stack Overflow


Edit on GitHub