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

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

irof
May 20, 2017
21k

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

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

irof

May 20, 2017
Tweet

Transcript

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

    ࿡ஈ 4USFBNͱͷ෇͖߹͍͔ͨ ࣣஈ ܕΛҙࣝ͢Δ ീஈ جຊతͳࢦ਑Λ࣋ͭ ۝ஈ 
  2. if (hoge.isFuga()) { //...(a) } //...ʢ͕͜͜௕͍΄Ͳμϝ͕͞૿͢ʣ if (hoge.isFuga()) { //...(b)

    } टΛ܏͛ͯ͠·͏JG  B Λ௨ͬͯ C Λ௨Βͳ͍ͷ͕ύε্ଘࡏ͢Δ
  3. int value = ... // 0 or 1 if (value

    == 0) { //...(a) } if (value == 1) { //...(b) } टΛ܏͛ͯ͠·͏JG  Լ͸WBMVF΍FMTFͱ͸ҧ͏ҙຯͳͷʁ B ΋ C ΋௨Βͳ͍ͷͬͯ͋Δͷʁ ͍͍ͩͨͷ৔߹͸FOVNΛ࢖͏΂͖ɻ FOVNΛ࢖͍౗͢
  4. Charset.forName("UTF-8") StandardCharset.UTF_8 list.isEmpty() list.size() == 0 !list.isEmpty() list.size() != 0

    list.size() > 0 ಉ͜͡ͱ͸ಉ͡Α͏ʹॻ͘ ಉ͡ϝιου಺ʹࠞࡏͯ͠ΔͱͭΒ͍
  5. ❌ ⭕ ৚݅ࣜΑΓϝιουΛ޷Ή if (list.size() == 0) { ... if

    (age >= 20) { ... if(list.isEmpty()) { ... if (isAdult()) { ...
  6. ❌ ⭕ BOE΍PS͸ϝιουΛநग़͢Δ if (hoge.isFoo() || hoge.isBar()) { //... }

    if (hoge.isFooOrBar()) { //... } ಉ͡ΦϒδΣΫτͳΒઈର΍Δ΂͖ ಉ͡ΦϒδΣΫτΛԿ౓΋ݺ͹ͳ͍ ৚݅ࣜΑΓϝιουΛ޷Ή
  7. ❌ ⭕ ·ͱΊΔͱ if (status == Status.SUCCESS || status ==

    Status.FAIURE) { //... } if (status.isFinish()) { //... } FOVNΛͨͩͷྻڍͱͯ͠࢖ͬͯΔͱ΍Γ͕ͪͰ͢Ͷ FOVNΛ࢖͍౗͢
  8. class Hoge { Foo foo; Bar bar; Hoge(Foo foo) {

    this.foo = foo; } Hoge(Bar bar) { this.bar = bar; } ... } த్൒୺ͳΫϥεΛ࡞Βͳ͍ GPP΋͘͠͸CBS͔͠ೖΒͳ͍Ϋϥε
  9. Object obj; if (condA()) obj = methodA(); if (condB()) obj

    = methodB(); if (condC()) obj = methodC(); // ... objΛ࢖ͬͯԿ͔͢Δ ࠶୅ೖΛආ͚Δ PCKͷঢ়ଶ͕ෆ҆ఆʹͳΔ
  10. class ImmutableObject { final Hoge hoge; ImmutableObject(Hoge hoge) { this.hoge

    = hoge; } } ΠϛϡʔλϒϧΦϒδΣΫτΛ޷Ή ఆٛͰ͸ʮTFUUFSΛ࣋ͨͳ͍ɺϑΟʔϧυ͕pOBMͰQSJWBUFɺΫϥε͕pOBMɺΠϯελϯεϑΟʔϧυ͕Πϛϡʔλϒϧʯ͚ͩͲɺͦ͜·Ͱؾʹ͠ͳ͍ɻ ϑΟʔϧυ͕શͯpOBMͳΫϥε
  11. ❌ ⭕ ίϯετϥΫλ͸νΣΠϯ͢Δ Hoge(Object v) { this.v = v; }

    Hoge() { this.v = "defaultValue"; } Hoge() { this("defaultValue"); }
  12. try (ExResource er = hoge::fuga) { //... } ----------------------------------- class

    Hoge { void fuga() { ... } } interface ExResource extends AutoCloseable { @Override void close(); } USZXJUISFTPVSDFTΛ࢖͏খٕ &YDFQUJPOΛ౤͛ͳ͍DMPTFϝιουΠϯλϑΣʔεʹϝιουϦϑΝϨϯεͰͭͳ͛Δɻ Ϋϥεʹ"VUP$MPTFBCMFΛ࣮૷ͤ͞Δͷ΋ݕ౼্ͨ͠Ͱɻ
  13. ❌ ⭕ ಋग़Ͱ͖Δ஋Λอ࣋͠ͳ͍ boolean available; void setValue(Object value) { if

    (value != null) this.available = true; } boolean isAvailable() { return this.value != null; }
  14. void method(Object parameter) { // ύϥϝʔλ͕null͔΋͠Εͳ͍ if (parameter == null)

    { return; } // ... } ๷ޚతϓϩάϥϛϯάΛආ͚Δ ໰୊ͷઌૹΓPSϊΠζ   OVMMʹ޲͖߹͏
  15. ❌ ⭕ ΑΓߴϨϕϧͷ"1*Λ࢖༻͢Δ @RequestMapping(value = "/hoge", method = RequestMethod.GET) String

    hoge() { ... @GetMapping("/hoge") String hoge() { ... ݻఆ஋ͷҾ਺͸ϝιουΛ࡞Δ
  16. ❌ ⭕ ϝιουϦϑΝϨϯεΛ޷Ή stream .filter(hoge -> hoge.isFuga()) ... stream .filter(Hoge::isHoge)

    ... ϥϜμࣜ͸؆୯ʹෳࡶͰಡΊͳ͍΋ͷʹͳΔɻϝιουϦϑΝϨϯεΛ࢖͏΄͏͕͍͍श׳͕ͭ͘ɻ
  17. class Hoge { LocalDate date = LocalDate.MIN; LocalDate getDate() {

    return date; } } ಛघͳ஋Λ֎ʹग़͞ͳ͍ .*/͕֎ʹग़ͯخ͍͜͠ͱ͸ͳ͍ OVMM͕Α͘࢖ΘΕΔ͚ͲɺOVMM͸ผ్ɻ   OVMMʹ޲͖߹͏
  18. ❌ ⭕ ΪϦΪϦ·ͰจࣈྻʹཔΒͳ͍ if (foo.str().equals(bar.str()) { ... } if (foo.hasSameStr(bar))

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

    = ""; // ͨͿΜnullΛ࢖͏΂͖͡Όͳ͍ Hoge hoge = null;   OVMMʹ޲͖߹͏
  20. ❌ ⭕ ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ Hoge hoge = new Hoge(); hoge.setValue(value); ...

    Hoge hoge = new Hoge(value); ΠϛϡʔλϒϧΦϒδΣΫτΛ޷Ή
  21. 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; ... ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ ຊ౰ʹσϑΥϧτίϯετϥΫλΛ࢖༻ͨ͠ΠϯελϯεͰ͍͍ͷʁ σϑΥϧτίϯετϥΫλͰੜ੒͞ΕΔΠϯελϯε͸׬શʹಈ࡞͢ΔΑ͏ʹ࡞ΒΕͯΔͷʁ ୯ͳΔ஋ͷίϯςφʹͳͬͯͳ͍ʁͦͷίʔυಡΈ΍͍͢ʁ