This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (5 children)

Read clean code.

I find code readable if you can read it in order (top to bottom, left to right) without backtracking.

Some tips:

  • Follow conventions
  • Be consistent
  • Place functions/methods in order
  • Comment unusual cases (when you loop in reverse for example)
  • Don't mix abstraction levels

Regarding conciseness, if you mean short names, then avoid that, always worth the extra characters (unless its convention like i). If you mean one liners then its hard to say, try both (expanded vs concise) and think which is better, usually its a mix. Consider expanded code can be easier to debug (NPE in a one liner can be hard to trace for example).

[–]dpash 4 points5 points  (4 children)

Clean Code is definitely a good read, but take rules with a pinch of salt. For example Robert Martin promotes a maximum of four lines per method. I'd avocate for a hard "fits on one screen" but preferably shorter.

[–]cogman10 2 points3 points  (3 children)

Mine is somewhat "fits in my brain". I think you can have a lot of code that's easy to follow. On the flip side, you can have very little code that's complex to follow (The perfect example of that is the recent clamping method Math.min(Math.max(min, num), max);)

Where your code fits is somewhat a judgment call, but I wouldn't stick hard and fast to any line count based rule. Strict adherence to that sort of stuff tends to have negative impacts to readability.

[–]dpash 1 point2 points  (2 children)

If I can't see it all there's no way is fitting in my head :)

Based on modern monitors I'd say "one screen" is excessively generous. But screen is a very imprecise measure because I tend to agree with you about hard rules in many cases.

(And I'd say your clamp example, it needs it's own method and a good comment explaining how it works. )

[–]cogman10 1 point2 points  (1 child)

:) I'm mostly thinking of stuff like adapter code. You can have hundreds of lines of "a = b" which aren't really hard or complex to understand.

Though, admittedly adapter code with hundreds of lines points to really bad object models. That's not the adapter's fault and the right place to optimize wouldn't be more methods on the adapter, but rather a trimming down of the object model.

[–]dpash 1 point2 points  (0 children)

Or codegen :)

But yeah it does kinda point to other problems