42
Don’t Reinvent The Wheel Kenju Wagatsuma

Dont Reinvent The Wheel ~ For All Android Beginners ~

Embed Size (px)

Citation preview

Page 1: Dont Reinvent The Wheel ~ For All Android Beginners ~

Don’t Reinvent The Wheel

Kenju Wagatsuma

Page 2: Dont Reinvent The Wheel ~ For All Android Beginners ~

Agenda: ->

❖ 1m : Introduction ❖ 3m : Main part ❖ 1m : Conclusion

Page 3: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * Introduce Myself * * @kenjuwagatsuma */

Page 4: Dont Reinvent The Wheel ~ For All Android Beginners ~

'me' = { name : 'Kenju Wagatsuma ( KJ )’,

}

Page 5: Dont Reinvent The Wheel ~ For All Android Beginners ~

'me' = { name : 'Kenju Wagatsuma ( KJ )’, company : 'Recruit Technologies Co.,LTD.',

}

Page 6: Dont Reinvent The Wheel ~ For All Android Beginners ~

'me' = { name : 'Kenju Wagatsuma ( KJ )’, company : 'Recruit Technologies Co.,LTD.', profession : 'Android Development',

}

Page 7: Dont Reinvent The Wheel ~ For All Android Beginners ~

'me' = { name : 'Kenju Wagatsuma ( KJ )’, company : 'Recruit Technologies Co.,LTD.', profession : 'Android Development', favs : { 'Book' : ‘Soft Skills', 'Music' : ‘The Beatles', 'Hobby' : ‘Acoustic Guitar', 'Sport' : 'Rugby' } }

Page 8: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * My Story * * @author me */

Page 9: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

/** * Validate user id input from a form * * @param String userID */ private void validateUserId(String userId) {

// Validate userId

}

```

Page 10: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

private void validateUserId(String userId) { // Check input field here if (userId == null || userId.length() == 0) { return; }

// Validate userId

}

```

Page 11: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

// Utility class public static class UtilClass { public static boolean checkStringInput(String str) { if (str == null || str.length() == 0) { return true; } else { return false; } } }

// Activity class public void MainActivity extends Activity { private void validateUserId(String userId) { if(UtilClass.checkStringInput(userId)) return;

// Validate userId // ... } }

```

Page 12: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * @codereview * */

Page 13: Dont Reinvent The Wheel ~ For All Android Beginners ~
Page 14: Dont Reinvent The Wheel ~ For All Android Beginners ~
Page 15: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

private void validateUserId(String userId) { // Check input field here if (userId == null || userId.length() == 0) { return; }

// Validate userId

}

```

Page 16: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

private void validateUserId(String userId) { // Check input field here if (TextUtils.isEmpty(userId)) { return; }

// Validate userId

}

```

Page 17: Dont Reinvent The Wheel ~ For All Android Beginners ~

Okay, I’ll look at the source code, sir…

Page 18: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

/** * Returns true if the string is null or 0-length. * @param str the string to be examined * @return true if str is null or zero length */ public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; }

```

Page 19: Dont Reinvent The Wheel ~ For All Android Beginners ~

……

Page 20: Dont Reinvent The Wheel ~ For All Android Beginners ~

……「いっしょやんけw」

Page 21: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

public static boolean checkStringInput(String str) { if (str == null || str.length() == 0) { return true; } else { return false; } }

```

My Code

Page 22: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; }

```

android.TextUtils

Page 23: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

public static boolean checkStringInput(String str) { if (str == null || str.length() == 0) { return true; } else { return false; } }

```

My Code

Page 24: Dont Reinvent The Wheel ~ For All Android Beginners ~

```java

public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; }

```

android.TextUtils

Page 25: Dont Reinvent The Wheel ~ For All Android Beginners ~

……「いっしょやんけw」(再)

Page 26: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * Why TextUtils? * (not my own code) * */

Page 27: Dont Reinvent The Wheel ~ For All Android Beginners ~

#47 「ライブラリーを知り、 ライブラリーを使う」

“Effective Java” 2nd Edition

Page 28: Dont Reinvent The Wheel ~ For All Android Beginners ~

#47 「ライブラリーを知り、ライブラリーを使う」

利点1 ❖ 標準ライブラリを使用することで、それを書いた専門家の知識と、それをあなたよりも前に使用した人々の経験を利用することになります。

“Effective Java” 2nd Edition

Page 29: Dont Reinvent The Wheel ~ For All Android Beginners ~

#47 「ライブラリーを知り、ライブラリーを使う」

利点2 ❖ 自分の課題に少しばかり関連している問題に対する場当たり的な解決策を書くことで、時間を無駄にする必要が無い。

“Effective Java” 2nd Edition

Page 30: Dont Reinvent The Wheel ~ For All Android Beginners ~

#47 「ライブラリーを知り、ライブラリーを使う」

利点3 ❖ 自分では何もしなくても、多くの人によって繰り返し書き直されるコードは、時間と共にパフォーマンスが改善される(自分のコードではそうはいかない)。

“Effective Java” 2nd Edition

Page 31: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * So what? * * @return Conclusion */

Page 32: Dont Reinvent The Wheel ~ For All Android Beginners ~

#47 「ライブラリーを知り、ライブラリーを使う」

For All Beginners,

Page 33: Dont Reinvent The Wheel ~ For All Android Beginners ~

1. Know Android Library

2. Use Library methods

3. See & Understand source codes

3 Steps for Learning,

Page 34: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * Appendix * */

Page 35: Dont Reinvent The Wheel ~ For All Android Beginners ~

❖ android.text.format.DateUtils ❖ android.text.format.Formatter ❖ android.text.TextUtils ❖ android.text.util.Linkify ❖ android.util.Pair<F, S> ❖ android.util.SparseArray<E> ❖ android.util.Log ❖ android.util.LruCache ❖ android.graphics.Color ❖ android.media.ThumbnailUtils

android.*

Page 36: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.text.TextUtils```java

/** * 与えられた文字列がすべて数値かどうかを判定

*/ public static boolean isDigitsOnly(CharSequence str) {

final int len = str.length(); for (int i = 0; i< len; i++) { if (!Character.isDigit(str.charAt(i)) { return false;

} } return true;

}

```

Page 37: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.text.TextUtils```java

/** * HTMLエンコード

*/ public static boolean htmlEncode(String s) { StringBuilder sb = new StringBuilder(); char c; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); switch(c) { case '<': sb.append("&lt;"); break; //...その他のエンコードすべき文字

default: sb.append(c); } } return sb.toString(); }

Page 38: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.text.TextUtils```java

/** * trim()で削除された空白の数を返します

*/ public static int getTrimmedLength(CharSequence s) { int len = s.length(); int start = 0; while (start < len && s.charAt(start) <= ' ') { start++; } int end = 0; while (end > start && s.charAt(end - 1) <= ' ') { end--; }

return end - start; }

```

Page 39: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.database.DatabaseUtils```java

/** * WHERE句作成のヘルパーメソッド

*/ public static String concatenateWhere(String a, String b) { if (TextUtils.isEmpty(a)) { return b; } if (TextUtils.isEmpty(b)) { return a; }

return "(" + a + ") AND (" + b + ")"; }

```

Page 40: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.database.DatabaseUtils```java

/** * Cursorの中身を出力するデバッグ用メソッド

*/ public static void dumpCursor(Cursor cursor) { dumpCursor(cursor, System.out); } public static void dumpCursor(Cursor cursor, PrintStream stream) { stream.println(">>>>> Dumping cursor " + cursor); if (cursor != null) { int startPos = cursor.getPosition();

cursor.moveToPosition(-1); while (cursor.moveToNext()) { dumpCurrentRow(cursor, stream); } cursor.moveToPosition(startPos); } stream.println("<<<<<"); } ```

Page 41: Dont Reinvent The Wheel ~ For All Android Beginners ~

android.database.DatabaseUtils```java

/** * Cursorの現在の行を出力する

*/ public static void dumpCurrentRow(Cursor cursor, PrintStream stream) { String[] cols = cursor.getColumnNames(); stream.println("" + cursor.getPosition() + " {"); int length = cols.length; for (int i = 0; i < length; i++) { String value; try { value = cursor.getString(i); } catch (SQLiteException e) { value = "<unprintable>"; } stream.println(" " + cols[i] + '=' + value); } stream.println("}"); }

```

Page 42: Dont Reinvent The Wheel ~ For All Android Beginners ~

/** * And Much More! * * Happy Coding! * */