You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
###Use the same vocabulary for the same type of variable
44
46
45
47
**Bad:**
48
+
Here we use three different names for the same underlying entity:
46
49
```python
47
50
get_user_info()
48
51
get_client_data()
49
52
get_customer_record()
50
53
```
51
54
52
55
**Good**:
56
+
If the entity is the same, you should be consistent in referring to it in your functions:
53
57
```python
54
-
get_user()
58
+
get_user_info()
59
+
get_user_data()
60
+
get_user_record()
55
61
```
62
+
63
+
**Even better**
64
+
Python is (also) an object oriented programming language. If it makes sense, package the functions together with the concrete implementation
65
+
of the entity in your code, as instance attributes, property methods, or methods:
66
+
67
+
```python
68
+
classUser:
69
+
info :str
70
+
71
+
@property
72
+
defdata(self) ->dict:
73
+
# ...
74
+
75
+
defget_record(self) -> Union[Record,None]:
76
+
# ...
77
+
```
78
+
56
79
**[⬆ back to top](#table-of-contents)**
57
80
58
81
###Use searchable names
@@ -65,7 +88,6 @@ Make your names searchable.
65
88
```python
66
89
# What the heck is 86400 for?
67
90
time.sleep(86400);
68
-
69
91
```
70
92
71
93
**Good**:
@@ -77,7 +99,6 @@ time.sleep(SECONDS_IN_A_DAY)
77
99
```
78
100
**[⬆ back to top](#table-of-contents)**
79
101
80
-
81
102
###Use explanatory variables
82
103
**Bad:**
83
104
```python
@@ -149,66 +170,46 @@ variable name.
149
170
150
171
**Bad:**
151
172
152
-
```php
153
-
class Car
154
-
{
155
-
public $carMake;
156
-
public $carModel;
157
-
public $carColor;
158
-
159
-
//...
160
-
}
173
+
```python
174
+
classCar:
175
+
car_make:str
176
+
car_model:str
177
+
car_color:str
161
178
```
162
179
163
180
**Good**:
164
181
165
-
```php
166
-
class Car
167
-
{
168
-
public $make;
169
-
public $model;
170
-
public $color;
171
-
172
-
//...
173
-
}
182
+
```python
183
+
classCar:
184
+
make:str
185
+
model:str
186
+
color:str
174
187
```
175
188
176
189
**[⬆ back to top](#table-of-contents)**
177
190
178
191
###Use default arguments instead of short circuiting or conditionals
179
192
180
-
**Not good:**
193
+
**Tricky**
181
194
182
-
This is not good because`$breweryName` can be`NULL`.
195
+
Why write:
183
196
184
-
```php
185
-
function createMicrobrewery($breweryName = 'Hipster Brew Co.')
186
-
{
187
-
// ...
188
-
}
197
+
```python
198
+
defcreate_micro_brewery(name):
199
+
name="Hipster Brew Co."if nameisNoneelse name
200
+
slug= hashlib.sha1(name.encode()).hexdigest()
201
+
# etc.
189
202
```
190
203
191
-
**Not bad:**
192
-
193
-
This opinion is more understandable than the previous version, but it better controls the value of the variable.
194
-
195
-
```php
196
-
function createMicrobrewery($name = null)
197
-
{
198
-
$breweryName = $name ?: 'Hipster Brew Co.';
199
-
// ...
200
-
}
201
-
```
204
+
... when you can specify a default argument instead? This also makes ist clear that
205
+
you are expecting a string as the argument.
202
206
203
207
**Good**:
204
208
205
-
If you support only PHP 7+, then you can use[type hinting](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) and be sure that the`$breweryName` will not be`NULL`.
206
-
207
-
```php
208
-
function createMicrobrewery(string $breweryName = 'Hipster Brew Co.')