Skip to content

Commit 4046a09

Browse files
committed
Small code improvements
1 parent 9c63b3a commit 4046a09

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ addExpireAt(86400);
7171
```php
7272
// Declare them as capitalized `const` globals.
7373
interface DateGlobal {
74-
const SECONDS_IN_A_DAY = 86400;
74+
const SECONDS_IN_A_DAY = 86400;
7575
}
7676

7777
addExpireAt(DateGlobal::SECONDS_IN_A_DAY);
@@ -85,6 +85,7 @@ addExpireAt(DateGlobal::SECONDS_IN_A_DAY);
8585
$address = 'One Infinite Loop, Cupertino 95014';
8686
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
8787
preg_match($cityZipCodeRegex, $address, $matches);
88+
8889
saveCityZipCode($matches[1], $matches[2]);
8990
```
9091

@@ -93,26 +94,29 @@ saveCityZipCode($matches[1], $matches[2]);
9394
$address = 'One Infinite Loop, Cupertino 95014';
9495
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
9596
preg_match($cityZipCodeRegex, $address, $matches);
97+
9698
list(, $city, $zipCode) = $matchers;
9799
saveCityZipCode(city, zipCode);
98100
```
99101
**[⬆ back to top](#table-of-contents)**
100102

101103
### Avoid Mental Mapping
104+
Don’t force the reader of your code to translate what the variable means.
102105
Explicit is better than implicit.
103106

104107
**Bad:**
105108
```php
106109
$l = ['Austin', 'New York', 'San Francisco'];
107110

108111
foreach($i=0; $i<count($l); $i++) {
112+
$li = $l[$i];
109113
oStuff();
110114
doSomeOtherStuff();
111115
// ...
112116
// ...
113117
// ...
114-
// Wait, what is `l` for again?
115-
dispatch(l);
118+
// Wait, what is `$li` for again?
119+
dispatch($li);
116120
}
117121
```
118122

@@ -179,7 +183,7 @@ function createMicrobrewery($name = null) {
179183
**Good**:
180184
```php
181185
function createMicrobrewery($breweryName = 'Hipster Brew Co.') {
182-
// ...
186+
// ...
183187
}
184188

185189
```
@@ -198,7 +202,7 @@ of the time a higher-level object will suffice as an argument.
198202
**Bad:**
199203
```php
200204
function createMenu($title, $body, $buttonText, $cancellable) {
201-
// ...
205+
// ...
202206
}
203207
```
204208

0 commit comments

Comments
 (0)