wleci
AboutProjectsBlogContact
Contact
Back to blog
INF.04ProgrammingClean Code

Code Comments: Types and Best Practices

Learn how to use comments in programming correctly. Explore single-line, multi-line, and documentation comments.

December 25, 20253 min read
Share:

Comments and Their Types

Source code is written for machines, but read by humans. Comments are fragments of a program that are completely ignored by the compiler or interpreter. Their only purpose is to help the programmer understand why a specific piece of code was created and how it works.

Comment

A non-executable part of source code used to describe program behavior, create documentation, or temporarily disable code fragments during testing.

Types of Comments

Most programming languages offer two basic ways to comment, and some add special documentation blocks.

CechaTypeUsage
Single-lineShort description in one line// This is a comment
Multi-lineLonger explanations, code blocks/* Comment content */
DocumentationGenerating API documentation/** @param id */

When to Use Comments?

A good comment doesn't describe what the code does (that should be clear from variable names), but why it was written that way.

Good Example

Explaining Intent

Commenting complex algorithms or unusual solutions that are not obvious at first glance.

Bad Example

Commenting the Obvious

Describing simple operations, e.g., i = i + 1; // increment by one. This just clutters the code.

Key Principle

Code as Documentation

Aim for code so clean that comments are only needed in exceptional situations.

Common Mistakes and Pitfalls

Outdated Comments

This is the biggest trap. If you change the code but forget to update the comment, you mislead other developers. An outdated comment is worse than no comment at all.

Leaving 'Dead' Code

Using comments to disable old code fragments and leaving them in the repository for months. Use Git for version control, not comments.

Examples in Different Languages

See how comment syntax varies depending on the technology:

cpp
// Single-line comment /* Multi-line comment */

Pro Tips

Use TODOs

A special type of comment // TODO: allows you to mark places that still need work. Most editors (like VS Code) can display a list of all such places in a project.

Good Commenting Checklist

Before you write a comment, check:

0/4

Quiz: Check Your Knowledge

What happens to comments during the program compilation process?

Flow Diagram (Mermaid)

What Next?

  • Find out what Javadoc is and how to automatically generate documentation from code.
  • Read about Clean Code standards regarding commenting.
  • Check how Docstrings work in Python.
  • Learn the keyboard shortcut for quick commenting (usually Ctrl + /).
  • Solve INF.04 tasks while paying attention to code readability.

You might also like

INF.04Programming

Relational and Logical Operators: How to Compare Data?

Understand how comparison operators work and how to combine conditions using AND, OR, and NOT. Essential knowledge for control statements.

2 min read
INF.04Programming

Conditional Operator: Short-hand if-else in one line

Learn the ternary operator. Find out how to shorten your code and when to use it instead of classic if-else statements.

2 min read
INF.04Programming

Mathematical Operators: The Foundation of Computing

Learn basic and advanced mathematical operators. Understand how modulo, incrementation works, and why operator precedence matters.

3 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland