DHI / DHI/python-package-development

Add section on inappropriate intimacy between classes

オープン
#35 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Jupyter Notebook
スター
8
フォーク
1
平均マージ
4分
マージ済み PR(30日)
1

説明

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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。