Skip to content

Instantly share code, notes, and snippets.

@justinko
Created February 26, 2012 01:35
Show Gist options
  • Save justinko/1912115 to your computer and use it in GitHub Desktop.
Save justinko/1912115 to your computer and use it in GitHub Desktop.
API Driven Design

Let's say I have a file that needs to be parsed and validated. Instead of jumping to tests, I want to first think about the objects involved and their messages.

I would then write out the above code.

After I'm satisfied with the objects and their interactions, I would then write a spec for FileObject or FileValidator#validate.

I consider this approach a technique above TDD. Before, when I used to jump straight into the specs, it would often result in working code, but bad design. TDD encourages you to think about design, but only in your head (which is obviously not as efficient as writing it out, not to mention the visual feedback).

NOTE: The very first thing I do is create a functional/high-level spec, so this approach only applies on the implementation level (units).

class FileManager
def self.validate_and_parse_file(file)
FileValidator.new(file).validate
FileParser.new(file).parse
end
end
FileManager.validate_and_parse_file(FileObject.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment