Always default to false - Paul Grayson

B044a0f039af800f4df09bf3b2465f18?s=47 Las Vegas Ruby Group
January 29, 2014
46

Always default to false - Paul Grayson

B044a0f039af800f4df09bf3b2465f18?s=128

Las Vegas Ruby Group

January 29, 2014
Tweet

Transcript

  1. 2.

    Be opinionated! • There is a right way • Consistency

    aids memory true false 1 exception 0 default
  2. 3.

    The default is already false or null in many contexts

    Ruby: @var, if x, methods, [], etc. -> nil SQL: NULL Javascript: undefined C#/Java: bool/boolean defaults false Originates in digital circuits? - default is OFF
  3. 5.

    Fields, variables, conditionals # DO THIS: if on_mailing_list send_email end

    # NOT THIS: if !on_mailing_list withhold_email end How would you even write that method? Body does not run by default
  4. 6.

    Be careful with true default <% if !out_of_stock %> <div

    id="order_form"> ... </div> <% end %> Must remember to not put anything essential in here.
  5. 7.

    Emphasize connections def f if x exceptional_stuff else default_stuff end

    more_stuff end The normal path Condition and code to handle it
  6. 8.

    true default complicates connections def f if !x default_stuff else

    exceptional_stuff end more_stuff end The normal path Condition and code to handle it
  7. 9.

    The “guard clause” pattern def f return y if x

    normal_stuff ... end The normal path Condition and code to handle it
  8. 10.

    Summary • Default false for all fields • Test for

    unusual conditions (when possible) • Beware of conditionals with true default true false 1 exception 0 default