34,333
edits
Changes
no edit summary
Comments in both programming and scripting languages provide a mechanism for the developer to write notes that are ignored by the compiler or interpreter. These notes are intended solely for either the developer or anyone else who may later need to modify the code. The main purpose of comments, therefore, is to allow the developer to make notes that help anyone who may read the code later to understand issues such as how a particular section of a program works, what a particular method does or what a variable is used to store. Commenting code is considered to be good practice. Rest assured that a section of Objective-C code that seems obvious when you write it will often be confusing when you return to it months, or even years later to modify it. By including explanatory comments alongside the code this becomes less of a problem.
Now, back to that old saying - "Don't comment bad code, re-write it!". What this phrase suggests is that if code is well written in the first place you do not need comments to explain what it does and how it does it. It also suggests that if you are having to write lots of comments to explain what a section of your Objective-C program does then you must have written it badly. Whilst one should always strive to write good code there is absolutely nothing wrong with including comments to explain what the code does. Even a well written program can be difficult to understand if it is solving a difficult problem so, ignore the old programmers programmer's adage and never hesitate to comment your Objective-C code.
Another useful application of comments in Objective-C is to ''comment out'' sections of a program. Putting comment markers around sections of code ensures that they are ignored by the compiler during compilation. This can be especially useful when you are debugging a program and want to try out something different, but do not want to have to delete the old code until you have tested that the new code actually works.