More on this book
Community
Kindle Notes & Highlights
Sometimes it’s not clear why code is needed, or why it was implemented in a particular way. Sometimes there are rules the developer followed, such as “always invoke a before b.” You might be able to guess at a rule by looking at all of the code, but this is painful and error-prone; a comment can make the rule explicit and clear.
They omit details and help the reader to understand the overall intent and structure of the code.
In reference to 'higher level comments that enhance code intuition'. Think one of the best ways to do this is to have a text file which explains the code at an extremely high and abstract level for the whole project; simply detail the script, its function, how it relates to other scripts, and a high level description of the goal of the code.
Engineers tend to be very detail-oriented. We love details and are good at managing lots of them; this is essential for being a good engineer. But, great software designers can also step back from the details and think about a system at a higher level. This means deciding which aspects of the system are most important,
This is the essence of abstraction (finding a simple way to think about a complex entity), and it’s also what you must do when writing higher-level comments. A good higher-level comment expresses one or a few simple ideas that provide a conceptual framework,
In other words, your higher level comments should explain the image you held within your mind when writing the code in order to recreate that image in the mind of the person attempting to understand it; thus granting the capacity for the person to look at the code with the eyes of the person who created it.
Red Flag: Implementation Documentation Contaminates Interface This red flag occurs when interface documentation, such as that for a method, describes implementation details that aren’t needed in order to use the thing being documented.
If your code is undergoing review and a reviewer tells you that something is not obvious, don’t argue with them; if a reader thinks it’s not obvious, then it’s not obvious. Instead of arguing, try to understand what they found confusing and see if you can clarify that, either with better comments or better code.
The term “count” is too generic: count of what? If someone sees an invocation of this method, they are unlikely to know what it does unless they read its documentation. A more precise name like getActiveIndexlets or numIndexlets
Red Flag: Vague Name If a variable or method name is broad enough to refer to many different things, then it doesn’t convey much information to the developer and the underlying entity is more likely to be misused.
Red Flag: Hard to Pick Name If it’s hard to find a simple name for a variable or method that creates a clear image of the underlying object, that’s a hint that the underlying object may not have a clean design.
Use names consistently The second important property of good names is consistency. In any program there are certain variables that are used over and over again.
This would mean across the whole application; i have definitely been guilty of not using consistent names between modules - need to correct this.
Overall, I would argue that readability must be determined by readers, not writers. If you write code with short variable names and the people who read it find it easy to understand, then that’s fine. If you start getting complaints that your code is cryptic, then you should consider using longer names
Gerrand makes one comment that I agree with: “The greater the distance between a name’s declaration and its uses, the longer the name should be.”
Or, the more detailed the name should be. I disagree; goal should be to make the code read as close to english as possible, this is what makes the python syntax so popular. Good chance anyone reading your code already speaks english, afterall.
Not only does this produce better documentation, but it also produces better designs and it makes the process of writing documentation more enjoyable.
Feynman says the best way to test your understanding, is to explain to someone else. This can also be in the context of writing; but i think in general that this is useful for any kind of expression.
Even if you do have the self-discipline to go back and write the comments (and don’t fool yourself: you probably don’t), the comments won’t be very good. By this time in the process, you have checked out mentally. In your mind, this piece of code is done; you are eager to move on to your next project.
This is exactly what i am like with my research projects; thus i should be writing the manuscript constantly as i'm doing the project, and submit it when i'm done.. Using latex is probably best way for this; since then any figures i make along the way i can easily just import into my document without much effort. Thus gives a 'written version' of the code / outcomes from running the code.
i'm also guilty of this when writing code tests :s. So again, this should be done as i go.
Comments serve as a canary in the coal mine of complexity.
The third and final benefit of writing comments early is that it makes comment-writing more fun. For me, one of the most enjoyable parts of programming is the early design phase for a new class, where I’m fleshing out the abstractions and structure for the class.
In contrast, if you write the code first, the abstractions will probably evolve as you code, which will require more code revisions than the comments-first approach.
Exactly, it is much better to develop your mental image more clearly than trying to code blindly. The mental model is the most important aspect of solving any problem.
Unfortunately, when developers go into existing code to make changes such as bug fixes or new features, they don’t usually think strategically. A typical mindset is “what is the smallest possible change I can make that does what I need?”
Too short sighted of course. Beter question is "how can i set this up to minimise work in the future?"
If you’re not making the design better, you are probably making it worse.
“Is this the best I can possibly do to create a clean system design, given my current constraints?”
The best way to ensure that comments get updated is to position them close to the code they describe,
The farther a comment is from its associated code, the less likely it is that it will be updated properly.
In general, the farther a comment is from the code it describes, the more abstract it should be (this reduces the likelihood that the comment will be invalidated by code changes).
comments are easier to maintain if they are higher-level and more abstract than the code.
Consistency creates cognitive leverage: once you have learned how something is done in one place, you can use that knowledge to immediately understand other places that use the same approach.
This is also true of learning in general; need to find a low-dimensional understanding of the world so one can reason from some basic premises in order to generate a model of the world.
Don’t change existing conventions. Resist the urge to “improve” on existing conventions. Having a “better idea” is not a sufficient excuse to introduce inconsistencies. Your new idea may indeed be better, but the value of consistency over inconsistency is almost always greater than the value of one approach over another.
Disagree. If it is enough better, you should refactor. The current code might be written in an overly complex way,for instance.