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


Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now