Comment

Comment by aleem

Refactoring is fun, especially on a slow day. The "Introducing an Explaining Variable"[1] refactoring is:

  if (version < 8 && ua ~ "IE") ...
is better written as

  IE8 = (version < 8 && ua ~ "IE");
  if (IE8) ...
This allows variable IE8 to be (1) reused later on in the code, (2) is self documenting and (3) sets a precedent for extending it easily by future devs:

  IE8 = (version < 8 && ua ~= "IE");
  mobile = (ua ~= "mobile")
  if (IE8 || mobile) ...
  
Another quick one is Guard Clauses[2].

  function {
    if (...) {
      ...
    } else {
      ...
    }
  }
where either block is long, is better written as

  function {
    if (...) {
      return ...;
    }
    ...
  }
This site http://sourcemaking.com/refactoring is worth a read even if you have been programming for years; especially if you have been programming for years and haven't developed good habits.

  [1] http://sourcemaking.com/refactoring/introduce-explaining-variable
  [2] http://sourcemaking.com/refactoring/replace-nested-conditional-with-guard-clauses
parent aleem 12 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton