Code Smell 264 – Hanlon’s Razor

Don’t Overcomplicate: Keep It Simple. Overdefensive code leads to unnecessary complexity.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Don’t Overcomplicate: Keep It Simple

TL;DR: Overdefensive code leads to unnecessary complexity.

\

Problems

  • Unnecessary complexity
  • Confusing logic
  • Hidden bugs
  • Harder maintenance
  • Slower performance
  • Cluttered Code

Solutions

  1. Simplify checks
  2. Trust your logic
  3. Focus on essentials
  4. Follow the K.I.S.S. principle
  5. Refactor regularly

Context

Overthinking and overdesigning your code can lead to unnecessary complexity.

You might feel a need to defend against every possible scenario, but this approach often produces bloated, confusing code.

Hanlon's Razor suggests that you should not assume malice when simple mistakes or misunderstandings are more likely. Avoid overly defensive programming and focus on clear, straightforward logic.

\ You might anticipate future problems that might never happen or try to make your code too flexible. Simple code is easier to maintain, debug, and understand.

Sample Code

Wrong

function processData(data) {
    if (typeof data === 'undefined') {
        throw new Error('Data is undefined');
    }

    if (typeof data !== 'object') {
        throw new Error('Data is not an object');
    }

    if (data === null) {
        throw new Error('Data is null');
    }

    if (Array.isArray(data)) {
        throw new Error('Data should not be an array');
    }

    if (!data.hasOwnProperty('items')) {
        return [];
    }

    if (!Array.isArray(data.items)) {
        throw new Error('Items should be an array');
    }

    if (data.items.length === 0) {
        return []; 
    }

    let processedItems = [];
    for (let item of data.items) {
        if (typeof item === 'undefined') {
            continue; // Skip undefined items
        }

        if (typeof item !== 'object') {
            continue; // Skip non-object items
        }

        if (item === null) {
            continue; // Skip null items
        }

        processedItems.push(processItem(item));
    }

    return processedItems;
}
function processData(data) {
    if (!Array.isArray(data.items)) {
        throw new Error('Invalid data');
    }

    return data.items
        .filter(item => typeof item === 'object' && item !== null)
        .map(item => processItem(item));
}

Detection

  • [x] Manual

Complicated code usually has more lines and long methods are a possible hint.

Tag(s)

  • Bloaters

Level

  • [x] Intermediate

AI Generation

AI generators can introduce this smell when they try to account for every possible edge case.

For example, dealing with NULLs is unnecessary if you completely avoid them.

AI Detection

AI tools can help detect overly defensive code by analyzing the logic and suggesting simplifications with proper guidance.

These tools often recommend removing unnecessary checks or combining them for clarity.

Conclusion

Avoid overthinking and overdesigning your code.

Focus on the most likely scenarios and write clear, straightforward logic.

Simplicity leads to better code quality and easier maintenance.

Related Reading

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iii-t7h3zkv?embedable=true

:::info More Info

https://en.wikipedia.org/wiki/Hanlon's_razor

:::

https://en.wikipedia.org/wiki/Defensive_programming?embedable=true

:::warning Disclaimer: Code Smells are my opinion.

:::

:::info Lead Photo by Nacho Fernández on Unsplash

:::


Simplicity is the ultimate sophistication.

Leonardo da Vinci

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


:::tip This article is part of the CodeSmell Series: How to Find the Stinky Parts of your Code

:::

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2024-08-18T19:59:32+00:00) Code Smell 264 – Hanlon’s Razor. Retrieved from https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/

MLA
" » Code Smell 264 – Hanlon’s Razor." Maximiliano Contieri | Sciencx - Sunday August 18, 2024, https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/
HARVARD
Maximiliano Contieri | Sciencx Sunday August 18, 2024 » Code Smell 264 – Hanlon’s Razor., viewed ,<https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 264 – Hanlon’s Razor. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/
CHICAGO
" » Code Smell 264 – Hanlon’s Razor." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/
IEEE
" » Code Smell 264 – Hanlon’s Razor." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/. [Accessed: ]
rf:citation
» Code Smell 264 – Hanlon’s Razor | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/08/18/code-smell-264-hanlons-razor-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.