To infinity but not beyond

TL;DR: Use Infinity instead of None when looking for minimums

Problems πŸ˜”

Solutions πŸ˜ƒ

  1. Remove the Accidental IFs
  2. Use infinite value (If your language supports it)
  3. Remove None check
  4. Respect math semantics and consistency
  5. Apply the null object pattern
  6. Reduce boilerplate, simplifying your code
  7. Use float('inf') as base case for minimums ♾️
  8. Use float('-inf') as base case for maximums -♾️
  9. Remove conditional branches

Refactorings βš™οΈ

https://hackernoon.com/refactoring-014-how-to-remove-if?embedable=true

https://hackernoon.com/code-refactoring-tips-no-015-remove-null?embedable=true

Context πŸ’¬

Problem 1:

You want to find the greatest number in a list of positive numbers.

You start with 0 and compare.

An amazing Null Object. No Accidental IFs involved. Clean code. πŸ‘Œ

Problem 2:

You want to find the lowest number in a list.

Most beginners start with None and check "if current is None or x < current".

You don’t need that.

You can start with float("inf") ♾️.

It behaves-as-a a number.

You can compare, sort, and minimize it.

This gives you simpler logic and polymorphic code.

The holy grail of behavior.

Polymorphism is the deadliest enemy of accidental IFs.

https://hackernoon.com/how-to-get-rid-of-annoying-ifs-forever-zuh3zlo?embedable=true

Sample Code πŸ“–

Wrong ❌

def find_minimum_price(products):
    min_price = None
    
    for product in products:
        if min_price is None:
            min_price = product.price
        elif product.price < min_price:
            min_price = product.price
    
    return min_price

def find_minimum_in_list(numbers):
    if not numbers:
        return None
    
    minimum = None
    for number in numbers:
        if minimum is None or number < minimum:
            minimum = number
    
    return minimum

# Usage leads to more None checks
prices = [10.5, 8.2, 15.0, 7.8]
min_price = find_minimum_in_list(prices)
if min_price is not None:
    print(f"Minimum price: ${min_price}")
else:
    print("No prices found")

Right πŸ‘‰

def find_minimum_price(products):
    min_price = float('inf')
        
    for product in products:
        if product.price < min_price:
            # This is an essential IF, you should not remove it
            min_price = product.price
            # No accidental IF here (if min_price is None:)
    
    return min_price if min_price != float('inf') else None

def find_minimum_in_list(numbers):
    minimum = float('inf')
    
    for number in numbers:
        if number < minimum:
            minimum = number
    
    return minimum if minimum != float('inf') else None

# Cleaner usage - polymorphic behavior
prices = [10.5, 8.2, 15.0, 7.8]
min_price = find_minimum_in_list(prices)
print(f"Minimum price: ${min_price}")

Detection πŸ”

You can grep your codebase for None inside loops.

If you check against None before comparing values, you probably can smell it.

Tags 🏷️

Level πŸ”‹

Why the Bijection Is Important πŸ—ΊοΈ

In math, the identity element for finding a minimum is positive infinity. ♾️

When you use None, you break the MAPPER

None is not a number.

It does not behave as a number; it is not polymorphic with numbers.

It is evil Null disguised as None.

You must then write special code to treat it.

That breaks the bijection between your code and math.

When you use float("inf"), you stay close to the real concept.

The code models the domain truthfully.

AI Generation πŸ€–

AI models that generate loops often use None as the starting point.

They may include unnecessary checks.

This typically occurs when the model attempts to mimic tutorials or is trained with bad code or overly simplified examples.

AI Detection 🧲

AI can easily detect and fix this issue when you provide clear instructions.

For example

Use Infinity for minimum search initialization

or

Apply the null object pattern for mathematical operations.

Try Them! πŸ› 

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Use Infinity for minimum search initialization

Without Proper Instructions

With Specific Instructions

ChatGPT

ChatGPT

Claude

Claude

Perplexity

Perplexity

Copilot

Copilot

Gemini

Gemini

DeepSeek

DeepSeek

Meta AI

Meta AI

Grok

Grok

Qwen

Qwen

Conclusion 🏁

Zero is not the default for everything.

When you want to find a minimum, you should start at infinity.

This clarifies your code, removes conditionals, and provides a better mathematical bijection.

Stop treating None like a number. None is Null. And Null is bad.

Infinity is polymorphic and is the null object for maximum math operations

Use it.

Relations πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxvi

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

More Information πŸ“•

https://hackernoon.com/how-to-get-rid-of-annoying-ifs-forever-zuh3zlo?embedable=true

https://hackernoon.com/null-the-billion-dollar-mistake-8t5z32d6?embedable=true

Disclaimer πŸ“˜

Code Smells are my opinion.

Credits πŸ™

Photo by Cris Baron on Unsplash


Code should model the problem, not the solution

Rich Hickey

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


This article is part of the CodeSmell Series.

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