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

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

えぐ
February 15, 2017
3.2k

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

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

えぐ

February 15, 2017
Tweet

Transcript

  1. 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
  2. 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
  3. ࣄલ৚݅Λද໌͢ΔϝϦοτ • ϝιου࢓༷ͷ୅ΘΓʹͳΔ • ίʔυΛ௚઀ΈΔػձ͕ଟ͍৔߹ʹ͸༗ޮ • σόοά͕༰қʹͳΔ - ྫ֎Λ౤͛Δ৔߹ •

    ܖ໿͕ഁΒΕͨ࣌ʹྫ֎Λ౤͛ΔͨΊ • ϝιουΛݺͿଆͱݺ͹ΕΔଆͷؒͰ੹೚ྖҬΛ໌֬ʹͰ͖Δ • ࣄલ৚͕݅कΒΕΔલఏͳͷͰඞཁҎ্ͷ๷ޚతॲཧΛ͠ͳͯ͘͢Ή
  4. 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 ίʔυྔ͕ଟ͘ෳࡶ
 هड़తͰ͸ͳ͍
  5. fun doSomething(foo: String) {
 requireNotNull(foo) { "" } // ࠓճͷέʔεͰ͸ෆཁͰ͋Δ͕


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

    "");
 checkNotNull(state != null, "");
 
 // ...
 } PreconditionΛࣗ࡞
  7. 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;
 }
 }
  8. 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;
 }
 }
  9. 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ͷΞϓϩʔν
  10. 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
  11. 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();
 }
 }
  12. 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();
 }
 }
  13. 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]
  14. ࢀߟจݙɾࢿྉ • ΦϒδΣΫτࢦ޲ೖ໳ ୈ2൛ ݪଇɾίϯηϓτ • https://goo.gl/O5M4Z7 • .̣̩̚ͷΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟ •

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