Upgrade to Pro — share decks privately, control downloads, hide ads and more …

はじめてのIT勉強会 #3 Readable Code Part 3

はじめてのIT勉強会 #3 Readable Code Part 3

仙台で開催した「はじめてのIT勉強会」第三回のセッションスライドです。
リーダブルコードの紹介と入門的な解説内容になっています。

Ruku Tanaka

June 29, 2017
Tweet

More Decks by Ruku Tanaka

Other Decks in Programming

Transcript

  1. public class PerformanceTester { public static final TcpConnectionSimulator wifi =

    new TcpConnectionSimulator( 500, /* Kbps */ 80, /* millisecs latency */ 200, /* jitter */ 1 /* packet loss % */); public static final TcpConnectionSimulator t3_fiber = new TcpConnectionSimulator( 45000, /* Kbps */ 10, /* millisecs latency */ 0, /* jitter */ 0 /* packet loss % */); public static final TcpConnectionSimulator cell = new TcpConnectionSimulator( 100, /* Kbps */ 400, /* millisecs latency */ 250, /* jitter */ 5 /* packet loss % */); } 不用意に目立っている
  2. public class PerformanceTester { public static final TcpConnectionSimulator wifi =

    new TcpConnectionSimulator( 500, /* Kbps */ 80, /* millisecs latency */ 200, /* jitter */ 1 /* packet loss % */); public static final TcpConnectionSimulator t3_fiber = new TcpConnectionSimulator( 45000, /* Kbps */ 10, /* millisecs latency */ 0, /* jitter */ 0 /* packet loss % */); public static final TcpConnectionSimulator cell = new TcpConnectionSimulator( 100, /* Kbps */ 400, /* millisecs latency */ 250, /* jitter */ 5 /* packet loss % */); }
  3. public class PerformanceTester { // new TcpConnectionSimulator(throughput, latency, jitter, packet_loss);

    // [Kbps] [ms] [ms] [percent] public static final TcpConnectionSimulator wifi = new TcpConnectionSimulator( 500, 80, 200, 1); public static final TcpConnectionSimulator t3_fiber = new TcpConnectionSimulator( 45000, 10, 0, 0); public static final TcpConnectionSimulator cell = new TcpConnectionSimulator( 100, 400, 250, 5); }
  4. assert(expandFullName($databaseConnection, "Doug Adams", $error) === "Mr. Douglas Adams"); assert($error ===

    ""); assert(expandFullName($databaseConnection, " Jake Brown ", $error) === "Mr. Jacob Brown III"); assert($error === ""); assert(expandFullName($databaseConnection, "No Such Guy", $error) === ""); assert($error === "No match found."); assert(expandFullName($databaseConnection, "John", $error) === ""); assert($error === "More than one result.");
  5. $expandNameTest->checkFullName("Doug Adams", "Mr. Douglas Adams", ""); $expandNameTest->checkFullName(" Jake Brown ",

    "Mr. Jacob Brown III", ""); $expandNameTest->checkFullName("No Such Guy", "", "No match found."); $expandNameTest->checkFullName("John", "", "More than one result."); function checkFullName(string $partialName, string $expectedFullName, string $expectedError) { $error = ''; $fullName = expandFullName($this->dbConn, $partialName, $error); assert($fullName === $expectedFullName); assert($error === $expectedError)); }
  6. // partialName , expectedFullName , expectedError checkFullName("Doug Adams" , "Mr.

    Douglas Adams" , ""); checkFullName("Jake Brown" , "Mr. Jacob Brown III" , ""); checkFullName("No Such Guy", "" , "No match found."); checkFullName("John" , "" , "More than one result."); checkFullName("Doug Adams", "Mr. Douglas Adams", ""); checkFullName(" Jake Brown ", "Mr. Jacob Brown III", ""); checkFullName("No Such Guy", "", "No match found."); checkFullName("John", "", "More than one result.");
  7. // POSTのパラメータをローカル変数に割り当てる $details = $request->getPost("details"); $location = $request->getPost("location"); $phone =

    $equest->getPost("phone"); $email = $request->getPost("email"); $url = $request->getPost("url"); バグを見つけてください
  8. // パラメータがnullの場合、空文字列で初期化する $details $phone $email $url $location = $details =

    $phone = $email = $url = $location === null ? "" : $details; === null ? "" : $phone; === null ? "" : $email; === null ? "" : $url; === null ? "" : $location; locationはどこ? なんで下にあるの?
  9. interface FrontendServer { public function viewProfile(HttpRequest $request); public function openDatabase(string

    $location, string $user); public function saveProfile(HttpRequest $request); public function extractQueryParam(HttpRequest $request, string $param); public function replyOK(HttpRequest $request, string $html); public function findFriends(HttpRequest $request); public function replyNotFound(HttpRequest $request, string $error); public function closeDatabase(string $location); }
  10. interface FrontendServer { // ハンドラ public function viewProfile(HttpRequest $request); public

    function saveProfile(HttpRequest $request); public function findFriends(HttpRequest $request); // リクエストとリプライのユーティリティ public function extractQueryParam(HttpRequest $request, string $param); public function replyOK(HttpRequest $request, string $html); public function replyNotFound(HttpRequest $request, string $error); // データベースのヘルパー public function openDatabase(string $location, string $user); public function closeDatabase(string $location); }
  11. // ユーザのメール帳をインポートして、システムのユーザと照合する。 // そして、まだ友達になっていないユーザのメールアドレスを取得する。 function suggestNewFriendEmails(User $user, string $emailPassword) {

    $friends = $user->getFriends(); $friendsEmails = array(); foreach ($friends as $friend) { $friendsEmails[] = $friend->getEmail(); } $contacts = importContacts($user->getEmail(), $emailPassword); $contactEmails = array(); foreach ($contacts as $contact) { $contactEmails[] = $contact->getEmail(); } $nonFriendEmails = array_diff($contactEmails, $friendsEmails); return $nonFriendEmails; }
  12. // ユーザのメール帳をインポートして、システムのユーザと照合する。 // そして、まだ友達になっていないユーザのメールアドレスを取得する。 function suggestNewFriendEmails(User $user, string $emailPassword) {

    $friends = $user->getFriends(); $friendsEmails = array(); foreach ($friends as $friend) { $friendsEmails[] = $friend->getEmail(); } $contacts = importContacts($user->getEmail(), $emailPassword); $contactEmails = array(); foreach ($contacts as $contact) { $contactEmails[] = $contact->getEmail(); } $nonFriendEmails = array_diff($contactEmails, $friendsEmails); return $nonFriendEmails; }
  13. // ユーザのメール帳をインポートして、システムのユーザと照合する。 // そして、まだ友達になっていないユーザのメールアドレスを取得する。 function suggestNewFriendEmails(User $user, string $emailPassword) {

    // ユーザの友達のメールアドレスを取得する。 $friends = $user->getFriends(); $friendsEmails = array(); foreach ($friends as $friend) { $friendsEmails[] = $friend->getEmail(); } // ユーザの連絡先からすべてのメールアドレスをインポートする。 $contacts = importContacts($user->getEmail(), $emailPassword); $contactEmails = array(); foreach ($contacts as $contact) { $contactEmails[] = $contact->getEmail(); } // まだ友達になっていないメールアドレスを探して返す。 $nonFriendEmails = array_diff($contactEmails, $friendsEmails); return $nonFriendEmails; }
  14. if ($eventEnded === true) { removeEvent($currentEvent); } else { echo("CurrentEvent:

    " + $currentEvent); } Java(K&R)スタイル C#(オールマン)スタイル