Skip to content

Instantly share code, notes, and snippets.

@dreamr
Last active December 29, 2015 00:29
Show Gist options
  • Save dreamr/7585971 to your computer and use it in GitHub Desktop.
Save dreamr/7585971 to your computer and use it in GitHub Desktop.
An attempt to put the thoughts that go on in my head when I code to something that can be followed.

Keeping code readable and idiomatic

Design considerations:

  • Avoid imperative conditional logic
  • If you can do it with an enumerator, then just do it
  • Keep 90% of the overall methods idempotent
  • Wrap small sections of imperative shell around composite idempotent methods
  • Think in terms of behaviors and values, not objects and messages
  • Identify and classify data clumps (primitives that do not work as sub data)
  • Use modules to wrap related behavior
  • Keep SOLID
  • Stay the hell away from nils, coerce and reject, use nullobjects
  • Prefer lambdas over blocks and procs for their closure model
  • Prefer Polymorphism/composition to inheritance
  • Real Duck Typing is not done by checking class or method, just call it
  • Coerce values to avoid nils - to_s, to_str, to_a, to_ary, to_i

Methods should be:

  • Idempotent if at all possible
  • Responsible for 1 and only 1 behavior
  • Composites of other methods, callable via lambdas
  • Absent of larger conditional logic than a single ternary or short circuit
  • Recursive in nature over loopy

Testing

  • Stick to MiniTest - it has what you need, you just don't know it yet
  • Test double only what you own, wrap external dependencies in methods to isolate them
  • Write REAL unit tests, don't test outside your method
  • In unit tests, do not hit any external dependencies (databases, rails, disks, code outside your scope)
  • Be mindful of functional tests, test the paths that matter to integrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment