Style Guide for Object Design
Style Guide for Object Design
Minimum price
Suggested price
Style Guide for Object Design

This book is 60% complete

Last updated on 2018-11-30

About the Book

This book is right in the middle between learning how to write code and learning how to apply design principles and patterns. Using lots of code samples it teaches you the basics of object design. It provides you with simple rules to follow when defining your classes, methods, properties and interfaces.

After the fundamentals, this book provides you with a discussion of object design related topics that are worth investigating.

Note: you can find the full table of contents in the book sample (click on "Read Free Sample" above).

About the Author

Matthias Noback
Matthias Noback

Matthias Noback has been building web applications since 2003. He is the author of A Year With Symfony, Principles of Package Design and Microservices for everyone. He is a regular speaker at conferences and regularly posts on his blog. While always striving for better programming practices in general, he’s taken a special interest in application architecture, Domain-Driven Design, testing, and application integration patterns.

Table of Contents

    • Introduction
      • For who is this book?
      • Design rules for this book
      • Conventions
      • Overview of the contents
  • The lifecycle of an object
    • 1. Creating services
      • 1.1 Inject dependencies and configuration values as constructor arguments
      • 1.2 All constructor arguments should be required
      • 1.3 Only use constructor injection
      • 1.4 There’s no such thing as an optional dependency
      • 1.5 Inject what you need, not where you can get it from
      • 1.6 Make all dependencies explicit
        • Turn static dependencies into object dependencies
        • Turn complicated functions into object dependencies
        • Make system calls explicit
      • 1.7 Data relevant for the task should be passed as method arguments instead of constructor arguments
      • 1.8 Don’t allow the behavior of a service to change after it has been instantiated
      • 1.9 Do nothing inside a constructor, only assign properties
      • 1.10 Throw an exception when an argument is invalid
      • 1.11 Define services as an immutable object graph with only a few entry points
      • 1.12 Summary
    • 2. Creating other objects
      • 2.1 Require the minimum amount of data needed to behave consistently
      • 2.2 Require data that is meaningful
      • 2.3 Extract new objects to prevent domain invariants from being verified in multiple places
      • 2.4 Extract new objects to represent composite values
      • 2.5 Use assertions to validate constructor arguments
      • 2.6 Don’t inject dependencies, optionally pass them as method arguments
      • 2.7 Use named constructors
        • Create from primitive type values
        • Introduce a domain-specific concept
        • Explain why something went wrong
      • 2.8 Don’t put anything more into an object than it needs
      • 2.9 Don’t test constructors
      • 2.10 Summary
    • 3. Manipulating objects
      • 3.1 Prefer immutable objects
        • Replace values instead of modifying them
      • 3.2 A modifier on an immutable object should return a modified copy
      • 3.3 Modifier methods on immutable objects should have declarative names
      • 3.4 Compare whole objects
      • 3.5 When comparing immutable objects, assert equality, not sameness
      • 3.6 Calling a modifier method should always result in a valid object
      • 3.7 On a mutable object, modifier methods should be command methods
      • 3.8 A modifier method should verify that the requested state change is valid
      • 3.9 Use internally recorded events to verify changes on mutable objects
      • 3.10 Don’t use fluent interfaces on mutable objects
      • 3.11 Summary
  • Using objects
    • 4. A template for implementing methods
      • 4.1 Pre-condition checks
      • 4.2 Failure scenarios
      • 4.3 Happy path
      • 4.4 Post-condition checks
      • 4.5 Return value
      • 4.6 Some rules for exceptions
        • Use custom exception classes only if needed
        • Naming invalid argument or logic exception classes
        • Naming runtime exception classes
        • Use named constructors to indicate reasons for failure
        • Add detailed messages
      • 4.7 Summary
    • 5. Retrieving information
      • 5.1 Use query methods for information retrieval
      • 5.2 Query methods should have single-type return values
      • 5.3 Avoid query methods that expose internal state
      • 5.4 Objects should be either command or query objects
        • CQRS with domain events
        • CQRS with shared state
      • 5.5 Define specific methods and return types for the queries you want to make
      • 5.6 Define an abstraction for queries that cross system boundaries
      • 5.7 Use stubs for test doubles with query methods
      • 5.8 Query methods should use other query methods, no command methods
      • 5.9 Summary
  • Thanks
  • Notes

The Leanpub 45-day 100% Happiness Guarantee

Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Write and Publish on Leanpub

Authors, publishers and universities use Leanpub to publish amazing in-progress and completed books and courses, just like this one. You can use Leanpub to write, publish and sell your book or course as well! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.

Learn more about writing on Leanpub