Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn C Programming

You're reading from   Learn C Programming A beginner's guide to learning C programming the easy and disciplined way

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781789349917
Length 646 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jeff Szuhay Jeff Szuhay
Author Profile Icon Jeff Szuhay
Jeff Szuhay
Arrow right icon
View More author details
Toc

Table of Contents (33) Chapters Close

Preface 1. Section 1: C Fundamentals
2. Running Hello, World! FREE CHAPTER 3. Understanding Program Structure 4. Working with Basic Data Types 5. Using Variables and Assignment 6. Exploring Operators and Expressions 7. Exploring Conditional Program Flow 8. Exploring Loops and Iteration 9. Creating and Using Enumerations 10. Section 2: Complex Data Types
11. Creating and Using Structures 12. Creating Custom Data Types with typedef 13. Working with Arrays 14. Working with Multi-Dimensional Arrays 15. Using Pointers 16. Understanding Arrays and Pointers 17. Working with Strings 18. Creating and Using More Complex Structures 19. Section 3: Memory Manipulation
20. Understanding Memory Allocation and Lifetime 21. Using Dynamic Memory Allocation 22. Section 4: Input and Output
23. Exploring Formatted Output 24. Getting Input from the Command Line 25. Exploring Formatted Input 26. Working with Files 27. Using File Input and File Output 28. Section 5: Building Blocks for Larger Programs
29. Working with Multi-File Programs 30. Understanding Scope 31. Other Books You May Enjoy Appendix

Some guidelines on commenting code

One of the best guidelines for commenting in code is the same guideline to follow in life. This is sometimes called the Goldilocks Principle, also known as the Three Bears Principle, named after the children's fairy tale, Goldilocks and the Three Bears. The essence of this guideline is not too much; not too little; just right. However, just right is subjective, depends on several factors, and will be different for each situation. Your own judgment and experience must be your guide to your Goldilock's moment.

These are essential guidelines to follow when commenting your code:

  • Assume the reader already knows the language: You are not teaching your reader how to code. Do not explain the obvious features of the language. You are explaining the non-obvious aspects of your code.
  • Write in full sentences with proper capitalization and punctuation: Comments are not code. They are the words you are writing to yourself or to other readers of your code. Your comments will be much less cryptic and more easily understood if you follow this guideline.
  • Comment on unusual uses of the language: Every language has oddities and idiosyncrasies that may not be used often or may be used in unexpected ways. These should be clarified and highlighted.
  • Try to comment in a way that is resilient to code changes: Very often, as code changes, comments are not necessarily updated to match. One way to mitigate this is to put comments in globs at the beginning of functions or to precede blocks of code rather than them being interspersed within code blocks so that if those change, the comments are still valid. You will see examples of this throughout this book.
  • Comment at a high level: Describe the intent of the code and the way it attempts to solve a problem. This guideline goes hand in hand with the first guideline we mentioned. The higher the level the comments are describing, the less likely they will need to be changed as the code changes.
  • Convey your intent: With your comments, strive to convey the intent of the code you are writing, why the code is needed, and what the code is trying to accomplish. What the code is actually doing should come from the code itself.

I am often surprised when I revisit code I wrote 6 months ago. Too often I find that I am scratching my head asking, why did I do this? or, what was I thinking here? (both cases of too little commenting). I also find that when I change code, I have to delete many comments that are no longer necessary (a case of too muchcommenting). I rarely find that I have commented too much when I have focused on the intent of the code (what was I trying to do here).

At one point in my career, I came across a programmer whose comments were completely divorced from the code that was there. I concluded that this programmer initially intended their algorithm to work one way, but then modified the code so significantly that the comments no longer matched the actual code at all. When I saw that programmer's name in subsequent code, after careful inspection, I more often than not simply deleted the code comments because I found them to be irrelevant. Please do not do this unless you are absolutely certain you understand the code and that the comments do not match the code.

Learning how to effectively comment on code is a lifelong challenge. I do not suppose you will learn this quickly. You will learn this after years of examining your own code and making your code clearer to yourself, let alone making your code clearer to others. As we work through various C example programs, I intend to demonstrate a variety of useful and resilient commenting techniques.

You have been reading a chapter from
Learn C Programming
Published in: Jun 2020
Publisher: Packt
ISBN-13: 9781789349917
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image