Skip to content

Fix 20 verified bugs, name magic numbers, clarify names and comments - #15

Merged
diegourban merged 10 commits into
masterfrom
clean-code-findings
Nov 6, 2026
Merged

diegourban merged 10 commits into
masterfrom
clean-code-findings

Conversation

@diegourban

Copy link
Copy Markdown
Owner

Clean-code PR D of 4: the rows approved from the findings review. STRUCTURAL rows, public-API renames, behaviour changes (B5, B10, B20, B25) and B27 are not included.

Bug fixes (each with a test that failed first)

Commit Rows Fix
1bdcee5 B1–B4 LinkedList: add(0, e) on a non-empty list; removeFromEnd() on empty → IllegalStateException; remove(size()) → IllegalArgumentException; removeFromStart() clears the stale previous
f0deabf B6–B9 Vector: add(size(), e) appends and works on an empty vector; remove validates its position (it used to remove the wrong element, or leave size −1)
92b440a B11–B13 Element.equals is type-safe and hashCode is overridden; Set rejects an empty word
03b59b6 B14–B17 Numbers.toArray is correct beyond the int range and for negatives; containsDigit(0, 0) is true; length(negative) counts digits
d59eba5 B18, B19, B22–B24 Statistics rejects empty data; lcm with 0 returns 0, and overflow now throws instead of returning a negative number; isAnagram lower-cases with Locale.ROOT

Comments and messages: e6fcd6a

B26 (Binet exact up to 70, not 67), B28 (BinarySearch uses O(log n) stack space), M6 (6k ± 1 explained), N18 (TriangleType display names in English), O1–O3 (English exception messages), O7–O11 (dead code, broken and redundant comments, gdc typo, Fisher–Yates).

Named constants: 3f5c252

BUCKET_COUNT, INITIAL_CAPACITY, KILOMETERS_PER_MILE / MILES_PER_KILOMETER, SQRT_5 / GOLDEN_RATIO, BigInteger.ZERO/ONE/TWO and valueOf, and a static BUFFER_SIZE.

Renames: 88c7139

Non-API names only: private members, parameters and locals (N1–N11, N14, N20–N25, N27). Verified: compiled without debug info, the bytecode is identical to before, except for the six renamed private members.

Visible behaviour changes callers may notice

  • TriangleType.getName() now returns English names.
  • LinkedList and Sequences exception messages changed.
  • Statistics.median / meanAbsoluteDeviation on an empty array now throw IllegalArgumentException.
  • lcm now throws on overflow.

Test plan

  • ./mvnw clean verify: 610 tests; doclint, Spotless, PMD and coverage floors all pass
  • CI, Codacy, Codecov green

🤖 Generated with Claude Code

diegourban and others added 9 commits November 6, 2026 17:17
- add(0, e) on a non-empty list now inserts at the start instead of
  throwing IllegalArgumentException
- removeFromEnd() on an empty list throws IllegalStateException, like
  removeFromStart(), instead of NullPointerException
- remove(position) rejects an unoccupied position with
  IllegalArgumentException instead of NullPointerException
- removeFromStart() clears the new first node's previous link

Each case has a test that failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- add(position, e) accepts position == size(), so it can append and
  insert into an empty vector
- remove(position) rejects an unoccupied position with
  IllegalArgumentException; before, a position past the end silently
  removed the last element and an empty vector's size dropped to -1

Each case has a test that failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Element.equals returns false for objects of other types instead of
  throwing ClassCastException
- Element.hashCode is now overridden from the name, so equal elements
  are found in hash-based collections
- Set.add/remove reject an empty word with IllegalArgumentException
  instead of StringIndexOutOfBoundsException

Each case has a test that failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- toArray casts after taking % 10, so numbers beyond the int range give
  the right digits (12345678901 was [-7, 0, 9, ...])
- toArray of a negative number gives the digits of its absolute value
  ([2, 1] for -12, was [-2, -1, 0])
- containsDigit(0, 0) is true
- length(negative) counts the digits of the absolute value (was 1)

Each case has a test that failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Anagram (B18, B19, B22-B24)

- Statistics.median and meanAbsoluteDeviation reject empty data with
  IllegalArgumentException (was ArrayIndexOutOfBoundsException / NaN)
- NumberTheory.lcm returns 0 when either number is 0 (was division by
  zero) and throws ArithmeticException on overflow via
  Math.multiplyExact (lcm(4000000007, 4000000009) was negative)
- StringUtils.isAnagram lower-cases with Locale.ROOT, so "TITLE" and
  "title" are anagrams under a Turkish default locale too

Each case has a test that failed before the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- B26: fibonacciSimpleBinetFormula is exact (rounded) up to n = 70, not
  67; Javadoc and the exhaustive property range now say 70
- B28: BinarySearch.find uses O(log n) stack space, not O(1)
- M6: explain the 6k +/- 1 step in isPrime
- N18: TriangleType display names in English: Equilateral, Scalene,
  Isosceles (was Equilatero, Escaleno, Isoceles)
- O1/O2: LinkedList exception messages in English, with the position
- O3: Sequences.fibonacci message is "n must be non-negative: <n>"
- O7: drop commented-out code in Geometry.distance
- O8: replace the formatter-split trailing comments in containsDigit
- O9: drop comments that restate the code
- O10: "gdc" typo; O11: name the Fisher-Yates shuffle

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- M1: Set.BUCKET_COUNT (26)
- M2: Vector.INITIAL_CAPACITY (100)
- M3/M4: Measurement.KILOMETERS_PER_MILE and MILES_PER_KILOMETER
- M5: Sequences.SQRT_5 and GOLDEN_RATIO instead of recomputing sqrt(5)
  four times; the exhaustive Fibonacci properties confirm identical
  results
- M7: BigInteger.ZERO/ONE/TWO and BigInteger.valueOf instead of parsing
  strings
- M8: Reader.BUFFER_SIZE is static

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-N25, N27)

Only non-API names: private members, parameters and locals, e.g.
theArray -> values, arrA -> sortedA, bNumber -> divisor,
minorsFound -> boundary, analysis -> position, guaranteesSpace ->
ensureCapacity, Reader's din/buf/cnt/c/ret/neg -> input/line/length/
nextByte/result/negative.

Verified: compiled without debug info, the bytecode is identical to
before except for the six renamed private members (RANDOM,
randomBetweenInclusive, sortedLowerCaseChars, ensureCapacity,
findSmallestIndex, input).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 21 complexity · 0 duplication

Metric Results
Complexity 21
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codecov-commenter

codecov-commenter commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.83%. Comparing base (73da738) to head (2ce7cd6).

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #15      +/-   ##
============================================
+ Coverage     99.65%   99.83%   +0.17%     
- Complexity      279      296      +17     
============================================
  Files            24       24              
  Lines           576      594      +18     
  Branches         94      102       +8     
============================================
+ Hits            574      593      +19     
+ Partials          2        1       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

⚠️ Resolve issues before merging

One correctness issue should be fixed before merging: Numbers.containsDigit does not preserve its sign-insensitive behavior for Long.MIN_VALUE because negation overflows. The required regression scenarios are otherwise covered, and no additional acceptance-criteria gaps were identified.

Test suggestions

  • Insert at position zero in a non-empty LinkedList and preserve previous links
  • Reject removing from the end of an empty LinkedList
  • Reject removal at an unoccupied LinkedList position without changing size
  • Clear the new first node's previous link after LinkedList removal
  • Insert into a Vector at size and into an empty Vector
  • Reject invalid Vector removals, including negative, oversized, and empty-vector positions
  • Compare Element with another type and use equal elements in hash-based collections
  • Reject empty words in Set add and remove
  • Convert values beyond int range and negative values to reversed digit arrays
  • Handle zero in containsDigit and negative values in length
  • Reject empty arrays for median and mean absolute deviation
  • Return zero for LCM with zero operands and throw on overflow
  • Perform locale-independent anagram comparison
  • Verify rounded simple Binet results through index 70
  • Verify English TriangleType display names and updated exception messages

Make this review better

TIP Add a ticket key to the branch or title so the reviewer can check the PR against it. Or tell us which ticket tracker you use here.
TIP Add Codacy review.md instructions or an AGENTS.md so the reviewer knows your conventions.

TIP How was this review? Give us feedback

public static boolean containsDigit(final long number, final long digit) {
long thisNumber = number >= 0 ? number : -number;
long thisDigit;
long remaining = number >= 0 ? number : -number;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Handle Long.MIN_VALUE without negating it, such as by using overflow-safe digit extraction or a string representation, and add a regression test for this boundary value.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

Covers the '\r' branch of the line-ending check, which no test reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diegourban
diegourban merged commit 6de0c8f into master Nov 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants