Refactor Boolean Flag Parameters to Options Pattern
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 17h 7m
- Merged PRs (30d)
- 358
Description
## Problem
Currently, several functions in our codebase use multiple boolean flags as parameters. This can lead to:
- Reduced code readability
- Difficulty understanding function calls at usage sites
- Hard-to-maintain function signatures as more options are added
## Proposal
Replace boolean flag parameters with a more maintainable options pattern using Python dictionary/dataclass parameters. For example, transform functions like:
```python
def get_compute_kernels(user_id, load_session: bool = True, load_user: bool = True, load_image: bool = True):
# implementation
...
```
Into either a dictionary-based approach:
```python
def get_compute_kernels(user_id, options: Optional[dict] = None):
default_options = {
"load_session": True,
"load_user": True,
"load_image": True
}
opts = options or default_options
...
```
Or preferably, a more type-safe dataclass approach:
```python
from dataclasses import dataclass, field
@dataclass
class KernelLoadingOptions:
load_session: bool = True
load_user: bool = True
load_image: bool = True
def process_data(data: list, options: Optional[KernelLoadingOptions] = None):
opts = options or KernelLoadingOptions()
...
```
## Benefits
- Improved readability at call sites
- Self-documenting parameter names
- Easier to add new options without breaking changes
## Questions
- Should we use dataclasses or dictionaries as the standard approach?\* Consider using `pydantic`
- What deprecation period should we use?
- Should we create shared option classes for common patterns?
- Do we want to enforce this pattern via flake8/pylint rules?
JIRA Issue: BA-42
Contributor guide
Assessment
This issue has not been assessed yet.