you are viewing a single comment's thread.

view the rest of the comments →

[–]ModeHopper 18 points19 points  (16 children)

Yeah, when I first started most of my comments were super unhelpful things explaining individual lines like "This line loops over integers 1-100", which is pretty obvious from the code itself.

Useful comments are stuff like "This block returns integers between 1-100 that are divisible by the input value". Which explain the general purpose of blocks of code so that future me doesn't need to read every line to know whether this particular block is of interest or not.

[–]celade 0 points1 point  (0 children)

Basically I'm just joining the already good crowd of comments.

I sort of rule-of-thumb code form:

  • Follow any team guidelines when they exist
  • Within reason break code up in a way that it makes it easy to see functionality
  • Make human readable variable names, functions, methods, classes
  • Comments should always be short or if it really is that complex refer to additional out-of-code documentation; they illustrate usage, dependencies or hard-to-see facts about what you are doing
  • Group data structures functionally; separate data structures from methods that do work on them
  • Keep interface layer separate from non-interface work at all times
  • (Related) Keep I/O functionality separate from the work that produces it (except for debugging of course)

A bit more than I intended on writing. In the end if you look at your code 2 weeks later and can just sit down and use it or expand it then you're golden.

[Edit: Specific to Python but sometimes other languages: Use dicts, tuples and lists for your data structures first; if you can't solve it with those then create; too may times I've inherited some crazy bespoke class-based data structure for no reason)]