Good and Bad Practices - a Limiting Perspective
Tags: architecture, webdev, beginners, programming
Published: 2020-11-15 19:32:31

---
title: Good and Bad Practices - a Limiting Perspective
published: true
description: An essay on the importance of keeping an open mind about what's good and bad in software
tags: architecture, webdev, beginners, programming
cover_image: https://i1.wp.com/pmihaylov.com/wp-content/uploads/2020/11/pexels-berke-can-5399299-scaled.jpg?resize=676%2C381&ssl=1
canonical_url: https://pmihaylov.com/good-and-bad-practices/
---

<!-- wp:paragraph -->
<p>Growing up as software engineers, we typically spend the first few years of our careers studying various technologies and concepts from the programming area - what a list is, what a hash table is, what's JavaScript, what's HTTP, what is a client-server architecture...</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The goal of <a href="https://pmihaylov.com/study-plan/">this venture</a> is for one to accumulate the necessary skills &amp; knowledge to land their first jobs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But apart from understanding "what" something is, over the years, the preliminary knowledge required to land your first job has expanded to also cover topics which help one not just write software that works, but also write "good code".</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Most aspiring software engineers nowadays are advised to read up about the SOLID principles, about what "clean code" is, they're also taught to split up their applications using the MVC "architecture" and have separate models, views and controllers.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:heading -->
<h2>Concepts vs. Principles</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The difference between learning how a linked list works and what the S in SOLID stands for is that the former is a concept which is easier to understand as it solves a problem much more familiar to a beginner - it helps one efficiently store data in particular use-cases.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The Single Responsibility Principle, on the other hand, is a principle which has been carved out in our software engineering books thanks to the deliberate efforts of generations of programmers who've accumulated years of both good and bad experience and they've distilled what they've learnt into a single-lined principle.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But the simplicity of it is deceptive.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Memorising a single-lined principle and a few examples of applying it it simple and anyone can do it. Any junior developer learns to cite the S in SOLID by heart before their first job interview.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, it is much more challenging to understand it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To understand and appreciate the solution, one must first understand the problem it solves.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Then, you can look at the principle as a clever approach to dealing with a common problem.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Trying to use a principle without understanding the problem it solves is like trying to use a hammer when you don't know what a nail is.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But we as a society, have always tried to circumvent the long and hard road to true understanding. We're always looking for the shortcut.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Hence, we are teaching what SOLID is quite well, but we're not teaching why it exists or what problem it solves nearly as well.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Suddenly, you receive a hammer and everything looks like a nail you can hit.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Good and Bad Practices</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Thanks to all this, we've developed the concepts of good and bad practices.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We've come so far as to be disgusted from certain patterns in our codebases. Using a global variable or directly initialising a dependency in your class instead of using dependency injection is considered a sin.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Why? </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Because it's a "bad practice". It is not "clean code".</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, in programming there are no perfect choices. There are only trade-offs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Every little idiom or witty technique you apply in your codebase has a cost associated with it. Most often, that cost is in increasing the complexity of the codebase.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After you do this too many times, it suddenly takes an hour to initialise a simple utility class you need for a small part of the larger task you are solving.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You want to directly construct a new class? No, you can't do that. You should use a factory instead. But wait, to make things even more sophisticated, we'll make it a "builder factory" so that the code is "extensible".</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This vicious cycle continues to a point where you have a small project, for which you need people with PhDs in software design to understand. And I've actually seen that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've worked on a giant project with half a million lines of code, which is easier to understand than a 10k lines project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>That is the price we pay for over-engineering by blindly following the "good practices".</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Oh, and by the way, this problem extends beyond code. It has become prevalent in our system designs as well. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, most projects built with a "micro services" architecture I've seen don't really need micro services. They could have just sticked to the not-so-glossy monolith and saved themselves dozens of problems, which the shiny buzzword brings with itself.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Dealing With Imperfect Principles</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Although I've been painting a not-so-rosy picture of all the "good practices" in this article so far, note that there is nothing wrong with the principles themselves. They have become popular and widely adopted because they are quite effective in solving some of the problems we often face while writing code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>However, by not understanding the problems they solve and the problems they introduce, it is not easy to recognise when to use them and when to avoid them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Therefore, before applying any sort of design patterns or good practices, read up about them. Don't just pick up the first vague article which either simply explains how to use them or, worse, try to sell them by outlining all the benefits while avoiding the costs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What's even better, try to figure that out yourself. Oftentimes, techniques for writing good code address the problem of making your project more maintainable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What that means is based on context. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Sometimes, making your project more maintainable means writing code which is easy to test. Other times, it is about writing code which is easier to read. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some techniques contribute to more specialised use-cases. Such are, for example, making the frameworks and peripherals of your codebase more detached from the rest of the project.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And note that certain techniques might aid you in some of those goals, while impeding you in others. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For example, applying dependency injection might make writing tests easier, but it makes initialising and using your components harder. Is it worth the cost?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It depends. Sometimes it is, other times it isn't. You are the one to decide as only you understand your project well enough.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>A Case Study</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Oftentimes, sticking to simpler idioms, condemned as bad practices, might actually be more beneficial in your use case.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Check out Go's standard library <a href="https://golang.org/pkg/net/http/">http package</a> for example. In it, there is a global http client variable.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Perhaps some bells start ringing right now - that makes the package harder to test, it raises the <a href="https://pmihaylov.com/my-thoughts-on-jcip/">risk for thread-safety issues</a>, it breaks encapsulation, etc etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And although most of those claims might be true, there is another thing which also holds true - it makes using the package much easier for simple use-cases.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks to that decision, in order to make a simple GET request, all you need to do is:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>http.Get("http://example.com/")</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>That's it. There are no initialisations, no configurations, no need to look into how to configure your timeouts &amp; headers. You just stick to the reasonable defaults which the global variable gives you and in most cases, that works just fine.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is an example of how, for the Go team, the widely adopted wisdom of avoiding global variables didn't apply to this specific use case they were aiming for - making the http package easy to use.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In software, there are no silver bullets. Not even the globally-recognised principles for writing good code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Every pattern, principle and idiom is created with a specific problem in mind. And all of them have a certain cost you'll have to pay for applying them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to use them effectively, you should examine both sides of the coin.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Otherwise, you will have to continue your endeavour in software engineering by blindly following certain patterns while condemning others as bad practices.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>At that point, decision making will no longer be about making rational choices, but more about maintaining religious beliefs.</p>
<!-- /wp:paragraph -->