Tech writer with 621K+ views. Sharing my opinion and what I learn. Danish C# backend engineer in FinTech. Ex Big4 senior tech consultant.


The art of turning 6 lines into 92 lines by replacing if-then-else and switch with objects — for the better.

Image for post
Image for post
Credit: the author

Sure, if-else and switch makes for condense, simple code. But your software should not be comprised of the fewest lines possible, sacrificing readability, maintainability, or flexibility.

I see a lot of branching happening over enums, or, other discrete values. Some developers even get aggravated when told not to use if-then-else.

But, what do you think the consequences of using enums in your if-then-else statement are?

Branching on discrete values makes your software difficult to change. Every new feature requires you to track down where the branching is happening, and modify your existing code accordingly.

This is definitely not how we want to develop great software. It’s perhaps a perfect first step towards making your code work. But, as you progress to making your code better, switch and if-then-else must be long gone. …

Image for post
Image for post


5 Ways to Replace If-Else. Beginner to advanced examples

Let me just say this right off the bat: If-Else is often a poor choice.

It leads to complicated designs, less readable code, and may be pain inducing to refactor.

Nevertheless, If-Else has become the de facto solution for code branching — which does make sense. It’s one of the first things any aspiring developer is taught. Unfortunately, lots of developers never advance to more suitable branching strategies.

Some live by the mantra: If-Else is a hammer and everything’s a nail.

The inability to determine when to use a more suitable approach is among those that distinguishes juniors from seniors.

I’ll show you some techniques and patterns that’ll put an end to this horrific practice. …


Not all code is created equal. And sometimes, garbage code is sufficient code.

Image by the Author
Image by the Author
Image by the Author

Who’s the judge anyway, right?

I’m a design patterns advocate. I like to write neat, clean code, thoughtfully applying design patterns where I see fit. It’s an incredibly rewarding feeling when you correctly and effortlessly implement a design pattern. The opposite is true when forcefully attempting to apply an ill-suited pattern.

Nevertheless, there are times when I simply need to move a user story from In-Progress to Testing. Fast.

Depending on the problem you’re solving, writing garbage code might be completely okay. By garbage code, I mean overly bloated and simple code that does, however, function correctly.

Don’t mistake garbage code for code that doesn’t meet the requirements or fails. That’s insufficient, buggy code, which is entirely different by nature. …


How to easily replace if-else statements. And no, a switch is not a suitable substitute.

Image for post
Image for post

If you want to learn how I use commands and handlers to keep my code neat and tidy, read on. It’s not about avoiding if-elseif-else. It’s about finding a more suitable approach.

Bear in mind that no single approach will ever get rid of traditional branch-based programming. You need to build a repository of techniques to draw from.

The method we’ll walk thru now is just one of many.

if-else is, at its very core, not bad. It’s merely just a hammer and nail situation we got going. …

Crafting great software is about focusing your attention on what matters at the right time

Image for post
Image for post
Photo by the author.

Rubbish software is produced when we try to do everything at once.

Principles, guidelines, best practices, and rules of thumb — they all make your life easier. Without them, ten-minute tasks can turn into ten-hour tasks.

One of the absolute best pieces of advice I received from my mentor very early on in my career was this simple one-liner:

“Make it work, make it better, make it faster.”

It’s a slight alteration of Kent Beck’s famous quote, and its simplicity is enabling and puzzling.

“Make it work” is quite easy to wrap your head around. You have a set of requirements, and you’re coding to fulfill them — kid stuff. …

It’s easy to refactor messy code once you follow a few simple guidelines

Image for post
Image for post
Photo by the author.

I’ll start by providing you exactly what you came for: the rules of thumb.

Are you curious about why these heuristics are useful? Then read the additional information included after the list.

12 Heuristics for Beginners to Become Refactoring Experts

Let’s dive headfirst into the matter. This list is a mix of heuristics I always have in the back of my mind when I venture into a refactoring session.

  1. Is your class’ list of parameters starting to grow too big? Then it’s likely doing too much. Its responsibilities are unclear and perhaps painful to test and debug. It’s a prime candidate for refactoring.
  2. Do you have methods inside classes that only use one of the class dependencies? You better pull that method into a class of its own — even if that class will only consist of a single method. …

Image for post
Image for post


Hard-coded, unconfigurable code is headache-inducing

It’s honestly not very difficult to write great, configurable classes. But, it might be if you’ve grown used to doing things in certain ways.

From my experience, the best way to turn an existing class into a configurable one is first to move logic into separate classes and then the moving hardcoded part into constructor or method arguments.

It’s that easy. Let me show you how.

But first, what’s so great about a configurable class?

Before you spend any time refactoring your existing classes into configurable ones, I’ll let you know why it’s so great.

A configurable class is damn easy to use. It’s extensible. It doesn’t do too much. …

Image for post
Image for post


I’m sure you’ve never looked at a class called “SomethingService” and right off the bat knew exactly what it was all about.

Truth be told, we’re all confused.

One thing is obvious after years of professional development, wading through tons of code. Developers are terrible at naming things.

As a rookie developer, I know you feel overwhelmed, puzzled or straight-up confused whenever you meet SomeService or WhateverManager class in the wild — or better, WhatevsManagerService.

Class names are like job titles


Business Developer? Yeah, you’re a salesperson. Social Media Ninja? You probably got a 3-week course in how to post cat pics on Instagram and develops a “sick” SoMe strategy. Software Developer? You might as well be a code monkey. Who knows which responsibilities you’re assigned.

There’s a distinction to make. Job title and job role. We sometimes (often) like to make our titles fancier than the role we’re having. …

Image for post
Image for post


Amp up your code quality, the easy way.

I’m not going to preach theory. Since you decided to click this cryptic title, I assume you’re already familiar with the SOLID principles abbreviations.

Chances are you’ve already looked at a bunch of other articles, tutorials, and videos about SOLID. No shame in having to get more perspectives on the same topic.

I’ll show you how easy it is to refactor garbage code, and hopefully, you’ll have a foundation to improve your code quality.

Luckily, it’s incredibly easy to write SOLID code.

Let’s get one thing straight before diving into code

So, I have no chance of demonstrating every aspect of each principle. I’ll merely be demonstrating the principles using a few practical examples. You have to do your own thinking from thereon. Incorporate what you think works. …

Image for post
Image for post


Making your code safer, maintainable, and enjoyable using custom classes with collection initialization

I’m sure even non-.NET developers will find the ideas useful.

For the uninitiated or anyone starting to pick up C#, collection initialization is one of those need-to-know language features C# has to offer.

The greatest thing about C# is how flexible the language is — making it likely the most enjoyable language to work with. Microsoft keeps adding new, awesome features to the language and .NET ecosystem. Even so many features that we often miss a few of the really handy, convenient ones.

Writing a custom class leveraging the collection initializer is one of the most overlooked features. This is even despite the collection initializer’s heavy use when initializing e.g. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store