Skip to content

Commit 155cb0d

Browse files
committed
Address functions doing one thing first
1 parent 14d22bb commit 155cb0d

File tree

1 file changed

+87
-87
lines changed

1 file changed

+87
-87
lines changed

README.md

Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,93 @@ def create_micro_brewery(name: str = "Hipster Brew Co."):
282282

283283
## **Functions**
284284

285+
### Functions should do one thing
286+
287+
This is by far the most important rule in software engineering. When functions
288+
do more than one thing, they are harder to compose, test, and reason about.
289+
When you can isolate a function to just one action, they can be refactored
290+
easily and your code will read much cleaner. If you take nothing else away from
291+
this guide other than this, you'll be ahead of many developers.
292+
293+
**Bad:**
294+
295+
```python
296+
from typing import List
297+
298+
299+
class Client:
300+
active: bool
301+
302+
303+
def email(client: Client) -> None:
304+
pass
305+
306+
307+
def email_clients(clients: List[Client]) -> None:
308+
"""Filter active clients and send them an email.
309+
"""
310+
for client in clients:
311+
if client.active:
312+
email(client)
313+
```
314+
315+
**Good**:
316+
317+
```python
318+
from typing import List
319+
320+
321+
class Client:
322+
active: bool
323+
324+
325+
def email(client: Client) -> None:
326+
pass
327+
328+
329+
def get_active_clients(clients: List[Client]) -> List[Client]:
330+
"""Filter active clients.
331+
"""
332+
return [client for client in clients if client.active]
333+
334+
335+
def email_clients(clients: List[Client]) -> None:
336+
"""Send an email to a given list of clients.
337+
"""
338+
for client in get_active_clients(clients):
339+
email(client)
340+
```
341+
342+
Do you see an opportunity for using generators now?
343+
344+
**Even better**
345+
346+
```python
347+
from typing import Generator, Iterator
348+
349+
350+
class Client:
351+
active: bool
352+
353+
354+
def email(client: Client):
355+
pass
356+
357+
358+
def active_clients(clients: Iterator[Client]) -> Generator[Client, None, None]:
359+
"""Only active clients"""
360+
return (client for client in clients if client.active)
361+
362+
363+
def email_client(clients: Iterator[Client]) -> None:
364+
"""Send an email to a given list of clients.
365+
"""
366+
for client in active_clients(clients):
367+
email(client)
368+
```
369+
370+
**[⬆ back to top](#table-of-contents)**
371+
285372
### Function arguments (2 or fewer ideally)
286373

287374
A large amount of parameters is usually the sign that a function is
@@ -466,93 +553,6 @@ create_menu(
466553

467554
**[⬆ back to top](#table-of-contents)**
468555

469-
### Functions should do one thing
470-
471-
This is by far the most important rule in software engineering. When functions
472-
do more than one thing, they are harder to compose, test, and reason about.
473-
When you can isolate a function to just one action, they can be refactored
474-
easily and your code will read much cleaner. If you take nothing else away from
475-
this guide other than this, you'll be ahead of many developers.
476-
477-
**Bad:**
478-
479-
```python
480-
from typing import List
481-
482-
483-
class Client:
484-
active: bool
485-
486-
487-
def email(client: Client) -> None:
488-
pass
489-
490-
491-
def email_clients(clients: List[Client]) -> None:
492-
"""Filter active clients and send them an email.
493-
"""
494-
for client in clients:
495-
if client.active:
496-
email(client)
497-
```
498-
499-
**Good**:
500-
501-
```python
502-
from typing import List
503-
504-
505-
class Client:
506-
active: bool
507-
508-
509-
def email(client: Client) -> None:
510-
pass
511-
512-
513-
def get_active_clients(clients: List[Client]) -> List[Client]:
514-
"""Filter active clients.
515-
"""
516-
return [client for client in clients if client.active]
517-
518-
519-
def email_clients(clients: List[Client]) -> None:
520-
"""Send an email to a given list of clients.
521-
"""
522-
for client in get_active_clients(clients):
523-
email(client)
524-
```
525-
526-
Do you see an opportunity for using generators now?
527-
528-
**Even better**
529-
530-
```python
531-
from typing import Generator, Iterator
532-
533-
534-
class Client:
535-
active: bool
536-
537-
538-
def email(client: Client):
539-
pass
540-
541-
542-
def active_clients(clients: Iterator[Client]) -> Generator[Client, None, None]:
543-
"""Only active clients"""
544-
return (client for client in clients if client.active)
545-
546-
547-
def email_client(clients: Iterator[Client]) -> None:
548-
"""Send an email to a given list of clients.
549-
"""
550-
for client in active_clients(clients):
551-
email(client)
552-
```
553-
554-
**[⬆ back to top](#table-of-contents)**
555-
556556
### Function names should say what they do
557557

558558
**Bad:**

0 commit comments

Comments
 (0)