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


Create your own user feedback survey