Designing Objects in Ruby
In this pack: sizing and rule strictness are set by the core
plugin's oo-design skill; each rule here keeps a core rule in this
language.
Overview
Ruby resolves a message when it is sent: a receiver answers from its
methods, or through method_missing, a module's methods run with the
host as self, and a Data value freezes only the value itself. This
skill applies the pack's core rules to plain Ruby, from 3.3 on, with
Minitest and no framework. It says which feature keeps each rule and
where Ruby stops helping: a contract written as a Minitest module, a
Data value that copies and freezes its array, an object each class
holds in place of a module, and messages defined with def or paired
with respond_to_missing?.
When to use
- Two or more classes play one role, and nothing in the code says what
each of them must answer.
- A value is a plain class with
attr_accessor, or a Data value that
hands out an array it holds.
- A module included by several classes reads or writes their instance
variables.
- Code checks
is_a?, kind_of?, instance_of? or a case over
classes before sending a message.
- A test reaches a private method with
send or reads a field with
instance_variable_get.
- A class answers messages only through
method_missing, and
respond_to? says false for them.
- An object forwards several messages to one it holds, and each
forwarding method is written by hand.
When not to use
- A framework builds or wires the objects, such as a web framework
whose models are table rows: outside this skill.
- The code is Python, which
oo-python covers.
- The question is one no Ruby feature changes, such as which object
owns a rule or where a system boundary runs: the core skills,
starting from
oo-design.
Core rules
Rule. Write each role's contract as one Minitest module of tests
that the test class of every implementation includes, because
nothing in Ruby declares the role, as
oo-honouring-substitutability rules 1 and 4,
oo-testing-object-oriented-code rule 1, and
oo-designing-interfaces rule 5 ask.
Here the module's name carries the role, and each implementation's
test class declares that its class plays it by including the module.
Minitest is a bundled gem, and a module included in a
Minitest::Test subclass has each test_ method run for that class.
Ruby enforces none of this: include adds methods and checks no
message, so a class that includes a module missing a method fails
only when the message is sent, and nothing makes an implementation
include the contract. The test run is the check.
Rule. Never ask which class an object is, with is_a?,
kind_of?, instance_of? or a case over classes, before sending
it a message, as oo-honouring-substitutability rule 6 asks. A
check puts the class into the caller: a new implementation must be
added to the check or it takes the wrong branch, and the caller
depends on the classes it lists rather than on the messages it
sends. oo-honouring-substitutability rule 6 says the check is the
visible sign that a subtype breaks its parent's contract, so the
fix belongs in the subtype or the hierarchy, never in the caller.
In Ruby, is_a? and kind_of? answer true for the class and every
ancestor and included module, instance_of? answers true only for
the exact class, and a case over classes uses Module#===, which
answers true for an instance of the class or its descendants.
Ruby enforces nothing here: the check answers at run time, and a
message an implementation lacks raises NoMethodError only when it
is sent. The contract test from rule 1 is what names the
implementation that cannot answer a role's message.
Default. Give a domain object readers only for what its callers
need, no attr_writer or attr_accessor, and change its state
through a method named for the domain action; a reader of a
collection answers a frozen copy, as
oo-encapsulating-state-and-behaviour rules 2 and 5 ask.
In Ruby, attr_writer
and attr_accessor define a name= method, so an assignment from
outside runs it; a class with only attr_reader raises
NoMethodError for that assignment. attr_reader answers the
object it holds, not a copy, so a caller that pushes to the array
it receives changes the object's state; freeze on a copy refuses
the caller's change with FrozenError. Freezing the array is not
enough for its elements: a string inside a frozen array still
changes in place unless the string was frozen too.
The exception is a value written with Data.define (rule 4): Ruby
gives it a reader for every member, and a reader of its collection
answers the frozen array the value holds, because the value has no
action that changes it and freeze refuses every caller's change.
Ruby raises NoMethodError for the missing writer and FrozenError
for the frozen array at run time; nothing enforces that a caller
needs only the readers the object offers, and instance_variable_set
still reaches the field (rule 6).
Default. Write a value with Data.define, check its rules in an
initialize that calls super, and freeze a copy of each
collection it holds, as oo-modeling-values-and-entities rules 2
and 3 and oo-constructing-valid-objects rule 3 ask.
In Ruby, Data.define gives the class a reader
for every member, freezes each instance, and answers equality and
hashing from all members; an assignment to a member raises
NoMethodError, and two instances with equal members answer true
to == and eql? and have the same hash. with builds a copy
through initialize, so a check in an overridden initialize that
calls super runs for the copy too. The freeze is shallow: an
array member still changes unless the constructor copied and froze
it, and its strings still change in place unless each string was
frozen.
The exception is a concept that must change while a caller holds
it: Ruby freezes every Data instance and gives it no setter, so
that concept is an entity written as a class, with a method that
owns the rule (rule 3), not a value.
Ruby enforces the freeze, the missing setters, the generated
equality and the call to initialize from with; the checks in
initialize run when the value is built, and nothing checks an
input the constructor never saw.
Default. Share behaviour across classes with an object each one
holds, forwarding to it with Forwardable, not with a module that
reads or writes the host's instance variables, as
oo-choosing-composition-over-inheritance rules 5 and 6 ask.
In Ruby, extend Forwardable makes
def_delegator and def_delegators define forwarding methods on
the class, each sending the named message to the held object; a
module's methods run with the host as self, so @field in the
module names the host's instance variable, and include gives
every host the same methods.
The exception is a module built from a message the host already
answers, such as Comparable, which answers <, <=, ==, >,
>=, between? and clamp from the host's <=>: it reads no
instance variable, so a host's fields cannot break it.
Ruby defines the forwarding methods at run time and starts no check
when a module is included; nothing stops a module reading a host's
field, and only the contract test from rule 1 checks that each held
object answers the role.
Rule. Never reach past an object's public messages from outside
it, tests included: no send to a private method, no
instance_variable_get or instance_variable_set; use
public_send where the message name is data, as
oo-encapsulating-state-and-behaviour rule 1,
oo-testing-object-oriented-code rule 1, and
oo-designing-for-testability rule 8 ask.
In Ruby, send calls a private method, and public_send raises
NoMethodError for one; instance_variable_get and
instance_variable_set read and write any instance variable, and a
private method called on another receiver raises NoMethodError,
while a call with self as its receiver reaches it. A test that
reaches in still passes, which is the danger: the test is tied to
one arrangement of the code.
Ruby refuses a private call on another receiver, but send,
instance_variable_get and instance_variable_set bypass that
refusal; nothing stops a test from using them, so only the rule and
review keep it out.
Default. Define each message an object answers with def, where
a reader and a search find it, and pair any method_missing with
respond_to_missing?, as oo-honouring-substitutability rule 4 and
oo-designing-interfaces rule 5 ask.
A message defined with def, or with define_method
when its name is computed as the class is built, is in the method
table: respond_to? answers true, methods lists it, and a reader
or a search finds it, so the contract module from rule 1 can name
it. method_missing is called by Ruby only when no method is
found; a message it answers is absent from methods and, without
respond_to_missing?, respond_to? answers false for it, so a
caller that asks first takes the wrong branch.
The exception is a proxy that must answer messages named by data,
such as one that forwards a patient's stored answers by field name:
define method_missing and respond_to_missing? from the same
list, and keep every message the proxy itself defines with def.
Ruby calls method_missing at run time and calls
respond_to_missing? from respond_to?; nothing makes the two
agree, so a caller that asks first can be told a message is absent
while sending it would answer.
Pattern
Formulary is the clinic's list of medicines a clinician may
prescribe: two formularies with the same medicines are the same
formulary, a value (oo-modeling-values-and-entities rule 1). The
first fence writes it as a plain class with attr_accessor, and the
second writes the same value with Data.define.
Before:
class Formulary
attr_accessor :medicines
def initialize(medicines)
@medicines = medicines
end
end
medicines = ["paracetamol", "amoxicillin"]
formulary = Formulary.new(medicines)
medicines << ""
formulary.medicines = []
formulary.medicines << ""
formulary.medicines << "amoxicillin"
formulary.medicines << "amoxicillin"
In the Before, attr_accessor gives the class a writer, so
formulary.medicines = [] replaces the value's contents (rule 4). The
constructor keeps the caller's array and the reader answers it, so
medicines << "" changes the value too (rule 4). Nothing refuses a
blank medicine or a duplicate (rule 4). The fence stores nothing: it
leaves out where a formulary is saved, under which identity, and in
which transaction.
After:
Formulary = Data.define(:medicines) do
def initialize(medicines:)
if medicines.empty?
raise ArgumentError, "a formulary names at least one medicine"
end
if medicines.any? { |medicine| medicine.strip.empty? }
raise ArgumentError, "a formulary names each medicine"
end
if medicines.uniq.length != medicines.length
raise ArgumentError, "a formulary lists a medicine once"
end
copies = medicines.sort.map { |medicine| medicine.dup.freeze }
super(medicines: copies.freeze)
end
end
formulary = Formulary.new(medicines: ["paracetamol", "amoxicillin"])
changed = formulary.with(medicines: ["paracetamol"])
begin
formulary.medicines = []
rescue NoMethodError
puts "a value has no setter"
end
begin
formulary.with(medicines: [])
rescue ArgumentError => error
puts error.message
end
In the After, Formulary is a Data value. Ruby freezes each
instance and gives it no setter, so the assignment raises
NoMethodError (rule 4). initialize refuses an empty list, a blank
medicine, and a duplicate, and raises ArgumentError with a message
that names the broken rule (rule 4). It orders the medicines,
duplicates and freezes each string, and freezes the array, so the
caller's later changes cannot reach the value; the reader answers the
frozen array the value holds (rule 4). with builds changed through
initialize, so the same checks run for the copy (rule 4). ==,
eql? and hash read the same member, so two formularies with the
same medicines, in any order, compare equal and hash alike (rule 4).
The fence stores nothing: it leaves out where
a formulary is saved, under which identity, and in which transaction.
Red flags
Places to look, not verdicts. A red flag becomes a finding only with
evidence: a measurement, a reproducible bug, or a failing test.
- A role played by two or more classes with no contract module that
both test classes include.
- A test that calls
send or instance_variable_get to reach a
private method or field.
is_a?, kind_of?, instance_of? or a case over classes before
a message.
- An
attr_accessor on a domain object.
- An
attr_reader that answers an array the object did not freeze.
- A
Data value that holds an array or a hash it did not copy and
freeze.
- A module that reads or writes its host's instance variables.
- A
method_missing with no respond_to_missing?.
- A value whose rules are checked in a factory or a caller instead of
its
initialize.
Forwardable forwarding the held object's whole interface.
Common mistakes
- Believing a
Data instance is deep-frozen because it is frozen.
- Believing
Data#with skips the overridden initialize.
- Believing
attr_reader answers a copy of an array.
- Believing
private stops send or instance_variable_get.
- Believing
respond_to? knows the messages method_missing answers.
- Adding
attr_accessor to quiet a caller that assigns a field.
- Putting a value's check in a factory, so
with skips it.
- Using
is_a? to choose behaviour, then adding a branch per class.
- Adding a public reader or a test-only method so a test can see
inside.
- Forwarding
@held.methods or every message of the held object by
habit.
Related skills
oo-design sizes a task and defines the rule labels.
oo-designing-interfaces shapes a role around its caller and names
the types that play it.
oo-honouring-substitutability says what every implementation of a
role must keep.
oo-testing-object-oriented-code says which messages a test may send
and what it may check.
oo-modeling-values-and-entities decides whether a concept is a
value or an entity, and what equality reads.
oo-constructing-valid-objects makes a constructor refuse broken
input.
oo-encapsulating-state-and-behaviour keeps an invariant with the
state it guards.
oo-choosing-composition-over-inheritance decides when to hold an
object instead of extending a class.
oo-designing-for-testability keeps a test out of a class's inside.
oo-python shows how the same rules read in Python.
oo-javascript shows how the same rules read in a language with no
type checker.
Further reading
1---2name: oo-ruby3description: Use when plain Ruby with no framework leans on duck typing, mixes in a module, defines a value with Data, or reaches for method_missing or send.4license: MIT5metadata:6 source: https://skillmd.com/skills/yutna/oo-ruby7 version: 0.21.08 pack: object-oriented-design-skills9---1011# Designing Objects in Ruby1213**In this pack:** sizing and rule strictness are set by the core14plugin's `oo-design` skill; each rule here keeps a core rule in this15language.1617## Overview1819Ruby resolves a message when it is sent: a receiver answers from its20methods, or through `method_missing`, a module's methods run with the21host as `self`, and a `Data` value freezes only the value itself. This22skill applies the pack's core rules to plain Ruby, from 3.3 on, with23Minitest and no framework. It says which feature keeps each rule and24where Ruby stops helping: a contract written as a Minitest module, a25`Data` value that copies and freezes its array, an object each class26holds in place of a module, and messages defined with `def` or paired27with `respond_to_missing?`.2829## When to use3031- Two or more classes play one role, and nothing in the code says what32 each of them must answer.33- A value is a plain class with `attr_accessor`, or a `Data` value that34 hands out an array it holds.35- A module included by several classes reads or writes their instance36 variables.37- Code checks `is_a?`, `kind_of?`, `instance_of?` or a `case` over38 classes before sending a message.39- A test reaches a private method with `send` or reads a field with40 `instance_variable_get`.41- A class answers messages only through `method_missing`, and42 `respond_to?` says false for them.43- An object forwards several messages to one it holds, and each44 forwarding method is written by hand.4546## When not to use4748- A framework builds or wires the objects, such as a web framework49 whose models are table rows: outside this skill.50- The code is Python, which `oo-python` covers.51- The question is one no Ruby feature changes, such as which object52 owns a rule or where a system boundary runs: the core skills,53 starting from `oo-design`.5455## Core rules56571. Rule. **Write each role's contract as one Minitest module of tests58 that the test class of every implementation includes, because59 nothing in Ruby declares the role, as60 `oo-honouring-substitutability` rules 1 and 4,61 `oo-testing-object-oriented-code` rule 1, and62 `oo-designing-interfaces` rule 5 ask.**63 Here the module's name carries the role, and each implementation's64 test class declares that its class plays it by including the module.65 Minitest is a bundled gem, and a module included in a66 `Minitest::Test` subclass has each `test_` method run for that class.6768 Ruby enforces none of this: `include` adds methods and checks no69 message, so a class that includes a module missing a method fails70 only when the message is sent, and nothing makes an implementation71 include the contract. The test run is the check.72732. Rule. **Never ask which class an object is, with `is_a?`,74 `kind_of?`, `instance_of?` or a `case` over classes, before sending75 it a message, as `oo-honouring-substitutability` rule 6 asks.** A76 check puts the class into the caller: a new implementation must be77 added to the check or it takes the wrong branch, and the caller78 depends on the classes it lists rather than on the messages it79 sends. `oo-honouring-substitutability` rule 6 says the check is the80 visible sign that a subtype breaks its parent's contract, so the81 fix belongs in the subtype or the hierarchy, never in the caller.82 In Ruby, `is_a?` and `kind_of?` answer true for the class and every83 ancestor and included module, `instance_of?` answers true only for84 the exact class, and a `case` over classes uses `Module#===`, which85 answers true for an instance of the class or its descendants.8687 Ruby enforces nothing here: the check answers at run time, and a88 message an implementation lacks raises `NoMethodError` only when it89 is sent. The contract test from rule 1 is what names the90 implementation that cannot answer a role's message.91923. Default. **Give a domain object readers only for what its callers93 need, no `attr_writer` or `attr_accessor`, and change its state94 through a method named for the domain action; a reader of a95 collection answers a frozen copy, as96 `oo-encapsulating-state-and-behaviour` rules 2 and 5 ask.**97 In Ruby, `attr_writer`98 and `attr_accessor` define a `name=` method, so an assignment from99 outside runs it; a class with only `attr_reader` raises100 `NoMethodError` for that assignment. `attr_reader` answers the101 object it holds, not a copy, so a caller that pushes to the array102 it receives changes the object's state; `freeze` on a copy refuses103 the caller's change with `FrozenError`. Freezing the array is not104 enough for its elements: a string inside a frozen array still105 changes in place unless the string was frozen too.106107 The exception is a value written with `Data.define` (rule 4): Ruby108 gives it a reader for every member, and a reader of its collection109 answers the frozen array the value holds, because the value has no110 action that changes it and `freeze` refuses every caller's change.111112 Ruby raises `NoMethodError` for the missing writer and `FrozenError`113 for the frozen array at run time; nothing enforces that a caller114 needs only the readers the object offers, and `instance_variable_set`115 still reaches the field (rule 6).1161174. Default. **Write a value with `Data.define`, check its rules in an118 `initialize` that calls `super`, and freeze a copy of each119 collection it holds, as `oo-modeling-values-and-entities` rules 2120 and 3 and `oo-constructing-valid-objects` rule 3 ask.**121 In Ruby, `Data.define` gives the class a reader122 for every member, freezes each instance, and answers equality and123 hashing from all members; an assignment to a member raises124 `NoMethodError`, and two instances with equal members answer true125 to `==` and `eql?` and have the same `hash`. `with` builds a copy126 through `initialize`, so a check in an overridden `initialize` that127 calls `super` runs for the copy too. The freeze is shallow: an128 array member still changes unless the constructor copied and froze129 it, and its strings still change in place unless each string was130 frozen.131132 The exception is a concept that must change while a caller holds133 it: Ruby freezes every `Data` instance and gives it no setter, so134 that concept is an entity written as a class, with a method that135 owns the rule (rule 3), not a value.136137 Ruby enforces the freeze, the missing setters, the generated138 equality and the call to `initialize` from `with`; the checks in139 `initialize` run when the value is built, and nothing checks an140 input the constructor never saw.1411425. Default. **Share behaviour across classes with an object each one143 holds, forwarding to it with `Forwardable`, not with a module that144 reads or writes the host's instance variables, as145 `oo-choosing-composition-over-inheritance` rules 5 and 6 ask.**146 In Ruby, `extend Forwardable` makes147 `def_delegator` and `def_delegators` define forwarding methods on148 the class, each sending the named message to the held object; a149 module's methods run with the host as `self`, so `@field` in the150 module names the host's instance variable, and `include` gives151 every host the same methods.152153 The exception is a module built from a message the host already154 answers, such as `Comparable`, which answers `<`, `<=`, `==`, `>`,155 `>=`, `between?` and `clamp` from the host's `<=>`: it reads no156 instance variable, so a host's fields cannot break it.157158 Ruby defines the forwarding methods at run time and starts no check159 when a module is included; nothing stops a module reading a host's160 field, and only the contract test from rule 1 checks that each held161 object answers the role.1621636. Rule. **Never reach past an object's public messages from outside164 it, tests included: no `send` to a private method, no165 `instance_variable_get` or `instance_variable_set`; use166 `public_send` where the message name is data, as167 `oo-encapsulating-state-and-behaviour` rule 1,168 `oo-testing-object-oriented-code` rule 1, and169 `oo-designing-for-testability` rule 8 ask.**170 In Ruby, `send` calls a private method, and `public_send` raises171 `NoMethodError` for one; `instance_variable_get` and172 `instance_variable_set` read and write any instance variable, and a173 private method called on another receiver raises `NoMethodError`,174 while a call with `self` as its receiver reaches it. A test that175 reaches in still passes, which is the danger: the test is tied to176 one arrangement of the code.177178 Ruby refuses a private call on another receiver, but `send`,179 `instance_variable_get` and `instance_variable_set` bypass that180 refusal; nothing stops a test from using them, so only the rule and181 review keep it out.1821837. Default. **Define each message an object answers with `def`, where184 a reader and a search find it, and pair any `method_missing` with185 `respond_to_missing?`, as `oo-honouring-substitutability` rule 4 and186 `oo-designing-interfaces` rule 5 ask.**187 A message defined with `def`, or with `define_method`188 when its name is computed as the class is built, is in the method189 table: `respond_to?` answers true, `methods` lists it, and a reader190 or a search finds it, so the contract module from rule 1 can name191 it. `method_missing` is called by Ruby only when no method is192 found; a message it answers is absent from `methods` and, without193 `respond_to_missing?`, `respond_to?` answers false for it, so a194 caller that asks first takes the wrong branch.195196 The exception is a proxy that must answer messages named by data,197 such as one that forwards a patient's stored answers by field name:198 define `method_missing` and `respond_to_missing?` from the same199 list, and keep every message the proxy itself defines with `def`.200201 Ruby calls `method_missing` at run time and calls202 `respond_to_missing?` from `respond_to?`; nothing makes the two203 agree, so a caller that asks first can be told a message is absent204 while sending it would answer.205206## Pattern207208`Formulary` is the clinic's list of medicines a clinician may209prescribe: two formularies with the same medicines are the same210formulary, a value (`oo-modeling-values-and-entities` rule 1). The211first fence writes it as a plain class with `attr_accessor`, and the212second writes the same value with `Data.define`.213214Before:215216```ruby217class Formulary218 attr_accessor :medicines219220 def initialize(medicines)221 @medicines = medicines222 end223end224225medicines = ["paracetamol", "amoxicillin"]226formulary = Formulary.new(medicines)227medicines << ""228formulary.medicines = []229formulary.medicines << ""230formulary.medicines << "amoxicillin"231formulary.medicines << "amoxicillin"232```233234In the Before, `attr_accessor` gives the class a writer, so235`formulary.medicines = []` replaces the value's contents (rule 4). The236constructor keeps the caller's array and the reader answers it, so237`medicines << ""` changes the value too (rule 4). Nothing refuses a238blank medicine or a duplicate (rule 4). The fence stores nothing: it239leaves out where a formulary is saved, under which identity, and in240which transaction.241242After:243244```ruby245Formulary = Data.define(:medicines) do246 def initialize(medicines:)247 if medicines.empty?248 raise ArgumentError, "a formulary names at least one medicine"249 end250 if medicines.any? { |medicine| medicine.strip.empty? }251 raise ArgumentError, "a formulary names each medicine"252 end253 if medicines.uniq.length != medicines.length254 raise ArgumentError, "a formulary lists a medicine once"255 end256 copies = medicines.sort.map { |medicine| medicine.dup.freeze }257 super(medicines: copies.freeze)258 end259end260261formulary = Formulary.new(medicines: ["paracetamol", "amoxicillin"])262changed = formulary.with(medicines: ["paracetamol"])263264begin265 formulary.medicines = []266rescue NoMethodError267 puts "a value has no setter"268end269270begin271 formulary.with(medicines: [])272rescue ArgumentError => error273 puts error.message274end275```276277In the After, `Formulary` is a `Data` value. Ruby freezes each278instance and gives it no setter, so the assignment raises279`NoMethodError` (rule 4). `initialize` refuses an empty list, a blank280medicine, and a duplicate, and raises `ArgumentError` with a message281that names the broken rule (rule 4). It orders the medicines,282duplicates and freezes each string, and freezes the array, so the283caller's later changes cannot reach the value; the reader answers the284frozen array the value holds (rule 4). `with` builds `changed` through285`initialize`, so the same checks run for the copy (rule 4). `==`,286`eql?` and `hash` read the same member, so two formularies with the287same medicines, in any order, compare equal and hash alike (rule 4).288The fence stores nothing: it leaves out where289a formulary is saved, under which identity, and in which transaction.290291## Red flags292293Places to look, not verdicts. A red flag becomes a finding only with294evidence: a measurement, a reproducible bug, or a failing test.295296- A role played by two or more classes with no contract module that297 both test classes include.298- A test that calls `send` or `instance_variable_get` to reach a299 private method or field.300- `is_a?`, `kind_of?`, `instance_of?` or a `case` over classes before301 a message.302- An `attr_accessor` on a domain object.303- An `attr_reader` that answers an array the object did not freeze.304- A `Data` value that holds an array or a hash it did not copy and305 freeze.306- A module that reads or writes its host's instance variables.307- A `method_missing` with no `respond_to_missing?`.308- A value whose rules are checked in a factory or a caller instead of309 its `initialize`.310- `Forwardable` forwarding the held object's whole interface.311312## Common mistakes313314- Believing a `Data` instance is deep-frozen because it is frozen.315- Believing `Data#with` skips the overridden `initialize`.316- Believing `attr_reader` answers a copy of an array.317- Believing `private` stops `send` or `instance_variable_get`.318- Believing `respond_to?` knows the messages `method_missing` answers.319- Adding `attr_accessor` to quiet a caller that assigns a field.320- Putting a value's check in a factory, so `with` skips it.321- Using `is_a?` to choose behaviour, then adding a branch per class.322- Adding a public reader or a test-only method so a test can see323 inside.324- Forwarding `@held.methods` or every message of the held object by325 habit.326327## Related skills328329- `oo-design` sizes a task and defines the rule labels.330- `oo-designing-interfaces` shapes a role around its caller and names331 the types that play it.332- `oo-honouring-substitutability` says what every implementation of a333 role must keep.334- `oo-testing-object-oriented-code` says which messages a test may send335 and what it may check.336- `oo-modeling-values-and-entities` decides whether a concept is a337 value or an entity, and what equality reads.338- `oo-constructing-valid-objects` makes a constructor refuse broken339 input.340- `oo-encapsulating-state-and-behaviour` keeps an invariant with the341 state it guards.342- `oo-choosing-composition-over-inheritance` decides when to hold an343 object instead of extending a class.344- `oo-designing-for-testability` keeps a test out of a class's inside.345- `oo-python` shows how the same rules read in Python.346- `oo-javascript` shows how the same rules read in a language with no347 type checker.348349## Further reading350351- [Mixins and contracts](references/mixins-and-contracts.md)