Modular – Programming – Extract it out...



Modular – Programming – Extract it out...

0 0


modular-prog


On Github ektaverma1 / modular-prog

Modular

Programming

Extract it out...

Ruby Sync up | Ankit Gupta / @ankit8898

Talk about

  • Why to do Modular programming?
  • Serving Modules as Code API
  • Cleaner presentable, maintable code
  • Modularization in testing
  • Be fast with latest
  • Questions

Wikipedia Says

Modular programming (also called "top-down design" and "stepwise refinement") is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.[1] Conceptually, modules represent a separation of concerns, and improve maintainability

Why to do Modular programming?

Keep your code, yourself and team Happy :)

The Dirty One

#A dirty one knows too much.
class Bank < ActiveRecord::Base
     
  def i_give_most_valued_customers
    #it will have business logic for valued customers
  end

  def i_give_employee_performance 
    #It will have business Logic for getting employee performance
  end
end

I feel good !

#A dirty one knows too much.
class Bank < ActiveRecord::Base
     
  include IGiveMostValuedCustomers
  include IGiveEmployeePerformance

  
end

Example Here

Serving Modules as Code API

Let classes talk to Modules

A Dirty One

#A dirty one knows too much.
class PurchasesController < ApplicationController
   
  before :i_will_do_x
  before :i_will_do_y
  before :i_will_do_z

end

class AcknowledgementsController < ApplicationController
   
  before :i_will_do_y
  before :i_will_do_z

end

I feel good !

class PurchasesController < ApplicationController
 
 ask_manager(controller_name)   

end

class AcknowledgementsController < ApplicationController
   
  ask_manager(controller_name)

end
module Manager
   
  def i_am_for_purchases
    before :i_will_do_x
    before :i_will_do_y
    before :i_will_do_z
  end

  def i_am_for_acknowledgements
     before :i_will_do_y
     before :i_will_do_z
  end

  def ask_manager(controller)
    send("i_am_for_#{controller}") 
  end

end

Example Here

Cleaner presentable, maintable code

Comments, valid method names & Examples

Example AR - Rails Example AM - Rails RedE Code

Modularization in testing

Extract out common methods - helpers - Fixtures in Modules

Plug in your Modules

RSpec.configure do |config|
    config.include MockHistoricalData::CaseA
    config.include MockHistoricalData::CaseB
    config.include LocaleSwitcher
  end
Example 1 - Mock Data & Usage Example 2 - Locale Switch & Usage

Be fast with latest

  • Handle Multiple Processes by Procfile and foreman
web: bundle exec rails server
sidekiq: bundle exec sidekiq -L log/sidekiq.log
redis: redis-server
Have Faster tests with Zeus Search for testing frameworks with Popular libraries. Eg shoulda-callbacks-matchers & rspec-sidekiq

Questions