Code Conquest

  • Home
  • What is Coding?
  • Tutorials
  • Training
  • Reviews
  • Knowledge Center
  • Versus
  • Blog
You are here: Home / Advanced Coding Concepts / Guide to Code Commenting

Guide to Code Commenting

Code commenting is the practice of sprinkling short, normally single-line notes throughout your code. These notes are called comments. They explain how your program works, and your intentions behind it.

Comments don’t have any effect on your program, but they are invaluable for people reading your code. Here’s an example of code commenting in action:

// increment variable
variable++;

Notice how the comment on the first line describes the line of code below it.

Coders are expected to comment their code to some degree. But if you haven’t been doing so, don’t stress. Learning how to comment your code is not that hard! This guide to code commenting will start you off. We’ll look at the usage and syntax of code comments, as well as two code commenting case studies.

Usage of Code Commenting

Code comments are useful for several purposes. A code comment can…

  • explain what a particular function does
  • explain something which might not be obvious to the reader
  • clarify your intention behind a certain line or block of code
  • serve as a reminder to change something in the future.

Code commenting also becomes invaluable when you want to ‘comment out’ code. This is when you turn a block of code into a comment because you don’t want that code to be run, but you still want to keep it on hand in case you want to re-add it.

Code Commenting Syntax

The syntax for code comments differs between coding languages. In many languages, two slashes indicate a single line comment:

// Code Comment

And to include a multi-line comment for when you want to ‘comment out’ code, the syntax is as follows:

/* Commented Out
Commented Out
Commented Out */

The above syntax is used by many languages, such as PHP and JavaScript. However, other languages use different syntax. To verify comment syntax for a particular language, consult the documentation.

Code Commenting Example: Explaining a Function

Unless you’re a crack PHP coder, you’d probably have no idea what the below code does. Or would you? The comment above the function explains what the function does – it checks if input is valid. The comment just saved you from bending over backwards trying to figure out what the function is doing.

// Check if input is valid
function is_valid($first_name, $last_name, $age) {
    if (
        !ctype_alpha($_POST['first_name']) OR
        !ctype_alpha($_POST['last_name']) OR
        !ctype_digit($_POST['age'])
        ) {
        return false;
    }
    return true;
}

I hope you are enjoying this article. To learn more on this topic, you can read this article on what is a good code comment ratio after finishing this one.

Code Commenting Example: Clarifying Your Intention

Consider the below example. If the variable animal is equal to 2, dog() is executed. If it’s equal to 1, cat() is executed, but dog() is executed as well. This is because there’s no break statement at the end of case 1, so that case falls through to the next one.

But is that a mistake or intentional? Unless the comment is included to clarify, there’s no way of knowing whether the coder meant for this to happen or just forgot to include the break statement.

switch(animal) {
    case 1:
        cat();
        // falls through
    case 2:
        dog();
        break;
}

Conclusion

It’s good to get into the habit of adding comments to your code, in case anyone else might ever read it. Even reading it yourself, you’ll find that your comments help to remind you what you were doing with your code.

Being able to comment your code efficiently is an important skill for any coder. In fact, being able to comment code is almost as important as being able to write code. Practice and it’ll soon become second-nature!



Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.

Popular Series

  • What is Coding?
  • How to Make Your First Website
  • Understanding Hex Color Codes
  • How to Become a Coder in 6 Months: a Step-by-Step Action Plan
  • How to Start a Coding Project

Get Our Newsletter

Enter your email address and we'll notify you whenever we add something new to the site.

Popular Blog Posts

  • The 50 Best Websites to Learn Python
  • The 50 Best Websites to Learn JavaScript
  • The 50 Best Websites to Learn PHP
  • Want to Switch Careers? Coding May Be the Perfect Solution!
  • 9 of the Hottest Tech Skills Hiring Managers Look for on LinkedIn

Latest Blog Posts

  • Effective Strategies For Debugging Code
  • Effective Time Management Tips for Programmers
  • Code Documentation: Tools and Techniques
  • Is Data Analytics Hard to Learn?
  • Comparing Data Science vs Computer Science

Explore Code Conquest

  • What is Coding?
  • Free Code Tutorials
  • Coding Training Recommendations
  • Coding Training Reviews
  • Knowledge Center
  • Cheat Sheets
  • Coding Quizzes
  • Versus

Deals, Discounts and Coupons

Deals

Code Conquest

  • Home
  • About
  • Privacy Policy
  • Contact Us
  • Terms of Use
  • Write for Us
  • Featured Review

Copyright © 2025 Code Conquest · About · Terms · Privacy · Contact Us · Write For Us