Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit155cb0d

Browse files
committed
Address functions doing one thing first
1 parent14d22bb commit155cb0d

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 typingimport List
297+
298+
299+
classClient:
300+
active:bool
301+
302+
303+
defemail(client: Client) ->None:
304+
pass
305+
306+
307+
defemail_clients(clients: List[Client]) ->None:
308+
"""Filter active clients and send them an email.
309+
"""
310+
for clientin clients:
311+
if client.active:
312+
email(client)
313+
```
314+
315+
**Good**:
316+
317+
```python
318+
from typingimport List
319+
320+
321+
classClient:
322+
active:bool
323+
324+
325+
defemail(client: Client) ->None:
326+
pass
327+
328+
329+
defget_active_clients(clients: List[Client]) -> List[Client]:
330+
"""Filter active clients.
331+
"""
332+
return [clientfor clientin clientsif client.active]
333+
334+
335+
defemail_clients(clients: List[Client]) ->None:
336+
"""Send an email to a given list of clients.
337+
"""
338+
for clientin 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 typingimport Generator, Iterator
348+
349+
350+
classClient:
351+
active:bool
352+
353+
354+
defemail(client: Client):
355+
pass
356+
357+
358+
defactive_clients(clients: Iterator[Client]) -> Generator[Client,None,None]:
359+
"""Only active clients"""
360+
return (clientfor clientin clientsif client.active)
361+
362+
363+
defemail_client(clients: Iterator[Client]) ->None:
364+
"""Send an email to a given list of clients.
365+
"""
366+
for clientin 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 typingimport List
481-
482-
483-
classClient:
484-
active:bool
485-
486-
487-
defemail(client: Client) ->None:
488-
pass
489-
490-
491-
defemail_clients(clients: List[Client]) ->None:
492-
"""Filter active clients and send them an email.
493-
"""
494-
for clientin clients:
495-
if client.active:
496-
email(client)
497-
```
498-
499-
**Good**:
500-
501-
```python
502-
from typingimport List
503-
504-
505-
classClient:
506-
active:bool
507-
508-
509-
defemail(client: Client) ->None:
510-
pass
511-
512-
513-
defget_active_clients(clients: List[Client]) -> List[Client]:
514-
"""Filter active clients.
515-
"""
516-
return [clientfor clientin clientsif client.active]
517-
518-
519-
defemail_clients(clients: List[Client]) ->None:
520-
"""Send an email to a given list of clients.
521-
"""
522-
for clientin 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 typingimport Generator, Iterator
532-
533-
534-
classClient:
535-
active:bool
536-
537-
538-
defemail(client: Client):
539-
pass
540-
541-
542-
defactive_clients(clients: Iterator[Client]) -> Generator[Client,None,None]:
543-
"""Only active clients"""
544-
return (clientfor clientin clientsif client.active)
545-
546-
547-
defemail_client(clients: Iterator[Client]) ->None:
548-
"""Send an email to a given list of clients.
549-
"""
550-
for clientin 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)

[8]ページ先頭

©2009-2025 Movatter.jp