Refactoring 005 - Replace Comment With Function Name

Written by mcsee | Published 2025/09/19
Tech Story Tags: refactoring | code-smells | clean-code | refactor-legacy-code | code-refactoring | bijection | refactor-with-ai | coding-comments

TLDRDon't comment on what you are doing. Name what you are doing.via the TL;DR App

Comments should add value. And function names too.

TL;DR: Don't comment on what you are doing. Name what you are doing.

Problems Addressed πŸ˜”

  • Bad Names
  • Comments

Related Code Smells πŸ’¨

https://hackernoon.com/code-smell-05-comment-abusers?embedable=true

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

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

Steps πŸ‘£

  1. Name the function with the previous comment
  2. Remove the Comment

Sample Code πŸ“–

Before 🚨

<?

function repl($str) {
  // Replaces with spaces the braces 
 
  $str = str_replace(array("\{","\}")," ",$str);
  return $str;

}

After πŸ‘‰

<?

// 1. Name the function with the previous comment
// 2. Remove the Comment

function replaceBracesWithSpaces($input) {
  
  return str_replace(array("\{","\}")," ", $input);

}

Type πŸ“

  • [x]Semi-Automatic

Some IDEs have this refactoring although naming is not fully automatic.

Safety πŸ›‘οΈ

This is a safe refactoring.

Why is the Code Better? ✨

Comments always lie.

It is hard to maintain comments.

On the contrary, Functions are alive and self-explanatory.

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

A comment only describes the code in natural language.

If you change the code, the comment and the behavior can drift apart, breaking the mapping between intention and execution.

When you replace a comment with a well-chosen function name, you create a direct bijection between "what the code does" and "how you call it."

The name becomes the single source of truth.

This keeps the mental model aligned with the actual implementation, so both the reader and the compiler operate on the same unambiguous contract.

Limitations ⚠️

As always, very important design decisions are valid comments.

Refactor with AI πŸ€–

Suggested Prompt: 1. Name the function with the previous comment 2. Remove the Comment

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

Tags 🏷️

  • Comments

Level πŸ”‹

  • [x]Beginner

Related Refactorings πŸ”„

https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true

See also πŸ“š

https://hackernoon.com/what-exactly-is-a-name-the-quest-part-i-fmw3udc?embedable=true

Credits πŸ™

Image by Jannik Texler on Pixabay


This article is part of the Refactoring Series

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings?embedable=true


Written by mcsee | I’m a sr software engineer specialized in Clean Code, Design and TDD Book "Clean Code Cookbook" 500+ articles written
Published by HackerNoon on 2025/09/19