Being mindful of other developers

Coding with love

Alex Peattie

CTO @ Peg

@alexpeattie

slides.com/alexpeattie/love

  • Cryptic (Junior)
  • Clever (Mid-level)
  • "Computer friendly" (Senior)

Cryptic

Junior

dd = Time.now + (wl == [] ? 1814400 : 1209600)
Loan.new(dd)
if wl == []
  dd = Time.now + 1814400
else
  dd = Time.now + 1209600
end

Loan.new(dd)
if waiting_list == []
  due_date = Time.now + 1814400
else
  due_date = Time.now + 1209600
end

Loan.new(due_date)
if waiting_list.empty?
  due_date = Time.now + 1814400
else
  due_date = Time.now + 1209600
end

Loan.new(due_date)
if waiting_list.empty?
  # due date 3 weeks from now
  due_date = Time.now + 1814400
else
  # due date 2 weeks from now
  due_date = Time.now + 1209600
end

Loan.new(due_date)
week = (7 * 24 * 60 * 60)

if waiting_list.empty?
  due_date = Time.now + (3 * week)
else
  due_date = Time.now + (2 * week)
end

Loan.new(due_date)
if waiting_list.empty?
  due_date = weeks_from_now(3)
else
  due_date = weeks_from_now(2)
end

Loan.new(due_date)
if waiting_list.empty?
  due_date = weeks_from_now(3)
else
  due_date = weeks_from_now(2)
end

Loan.new(due_date)
dd = Time.now + (wl == [] ? 1814400 : 1209600)
Loan.new(dd)

Clever

Mid-level

class Yogi
  def cat_pose
    imitate Cat.new
  end

  def cow_pose
    imitate Cow.new
  end

  def cobra_pose
    imitate Cobra.new
  end
end

denise = Yogi.new
denise.cat_pose
class Yogi
  %w(cat cow cobra).each do |animal|
    define_method("#{animal}_pose") do
      imitate Object.const_get(animal.capitalize).new
    end
  end
end

denise = Yogi.new
denise.cat_pose
  • Confusing
  • Hard to search
  • Tightly coupled
class Yogi
  def cat_pose
    imitate Cat.new
  end

  def cow_pose
    imitate Cow.new
  end

  def cobra_pose
    imitate Cobra.new
  end
end

denise = Yogi.new
denise.cat_pose

"Computer friendly"

Senior

def is_something?(string)
  1.upto(string.length) do |n|
    return false if string[n - 1] != string[-n]
  end
  true
end

A

def is_something?(string)
  string == string.reverse
end

B

def is_something?(string)
  string == string.reverse
end
def is_something?(string)
  1.upto(string.length) do |n|
    return false if string[n - 1] != string[-n]
  end
  true
end

The quick brown fox jumps over the lazy dog

The quick brown fox jumps over the lazy dog

god yzal eht revo spmuj xof nworb kciuq ehT

B is 30% slower

but....

But for 1 million runs with a 20,000 word string..

the difference is 2s

AWS clusters start from $0.013/hr

Conclusion

  • Coding isn't a solitary pursuit
  • Be mindful of being...
  • Cryptic
  • Clever
  • Computer friendly
  • Love your fellow developers!

alex@peg.co

@alexpeattie

slides.com/alexpeattie/love

Being mindful of other developers: coding with love

By alexpeattie

Being mindful of other developers: coding with love

  • 3,002