eLEARNING SERIES

THE AVERNA BLOG

Everything I Need to know I Learned from Python

800x300_Feasibility Study


“Simplicity is the ultimate sophistication”, Leonardo DaVinci.

The principles of Python Programming can be taken as not only a best practice for writing code, but as an important life lesson. Confucius said, “Life is really simple, but we insist on making it complicated”. To this I say, he’s got a point. Python is a programming language that was designed to keep your goals clear and concise, while everything else just becomes clutter. If it doesn’t bring you joy, or in this case, if it doesn’t have an obvious purpose, get rid of it.

Guido van Rossum, the author of Python, recognized this over 20 years ago and created a language that emphasized the importance of readability and minimalism. While this philosophy goes hand in hand with Python, it holds true for most, if not all programming languages.

How to become a Pythonista

Staying true to the philosophy of Python will actually help improve results in any language. Consider the following tips while writing code:

  1. Less is More. Focus on granularity to keep your functions clear. Ideally, the name of the function will reveal everything you need to know about it. Avoid complex if/else process flows and split up the different cases, if needed.

  2. Have a One-Track Mind. Each function should have a single purpose. As soon as your function starts multi-tasking, it becomes harder to test and validate. Like driving and texting, finish one thing before starting the next.

     test-guru-490-750Guru’s Guide: If your function name contains a conjunction (`parse_and_save` for example), you’re better off splitting it into two.




  3. You Gotta Keep it Validated. The code you write needs to be testable. There is nothing more frustrating than writing line after line to find out it doesn’t work. By keeping the input-output of your functions simple, writing these tests becomes simplified.

A Cleaner Code from Start to Finish

By starting simple, it will end simply. If something in your code isn’t working, having followed the 3 tips above will make it simple to fix. Even documentation is simplified, particularly if your docstring is clear. Python encourages you to limit your docstring to 80 characters, describing what your function is meant to do. If it takes more than that, chances are it is doing too much and it should be split up into smaller functions.

While we all know how important these steps are, they are often overlooked. For example, people often avoid writing tests for their code. They are deemed unnecessary, or it takes too much time, or the code becomes less flexible later. These are all untrue. Testing is the guarantee that what you want to get done is getting done. Just because SOMETHING happened, that doesn’t mean it was the right thing. By testing throughout, you have the confidence of knowing your code is reliable. Once you pass the learning curve of including these tests, they become a powerful debugging tool and will save you time. You have essentially created a sandbox with your objects in it. Just stick a breakpoint in there and within seconds, the issue will present itself. Testing allows you to get to know your code better, and that gives you flexibility.

These are the principles of Python but can be applied to any language and the results will be the same. Personally, I see no reason to stop there. In life, we often look for more when less is better. By looking at the big picture while staying single-focused, you will be understood, and you will get results.

“Knowledge is a process of piling up facts; wisdom lies in their simplification”, Martin H. Fischer.