Skip to main content
blog

The Impact of AI on Code Commenting and Software Documentation

November 15, 2024 · 1229 words · 8 min read

The Impact of AI on Code Commenting and Software DocumentationImage unavailableTry again
Table of Contents
Preparing narration.

Comments and documentation answer questions that the code cannot answer by itself. A function can show how it calculates a value, but it may not show why the product needs that rule, which input is trusted, or what must stay true when the code changes.

That information is easy to postpone. Then a few months pass, the original author is busy, and a harmless-looking change turns into archaeology. AI tools can help you write a first draft, but they cannot take responsibility for whether that draft describes the code honestly.

What good documentation is supposed to do

Start with the reader. Someone opening a file should be able to understand the purpose of the module, the assumptions around its inputs, and the unusual decisions that would otherwise look like mistakes.

Comments are most useful when they explain a reason or a constraint. They should not narrate obvious syntax. This comment adds little information:

Python
# Add one to the count.
count += 1

This one gives the next reader something they could not get by staring at the line:

Python
# Keep the first event in the count because the reporting API uses one-based totals.
count += 1

Documentation outside the code has a different job. A README can explain how to run a project. An API document can describe a request and response. A comment belongs near the decision it explains, so it should stay short enough to update when that decision changes.

Where AI helps

AI assistants are useful when the work is repetitive and the boundaries are clear. They can summarize a file, suggest a docstring, or turn a function signature into a rough explanation. Tools such as GitHub Copilot and Amazon CodeWhisperer can also propose comments while you write code.

For example, an assistant may draft this explanation for a recursive factorial function:

Python
# This function calculates the factorial of a given integer.
# It uses recursion to find the product of all positive integers up to n.
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

The draft is a starting point, not a completed review. It does not say what should happen for a negative value, and it assumes that recursion is the right detail for the reader. You still need to decide whether the comment belongs, what contract the function should have, and how that contract is enforced.

AI can also summarize a long file before you read it closely. That can help you find the main entry points, but a summary is a map, not proof. Check it against the code before you copy it into a README or an issue.

When a generated comment is dangerous

The code tells the model what exists. It does not always tell the model why it exists. A generated sentence can sound confident while inventing an intention, missing an edge case, or describing an old version of the implementation.

That matters most around permissions, money, security checks, retries, data retention, and compatibility rules. A wrong comment in one of those places can push the next person toward the wrong fix.

There is another failure mode: stale truth. If a comment repeats what the next line does and that line changes, the comment can drift without anyone noticing. The reader then has two conflicting versions of the program.

My caveat is simple: I would rather leave a small section undocumented for a moment than merge a polished explanation that nobody verified. A plain TODO that names the missing decision is more honest than an incorrect paragraph.

A review loop that works

Use the generated text in a short loop. First, ask the tool for a draft that focuses on behavior and assumptions. Then read the code yourself and delete anything that merely repeats syntax. Finally, run the tests and update the comment if the test exposes a different contract.

Keep the prompt close to the question you need answered. "Explain this file" is broad. "Describe why this cache entry is rejected when the version changes" gives the tool a narrower task and gives you a clearer result to review.

Do not send secrets, private data, or proprietary implementation details to a service unless your project allows it. Also check generated comments for sensitive names, internal URLs, and details that should not be public. Documentation can leak information even when the code path itself is protected.

Can AI help with software testing too

The same review rule applies to generated tests. A test is useful when it checks a behavior you care about, not when it only makes the coverage number larger.

Keploy is mentioned here because it can generate test cases and stubs or mocks for unit and integration testing from API interactions. If you use a tool like that, inspect the captured inputs, remove sensitive values, and confirm that the expected response represents a real contract before keeping the test.

Keploy documentation logoImage unavailableTry againKeploy documentation logo

The image above belongs in the testing section because the connection is practical: documentation tells you what a boundary should do, and a test can check that the boundary keeps doing it. The tool can save typing, but you still own the test's meaning.

The part AI cannot sign off on

AI is good at producing a plausible first pass. You are still responsible for the final sentence. Check every claim against the code, the tests, and the project rules, then leave a comment only when it will help the next reader make a better decision.

That is the useful division of labor. Let the tool handle the blank page and repetitive wording. Keep the judgment, context, and security review with the person who understands what the software is allowed to do.

For more writing about software, follow me on Twitter (swapnoneel123). You can also browse my GitHub (Swpn0neel) projects.

Thank you graphic for AI code commenting blogImage unavailableTry againThank you graphic for AI code commenting blog


FAQ’s

Can AI-generated documentation fully replace human effort?

No, AI-generated documentation should complement, not replace, human effort. While AI tools provide a great starting point, developers are needed to review, refine, and ensure that comments accurately reflect the code’s purpose. Human oversight ensures clarity, avoids inaccuracies, and accounts for context and project-specific nuances that AI might miss.

Does using AI for documentation affect project security?

Potentially, yes. AI tools may inadvertently generate comments that expose sensitive logic or highlight potential weaknesses in the code. It’s essential to review all AI-generated comments for security implications and ensure they align with the project’s security protocols.

Are AI documentation tools customizable for specific project needs?

Yes, many AI-powered documentation tools offer some degree of customization. Developers can configure rules for comment styles, preferred templates, or even teach the model about specific code structures common to the project or organization. This ensures more accurate, tailored output.

Can AI-generated documentation be used as a learning tool?

Absolutely! For newer developers or team members, AI-generated documentation can serve as a useful starting point to understand unfamiliar codebases. It can provide immediate context and overviews, aiding faster comprehension. Paired with tools like Keploy for testing, new members can experiment and learn how different parts of the system interact.

How can Keploy help enhance automation in software development?

Keploy, known for its focus on test generation and automation, can be a complementary tool when used with AI-driven documentation tools. By generating test cases automatically, it ensures that critical functionalities are validated.

aidocumentationclean-codesoftware-engineering