Focus On Behavior, Not State, for a More Maintainable Codebase

How we moved from focusing on state (what an object is) towards behavior (what an object does) while building our Purchase Options APIs as a way to create a more maintainable codebase.

John DeWyze
11 min readintermediate
--
View Original

Overview

The article discusses the importance of focusing on behavior rather than state in software development to create a more maintainable codebase. It highlights the challenges faced while implementing new features at Shopify and presents solutions that emphasize encapsulation and abstraction to prevent code complexity.

What You'll Learn

1

How to refactor code to focus on behavior rather than state

2

Why encapsulation is crucial for maintaining clean code

3

When to implement a translation layer for data reporting

Prerequisites & Requirements

  • Understanding of object-oriented programming principles
  • Experience with Ruby on Rails or similar frameworks(optional)

Key Questions Answered

What is the main focus for creating a maintainable codebase?
The article emphasizes focusing on behavior rather than state when writing code. This approach helps in creating a more maintainable codebase by reducing complexity and preventing the leakage of internal logic across different components.
How does encapsulation improve code maintainability?
Encapsulation improves code maintainability by bundling methods that operate on data with the data itself, which prevents external components from depending on the internal structure of objects. This leads to cleaner, more modular code.
When should a translation layer be implemented in code?
A translation layer should be implemented when there is a need to insulate data teams from changes in the underlying schema. This allows for easier reporting and prevents the need for constant adjustments in the codebase due to evolving data requirements.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Backend
Ruby On Rails
Used for building the Shopify platform and implementing the discussed features.
Tools
Packwerk
A Ruby gem used to enforce boundaries and modularize Rails applications.

Key Actionable Insights

1
Refactor existing code to prioritize behavior over state by creating wrapper classes that encapsulate logic.
This approach helps in managing complexity and allows for easier modifications in the future, as changes to the internal logic won't affect other parts of the codebase.
2
Implement a translation layer to facilitate reporting needs without exposing internal data structures.
This ensures that data teams can access meaningful insights without being affected by changes in the codebase, thus maintaining the integrity of both the application and the reporting systems.
3
Use encapsulation to limit the exposure of internal logic in your code.
By ensuring that components only interact with necessary methods, you can reduce dependencies and improve the overall maintainability of your code.

Common Pitfalls

1
Tightly coupling different domain concepts can lead to complex and unmanageable code.
This often happens when developers do not consider the future scalability of their code. To avoid this, focus on creating abstractions that separate concerns and allow for easier modifications.

Related Concepts

Encapsulation
Abstraction
Object-oriented Programming
Code Maintainability