Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created May 13, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/a6499bd99844d2be3bc5 to your computer and use it in GitHub Desktop.
Save jeremyf/a6499bd99844d2be3bc5 to your computer and use it in GitHub Desktop.
Excerpt from Clean Code

Advice regarding mixing data structures and OO.

Procedural vs. OO

Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.

The compliment is also true:

Procedural code makes it hard to add new data structures because all the functions must change. OO code makes it hard to add new functions because all the classes must change.

"Clean Code: A Handbook of Agile Software Craftsmanship" by Robert Martin(p101)

The Active Record Pattern

Active Records are special forms of DTOs [Data Transfer Object]. They are data structures with public variables; but they typically have navigational methods like save and find. Typically these Active Records are direct translations from database tables, or other data sources.

Unfortunately we often find that developers treat try to treat these data structures as though they were objects by putting business rule methods in them. This is awkward because it creates a hybrid between a data structure and an object.

The solution, of course, is to treat the ActiveRecord as a data structure and to create separate objects that contain the business rules and that hide their internal data (which are probably just instances of the ActiveRecord).

"Clean Code: A Handbook of Agile Software Craftsmanship" by Robert Martin(p101)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment