Exercismのすすめ / Recommend Exercism

y_sone
July 31, 2019

Exercismのすすめ / Recommend Exercism

海外メンターによるコードレビューサービス「Exercism」を使った感想です。
PHP勉強会@20190731にて登壇。

Exercism: https://exercism.io/

y_sone

July 31, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

25 slides
  1. すすめかた 1. 課題に必要なファイルをダウンロード 2. 仕様確認 3. 実装 4. テスト 5.

    提出 6. メンターからコードレビューをもらう 7. 修正後、再提出 8. 合格
  2. 例① - 日付を10日加算したオブジェクトを返すメソッド - 今のままだとDateTimeImmutableクラスのオブジェクトしか使えない function addDay1(DateTimeImmutable $date_obj) { return

    $date_obj->add(new DateInterval('P10D')); } $im_date = new DateTimeImmutable('2019-01-01'); $date = new DateTime('2019-01-01'); var_dump(addDay1($im_date)); // 加算されたDateTimeImmutableオブジェクトが返る var_dump(addDay1($date)); // Fatal Error
  3. レビューはこんな感じ The solution to accept both Datetime and DatetimeImmutable objects,

    is to ensure to clone the argument object and assigning it to a new variable prior to adding time. DateTimeオブジェクトとDateTimeImmutableオブジェクトの両方を使うためには、時間を 加算する前に引数のオブジェクトをcloneして、新しい変数に割り当てると良いよ。 This exercise is a great opportunity to learn the difference between the DateTime and DateTimeImmutable classes. What would happen, if a DateTime object were (first accepted by the function and second) to be passed? この演習はDateTimeクラスとDateTimeImmutableクラスの違いを学ぶ良い機会だ。 DateTimeオブジェクトが(最初に(時間を加算する)関数を通過して、その次に)渡される とどうなるかな?
  4. 修正したよ - DateTimeクラスとDateTimeImmutableクラスの両方のオブジェクトが使える - DateTimeクラスの場合、オブジェクトの元の値を変えずにメソッドを実行できる function addDay2($date_obj) { $new_date_obj =

    clone $date_obj; return $new_date_obj->add(new DateInterval('P10D')); } $im_date = new DateTimeImmutable('2019-01-01'); $date = new DateTime('2019-01-01'); var_dump(addDay2($im_date)); // 加算されたDateTimeImmutableオブジェクトが返る var_dump(addDay2($date)); // 加算されたDateTimeオブジェクトが返る var_dump($date); // 加算前のDateTimeオブジェクトが返る
  5. 例② class Greet { function reply1(string $your_greet): string { $is_question

    = preg_match('/[\?]/', substr(trim($your_greet), -1)); $is_exclamation = preg_match('/[\!]/', substr(trim($your_greet), -1)); $is_space = preg_match('/^[\s]+$/', $your_greet); // 末尾が?の場合 if ($is_question) { $res = 'Wait a moment.'; // 末尾が!の場合 } elseif ($is_exclamation) { $res = 'You\'re looking well'; // 文字が存在しない場合 } elseif (!$your_greet || $is_space) { $res = 'Pardon?'; } else { $res = 'Hello.'; } return $res; } } $greeting = New Greet; var_dump($greeting->reply1('Hi!')); - 渡された文字列の種類によって異なる返事を するメソッド - 判定用のパラメータを作ってから、if文で処理 を分けている
  6. レビューはこんな感じ The verifications on lines 5-7 can be abstracted into

    separate methods. Thus making it possible to call them only when needed. And lessen the code complexity number by using only if statements together with a direct return. 5〜7行目の検証は別々のメソッドに抽象化することができる。よって、必要なときにだけ そのメソッドを呼び出すことを可能にできる。また、if文だけを使いダイレクトにreturnを返 すことによって、コードの複雑さを減らせるんだ。
  7. 修正したよ 次ページへ続く>>> class Greet { function reply2(string $your_greet): string {

    // 末尾が?の場合 if ($this->isQuestion($your_greet)) { return 'Wait a moment.'; } // 末尾が!の場合 if ($this->isExclamation($your_greet)) { return 'You\'re looking well'; } // 文字が存在しない場合 if ($this->isSilence($your_greet)) { return 'Pardon?'; } return 'Hello.'; }
  8. - コード量が少ないので利点が分かりにくい&賛否両論かも・・・ - 文字列の判定を別メソッドに分けたので、コードの見通しがよくなった - if文とreturnを一対にしているので、処理の流れが直感的に理解できる function isQuestion(string $your_greet): bool

    { return preg_match('/[\?]/', substr(trim($your_greet), -1)) ? true : false; } function isExclamation(string $your_greet): bool { return preg_match('/[\!]/', substr(trim($your_greet), -1)) ? true : false; } function isSilence(string $your_greet): bool { return !$your_greet || preg_match('/^[\s]+$/', $your_greet) ? true : false; } } $greeting = New Greet; var_dump($greeting->reply2('Hi!'));

More Decks by y_sone

Other Decks in Programming