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

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

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

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

Yasutaka KOUNO

April 27, 2017
Tweet

More Decks by Yasutaka KOUNO

Other Decks in Programming

Transcript

  1. www.sekainet.co.jp ͲͪΒ͕༏Ε͍ͯΔʁ // ύλʔϯ1 int i = 0; while (list.size()

    > i) { Object element = list.get(i); System.out.println(element); i++; } // ύλʔϯ2 for (Object element : list) { System.out.println(element); }
  2. www.sekainet.co.jp ൚༻తͳ໊લΛཞ༻͍ͯ͠·ͤΜ͔ʁ Έͳ͞Μ͕ίʔυΛॻ͘ͱ͖ɺ
 ࣍ͷΑ͏ͳ໊લΛΑ͘࢖ͬͯ͸͍·ͤΜ͔ʁ get, set, str, text, list, map,

    result, buf, temp, obj1, obj2, size, date, hoge, foo ͜ΕΒ͸൚༻తͰศརͰ͕͢ɺͲ͜Ͱ΋࢖͑ΔΘ͚ Ͱ͸͋Γ·ͤΜɻ
  3. www.sekainet.co.jp /** WebαΠτͷΠϯλʔϑΣʔε. */ interface WebSite { Page getPage(); }

    /** WebαΠτͷΠϯλʔϑΣʔεʢվળҊʣ. */ interface WebSite { Page downloadPage(); } Javaͷgetter(ϓϩύςΟ)͔ͳʁ ҧ͏ͳΒͲ͔͜Βऔͬͯ͘Δͷʁ ίϝϯτ͕ͳͯ͘΋Πϯλʔωοτ͔Β μ΢ϯϩʔυ͢Δ͜ͱ͕ਪଌͰ͖Δɻ
  4. www.sekainet.co.jp /** ೋ෼໦Ϋϥε. */ class BinaryTree { int size(); }

    /** ೋ෼໦ΫϥεʢվળҊʣ. */ class BinaryTree { int height(); int numNodes(); } ߴ͞ʁ ϊʔυͷ਺ʁ ͳΜͷαΠζʁ
  5. www.sekainet.co.jp /** εϨουΫϥε. */ class Thread { void stop(); }

    /** εϨουΫϥεʢվળҊʣ. */ class Thread { void kill(); void pause(); // resume()ͰॲཧΛ࠶։ } ڧ੍ऴྃʁɹҰ࣌ఀࢭʁ
  6. www.sekainet.co.jp Ͱ΋ɺ΍Γա͗ʹ͸஫ҙ͠Α͏ɻ class String { // ͨͱ͑͹ɺStringʹ͜Μͳϝιου͕͋ͬͨΒࠞཚͯ͠͠·͏ɻ String[] split(String regex);

    String[] divide(String regex); String[] partition(String regex); // "boo:and:foo".split(":") > ["boo", "and", "foo"] // "boo:and:foo".divide(":") > ["boo", ":", "and", ":", "foo"] // "boo:and:foo".partition(":") > ["boo", ":and", ":foo"] }
  7. www.sekainet.co.jp ୹ͯ͘΋໰୊ͳ͍ม਺໊ // ࠨӈͷ஋ΛೖΕସ͑Δ if (right < left) { int

    tmp = right; right = left; left = tmp; } ͜ͷtmp͸ੜଘظ͕ؒ୹͘ Ұ࣌తͳม਺ͱ͍͏ҙਤ͕఻ΘΔ
  8. www.sekainet.co.jp ୹͍ͱ໰୊͕͋Δม਺໊ // Ϣʔβʔ৘ใΛจࣈྻԽ String tmp = user.name(); tmp +=

    " " + user.phone_number(); tmp += " " + user.email(); ... template.put("user_info", tmp); template.put("server_info", tmp); // ؒҧ͍ʹؾ෇͔ͳ͍ userInfoͳͲͷม਺໊Ͱ͋Ε͹ɺ
 ΑΓΘ͔Γ΍͘͢ɺؒҧ͍Λ๷͛Δ
  9. www.sekainet.co.jp ϧʔϓΠϯσοΫε i, j, k, idx͸ɺ୹͍͚ͲΑ͘ҙຯΛද͍ͯ͠Δɻ Ͱ΋ɺ࣍ͷΑ͏ͳέʔεͰ͸͘͢͝ಡΈͮΒ͍ɻ for (int i

    = 0; i < clubs.size(); i++) for (int j = 0; j < clubs[i].members.size(); j++) for (int k = 0; k < users.size(); k++) if (clubs[i].members[k] == users[j]) System.out.println("user[" + j + "] is in club[" + i + "]"); membersͷΠϯσοΫε͸ j usersͷΠϯσοΫε͸ k ifจҎԼͰऔΓҧ͍͑ͯΔ
  10. www.sekainet.co.jp ϧʔϓΠϯσοΫεͷվળ ͳΜͷͨΊͷΠϯσοΫε͔໌֬ʹ͠Α͏ // ci = clubs_idx, mi = members_idx,

    ui = user_idx for (int ci = 0; ci < clubs.size(); ci++) for (int mi = 0; mi < clubs[ci].members.size(); mi++) for (int ui = 0; ui < users.size(); ui++) if (clubs[ci].members[ui] == users[mi]) System.out.println("user[" + mi + "] is in club[" + ci + "]"); members[ui]ͳͲͷ
 ؒҧ͍͕Θ͔Γ΍͘͢ͳΔ
  11. www.sekainet.co.jp ϧʔϓΠϯσοΫεΛආ͚Δ for (Club club : clubs) for (User member

    : club.members) for (User user : users) if (member == user) System.out.println("user[" + user + "] is in club[" + club + "]"); ՄೳͰ͋Ε͹֦ுforจͳͲΛ࢖༻ͯ͠ϧʔϓΠϯ σοΫεͷ࢖༻ͦͷ΋ͷΛආ͚Α͏ɻ ΠϯσοΫεͷؒҧ͍ͷ༨஍͕ͳ্͍ʹɺ ίʔυ΋γϯϓϧʹ
  12. www.sekainet.co.jp /** ೚ҙͷTCP/IPϙʔτΛαʔό͕ϦοεϯͰ͖Δ͔֬ೝ͢Δ. */ public boolean serverCanStart() { ... }

    /** ೚ҙͷTCP/IPϙʔτΛαʔό͕ϦοεϯͰ͖Δ͔֬ೝ͢Δ.(मਖ਼൛) */ public boolean canListenOnPort() { ... } ػೳʹରͯ͠ɺϝιου໊͕แׅత͗͢Δ
  13. www.sekainet.co.jp //ॲཧ࣌ؒͷܭଌʢमਖ਼൛ʣ long startMs = new Date().getTime(); ... long elapsedMs

    = new Date().getTime() - startMs; System.out.println("ಡΈࠐΈ࣌ؒɿ" + (elapsedMs / 1000L) + "ඵ"); // ॲཧ࣌ؒͷܭଌ long start = new Date().getTime(); ... long elapsed = new Date().getTime() - start; System.out.println("ಡΈࠐΈ࣌ؒɿ" + elapsed + "ඵ"); elapsedʹ͸ϛϦඵ͕ ઃఆ͞Ε͍ͯΔ
  14. www.sekainet.co.jp ॏཁͳଐੑͷ෇༩ྫ ม਺໊ վળޙͷม਺໊ password plaintextPassword hashedPassword comment unescapedComment html

    htmlUtf8 data dataUrlEncoded // ͜ͷΑ͏ʹࠞಉ͠қ͍৔ॴͰ׆༻͢Δͱྑ͍ if (passwordMatches(plaintextPassword, hashedPassword)) { ... }
  15. www.sekainet.co.jp /** Ϣʔβʔςʔϒϧʹର͢ΔݕࡧΛߦ͏. */ interface UserRepository { List<User> findAdminUsersFromUserTable(); }

    UserRepositoryͳΒUserTable͔Β
 ݕࡧ͢Δͷ͸ࣗ໌ /** Ϣʔβʔςʔϒϧʹର͢ΔݕࡧΛߦ͏(վળҊ). */ interface UserRepository { List<User> findAdminUsers(); }
  16. www.sekainet.co.jp // είʔϓ͕খ͚͞Ε͹୹໊͍લͰ΋͍͍ if (debug) { Map<String, Integer> m =

    new HashMap<>(); lookUpNamesNumbers(m); System.out.println(m); } // Ұํɺϝϯόม਺΍άϩʔόϧม਺ͩͱmͷ໨త͕෼͔Βͳ͍ lookUpNamesNumbers(m); System.out.println(m);
  17. www.sekainet.co.jp লུͷ͔ͨ͠ʹ͸ؾΛ͚ͭΔ // ͜ͷলུ͸ɺ৽͍͠ϝϯόʔ͕ཧղͰ͖ΔͩΖ͏͔ʁ class BEManager // BackEndManagerͷུ // ͜ͷলུ͸ཧղͰ͖Δ

    eval // evaluation doc // document str // string ϓϩδΣΫτݻ༗ͷ΋ͷ͸লུ͠ͳ͍΄͏͕ྑ͍ ϓϩάϥϚʹͱͬͯҰൠతͳলུ͸ͯ͠΋ྑ͍
  18. www.sekainet.co.jp جຊతͳίʔσΟϯάελΠϧΛकΖ͏ // ΫϥεɾΠϯλʔϑΣʔε͸େจࣈͰ࢝ΊΔ public class TaskQueue { // ఆ਺(static

    final)͸େจࣈͱΞϯμʔείΞ public static final int LIMIT_TASK_NUM = 100; // ม਺΍ϝιου͸খจࣈͰ࢝ΊΔ private Queue<Task> tasks; public void addQueue(Task task) { ... } public Object executeQueue() { ... } } https://kazurof.github.io/GoogleJavaStyle-ja/ ࢀߟࢿྉ: Google Java Style Guide
  19. www.sekainet.co.jp • ໌֬ͳ୯ޠΛબͿ ✴ ঢ়گʹԠͯ͡getͰ͸ͳ͘fetch΍downloadΛ࢖͏ • ൚༻తͳ໊લΛආ͚Δ ✴ ໌֬ͳཧ༝͕ͳ͚Ε͹ɺtmp΍resultΛ࢖Θͳ͍ •

    ΑΓ۩ମతͳ໊લΛ͚ͭɺแׅతʹ͗͢͠ͳ͍ ✴ serverCanStart()ΑΓcanListenOnPort()ͷ΄͏͕໌֬ • େ੾ͳ৘ใΛ௥Ճ͢Δ ✴ ϛϦඵΛද͢msΛ෇͚ΔɻੜύεϫʔυΛplaintextPasswordͱ͢Δɻ • είʔϓʹ߹໊ͬͨલΛ෇͚Δ ✴ ୹͍είʔϓͳΒ୹໊͍લͰྑ͍ɻ௕͍είʔϓͳΒ௕ͯ͘΋໌֬ʹɻ • େจࣈ΍ΞϯμʔείΞͳͲʹҙຯΛࠐΊΔ ✴ ྫ͑͹ɺҰ໨Ͱఆ਺ͱղΔΑ͏ͳ໊લ(LIMIT_TASK_NUM)Λ͚ͭΔɻ