Skip to content

Commit 4c2133d

Browse files
change example for Function names should say what they do
1 parent 961e006 commit 4c2133d

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,26 +284,41 @@ function isClientActive($client) {
284284
### Function names should say what they do
285285

286286
**Bad:**
287+
287288
```php
288-
function addToDate($date, $month) {
289-
// ...
290-
}
289+
class Email
290+
{
291+
//...
291292

292-
$date = new \DateTime();
293+
public function handle()
294+
{
295+
mail($this->to, $this->subject, $this->body);
296+
}
297+
}
293298

294-
// It's hard to to tell from the function name what is added
295-
addToDate($date, 1);
299+
$message = new Email(...);
300+
// What is this? A handle for the message? Are we writing to a file now?
301+
$message->handle();
296302
```
297303

298-
**Good**:
304+
**Good:**
305+
299306
```php
300-
function addMonthToDate($month, $date) {
301-
// ...
307+
class Email
308+
{
309+
//...
310+
311+
public function send()
312+
{
313+
mail($this->to, $this->subject, $this->body);
314+
}
302315
}
303316

304-
$date = new \DateTime();
305-
addMonthToDate(1, $date);
317+
$message = new Email(...);
318+
// Clear and obvious
319+
$message->send();
306320
```
321+
307322
**[⬆ back to top](#table-of-contents)**
308323

309324
### Functions should only be one level of abstraction

0 commit comments

Comments
 (0)