Refactoring 011 - Replace Comments with Tests

Refactoring 011 - Replace Comments with Tests

Leader ●1 ●47 ●113
calendar_today β€’ schedule9 min read

Comments are dead. Tests are alive

TL;DR: Take your comment, compact it, and name your functions.
Now test it and remove the comments.

Problems Addressed πŸ˜”

  • Maintainability

  • Readability

https://coderlegion.com/8106/code-smell-05-comment-abusers

https://maximilianocontieri.com/code-smell-183-obsolete-comments

https://maximilianocontieri.com/code-smell-146-getter-comments

https://maximilianocontieri.com/code-smell-112-testing-private-methods

Context πŸ’¬

Comments often act as a placeholder for missing logic or verification.

A comment explaining "what" a method does creates passive documentation.

This documentation is detached from execution.

As the code evolves, comments rot.

They stop reflecting the truth and mislead the reader.

When you convert comments into automated tests, you create active contracts.

These contracts prove what the code actually does.

Steps πŸ‘£

  1. Take the comment of the method explaining what the function does.

  2. Rename the method with the comment description (the what).

  3. Create tests to verify the comments.

  4. Omit irrelevant implementation details.

Sample Code πŸ’»

Before 🚨

def multiply(a, b):
    # This function multiplies two numbers and returns the result
    # If one of the numbers is zero, the result will be zero
    # If the numbers are both positive, the result will be positive
    # If the numbers are both negative, the result will be positive
    # The multiplication is done by invoking a primitive
    return a * b

# This code has a comment that explains what the function does.
# Instead of relying on this comment
# to understand the behavior of the code,
# You can write some unit tests
# that verify the behavior of the function.

After πŸ‘‰

def multiply(first_multiplier, second_multiplier):
    return first_multiplier * second_multiplier

class TestMultiply(unittest.TestCase):
    def test_multiply_both_positive_outcome_is_positive(self):
        result = multiply(2, 3)
        self.assertEqual(result, 6)
    def test_multiply_both_negative_outcome_is_positive(self):
        result = multiply(-2, -4)
        self.assertEqual(result, 8)
    def test_multiply_first_is_zero_outcome_is_zero(self):
        result = multiply(0, -4)
        self.assertEqual(result, 0)
    def test_multiply_second_is_zero_outcome_is_zero(self):
        result = multiply(3, 0)
        self.assertEqual(result, 0)
    def test_multiply_both_are_zero_outcome_is_zero(self):
        result = multiply(0, 0)
        self.assertEqual(result, 0)

# You define a test function called test_multiply,
# which calls the multiply function with different arguments
# and verifies that the result
# is correct using the assertEqual method.

# 1. Take the comment of the method explaining what the function does.
# 2. Rename the method with the comment description (the what).
# 3. Create tests to verify the comments.
# 4. Omit irrelevant implementation details

Type πŸ“

[X] Semi-Automatic

You can rewrite the comment and compact it.

It isn't always an algorithmic process.

Safety πŸ›‘οΈ

This isn't a safe refactor, but it increases coverage.

Why is the Code Better? ✨

Comments lie. The code doesn't.

How Does it Improve the Bijection? πŸ—ΊοΈ

Comments describe intended behavior as passive text.

Tests prove behavior as executable contracts.

Your code maps directly to the real-world model.

https://coderlegion.com/6102/current-software-design-problems-addressed-in-a-minimalist-way

Limitations ⚠️

You can't test private methods directly.

When a comment is on a private method, test it indirectly.

Alternatively, extract the private method into a separate object.

You can leave comments reflecting important design decisions.

Tags 🏷️

  • Comments

Level πŸ”‹

[X] Beginner

https://coderlegion.com/20107/refactoring-010-extract-method-object

Refactor with AI πŸ€–

Suggested Prompt: 1. Take the comment of the method explaining what the function does.2. Rename the method with the comment description (the what).3. Create tests to verify the comments.4. Omit irrelevant implementation details.

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

See also πŸ“š

https://coderlegion.com/6102/current-software-design-problems-addressed-in-a-minimalist-way

https://coderlegion.com/6313/just-follow-this-design-principle

Credits πŸ™

Image by philm1310 from Pixabay


This article is part of the Refactoring Series.

https://coderlegion.com/13921/how-to-improve-your-code-with-easy-refactorings

Part 2 of 3 in Refactorings

1 Comment

1 vote
πŸ”₯ Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Your AI Doesn't Just Write Tests. It Runs Them Too.

Kevin Martinez - May 12

Refactoring 012 - Reify Associative Arrays

Maxi Contieri - Jul 8

Refactoring 010 - Extract Method Object

Maxi Contieri - Jun 9

Refactoring 009 - Protect Public Attributes

Maxi Contieri - Apr 2

Refactoring 008 - Convert Variables to Constant

Maxi Contieri - Mar 28
chevron_left
6.5k Points β€’ 161 Badges
Buenos Aires, Argentina β€’ maximilianocontieri.com
67Posts
3Comments
4Connections
Learn something new every day
Software Engineer and author of Clean Code Cookbook (https://amzn.to/4... Show more

Related Jobs

View all jobs β†’

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!