DEV Community

Mauricio Ramirez
Mauricio Ramirez

Posted on

From "Automatic Mode" to Code Analysis: What I Learned Analyzing an Edge Case in Java

I have been programming for a while, but recently I made a conscious decision to change my approach: to stop just making code "work" and start asking myself why things work the way they do.

One of the habits I adopted to improve my Java skills is exploring open-source repositories, reading other people's code, and analyzing their thought processes. It is a very rewarding experience.

While checking the TheAlgorithms / Java repository, I found an exercise in the math section that seems simple at first glance: find the absolute maximum value of an array of integers.

That is exactly where I learned a valuable lesson about how technical pride can blind us to an edge case.

The Trap of Assuming Other People's Code is "Wrong"

When we lack knowledge or read code too quickly, our first reaction is often to assume that what we are reading is redundant or poorly written. It is easier to think, "The person who wrote this didn't know what they were doing," than to question our own understanding.

That happened to me with this exercise. While reading the implementation, I found a condition inside an if statement that, at first look, seemed completely unnecessary.

Fortunately, instead of closing the file, I paused, took a breath, and analyzed the math and logic behind that decision.

The Core Concept: What is an Absolute Maximum?

To give some context, the absolute value of a number is its value without considering its sign:

|10| = 10
|-10| = 10

The absolute maximum finds the largest value in a sequence of numbers, regardless of whether they are positive or negative.

Source Code and Unit Tests

First, let's look at the implementation of AbsoluteMax.java:

Java

package com.thealgorithms.maths;

public final class AbsoluteMax {

    private AbsoluteMax() {
    }

    /**
     * Finds the absolute maximum value among the given numbers.
     *
     * @param numbers The numbers to compare.
     * @return The absolute maximum value.
     * @throws IllegalArgumentException If the input array is empty or null.
     */
    public static int getMaxValue(int... numbers) {
        if (numbers == null || numbers.length == 0) {
            throw new IllegalArgumentException("Numbers array cannot be empty or null");
        }

        int absMax = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
                absMax = numbers[i];
            }
        }
        return absMax;
    }
}
Enter fullscreen mode Exit fullscreen mode

And here are the unit tests in AbsoluteMaxTest.java:

Java

package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;

public class AbsoluteMaxTest {

    @Test
    void testGetMaxValue() {
        assertEquals(16, AbsoluteMax.getMaxValue(-2, 0, 16));
        assertEquals(-22, AbsoluteMax.getMaxValue(-3, -10, -22));
        assertEquals(-888, AbsoluteMax.getMaxValue(-888));
        assertEquals(-1, AbsoluteMax.getMaxValue(-1, -1, -1, -1, -1));
    }

    @Test
    void testGetMaxValueWithNoArguments() {
        assertThrows(IllegalArgumentException.class, AbsoluteMax::getMaxValue);
    }

    @Test
    void testGetMaxValueWithSameAbsoluteValues() {
        assertEquals(5, AbsoluteMax.getMaxValue(-5, 5));
        assertEquals(5, AbsoluteMax.getMaxValue(5, -5));
        assertEquals(12, AbsoluteMax.getMaxValue(-12, 9, 3, 12, 1));
        assertEquals(12, AbsoluteMax.getMaxValue(12, 9, 3, -12, 1));
    }
}
Enter fullscreen mode Exit fullscreen mode

The "Redundant" Line That Was Actually Essential

The algorithm follows these steps:

  1. Validates that the array is not null or empty.

  2. Initializes absMax with the first element (numbers[0]).

  3. Loops through the array with a for loop to evaluate the conditions.

The condition inside the if statement that confused me was this:

Java

if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
    absMax = numbers[i];
}
Enter fullscreen mode Exit fullscreen mode

At first, I thought: If I already know the absolute values are equal in the second part of the OR, why am I checking if numbers[i] > absMax again? That seems redundant!

The Tie-Breaker (Edge Case)

Let's look at a concrete example:

  • Current absMax = -10
  • numbers[i] in the loop = 10
  1. First validation: Math.abs(10) > Math.abs(-10)10 > 10false.

  2. Without the second part of the condition, the program would ignore the positive 10 and keep -10 as the maximum just because it appeared first in the array.

  3. Second validation (the tie-breaker): Math.abs(10) == Math.abs(-10) && 10 > -10true.

This second part does not evaluate absolute values; it compares the actual signed values. It acts as a tie-breaker in favor of positive numbers, ensuring deterministic behavior for tests like assertEquals(5, AbsoluteMax.getMaxValue(-5, 5)), choosing 5 over -5.

Conclusion

What looked like an unoptimized line of code was actually an elegant solution to handle magnitude collisions properly.

We often think we are fine as we are, hiding our weaknesses behind thoughts like "that isn't useful," "I only learn what I need," or "other people are wrong." Deep down, these thoughts just prevent us from questioning ourselves.

Coding on "automatic mode" for a long time made me overlook these details. Taking the time to understand the reason behind every single line helped me understand absolute numbers and Java logic better, but above all, it taught me technical humility: before criticizing a line of code, make sure you understand all the edge cases it is trying to solve.

Top comments (0)