Good code is self-documenting – so the adage goes!
Comments and codes have inverse relationship – it is believed that better codes need less comments! Only when one reaches a point – where, it would be difficult to grasp the underlying logic without few clues, should one resort to adding comments. This is true; but, is it always true?
The use case for writing comments – when you write large amount of codes, it might be difficult for others – including you, for that matter! – to remember why were the codes written in the first place! At such junctures, having comments as a guide posts can be a time-saver.
Before we take a deep dive into why one needs to write comments around codes – and explore different types of comments – let me draw your attention to my previous article Bugged by Bugs? Master Debugging! In the article, I discuss how to debug your code using Developer Console; it is also a good first step in understanding when, and how, to use comments!
This is fourth article in learning Apex series. If you have not read previous articles – Mastering Apex Variables – a Stepping Stone in your Apex Journey and Pre-requisites for Learning Apex – Logical Mind and Confidence! – I strongly recommend going through them now. Also, there are few assignments for you in the articles; go ahead! take a crack at it right now! It should not take you more than 30 minutes to complete them. Irrespective of whether you are a newbie, just starting a career as a Salesforce Developer, or have some programming background, completing the assignments will help you to – (1) either learn few new things; or (2) at the least, help you to brush up your concepts. At a bare minimum, these articles and assignments will help you to brush up your basic concepts of programming. Good Luck!