Applying Postel's Law in API Design

Michael Feathers has a great article on The Universality of Postel's Law

It’s like many other systems principles - once you internalize it you can use it in design. It becomes another tool in the toolbox. -- Michael Feathers

Following is the code from aws-sdk for Ruby. It violates Postel's Law:

module Aws
  module Plugins
    # @api private
    class S3BucketNameRestrictions < Seahorse::Client::Plugin
      class Handler < Seahorse::Client::Handler
        def call(context)
          if context.params.key?(:bucket) && context.params[:bucket].include?('/')
            msg = ":bucket option must not contain a forward-slash (/)"
            raise ArgumentError, msg
          end
          @handler.call(context)
        end
      end
      handler(Handler)
    end
  end
end

Here is the version that fixes this problem:

module Aws
  module Plugins
    # @api private
    class S3BucketNameRestrictions < Seahorse::Client::Plugin
      class Handler < Seahorse::Client::Handler
        def call(context)
          if context.params.key?(:bucket) && context.params[:bucket].include?('/')
            context.params[:bucket] = URI.parse(context.params[:bucket]).host
          end
          @handler.call(context)
        end
      end
      handler(Handler)
    end
  end
end

The number of lines of code has not increased. It is liberal in what it accepts and is developer friendly.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.