Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

ࣄલ৚݅

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

public final Observable 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(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable

Slide 6

Slide 6 text

public final Observable 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(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable

Slide 7

Slide 7 text

ࣄલ৚݅Λද໌͢ΔϝϦοτ • ϝιου࢓༷ͷ୅ΘΓʹͳΔ • ίʔυΛ௚઀ΈΔػձ͕ଟ͍৔߹ʹ͸༗ޮ • σόοά͕༰қʹͳΔ - ྫ֎Λ౤͛Δ৔߹ • ܖ໿͕ഁΒΕͨ࣌ʹྫ֎Λ౤͛ΔͨΊ • ϝιουΛݺͿଆͱݺ͹ΕΔଆͷؒͰ੹೚ྖҬΛ໌֬ʹͰ͖Δ • ࣄલ৚͕݅कΒΕΔલఏͳͷͰඞཁҎ্ͷ๷ޚతॲཧΛ͠ͳͯ͘͢Ή

Slide 8

Slide 8 text

ࣄલ৚݅Λද໌͢Δํ๏

Slide 9

Slide 9 text

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 ίʔυྔ͕ଟ͘ෳࡶ
 هड़తͰ͸ͳ͍

Slide 10

Slide 10 text

fun doSomething(foo: String) {
 requireNotNull(foo) { "" } // ࠓճͷέʔεͰ͸ෆཁͰ͋Δ͕
 require(foo.length <= 100) { "" }
 checkNotNull(state) { "" } 
 // ...
 } Precondition.kt in Kotlin ͪ͜Βͷ΄͏͕ΑΓهड़త

Slide 11

Slide 11 text

public void doSomething(String foo) {
 requireNotNull(foo, "");
 require(foo.length() <= 100, "");
 checkNotNull(state != null, "");
 
 // ...
 } PreconditionΛࣗ࡞

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

public static 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ͷΞϓϩʔν

Slide 15

Slide 15 text

ಛʹࣄલ৚݅Λද໌͍ͨ͠έʔε

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

public final Observable 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(this, count, time, unit, scheduler, bufferSize, delayError));
 } RxJava.Observable

Slide 18

Slide 18 text

• ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ

Slide 19

Slide 19 text

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();
 }
 }

Slide 20

Slide 20 text

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();
 }
 }

Slide 21

Slide 21 text

• ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ • υϝΠϯϞσϧ • ϏδωεϧʔϧΛຬͨͨ͢Ί

Slide 22

Slide 22 text

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]

Slide 23

Slide 23 text

• ϥΠϒϥϦ • ࣄલ৚݅ͷද໌͕ͦͷ··࢓༷ʹͳΓಘΔ • ঢ়ଶΛ΋ͭΫϥε • Πϯελϯε͕ෆਖ਼ͳঢ়ଶʹͳΔͷΛ๷͙ • σόοάΛ༰қʹ͢Δ • υϝΠϯϞσϧ • ϏδωεϧʔϧΛຬͨͨ͢Ί

Slide 24

Slide 24 text

ʮࣄલ৚݅ʯ΍ʮܖ໿ʹΑΔઃܭʯΛ
 ΑΓৄ͘͠஌Γ͍ͨํ͸
 ʮΦϒδΣΫτࢦ޲ೖ໳ʯΛಡΜͰΈ͍ͯͩ͘͞ɻ ࠷ޙʹ

Slide 25

Slide 25 text

͋Γ͕ͱ͏͍͟͝·ͨ͠

Slide 26

Slide 26 text

ࢀߟจݙɾࢿྉ • ΦϒδΣΫτࢦ޲ೖ໳ ୈ2൛ ݪଇɾίϯηϓτ • https://goo.gl/O5M4Z7 • .̣̩̚ͷΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟ • https://goo.gl/iYF1DJ • ࣮ફυϝΠϯۦಈઃܭ • https://goo.gl/rxmrnC • Crash Fast: AndroidͷΫϥογϡʹର͢ΔSquareͷΞϓϩʔν • https://goo.gl/wCJaJW