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

Commite94c4b9

Browse files
zacharyaanglinzedr
authored andcommitted
AddedAvoid Side Effects sub-section.
Edited wrapping linesRemoved title case on Avoid Side Effects`Avoid side effects`
1 parente9d7829 commite94c4b9

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

‎README.md‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ you are expecting a string as the argument.
207207
**Good**:
208208

209209
```python
210-
defcreate_micro_brewery(name:str="Hipster Brew Co."):
210+
defcreate_micro_brewery(name:str="Hipster Brew Co."):
211211
slug= hashlib.sha1(name.encode()).hexdigest()
212212
# etc.
213213
```
@@ -496,7 +496,9 @@ def parse(tokens: list) -> list:
496496

497497
###Don't use flags as function parameters
498498

499-
Flags tell your user that this function does more than one thing. Functions should do one thing. Split your functions if they are following different code paths based on a boolean.
499+
Flags tell your user that this function does more than one thing. Functions
500+
should do one thing. Split your functions if they are following different code
501+
paths based on a boolean.
500502

501503
**Bad:**
502504

@@ -523,3 +525,52 @@ def create_temp_file(name: str) -> None:
523525
```
524526

525527
**[⬆ back to top](#table-of-contents)**
528+
529+
###Avoid side effects
530+
531+
A function produces a side effect if it does anything other than take a value in
532+
and return another value or values. A side effect could be writing to a file,
533+
modifying some global variable, or accidentally wiring all your money to a
534+
stranger.
535+
536+
Now, you do need to have side effects in a program on occasion - for example, like
537+
in the previous example, you might need to write to a file. In these cases, you
538+
should centralize and indicate where you are incorporating side effects. Don't have
539+
several functions and classes that write to a particular file - rather, have one
540+
(and only one) service that does it.
541+
542+
The main point is to avoid common pitfalls like sharing state between objects
543+
without any structure, using mutable data types that can be written to by anything,
544+
and not centralizing where your side effects occur. If you can do this, you will be
545+
happier than the vast majority of other programmers.
546+
547+
**Bad:**
548+
549+
```python
550+
# Global variable referenced by following function.
551+
# If another function used this name, now it'd be an array and could break.
552+
name='Ryan McDermott'
553+
554+
defsplit_into_first_and_last_name() ->None:
555+
global name
556+
name= name.split()
557+
558+
split_into_first_and_last_name()
559+
560+
print(name)# ['Ryan', 'McDermott']
561+
```
562+
563+
**Good:**
564+
```python
565+
defsplit_into_first_and_last_name(name:str) ->None:
566+
return name.split()
567+
568+
name='Ryan McDermott'
569+
new_name= split_into_first_and_last_name(name)
570+
571+
print(name)# 'Ryan McDermott'
572+
print(new_name)# ['Ryan', 'McDermott']
573+
```
574+
575+
**[⬆ back to top](#table-of-contents)**
576+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp