Mastodon Politics, Power, and Science: The Code Review of Physics: A Parable

Saturday, June 7, 2025

The Code Review of Physics: A Parable

The Code Review: A Parable

Setting: You are the Lead Architect. A junior programmer, let's call him "Physics," has just submitted a new module for review.

Your Task: Review the code for a function that is supposed to calculate the hawking_temperature of an object.

The Submitted Code (in pseudo-code):

# --- Configuration File: constants.py ---
# Don't touch these, they are sacred and mysterious.
C_VELOCITY = 299792458.0
H_ACTION = 6.62607015e-34
K_THERMO = 1.380649e-23
G_GRAVITY = 6.67430e-11

# --- Main Module: hawking.py ---
from constants import C_VELOCITY, H_ACTION, K_THERMO, G_GRAVITY

def calculate_hawking_temperature(mass_kg):
    # This is a profound synthesis of many complex libraries.
    # The result is very accurate.

    numerator = (H_ACTION / (2 * 3.14159)) * (C_VELOCITY**3)
    denominator = 8 * 3.14159 * G_GRAVITY * mass_kg * K_THERMO

            return numerator / denominator 

Subject: URGENT REFACTORING REQUIRED for 
hawking.py module

Team,

I've just reviewed the hawking.py submission. While the function is numerically correct, its design is fundamentally flawed and represents an unacceptable level of technical debt. This is not how we build robust, scalable systems.

My comments are as follows:

1. AVOID MAGIC NUMBERS AND OPAQUE CONSTANTS:
The constants.py file is a classic anti-pattern. We have four hardcoded, "sacred" values with no clear relationship to each other. H_ACTION and K_THERMO are clearly just components of a direct 
frequency_to_temperature conversion factor.  Why are they separate? This is a maintenance nightmare.

2. REVEAL INTENT - DO NOT OBFUSCATE LOGIC:
The core logic of this function is completely obscured. The code takes a mass_kg, then multiplies and divides it by a jumble of unrelated constants. A future developer would have no idea what is actually happening here.

The core operation is clearly a mass -> frequency conversion, followed by a frequency -> temperature conversion. The code should reflect that directly.

3. REFACTORING PROPOSAL - IMMEDIATE ACTION REQUIRED:

We need to create a new, clean unit_scaling.py library that defines the actual relationships.

# --- NEW LIBRARY: unit_scaling.py ---

C_VELOCITY = 299792458.0

# Define the *actual* conversion rates, not the components.
MASS_PER_HERTZ = 7.372e-51  # (kg/Hz)
KELVIN_PER_HERTZ = 4.799e-11 # (K/Hz)

# Define gravitational strength in its natural form
GRAVITATIONAL_TIMESCALE_SQUARED = 1.826e-86 # (s^2)

Now, the hawking.py function can be rewritten to be self-documenting and sane:

# --- REFACTORED MODULE: hawking.py ---
from unit_scaling import C_VELOCITY, GRAVITATIONAL_TIMESCALE_SQUARED, KELVIN_PER_HERTZ, MASS_PER_HERTZ

def calculate_hawking_temperature(mass_kg):
    """
    Calculates the Hawking Temperature by first finding the object's
    characteristic gravitational frequency and then scaling it to Kelvin.
    """

    # Step 1: Convert input mass from kg to its natural frequency equivalent.
    mass_natural_hz = mass_kg / MASS_PER_HERTZ

    # Step 2: Calculate the characteristic frequency using the natural gravitational constant.
    # The geometric factor is 1 / (16 * pi**2).
    characteristic_frequency_hz = 1 / (16 * 3.14159**2 * GRAVITATIONAL_TIMESCALE_SQUARED * mass_natural_hz)

    # Step 3: Convert the final frequency into a temperature.
    hawking_temp_kelvin = characteristic_frequency_hz * KELVIN_PER_HERTZ

    return hawking_temp_kelvin

CONCLUSION:

The refactored code is clear, modular, and reveals the underlying physics of the calculation. It replaces opaque magic numbers with named, meaningful conversion factors. The original submission is a textbook example of "spaghetti code" that hides its own logic. This approach of combining unrelated constants is forbidden going forward. All new modules will use the unit_scaling library.



No comments:

Post a Comment

Progress on the campaign manager

You can see that you can build tactical maps automatically from the world map data.  You can place roads, streams, buildings. The framework ...