ふつうのJavaコーディング #ccc_e1

7919a30bc59113c94f31676158cb5881?s=47 irof
May 20, 2017
17k

ふつうのJavaコーディング #ccc_e1

JJUG CCC 2017 Spring E1
2017-05-20 10:00-10:45

7919a30bc59113c94f31676158cb5881?s=128

irof

May 20, 2017
Tweet

Transcript

  1. 15.

    ॳஈ ೖ໳ ೋஈ ݞ׳Β͠ ࡾஈ ϝιουΛखջ͚Δ ࢛ஈ ҆৺Ͱ͖ΔΦϒδΣΫτ ޒஈ දݱྗʹͩ͜ΘΔ

    ࿡ஈ 4USFBNͱͷ෇͖߹͍͔ͨ ࣣஈ ܕΛҙࣝ͢Δ ീஈ جຊతͳࢦ਑Λ࣋ͭ ۝ஈ 
  2. 16.
  3. 19.
  4. 21.

    if (hoge.isFuga()) { //...(a) } //...ʢ͕͜͜௕͍΄Ͳμϝ͕͞૿͢ʣ if (hoge.isFuga()) { //...(b)

    } टΛ܏͛ͯ͠·͏JG  B Λ௨ͬͯ C Λ௨Βͳ͍ͷ͕ύε্ଘࡏ͢Δ
  5. 22.

    int value = ... // 0 or 1 if (value

    == 0) { //...(a) } if (value == 1) { //...(b) } टΛ܏͛ͯ͠·͏JG  Լ͸WBMVF΍FMTFͱ͸ҧ͏ҙຯͳͷʁ B ΋ C ΋௨Βͳ͍ͷͬͯ͋Δͷʁ ͍͍ͩͨͷ৔߹͸FOVNΛ࢖͏΂͖ɻ FOVNΛ࢖͍౗͢
  6. 24.

    Charset.forName("UTF-8") StandardCharset.UTF_8 list.isEmpty() list.size() == 0 !list.isEmpty() list.size() != 0

    list.size() > 0 ಉ͜͡ͱ͸ಉ͡Α͏ʹॻ͘ ಉ͡ϝιου಺ʹࠞࡏͯ͠ΔͱͭΒ͍
  7. 25.
  8. 30.

    ❌ ⭕ ৚݅ࣜΑΓϝιουΛ޷Ή if (list.size() == 0) { ... if

    (age >= 20) { ... if(list.isEmpty()) { ... if (isAdult()) { ...
  9. 32.

    ❌ ⭕ BOE΍PS͸ϝιουΛநग़͢Δ if (hoge.isFoo() || hoge.isBar()) { //... }

    if (hoge.isFooOrBar()) { //... } ಉ͡ΦϒδΣΫτͳΒઈର΍Δ΂͖ ಉ͡ΦϒδΣΫτΛԿ౓΋ݺ͹ͳ͍ ৚݅ࣜΑΓϝιουΛ޷Ή
  10. 33.

    ❌ ⭕ ·ͱΊΔͱ if (status == Status.SUCCESS || status ==

    Status.FAIURE) { //... } if (status.isFinish()) { //... } FOVNΛͨͩͷྻڍͱͯ͠࢖ͬͯΔͱ΍Γ͕ͪͰ͢Ͷ FOVNΛ࢖͍౗͢
  11. 36.
  12. 37.

    class Hoge { Foo foo; Bar bar; Hoge(Foo foo) {

    this.foo = foo; } Hoge(Bar bar) { this.bar = bar; } ... } த్൒୺ͳΫϥεΛ࡞Βͳ͍ GPP΋͘͠͸CBS͔͠ೖΒͳ͍Ϋϥε
  13. 38.

    Object obj; if (condA()) obj = methodA(); if (condB()) obj

    = methodB(); if (condC()) obj = methodC(); // ... objΛ࢖ͬͯԿ͔͢Δ ࠶୅ೖΛආ͚Δ PCKͷঢ়ଶ͕ෆ҆ఆʹͳΔ
  14. 39.

    class ImmutableObject { final Hoge hoge; ImmutableObject(Hoge hoge) { this.hoge

    = hoge; } } ΠϛϡʔλϒϧΦϒδΣΫτΛ޷Ή ఆٛͰ͸ʮTFUUFSΛ࣋ͨͳ͍ɺϑΟʔϧυ͕pOBMͰQSJWBUFɺΫϥε͕pOBMɺΠϯελϯεϑΟʔϧυ͕Πϛϡʔλϒϧʯ͚ͩͲɺͦ͜·Ͱؾʹ͠ͳ͍ɻ ϑΟʔϧυ͕શͯpOBMͳΫϥε
  15. 41.

    ❌ ⭕ ίϯετϥΫλ͸νΣΠϯ͢Δ Hoge(Object v) { this.v = v; }

    Hoge() { this.v = "defaultValue"; } Hoge() { this("defaultValue"); }
  16. 43.

    try (ExResource er = hoge::fuga) { //... } ----------------------------------- class

    Hoge { void fuga() { ... } } interface ExResource extends AutoCloseable { @Override void close(); } USZXJUISFTPVSDFTΛ࢖͏খٕ &YDFQUJPOΛ౤͛ͳ͍DMPTFϝιουΠϯλϑΣʔεʹϝιουϦϑΝϨϯεͰͭͳ͛Δɻ Ϋϥεʹ"VUP$MPTFBCMFΛ࣮૷ͤ͞Δͷ΋ݕ౼্ͨ͠Ͱɻ
  17. 45.

    ❌ ⭕ ಋग़Ͱ͖Δ஋Λอ࣋͠ͳ͍ boolean available; void setValue(Object value) { if

    (value != null) this.available = true; } boolean isAvailable() { return this.value != null; }
  18. 49.

    void method(Object parameter) { // ύϥϝʔλ͕null͔΋͠Εͳ͍ if (parameter == null)

    { return; } // ... } ๷ޚతϓϩάϥϛϯάΛආ͚Δ ໰୊ͷઌૹΓPSϊΠζ   OVMMʹ޲͖߹͏
  19. 55.

    ❌ ⭕ ΑΓߴϨϕϧͷ"1*Λ࢖༻͢Δ @RequestMapping(value = "/hoge", method = RequestMethod.GET) String

    hoge() { ... @GetMapping("/hoge") String hoge() { ... ݻఆ஋ͷҾ਺͸ϝιουΛ࡞Δ
  20. 58.
  21. 60.

    ❌ ⭕ ϝιουϦϑΝϨϯεΛ޷Ή stream .filter(hoge -> hoge.isFuga()) ... stream .filter(Hoge::isHoge)

    ... ϥϜμࣜ͸؆୯ʹෳࡶͰಡΊͳ͍΋ͷʹͳΔɻϝιουϦϑΝϨϯεΛ࢖͏΄͏͕͍͍श׳͕ͭ͘ɻ
  22. 62.
  23. 63.

    class Hoge { LocalDate date = LocalDate.MIN; LocalDate getDate() {

    return date; } } ಛघͳ஋Λ֎ʹग़͞ͳ͍ .*/͕֎ʹग़ͯخ͍͜͠ͱ͸ͳ͍ OVMM͕Α͘࢖ΘΕΔ͚ͲɺOVMM͸ผ్ɻ   OVMMʹ޲͖߹͏
  24. 64.

    ❌ ⭕ ΪϦΪϦ·ͰจࣈྻʹཔΒͳ͍ if (foo.str().equals(bar.str()) { ... } if (foo.hasSameStr(bar))

    { ... } ʮ͍࣋ͬͯΔจࣈྻϓϩύςΟͰൺֱ͍ͨ͠ʯίʔυ͕ҰͭͰ΋ग़͖ͯͨΒɺ͜ͷΫϥε͸ͦ͏ݴ͏ൺֱͷ͞ΕํΛ͢Δ΋ͷͳͷͰ͢ɻ Ͱ͖Ε͹͜ͷൺֱʹ͸ద੾ͳ໊લΛ༩͍͑ͨͱ͜Ζɻ͜͜Ͱڍ͛ͨΑ͏ͳػցతͳ໊લ͡Όͳ͘Ͷɻ
  25. 68.

    // ͨͿΜ0Λ࢖͍ͨ͘͸ͳ͍ int i = 0; // ͨͿΜۭจࣈʹ͍ͨ͠Θ͚Ͱ͸ͳ͍ String str

    = ""; // ͨͿΜnullΛ࢖͏΂͖͡Όͳ͍ Hoge hoge = null;   OVMMʹ޲͖߹͏
  26. 70.

    ❌ ⭕ ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ Hoge hoge = new Hoge(); hoge.setValue(value); ...

    Hoge hoge = new Hoge(value); ΠϛϡʔλϒϧΦϒδΣΫτΛ޷Ή
  27. 71.

    class Hoge { final Fuga fuga; final Piyo piyo; Hoge()

    { this(new Fuga(), new Piyo()); } Hoge(Fuga fuga, Piyo piyo) { this.fuga = fuga; this.piyo = piyo; ... ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ ຊ౰ʹσϑΥϧτίϯετϥΫλΛ࢖༻ͨ͠ΠϯελϯεͰ͍͍ͷʁ σϑΥϧτίϯετϥΫλͰੜ੒͞ΕΔΠϯελϯε͸׬શʹಈ࡞͢ΔΑ͏ʹ࡞ΒΕͯΔͷʁ ୯ͳΔ஋ͷίϯςφʹͳͬͯͳ͍ʁͦͷίʔυಡΈ΍͍͢ʁ
  28. 73.
  29. 77.
  30. 97.