There’s a number of things we can should take into account when writing code — boundary cases, etc., that can make or break a programme. Yes, testing is important, but I think that developing good defensive programming practices is even more important. That said; I present the following, fragile, code. Just about every line has a case where it can fail. Can you break my code? I’ll post some test cases which cause it to fail later.
And for those looking for the next installment of O_RLY?, it’s coming soon; I’ve been embroiled in a hideous move.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
class Fragile # not an error, but is this what we want? attr_reader :name # Initially, we all start in the same place.... # But we can get off to a bad start. def initialize(name) @name = name end # Slice it; dice it; # what causes the salad to shoot the wrong way? def caesar_salad() @name.downcase!.tr("abcdefghijklmnopqrstuvwxyz", "zyxwvutsrqponmlkjihgfedbca") end # Change is hard. # original can be set with different values # such that errors can be caused by every # line def morph(original) s=original.upcase.gsub(/[^A-Z]/,"") s.tr!("ABC","123").squeeze! s.capitalize!.slice!(0,5) s=s[0].succ end # Everyone has their limit # what will cause me to break? def counter(limit) 1.upto(limit){ |n| puts n} end # Shifting into gear; if you can't find 'em, grind 'em def red_shift(num,shift) (0 + num ) << shift end # If I'm not careful, I'll lose a sandal.... # what will break me? def hermes(message) self.send(:sos,message.inspect) end # I may not break, but I have issues... def method_missing(symbol,*args) case symbol when :sos puts "I'll send an SOS to the world;\nMy message in a bottle is:\n#{args[0]}" end end end |
Edit: caesar_salad
should have had @name instead of name
1 ping
Ramblings » Blog Archive » Why my code is broken….. (break my code redux)
August 26, 2008 at 6:47 am (UTC -5) Link to this comment
[…] I posted an article entitled Break my code, please, wherein I posted a very fragile piece of code, with the challenge to find ways in which to break […]