Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bad Example] Function names should say what they do #23

Closed
peter-gribanov opened this issue Aug 29, 2017 · 4 comments
Closed

[Bad Example] Function names should say what they do #23

peter-gribanov opened this issue Aug 29, 2017 · 4 comments

Comments

@peter-gribanov
Copy link
Contributor

Function names should say what they do

Bad:

function addToDate(\DateTime $date, $month) {
    $date->modify(sprintf('+%d months', $month));
}

$date = new \DateTime();

// It's hard to to tell from the function name what is added
addToDate($date, 1);

Good:

function addMonthToDate($month, \DateTime $date) {
    $date->modify(sprintf('+%d months', $month));
}

$date = new \DateTime();
addMonthToDate(1, $date);

What is the purpose of addToDate()/addMonthToDate() functions?
What are they needed for?
These functions are meaningless.

@hailwood
Copy link

hailwood commented Aug 29, 2017

perhaps something more like

class Email 
{
    //...

    public function handle() {
        mail($this->to, $this->subject, $this->body);
    }

}

$message = new Email(...);
// What is this? A handle for the message? Are we writing to a file now?
$message->handle();

compared to

class Email 
{
    //...

    public function send() {
        mail($this->to, $this->subject, $this->body);
    }

}

$message = new Email(...);
// Clear and obvious
$message->send();

@peter-gribanov
Copy link
Contributor Author

@hailwood your example i like more

@piotrplenik
Copy link
Owner

@hailwood Could you create PR for that?

@TomasVotruba
Copy link
Contributor

Closed by #44

TomasVotruba pushed a commit that referenced this issue Feb 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants