Skip to content

Commit cd7f927

Browse files
Add anti patterns sectiAdd “Anti-Patterns” section to README with explanations (#256)on (#459)
* added default return value in get_localizer to prevent KeyError * cleaned up docstring formatting and removed unnecessary asterisks * Add Anti-Patterns section explaining discouraged design patterns
1 parent 5110def commit cd7f927

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,27 @@ You can also run `flake8` or `pytest` commands manually. Examples can be found i
120120
## Contributing via issue triage [![Open Source Helpers](https://www.codetriage.com/faif/python-patterns/badges/users.svg)](https://www.codetriage.com/faif/python-patterns)
121121

122122
You can triage issues and pull requests which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to [subscribe to python-patterns on CodeTriage](https://www.codetriage.com/faif/python-patterns).
123+
124+
125+
## 🚫 Anti-Patterns
126+
127+
This section lists some common design patterns that are **not recommended** in Python and explains why.
128+
129+
### 🧱 Singleton
130+
**Why not:**
131+
- Python modules are already singletons — every module is imported only once.
132+
- Explicit singleton classes add unnecessary complexity.
133+
- Better alternatives: use module-level variables or dependency injection.
134+
135+
### 🌀 God Object
136+
**Why not:**
137+
- Centralizes too much logic in a single class.
138+
- Makes code harder to test and maintain.
139+
- Better alternative: split functionality into smaller, cohesive classes.
140+
141+
### 🔁 Inheritance overuse
142+
**Why not:**
143+
- Deep inheritance trees make code brittle.
144+
- Prefer composition and delegation.
145+
- “Favor composition over inheritance.”
146+

patterns/creational/builder.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""
2-
*What is this pattern about?
2+
What is this pattern about?
33
It decouples the creation of a complex object and its representation,
44
so that the same process can be reused to build objects from the same
55
family.
66
This is useful when you must separate the specification of an object
77
from its actual representation (generally for abstraction).
88
9-
*What does this example do?
10-
9+
What does this example do?
1110
The first example achieves this by using an abstract base
1211
class for a building, where the initializer (__init__ method) specifies the
1312
steps needed, and the concrete subclasses implement these steps.
@@ -22,16 +21,15 @@ class for a building, where the initializer (__init__ method) specifies the
2221
In general, in Python this won't be necessary, but a second example showing
2322
this kind of arrangement is also included.
2423
25-
*Where is the pattern used practically?
26-
27-
*References:
28-
https://sourcemaking.com/design_patterns/builder
24+
Where is the pattern used practically?
25+
See: https://sourcemaking.com/design_patterns/builder
2926
30-
*TL;DR
27+
TL;DR
3128
Decouples the creation of a complex object and its representation.
3229
"""
3330

3431

32+
3533
# Abstract Building
3634
class Building:
3735
def __init__(self) -> None:

patterns/creational/factory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def get_localizer(language: str = "English") -> Localizer:
5454
"Greek": GreekLocalizer,
5555
}
5656

57-
return localizers[language]()
57+
return localizers.get(language, EnglishLocalizer)()
58+
5859

5960

6061
def main():

0 commit comments

Comments
 (0)