Skip to content

Instantly share code, notes, and snippets.

View benkoshy's full-sized avatar

Ben Koshy benkoshy

View GitHub Profile
@benkoshy
benkoshy / gun_kata.md
Last active April 19, 2017 08:56
A Gun Kata - problem on code review
class Gun
  attr_reader :smart_ammo

  def initialize(ammo)    
    @smart_ammo = SmartAmmo.at(ammo)
  end

  # how it sounds from start to finish
  def sound_of_the_whole_nine_yards
@benkoshy
benkoshy / machine-gun-kata-originalproblemCode.md
Created April 11, 2017 01:54
Machine gun Kata - original code
    module MG_tools
      Rof = {:sustained => rand(3..9), :rapid => rand(5..9), :cyclic => 
    rand(1..200), :cease => 0}
    end

 class M249 
@benkoshy
benkoshy / DependOnBehaviourNotData-p1.md
Last active April 12, 2017 23:48
Send messages, and don't worry about knowing how to access data - P1
    class Program
    {
        static void Main(string[] args)
        {
            Account freddie = new Account(5000000);

            // freddie buys a grand piano
            freddie.balance = freddie.balance - 500000m;
@benkoshy
benkoshy / DependOnBehaviourNotData-p2.md
Created April 12, 2017 23:55
Depend on behaviour not data - All the changes that would have to be made if something changed.
    /// One GBP sterling is worth 1.25 USD.
    /// We'd have to change the code to reflect this.
    class Program
    {
        static void Main(string[] args)
        {
            Account freddie = new Account(5000000);
@benkoshy
benkoshy / DependOnBehaviourNotData-p3.md
Created April 13, 2017 00:03
Depend on Behaviour not data: Here we make the change only once, but it is reflected everywhere. It's much easier to maintain.
/// Notice how similar it is to the first example? 
/// The code is only changed in one place
/// and the change is propogated everywhere else

    class Program
    {
        static void Main(string[] args)
 {
@benkoshy
benkoshy / person_class_restructured.md
Created April 13, 2017 11:08
Restructuring the person class - code review
## Apologies for pasting here but Stack overflow formatting is simply terrible.

class Person
    	attr_reader :first_name, :last_name, :separator
        	def initialize(first_name = nil, last_name = nil, separator = nil)
        		@first_name = set_first_name(first_name)
        		@last_name = set_last_name(last_name)
        		@separator = set_separator(separator)
@benkoshy
benkoshy / depend_on_behaviour_not_data-p1.md
Last active April 14, 2017 01:02
Hide data structures and send messages
# consider this person class:

class Person
	attr_reader :data
	def initialize(data)
		@data = data
	end
end
@benkoshy
benkoshy / DependOnBehaviourNotData-p3.md
Created April 14, 2017 01:03
Depend on behaviour not data - blog post - part 2
# let's count the changes: 6 changes were made
# because we changed the data structure

p = Person.new({age: 20, weight: 30})
age = p.data[:age]
weight = p.data[:weight]
p1 = Person.new({age: 54, weight: 144})
p1_age = p1.data[:age]
p1_weight = p1.data[:weight]
@benkoshy
benkoshy / DependOnBehaviourNotData-p4.md
Last active April 14, 2017 01:15
Example where data structure is hidden
# consider this person class:

class Person
	attr_reader :age, :weight
	def initialize(data)
		@age = data[0]
    @weight = data[1]
	end
end
@benkoshy
benkoshy / DependOnBehaviourNotData-p5.md
Created April 14, 2017 01:18
Showing how many changes required if now using a hash
# consider the situation where the boss
# required you to use a hash.
# notice how many changes are made:
# just two. This means the code is
# much easier to maintain and easier
# to subsequently change. Because you will
# only be making those changes in a few places
# vs changing everything everywhere!