Skip to content

Add section on inappropriate intimacy between classes #35

Description

@ecomodeller

Classes should interact through public APIs, not by reaching into each other's private attributes.

Example from MIKE IO plotting refactor:

Before (inappropriate intimacy):

# Plotter reaches into geometry internals for subsettingvalues=values[self.da.geometry.top_elements]
geometry=self.da.geometry.geometry2d# Plotter uses private attribute to check for time axisifself.da._has_time_axis:
returnself.da.values[0]

After (using public API):

# Use sel/isel for subsettingda=da.sel(layers="top")
geometry=da.geometry# Use public dims propertyda=self.da.isel(time=0) if"time"inself.da.dimselseself.da

Key principles:

  • Never access private attributes (prefixed with _) of another class
  • Use public methods like sel, isel, and public properties like dims
  • If you need to access private state, that's a signal the class is missing a public API
  • Plotters should plot, not subset — keep data manipulation in the data layer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions