Topic Archive

Best Practices

  • A challenge in software development is to keep things simple 🤯 For your code to not grow overly complex over time 😱 Simple is better than complex.Complex is better than complicated. Zen of Python 🐍 Simplicity in your code means fewer possibilities for bugs to hide and easier debugging when they do arise 📈 It also makes your code more understandable and maintainable, which is crucial in a team setting or when returning to your code after a period of time. Photo by Pablo Arroyo on Unsplash Let’s look at 5 examples: 1. Are all numbers divisible? A good first example is underutilizing…


  • Anyone who’s worked with Python knows that modules can be a Godsend, saving you time, effort, and many lines of code. They even have namespacing built-in 💪 😍 To expand on this a bit: However, not all ways of using modules are equally beneficial. In this article, we will discuss why using import * can be more problematic than it’s worth, and what you should do instead. Why is this a problem? When you use import *, Python brings in every single function and variable from the other module into your own. This can cause something called “namespace pollution” 😱…


  • When to refactor your code?

    ·

    ·

    10 min read

    How to make refactoring part of your Definition of Done Writing code is an iterative process. The first iteration is usually not the best result. Grooming and polishing ✨ are needed before the code is ready to share with the world (and your future self). There is a saying in software development that illustrates the importance of polishing code: The first step is clear: your code should solve a particular problem. But what if your program’s input is huge and requires clever code optimizations to reach the required performance? Maybe you would be tempted to skip step two and jump immediately…


  • Python allows you to use *args and **kwargs in function definitions to accept an arbitrary number of positional and keyword arguments, respectively. Here is a simple example: Different types of function arguments In the above example, the arbitrary_args function is defined to accept any number of positional and keyword arguments using the *args and **kwargs syntax. When the function is called with arbitrary_args(1, 2, 3, a=”apple”, b=”banana”), the *args collects all the positional arguments (1, 2, 3) into a tuple, and **kwargs gathers the keyword arguments (a and b) into a dictionary. Consequently, the function prints the tuple (1, 2,…


  • In the world of programming, errors are inevitable. But how we choose to handle these errors can make the difference between a system that is robust and user-friendly and one that is fraught with ambiguous issues 😱 The Zen of Python famously states, “Errors should never pass silently.” This principle emphasizes the importance of addressing issues head-on rather than ignoring them 😅 In this article, we’ll delve into the consequences of silent failures, the value of transparent error handling, and practical ways to ensure that errors in our Python code are always brought to light. Requests Let’s look at a…


  • The short answer: deeply nested code can be hard to read and understand (and this not only applies to Python, but for any code really). Each level of indentation adds a level of complexity and an additional condition that the reader (which is often you!) has to keep in their head while trying to understand the code. Here is an example of deeply nested code and how we can refactoring it to make it more readable (and therefor maintainable): In the refactoring we reduce the level of nesting by using “early returns”. Instead of nesting conditions, you can invert the…


  • This was a Pybites email first. To always get the latest and greatest content, subscribe here. Lately I have been learning some new libraries for weekly PDM Code Clinic demo sessions (e.g. PyScript, Flet, PySimpleGUI, Playwright, htmx, Redis, Leaflet, etc.) There is a general approach I take that makes this less painful and more fun. Of course having done development for quite a while makes things easier, but even after years of experience this craft does not come easy per se. The following tips are still generic enough that they help developers of all levels. Let’s also time the steps…


  • Reflections on the Zen of Python

    ·

    ·

    5 min read

    An initial version of this article appeared as a Pybites email first. If you like it join our friends list to get our valuable Python, developer and mindset content first … How following the Zen of Python will make your code better, a lot better. This epic set of axioms (triggered by typing import this in the Python REPL) says a lot about code quality and good software design principles. Although each one is profound, let’s look at a few in particular: Explicit is better than implicit. This is an important one. If we don’t state things explicitly we miss obvious facts…


  • Tips for clean code in Python

    ·

    ·

    5 min read

    In this article I will give you 10 tips for cleaner code. Hope this helps improve your Python code. 1. Smaller units. Break long functions (methods) into multiple smaller ones that do one thing (SRP or “Single-responsibility principle”). This will make your code more modular and easier to test. For example, a function that parses a csv file, builds up a result list and prints the results does 3 things and should be split accordingly. For more about writing better functions in Python, see this article. Update: a great addition by Michael Kennedy is to watch for comments in long…


  • Somebody asked the other day for tips on how to refactor a mega-class? It was actually one of the first tasks on the new job, ouch! A single class, several thousands lines of code, no tests available 😮 You might scratch your head and say WTF?! After all, good developers decouple code into manageable pieces and write a test suite, no? Well, not everywhere, horror stories like this are more common than you might think 😥 So the person tasked with this was now puzzled 😵 – How do you figure out what is going on with a new class?…