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

コードで事前条件を表明する

A051bd5f00287a5a332a146b04f14595?s=47 えぐ
February 15, 2017
2.6k

 コードで事前条件を表明する

potatotips #37で発表したスライドです

A051bd5f00287a5a332a146b04f14595?s=128

えぐ

February 15, 2017
Tweet

Transcript

  1. ίʔυͰ
 ࣄલ৚݅Λද໌͢Δ potatotips #37 2017/2/15

  2. ࣗݾ঺հ • ֎ࢁ ࣏༝ • @duane0728 • Ϡϑʔגࣜձࣾ

  3. ࣄલ৚݅

  4. ࣄલ৚݅ • ʮΦϒδΣΫτࢦ޲ೖ໳ʯͰ঺հ͞Ε͍ͯΔ
 ʮܖ໿ʹΑΔઃܭʯʹ͋Δߟ͑ํ • ࣄલ৚݅͸ϝιουΛ࢖͏ଆ͕ຬͨ͢΂͖৚݅

  5. public final Observable<T> takeLast(long count, long time, TimeUnit unit, Scheduler

    scheduler, boolean delayError, int bufferSize) {
 ObjectHelper.requireNonNull(unit, "unit is null");
 ObjectHelper.requireNonNull(scheduler, "scheduler is null");
 ObjectHelper.verifyPositive(bufferSize, "bufferSize");
 if (count < 0) {
 throw new IndexOutOfBoundsException( "count >= 0 required but it was " + count);
 }
 return RxJavaPlugins.onAssembly(new ObservableTakeLastTimed<T>(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable
  6. public final Observable<T> takeLast(long count, long time, TimeUnit unit, Scheduler

    scheduler, boolean delayError, int bufferSize) {
 ObjectHelper.requireNonNull(unit, "unit is null");
 ObjectHelper.requireNonNull(scheduler, "scheduler is null");
 ObjectHelper.verifyPositive(bufferSize, "bufferSize");
 if (count < 0) {
 throw new IndexOutOfBoundsException( "count >= 0 required but it was " + count);
 }
 return RxJavaPlugins.onAssembly(new ObservableTakeLastTimed<T>(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable
  7. ࣄલ৚݅Λද໌͢ΔϝϦοτ • ϝιου࢓༷ͷ୅ΘΓʹͳΔ • ίʔυΛ௚઀ΈΔػձ͕ଟ͍৔߹ʹ͸༗ޮ • σόοά͕༰қʹͳΔ - ྫ֎Λ౤͛Δ৔߹ •

    ܖ໿͕ഁΒΕͨ࣌ʹྫ֎Λ౤͛ΔͨΊ • ϝιουΛݺͿଆͱݺ͹ΕΔଆͷؒͰ੹೚ྖҬΛ໌֬ʹͰ͖Δ • ࣄલ৚͕݅कΒΕΔલఏͳͷͰඞཁҎ্ͷ๷ޚతॲཧΛ͠ͳͯ͘͢Ή
  8. ࣄલ৚݅Λද໌͢Δํ๏

  9. public void doSomething(String foo) {
 if (foo == null) {


    throw new IllegalArgumentException("");
 }
 if (foo.length() > 100) {
 throw new IllegalArgumentException("");
 }
 if (state != null) {
 throw new IllegalStateException("");
 }
 
 // ...
 } If-Then-Throw ίʔυྔ͕ଟ͘ෳࡶ
 هड़తͰ͸ͳ͍
  10. fun doSomething(foo: String) {
 requireNotNull(foo) { "" } // ࠓճͷέʔεͰ͸ෆཁͰ͋Δ͕


    require(foo.length <= 100) { "" }
 checkNotNull(state) { "" } 
 // ...
 } Precondition.kt in Kotlin ͪ͜Βͷ΄͏͕ΑΓهड़త
  11. public void doSomething(String foo) {
 requireNotNull(foo, "");
 require(foo.length() <= 100,

    "");
 checkNotNull(state != null, "");
 
 // ...
 } PreconditionΛࣗ࡞
  12. public final class Precondition {
 
 public static <T> T

    requireNotNull(T value, String message) {
 if (value == null) {
 throw new IllegalArgumentException(message);
 }
 return value;
 }
 
 public static <T> void require(boolean condition, String message) {
 if (!condition) {
 throw new IllegalArgumentException(message);
 }
 }
 
 public static <T> T checkNotNull(T value, String message) {
 if (value == null) {
 throw new IllegalStateException(message);
 }
 return value;
 }
 }
  13. public final class Precondition {
 
 public static <T> T

    requireNotNull(T value, String message) {
 if (value == null) {
 throw new IllegalArgumentException(message);
 }
 return value;
 }
 
 public static <T> void require(boolean condition, String message) {
 if (!condition) {
 throw new IllegalArgumentException(message);
 }
 }
 
 public static <T> T checkNotNull(T value, String message) {
 if (value == null) {
 throw new IllegalStateException(message);
 }
 return value;
 }
 }
  14. public static <T> T requireNotNull(T value, String message) {
 if

    (value == null) {
 IllegalArgumentException iae = new IllegalArgumentException(message);
 StackTraceElement[] source = iae.getStackTrace();
 
 StackTraceElement[] updated = new StackTraceElement[source.length - 1];
 System.arraycopy(source, 1, updated, 0, updated.length);
 
 iae.setStackTrace(updated);
 throw iae;
 }
 return value;
 } ࢀߟݩ: Crash Fast: AndroidͷΫϥογϡʹର͢ΔSquareͷΞϓϩʔν
  15. ಛʹࣄલ৚݅Λද໌͍ͨ͠έʔε

  16. • ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ

  17. public final Observable<T> takeLast(long count, long time, TimeUnit unit, Scheduler

    scheduler, boolean delayError, int bufferSize) {
 ObjectHelper.requireNonNull(unit, "unit is null");
 ObjectHelper.requireNonNull(scheduler, "scheduler is null");
 ObjectHelper.verifyPositive(bufferSize, "bufferSize");
 if (count < 0) {
 throw new IndexOutOfBoundsException( "count >= 0 required but it was " + count);
 }
 return RxJavaPlugins.onAssembly(new ObservableTakeLastTimed<T>(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable
  18. • ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ

  19. class Foo {
 
 private Bar state;
 
 void setState(Bar

    state) {
 if (state == null) {
 throw new IllegalArgumentException("state must not be null.");
 }
 this.state = state;
 }
 
 void useState() {
 if (state == null) {
 throw new IllegalStateException("setState must be called.");
 }
 state.doSomething();
 }
 }
  20. class Foo {
 
 private Bar state;
 
 void setState(Bar

    state) {
 if (state == null) {
 throw new IllegalArgumentException("state must not be null.");
 }
 this.state = state;
 }
 
 void useState() {
 if (state == null) {
 throw new IllegalStateException("setState must be called.");
 }
 state.doSomething();
 }
 }
  21. • ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ

    • υϝΠϯϞσϧ • ϏδωεϧʔϧΛຬͨͨ͢Ί
  22. final class EmailAddress {
 
 private final String address;
 


    EmailAddress(String address) {
 if (address == null) { 
 throw new IllegalArgumentException();
 }
 if (address.length() == 0) {
 throw new IllegalArgumentException();
 }
 if (address.length() > 100) {
 throw new IllegalArgumentException();
 }
 if (!Pattern.matches("regex", address)) {
 throw new IllegalArgumentException();
 }
 
 this.address = address;
 }
 } Ҿ༻ݩ: ࣮ફυϝΠϯۦಈઃܭ[5.3]
  23. • ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ

    • υϝΠϯϞσϧ • ϏδωεϧʔϧΛຬͨͨ͢Ί
  24. ʮࣄલ৚݅ʯ΍ʮܖ໿ʹΑΔઃܭʯΛ
 ΑΓৄ͘͠஌Γ͍ͨํ͸
 ʮΦϒδΣΫτࢦ޲ೖ໳ʯΛಡΜͰΈ͍ͯͩ͘͞ɻ ࠷ޙʹ

  25. ͋Γ͕ͱ͏͍͟͝·ͨ͠

  26. ࢀߟจݙɾࢿྉ • ΦϒδΣΫτࢦ޲ೖ໳ ୈ2൛ ݪଇɾίϯηϓτ • https://goo.gl/O5M4Z7 • .̣̩̚ͷΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟ •

    https://goo.gl/iYF1DJ • ࣮ફυϝΠϯۦಈઃܭ • https://goo.gl/rxmrnC • Crash Fast: AndroidͷΫϥογϡʹର͢ΔSquareͷΞϓϩʔν • https://goo.gl/wCJaJW