Code Smells ♨️– Afraid To Fail

Martin Jurran
Software Design Patterns
2 min readFeb 16, 2024

The idea of being scared of failure is not uncommon. In fact, this fear even has a professional name — atychiphobia. However, it’s not a helpful behavior to have when it comes to coding.

Trying to hide mistakes can lead to bigger problems down the line. This is why it’s essential to face the fear of admitting failure. It may be tempting to hope that no one will notice a mistake, but this can cause issues to stack up and make things more difficult in the long run.

In coding, this fear of failure can result in cluttered code. After a function is called, additional lines of code may be necessary to check for errors or other issues that may have occurred during the function call. This can make the code harder to read and understand since the relevant code is spread out over different parts of the program.

If you expect a method to fail, it’s best to let it fail and handle it accordingly. It’s not helpful to create complicated code to check for errors that can be easily caught with an exception or special return object. By embracing the Fail Fast Principle, you can ensure that any issues are caught immediately, before they become bigger problems.

Causation

Afraid to Fail code occurs when programmers are hesitant to admit that their code has errors or won’t work as expected.

This behavior may be due to a fear of criticism or negative feedback, or a desire to avoid reworking the code.

However, ignoring or hiding these mistakes can lead to technical debt, which is when technical problems stack up and become more difficult to resolve over time.

Problems 🤔

  • Code Pollution

Adding unnecessary if conditions, switches or null checks

  • Coupling

It’s essential to follow the “Keep It Simple Stupid” principle when writing code. This means avoiding overcomplicating things by adding unnecessary features or personal creations that can make the code more challenging to understa

  • Fail Fast Violation

When designing a system, it’s important to embrace the Fail Fast concept. This means that any issues or errors that could potentially lead to failure should be reported immediately. By catching these problems early on, you can prevent bigger issues down the line.

Example

Smelly

public string GetUserName(int userId)
{
try
{
var user = db.Users.Find(userId);
return user.Name;
}
catch
{
return null;
}
}

Solution

public string GetUserName(int userId)
{
var user = db.Users.Find(userId);

return user?.Name;
}

Refactoring

Move Method

(Photo by the author, Illustrations by Takashi Mifune under free use)

--

--

Martin Jurran
Software Design Patterns

Personal blog of a Software Engineer | Azure DevOps, C#/.NET, JavaScript