Upgrade to Pro — share decks privately, control downloads, hide ads and more …

less-code-20-min-wide-theorem_local.pdf

 less-code-20-min-wide-theorem_local.pdf

Where do simplicity and code meet? This presentation is an attempt to show why certain tools can be considered simple, minimalistic, or otherwise part of the less code culture.

Lucas Tolchinsky

July 18, 2018
Tweet

More Decks by Lucas Tolchinsky

Other Decks in Technology

Transcript

  1. assert-url 1 def test_foo 2 post "/foo" 3 4 assert_equal

    201, last_response.status 5 assert_equal "http://localhost:8080/foo/1", 6 last_response.location 7 end
  2. assert-url 1 include AssertUrl 2 3 def test_foo 4 post

    "/foo" 5 6 assert_equal 201, last_response.status 7 assert_path_equal "/foo/1", last_response.location 8 end
  3. assert-url 1 module AssertUrl 2 PARTS = %W[scheme host port

    path query fragment] 3 4 PARTS.each do |part| 5 error = const_set("#{part.capitalize}Error", Class.new(StandardError)) 6 7 define_method(:"assert_#{part}_equal") do |expected, value| 8 expected = normalize(part, expected) 9 value = urify(value).send(part.to_sym) 10 11 expected == value || (raise error, "expected #{expected}, got #{value}") 12 end 13 end 53 end
  4. assert-url 1 module AssertUrl 2 PARTS = %W[scheme host port

    path query fragment] 3 4 PARTS.each do |part| 5 error = const_set("#{part.capitalize}Error", Class.new(StandardError)) 6 7 define_method(:"assert_#{part}_equal") do |expected, value| 8 expected = normalize(part, expected) 9 value = urify(value).send(part.to_sym) 10 11 expected == value || (raise error, "expected #{expected}, got #{value}") 12 end 13 end 53 end
  5. assert-url 1 module AssertUrl 2 PARTS = %W[scheme host port

    path query fragment].each do |part| 3 const_set("#{part.capitalize}Error", Class.new(StandardError)) 4 end 5 6 def assert_scheme_equal(expected, value) 7 value = urify(value).scheme 8 9 expected.to_s == value || raises(SchemeError, expected, value) 10 end 11 12 def assert_host_equal(expected, value) 13 value = urify(value).host 14 15 expected == value || raises(HostError, expected, value) 16 end 137 end
  6. Devise 1 require "devise" 2 3 # In the model

    4 class User 5 devise :database_authenticatable 6 end 7 8 # In the controller 9 class ApplicationController < ActionController::Base 10 before_action :authenticate_user! 11 end
  7. Devise 1 require "devise" 2 3 # In the model

    4 class User 5 devise :database_authenticatable 6 end 7 8 # In the controller 9 class ApplicationController < ActionController::Base 10 before_action :authenticate_user! 11 end
  8. Devise 112 def self.define_helpers(mapping) #:nodoc: 113 mapping = mapping.name 114

    115 class_eval <<-METHODS, __FILE__, __LINE__ + 1 116 def authenticate_#{mapping}!(opts={}) 117 opts[:scope] = :#{mapping} 118 warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) 119 end 120 METHODS 121 end https://github.com/plataformatec/devise/blob/c9a2d0654e9fc1aaebe6f99ef6fbc55c55a91fdd/lib/devise/controllers/helpers.rb#L112 112 def self.define_helpers(mapping) #:nodoc: 113 mapping = mapping.name 114 115 class_eval <<-METHODS, __FILE__, __LINE__ + 1 116 def authenticate_#{mapping}!(opts={}) 117 opts[:scope] = :#{mapping} 118 warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) 119 end 120 METHODS 121 end
  9. Shield 1 # In the model 2 class User 3

    include Shield::Model 4 5 def self.fetch(username) 6 # Find user 7 end 8 end 9 10 # In the controller/routes 11 @user = User.authenticate("tonchis", "pass1234")
  10. Shield https://github.com/cyx/shield/blob/40e573a23f07aeb6d88adc2c36967ca67c5aa018/lib/shield.rb#L78-L84 78 def authenticate(username, password) 79 user = fetch(username)

    80 81 if user and is_valid_password?(user, password) 82 return user 83 end 84 end 78 def authenticate(username, password) 79 user = fetch(username) 80 81 if user and is_valid_password?(user, password) 82 return user 83 end 84 end
  11. > La perfection est atteinte, non pas lorsqu'il n'y a

    plus rien à ajouter, mais lorsqu'il n'y a plus rien à retirer. - Antoine de Saint-Exupéry
  12. Fat 1 require "fat" 2 3 hash = { 4

    "foo" => { 5 "bar" => { 6 "baz" => "value", 7 }, 8 }, 9 } 10 11 hash["foo"]["not"]["baz"] # undefined method [] for nil 12 13 Fat.at(hash, "foo", "not", "baz") # Fat::FatError: "foo.not" is nil 14 15 Fat.at(hash, "foo.bar.baz") # When keys are Strings. 16 17 Fat.at(hash, "foo:bar:baz") # When keys are Symbols. 1 require "fat" 2 3 hash = { 4 "foo" => { 5 "bar" => { 6 "baz" => "value", 7 }, 8 }, 9 } 10 11 hash["foo"]["not"]["baz"] # undefined method [] for nil 12 13 Fat.at(hash, "foo", "not", "baz") # Fat::FatError: "foo.not" is nil 14 15 Fat.at(hash, "foo.bar.baz") # When keys are Strings. 16 17 Fat.at(hash, "foo:bar:baz") # When keys are Symbols.
  13. Fat https://gitlab.com/tonchis/fat/blob/v1.0.0/ext/fat/fat.c#L63 63 static inline void parse_fields(VALUE args, VALUE *fields)

    { 64 if (RARRAY_LEN(args) == 1) { 65 *fields = rb_str_split(RARRAY_PTR(args)[0], "."); 66 67 if (RARRAY_LEN(*fields) == 1) { 68 VALUE split = rb_str_split(RARRAY_PTR(args)[0], ":"); 69 70 if (RARRAY_LEN(split) == 1) { 71 rb_raise(rb_eFatError, "Single argument expected..."); 72 } else { 73 for (long i = 0; i < RARRAY_LEN(split); i++) { 74 rb_ary_store(*fields, i, str_to_sym(RARRAY_AREF(split, i))); 75 } 76 } 77 } 78 } else { 79 *fields = args; 80 } 81 }
  14. Cuba 1 require "cuba" 2 3 # This block catches

    `GET /home` 4 Cuba.define do 5 on "home" do 6 on get do 7 res.write("I'm home!") 8 end 9 end 10 end
  15. Cuba 1 require "cuba" 2 3 # Which is the

    same as this one 4 Cuba.define do 5 on get do 6 on "home" do 7 res.write("I'm home!") 8 end 9 end 10 end
  16. Cuba 1 require "cuba" 2 3 # And the same

    as this other one 4 Cuba.define do 5 on get, "home" do 6 res.write("I'm home!") 7 end 8 end
  17. Cuba https://github.com/soveran/cuba/blob/1dce54d0926c5d6e5daab033a8e9685c6faa4227/lib/cuba.rb#L167 167 def on(*args, &block) 168 try do 169

    @captures = [] 170 171 return unless args.all? { |arg| match(arg) } 172 173 yield(*captures) 174 175 if res.status.nil? 176 if res.body.empty? 177 res.status = 404 178 else 179 res.headers[CONTENT_TYPE] ||= DEFAULT_CONTENT_TYPE 180 res.status = 200 181 end 182 end 183 184 halt(res.finish) 185 end 186 end 167 def on(*args, &block) 168 try do 169 @captures = [] 170 171 return unless args.all? { |arg| match(arg) } 172 173 yield(*captures) 174 175 if res.status.nil? 176 if res.body.empty? 177 res.status = 404 178 else 179 res.headers[CONTENT_TYPE] ||= DEFAULT_CONTENT_TYPE 180 res.status = 200 181 end 182 end 183 184 halt(res.finish) 185 end 186 end
  18. Syro 1 require "syro" 2 3 # This catches `GET

    /home` 4 Syro.new do 5 on "home" do 6 get do 7 res.write("I'm home!") 8 end 9 end 10 end
  19. Syro 1 require "syro" 2 3 # `GET /home` =>

    404 Not Found 4 Syro.new do 5 get do 6 on "home" do 7 res.write("I'm home!") 8 end 9 end 10 end
  20. Syro https://github.com/soveran/syro/blob/1.0.0/lib/syro.rb#L176-L182 176 def get 177 if root? && req.get?

    178 yield 179 180 halt(res.finish) 181 end 182 end 176 def get 177 if root? && req.get? 178 yield 179 180 halt(res.finish) 181 end 182 end
  21. less code • Optimize for readability • Make your solutions

    explicit • Avoid redundant interfaces