Topics List
Basic Concepts

Comments
Commenting your Source Code

In programming, comments are essential for explaining code logic, especially in complex programs. They provide insights into the purpose of functions, loops, and variables, making it easier for developers to understand and maintain the code. Let´s look at an example in Kenzie to demonstrate the importance of comments.

Before (Without Comments):



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

After (With Comments):



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

Comparison:

Without Comments (Before):


Functions are defined without explanation.
Code is less readable and harder to understand, especially for someone new to the code.

With Comments (After):


Each function has a comment explaining its purpose and logic.
Code is more understandable and easier to maintain, even for those unfamiliar with the codebase.

Conclusion:


Adding comments to your code is crucial for improving readability and maintainability. They provide valuable insights into the code´s functionality, making it easier for others to collaborate and understand the code.
Certainly! In the following example, we´ll demonstrate that while comments are essential for clarity, it´s also important to maintain a balance and not over-comment the code.

Without Comments (Before):



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

With Comments (After):



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

Conclusion:


Adding comments to code, especially in complex algorithms, greatly enhances its readability and maintainability. Comments provide insights into the logic and purpose of the code, making it easier for developers to understand and modify it in the future.

Another Example:


Code Without Comments



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

Code With Comments



Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

In the first code snippet without comments, it´s difficult to understand the purpose of the calculations and how the functions are used. However, in the second code snippet with comments, each function is clearly explained, along with comments describing the main program´s functionality. This makes the code much easier to understand and maintain, especially as the program grows in size and complexity.