3 ms·
> - Subroutines must have one entry point and one exit point, and accomplish one task. Sometimes code is much cleaner if you exit a method or a function early.
by V-2 8y ago
> - Subroutines must have one entry point and one exit point, and accomplish one task.
Sometimes code is much cleaner if you exit a method or a function early. Especially in languages with automatic memory management where you don't need to concern yourself about leaks so much.
> - Add lots of comments in your code to help others or even yourself in case you forget what your thinking was.
Lots of comments is a red flag to me. It often indicates the code itself is of poor readability. But it's usually easier to just slap some comment on it than to properly rewrite it... "A comment is a failure to express yourself in code" (Uncle Bob).
> These are just a few examples of good programming practice
I'd argue they are debatable, and at the very least not necessarily universal.
- HereBeBeasties 8y agoAh, the most important rule - there are no rules, only compromises.
- deleted 8y ago[deleted]
- ChucklesNorris 8y agoAbout the comments, by no means do I mean that comments can be a crutch and should allow for sloppier coding, but sometimes the problem that you're trying to solve requires a lot of brainstorming, and when you hit your "aha" moment, you'd better write down what your thinking is, in English. The code itself might be pretty hairy to go over and decipher if that's all you've left yourself. But I get what you mean, and you make a valid point.
- V-2 8y agoSometimes code simply cannot speak for itself, and then a comment is very welcome, sure. What I learned though is that a lot of programmers suffer from a form of a "comment blindness". I was once asked to explain a piece of code for which I already provided that exact explanation in the form of a neat, verbose comment, on this every screen : ) In my experience, programmers develop this "comment blindness" kind of like an everyday person numbs themselves to omnipresent ads via so-called "banner blindness". Basically, most of the comments one encounters are redundant, stale etc. and therefore useless. So the less comments overall, the better the chance for the ones that matter to stand out. At the end, there's no silver bullet, and while as experienced programmers we find it obvious and needless to say, I guess it probably is a valid takeaway for juniors to take most guidelines as rules of thumb only, and avoid falling into dogmatism (the mother of cargo cult)...
- ChucklesNorris 8y agoI remember looking at used books in my college bookstore, and found one which had nearly every sentence highlighted in yellow. The book might as well have been dipped in yellow ink! Overuse of the highlighter amounted to not using it at all. I agree, comment judiciously.
- Retra 8y agoI put a lot of comments in my code, but not to explain what the code does, moreso to explain why it is doing what it is doing, what it used to do and why it changed, and what else I think it could do instead of what it is.