Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Beginner's guide to Complex Data Types

A Beginner's guide to Complex Data Types

An introduction on how to spot composite types hiding in your code and ways to model and persist them.

Denis Brumann

February 07, 2017
Tweet

More Decks by Denis Brumann

Other Decks in Programming

Transcript

  1. What am I talking about? Latitude and longitude
 form a

    unit If one is missing or invalid the coordinates are invalid
  2. Definition In computer science, a composite data type or compound

    data type is any data type which can be constructed in a program using the programming language's primitive data types and other composite types. — https://en.wikipedia.org/wiki/Composite_data_type
  3. Typical cases Money Coordinates Address Recurring cycles involving e.g. a

    combined year + month, like 2017/02, form a term (e.g. for reports) An entity’s ID combined with another property
 (e.g. a revision number or date) forming composite ID
  4. How to spot complex types? Long list of properties or

    large number of constructor arguments
  5. How to spot complex types? Mismatch between database and code

    can be an indicator; having to combine primitive types before fetch/insert
  6. How to spot complex types? Conditionals involving multiple primitive types

    Bonus points for when (InvalidArgument)Exceptions are involved
  7. What not to do with them? Don’t incorporate them in

    other entities* Don’t duplicate the logic in your code Don’t spread the logic * it depends
 — Don’t rip apart your entities just for the sake of having smaller, coherent types
  8. PHP 7 is your friend Return types will cause a

    (catchable) TypeError to be thrown when value was not set before
  9. Database mapping Advanced field value conversion using custom mapping types

    http://docs.doctrine-project.org/ projects/doctrine-orm/en/latest/ cookbook/advanced-field-value- Separating concerns using Embeddables http://docs.doctrine-project.org/ projects/doctrine-orm/en/latest/ tutorials/embeddables.html
  10. Embeddables Declare complex type as Embeddable and map properties to

    database fields Use @Embedded in your Entity