Featured Articles:

Friday, October 23, 2026

Your Code Comments Are More Important Than You Think: When To Write Them And When To Delete Them

Welcome everyone to another blog post about the only part of your code the computer completely ignores: comments in C#. They're free, they're easy, and believe it or not, they're still one of the most misused tools in a beginner's toolbox. Some devs comment every single line, others never write one at all, and both groups end up equally confused six months later. I was not even going to write an article about it until I realized the importance of comments, and I wish I knew about it when I started out coding.


Let's untangle this!


The Three Flavors Of Comments

C# gives you three kinds of comments, and all of them get thrown away when your game compiles:


// A single-line comment
int health = 100; // it can also sit after a line of code

/* A block comment
   can stretch across as many lines as you like */

/// A triple-slash comment is for documentation (more on that below)
  
  • // - a single-line comment. By far the one you'll use most.
  • /* ... */ - a block comment, handy for longer explanations.
  • /// - a documentation comment, which editors can turn into tooltips.

Writing a comment is the easy part. Knowing when to write one is the actual skill.


Code Comments Best Practices: Explain WHY, Not WHAT

Your code already shows what it does. A comment can't do that job any more better, and usually does it even worse. What the code can't tell you is why it does it that way. That single idea is the heart of code comments best practices.

Here's what NOT to do:


// Set health to 100
health = 100;

// Loop through every enemy
foreach (Enemy enemy in enemies)
{
    // Call TakeDamage on the enemy
    enemy.TakeDamage(10);
}
  

Every one of those comments repeats the line right below it. They add noise, and they add a chore, since someone has to update them whenever the code changes. (Spoiler: nobody does.)

Now compare that with comments that earn their keep:


// Must run in Awake, not Start: other scripts read Health during their own Start.
private void Awake()
{
    health = maxHealth;
}

// 0.2 seconds is the longest the hit animation can play before it clips into the next one.
private const float KnockbackDuration = 0.2f;
  

Those tell you something the code never could. Good things to comment:

  • Why a weird-looking choice was made - the "why on earth is it written like this?" moments
  • Workarounds - for a bug in the engine, a platform, or a package
  • Execution order warnings - "don't reorder these lines, they depend on each other"
  • Magic numbers and non-obvious math - where a number came from
  • Warnings to future you - "changing this breaks old save files"


The Comments That Hurt

Some comments are worse than no comment at all:

  • Comments that restate the code - like the ones above. Pure clutter.
  • Outdated comments - comments never fail to compile, so nothing warns you when one stops being true. A wrong comment is worse than a missing one, because you'll trust it.
  • Commented-out code - big blocks of dead code make a file harder to read and leave you wondering if it matters. Delete it. Version control like Git remembers it for you.


Let The Names Do The Talking

A lot of comments are just bandages on vague names. Fix the name, and the comment disappears:


// Bad: the comment is doing the name's job
// seconds until the next shot can be fired
float t;

// Better: the name says it all
float shotCooldownSeconds;

// Bad: a comment explaining a tangled condition
// check if the player can open the door
if (distance < 2f && hasKey && !isBusy)
{
    OpenDoor();
}

// Better: a well-named variable explains itself
bool canOpenDoor = distance < 2f && hasKey && !isBusy;
if (canOpenDoor)
{
    OpenDoor();
}
  

The same trick works on methods. If you're about to write a comment above a ten-line block, try moving that block into its own method and naming it whatever the comment would have said.


XML Documentation Comments: Tooltips In Your Editor

Triple-slash comments with a little XML inside are called XML documentation comments. Put one above a method, and most editors show it as a tooltip whenever you hover over or call that method. Nobody has to open the file to remember what it does.


/// <summary>
/// Reduces health by the given amount. Kills the player at zero.
/// </summary>
/// <param name="amount">How much damage to deal.</param>
public void TakeDamage(int amount)
{
    health -= amount;
}
  

Save them for public methods and classes that other scripts will call. Don't bother with one for every tiny private helper. One more thing: these don't show up in the Unity Inspector. For a tooltip on an Inspector field, use the [Tooltip("...")] attribute above the field instead.


TODO Comments: Notes To Future You

A TODO comment marks something you plan to come back to, and many editors collect them into a handy list:


// TODO: replace with object pooling once the enemy count passes 50
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
  

A good TODO says what and why. A bad one says "TODO: fix this," which leaves everyone asking fix what, and when? And remember, a TODO nobody comes back to is just a comment with ambition.


So Which Approach Should You Actually Use?

Here's the no-fluff version, the gut check you can run through in your head:

  • Something weird or non-obvious about why → a short // comment
  • Explaining what a block does → rename things or extract a method instead
  • A public method other scripts will call → XML documentation comments
  • Something you'll fix later → // TODO: with what and why
  • Old code you might want back → delete it, Git has your back
  • A tooltip on an Inspector field → the [Tooltip] attribute
  • Not sure → follow code comments best practices and ask "will this still be true in six months?"

A comment should answer one question: why. If it answers "what," your code probably needs a better name instead.



Wrapping Up

Comments aren't a measure of how good your code is, and more of them doesn't mean better. A few well-placed comments in C# that explain the "why" beat a wall of lines that repeat the code. Name things clearly, comment the surprising parts, and delete anything that no longer tells the truth. It really helps when you get back to your project the next day, or any other time in the future.


 ~happy Coding!

No comments:

Post a Comment