DHI / DHI/python-package-development

Add section on inappropriate intimacy between classes

Open
#35 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
8
Forks
1
Avg merge
4m
Merged PRs (30d)
1

Description

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

**Example from MIKE IO plotting refactor:**

Before (inappropriate intimacy):
```python
# Plotter reaches into geometry internals for subsetting
values = values[self.da.geometry.top_elements]
geometry = self.da.geometry.geometry2d

# Plotter uses private attribute to check for time axis
if self.da._has_time_axis:
return self.da.values[0]
```

After (using public API):
```python
# Use sel/isel for subsetting
da = da.sel(layers="top")
geometry = da.geometry

# Use public dims property
da = self.da.isel(time=0) if "time" in self.da.dims else self.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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.