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

Readable Code 勉強会 in 仙台 Part 1: 名前を改善する

Readable Code 勉強会 in 仙台 Part 1: 名前を改善する

2014/05/15日に行ったリーダブルコードの勉強会で使用したスライドです。

Yasutaka KOUNO

May 15, 2014
Tweet

More Decks by Yasutaka KOUNO

Other Decks in Technology

Transcript

  1. ͲͪΒ͕༏Ε͍ͯΔʁ // ύλʔϯ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. ͲͪΒ͕༏Ε͍ͯΔʁ // exponent ࢦ਺, mantissa Ծ਺ // ύλʔϯ1 if (exponent

    >= 0) { return mantissa * (1 << exponent); } else { return mantissa / (1 << -exponent); } ! // ύλʔϯ2 return exponent >= 0 ? mantissa * (1 << exponent) : mantissa / (1 << -exponent);
  3. ͲͪΒ͕༏Ε͍ͯΔʁ // ύλʔϯ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); }
  4. ͲͪΒ͕༏Ε͍ͯΔʁ // exponent ࢦ਺, mantissa Ծ਺ // ύλʔϯ1 if (exponent

    >= 0) { return mantissa * (1 << exponent); } else { return mantissa / (1 << -exponent); } ! // ύλʔϯ2 return exponent >= 0 ? mantissa * (1 << exponent) : mantissa / (1 << -exponent);
  5. ୹͚Ε͹͍͍Θ͚͡Όͳ͍ // ͜Ε͸Կ΍ͬͯΔͷ͔Α͘Θ͔Βͳ͍ assert (((bucket = findBucket(key)) != null) ||

    !bucket.isOccupied()); ! ! // Ͱ΋ɺ͜ΕͳΒΘ͔Δ bucket = findBucket(key); if (bucket != null) assert (!bucket.isOccupied());
  6. /** WebαΠτͷΠϯλʔϑΣʔε. */ interface WebSite { Page getPage(); } /**

    WebαΠτͷΠϯλʔϑΣʔεʢվળҊʣ. */ interface WebSite { Page fetchPage(); } Ͳ͔͜Βऔͬͯ͘Δͷʁ
  7. /** ೋ෼໦Ϋϥε. */ class BinaryTree { int size(); } /**

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

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

    public String[] divide(String regex); public 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"] }
  10. // n࣍ݩ্ͷೋ఺ؒͷڑ཭ΛٻΊΔɻྫɿ̎࣍ݩͷ৔߹ pointA = [5, 3] public static double distance(double[]

    pointA, double[] pointB) { // ೋ఺ͷ࣍ݩ͕ҟͳΔ৔߹͸ඇ਺Λฦ͢ if (pointA.length != pointB.length) return Double.NaN; double result = 0.0; for (int i = 0; i < pointA.length; i++) { result += Math.pow(pointA[i] - pointB[i], 2); } return result; } ̎఺ؒͷڑ཭ΛٻΊΔίʔυ ฏํ $ ͷ··ʂ
  11. //n࣍ݩ্ͷೋ఺ؒͷڑ཭ΛٻΊΔɻʢमਖ਼൛ʣ public static double distance(double[] pointA, double[] pointB) { //

    ೋ఺ͷ࣍ݩ͕ҟͳΔ৔߹͸ඇ਺Λฦ͢ if (pointA.length != pointB.length) return Double.NaN; double squareDistance = 0.0; // ೋ఺ؒͷڑ཭ͷฏํ for (int i = 0; i < pointA.length; i++) { squareDistance += Math.pow(pointA[i] - pointB[i], 2); } return Math.sqrt(squareDistance); // ೋ఺ؒͷڑ཭ͷฏํࠜΛฦ͢ } ̎఺ؒͷڑ཭ΛٻΊΔίʔυʢमਖ਼൛ʣ ฏํͷ··ͳΒόάͱΘ͔Δ
  12. ͜ͷtmp͸໰୊ͳ͍ // ࠨӈͷ஋ΛೖΕସ͑Δ if (right < left) { tmp =

    right; right = left; left = tmp; } ʮҰ࣌తͳ஋ͷஔ͖৔ʯ ͱ͍͏ҙਤ͕໌֬
  13. ͜ͷtmp͸μϝ // Ϣʔβʔ৘ใΛจࣈྻԽ String tmp = user.name(); tmp += "

    " + user.phone_number(); tmp += " " + user.email(); ... template.put("user_info", tmp); tmpͰ͸ͳ͘ɺuserInfo౳ʹ͢΂͖ ΋͠zpMF@JOGPz౳ʹ ؒҧ͑ͯ΋ؾ͔ͮͳ͍
  14. ϧʔϓΠςϨʔλʔ i, j, k, iter͸ɺ୹͍͚ͲΑ͘ҙຯΛද͍ͯ͠Δɻ 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 + "]"); όάɿLͱK͕ٯ Ͱ΋ɺ࣍ͷΑ͏ͳέʔεͰ͸Ͳ͏ͩΖ͏
  15. ϧʔϓΠςϨʔλʔ վળɿͳΜͷΠςϨʔλʔ͔໌֬ʹ͠Α͏ // ci = clubs_iter, mi = members_iter, ui

    = user_iter 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 + "]"); όά͕͙͢ʹղΔ
  16. /** ೚ҙͷTCP/IPϙʔτΛαʔό͕ϦοεϯͰ͖Δ͔֬ೝ͢Δ. */ public boolean serverCanStart() { ... } ػೳʹର͠ɺ

    ϝιου໊͕ந৅త /** ೚ҙͷTCP/IPϙʔτΛαʔό͕ϦοεϯͰ͖Δ͔֬ೝ͢Δ.(मਖ਼൛) */ public boolean canListenOnPort() { ... }
  17. // ॲཧ࣌ؒͷܭଌ long start = new Date().getTime(); ... long elapsed

    = new Date().getTime() - start; System.out.println("ಡΈࠐΈ࣌ؒɿ" + elapsed + "ඵ"); ϛϦඵΛอ͍࣋ͯ͠ΔͨΊɺόά //ॲཧ࣌ؒͷܭଌʢमਖ਼൛ʣ long start_ms = new Date().getTime(); ... long elapsed_ms = new Date().getTime() - start_ms; System.out.println("ಡΈࠐΈ࣌ؒɿ" + (elapsed_ms / 1000L) + "ඵ");
  18. ϝιουͷԾҾ਺ ୯ҐΛ௥Ճͨ͠ԾҾ਺ start(int delay) delaySecs, delay_secs createCache(int size) sizeMb, size_mb

    throttleDownload(float limit) maxKbps, max_kbps rotate(float angle) degreesCw, degrees_cw ୯ҐΛ௥Ճͨ͠ྫ
  19. ม਺໊ վળޙͷม਺໊ password plaintextPassword comment unescapedComment html htmlUtf8 data dataUrlEnc

    ॏཁͳଐੑΛ௥Ճͨ͠ྫ όάʹͳΓͦ͏ͳͱ͜Ζ͚ͩ࢖͏ͷ͕ϙΠϯτ
  20. // είʔϓ͕খ͚͞Ε͹୹໊͍લͰ΋͍͍ if (debug) { Map<String, Integer> m = new

    HashMap<>(); lookUpNamesNumbers(m); System.out.println(m); } ! ! // ΋͠ɺϝϯόม਺΍άϩʔόϧม਺ͩͱmͷ໨త͕෼͔Βͳ͍ lookUpNamesNumbers(m); System.out.println(m);
  21. جຊ͚ͩͲɺ͓͞Β͍͠Α͏ // ΫϥεɾΠϯλʔϑΣʔε͸େจࣈͰ࢝ΊΔ 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() { ... } }
  22. ໊લʹ৘ใΛ٧ΊࠐΉʹ͸ • ໌֬ͳ୯ޠΛબͿ ✴ ঢ়گʹԠͯ͡getͰ͸ͳ͘fetch΍downloadΛ࢖͏ • ൚༻తͳ໊લΛආ͚Δ ✴ ໌֬ͳཧ༝͕ͳ͚Ε͹ɺtmp΍resultΛ࢖Θͳ͍ •

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