December 2006
Intermediate to advanced
610 pages
22h 58m
English
The following example illustrates these commenting principles. Consider the following snippet of C++ code. Idiomatic criticisms aside, it is not at all clear what’s going on.
for (int i = 0; i < wlst.sz(); ++i) k(wlst[i]);
Yuck. There’s some room for improvement here, so let’s improve. The code can be made less cryptic by applying sensible layout rules and adding a few comments:
// Iterate over all widgets in the widget listfor (int i = 0; i < wlst.sz(); ++i) {// Print out this widgetk(wlst[i]); }
Much better! Now it’s entirely clear what the code snippet is supposed to be doing. I’m still not entirely happy, though. With appropriate function and variable names, we no longer need any comments at all, since the code describes itself: ...
Read now
Unlock full access