Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Why don't you use enum ?
Search
Masayuki Izumi
August 11, 2015
Technology
2
3.1k
Why don't you use enum ?
【第20回】potatotips(iOS/Android開発Tips共有会) 発表資料
http://connpass.com/event/17624/
Masayuki Izumi
August 11, 2015
Tweet
Share
More Decks by Masayuki Izumi
See All by Masayuki Izumi
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
820
Next.js App Router を例に考える、技術選定・技術との距離感 #技術選定_findy / findy 2024-01-24
izumin5210
13
5.1k
雑なコード生成のすすめ #nihonbashijs 8 / zatsu-code-generation
izumin5210
0
45
React Server Components で複雑さに立ち向かう #コンポーネント_findy / findy 2023-10-04
izumin5210
10
2.7k
みんなで育てる GraphQL スキーマ, それを支える Protobuf / GraphQL and Protobuf #tech_stand
izumin5210
7
3.4k
GraphQL 導入の反省と再挑戦 / jsconf jp 2021
izumin5210
11
7.9k
HTTP クライアントを作ろうとして学ぶ、使いやすいインタフェース / #GoCon_Sendai 2020
izumin5210
7
4.9k
個人の・組織の Go 筋を強化する / Gophers Code Reading Party
izumin5210
1
300
今あらためて読み直したい Go 基礎知識 その2 / golang.tokyo #25
izumin5210
10
9k
Other Decks in Technology
See All in Technology
コンテナセキュリティの基本と脅威への対策
kyohmizu
3
750
GraphQL 成熟度モデルの紹介と、プロダクトに当てはめた事例 / GraphQL maturity model
mh4gf
7
1.3k
ChatworkのSRE部って実は 半分くらいPlatform Engineering部かもしれない
saramune
0
150
検証を通して見えてきたTiDBの性能特性
lycorptech_jp
PRO
6
3.7k
アクセス制御にまつわる改善 / Improving access control
itkq
0
510
自己改善からチームを動かす! 「セルフエンジニアリングマネージャー」のすゝめ
shoota
6
240
日本におけるデータエンジニアリングのこれまでとこれから
foursue
16
4.1k
どうするコスト最適化のトレードオフ
tetsuyaooooo
1
490
ワールドカフェI /チューターを改良する / World Café I and Improving the Tutors
ks91
PRO
0
120
家族アルバム みてねにおけるGrafana活用術 / Grafana Meetup Japan Vol.1 LT
isaoshimizu
1
220
Java EE/Jakarta EEの現状と将来―クラウドネイティブ時代にJava EEは対応できるのか?―
takakiyo
1
130
EMとして2023年度に頑張ったこと / What we did well in FY2023 as a EM
pauli
1
160
Featured
See All Featured
Fireside Chat
paigeccino
21
2.6k
A Modern Web Designer's Workflow
chriscoyier
689
190k
GraphQLとの向き合い方2022年版
quramy
32
12k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
Large-scale JavaScript Application Architecture
addyosmani
504
110k
Building a Scalable Design System with Sketch
lauravandoore
456
32k
Being A Developer After 40
akosma
57
580k
What's new in Ruby 2.0
geeforr
337
31k
Testing 201, or: Great Expectations
jmmastey
28
6.3k
Into the Great Unknown - MozCon
thekraken
10
990
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
274
13k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Transcript
Why don’t you use enum ? Masayuki IZUMI - @izumin5210
potatotips #20
Ƅ Rekimoto Lab. at the University of Tokyo (2008-2015: Akashi-NCT)
Ɠ Enginner at Wantedly, Inc. (2014.9-2015.2: Dmetlabel, Inc.) 2
2 Ruby JavaScript Android Design pry(main) > izumin.skill_ratio
Enum
Enum as constants public class Status { public static final
int OK = 0; public static final int ERROR = -1; }
public enum Status { OK, ERROR; } public class Status
{ public static final int OK = 0; public static final int ERROR = -1; } Enum as constants
Example - Enum as constants public void onSomethingAction(int status) {
switch (status) { case Status.OK: Log.d(TAG, "status: OK"); break; case Status.ERROR: Log.d(TAG, "status: ERROR"); break; } }
Example - Enum as constants public void onSomethingAction(int status) {
switch (status) { case Status.OK: // => int Log.d(TAG, "status: OK"); break; case Status.ERROR: // => int Log.d(TAG, "status: ERROR"); break; } }
Example - Enum as constants public void onSomethingAction(Status status) {
Log.d(TAG: "status: " + status.name()); }
Example - Enum as constants public void onSomethingAction(Status status) {
Log.d(TAG: "status: " + status.name()); } \ enum /
* type safe ! - "int enum pattern" is fragile
- Add/Remove a new item easliy * Prefixes of names are unnecessary - Because each enum types has its own namespace Enum as constants
Enum that has member constants/methods public enum Status { OK("Succeed
the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Enum that has member constants/methods public enum Status { OK("Succeed
the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Enum that has member constants/methods public enum Status { OK("Succeed
the process!"), ERROR("Error has occurred..."); private final String message; Status(String message) { this.message = message; } private String getMessage() { return message; } }
Example - Enum that has member constants/methods public void onSomethingAction(int
status) { switch (status) { case Status.OK: Log.d(TAG, "Succeed the process!"); break; case Status.ERROR: Log.d(TAG, "Error has occurred..."); break; } }
Example - Enum that has member constants/methods public void onSomethingAction(Status
status) { Log.d(TAG, status.getMessage()); }
Example - Enum that has member constants/methods public void onSomethingAction(Status
status) { Log.d(TAG, status.getMessage()); } \ enum /
Constant-specific method implementations public enum Status { OK("Succeed the process!")
{ @Override void log(String tag) { Log.d(tag, message); } }, ERROR("Error has occurred...") { @Override void log(String tag) { Log.e(tag, message); } }; // ... abstract void log(String tag); }
Constant-specific method implementations public enum Status { OK("Succeed the process!")
{ @Override void log(String tag) { Log.d(tag, message); } }, ERROR("Error has occurred...") { @Override void log(String tag) { Log.e(tag, message); } }; // ... abstract void log(String tag); }
Constant-specific method implementations public enum Status { OK("Succeed the process!")
{ @Override void log(String tag) { Log.d(tag, message); } }, ERROR("Error has occurred...") { @Override void log(String tag) { Log.e(tag, message); } }; // ... abstract void log(String tag); }
Constant-specific method implementations public enum Status { OK("Succeed the process!")
{ @Override void log(String tag) { Log.d(tag, message); } }, ERROR("Error has occurred...") { @Override void log(String tag) { Log.e(tag, message); } }; // ... abstract void log(String tag); }
* type safe !! * Implements comparable / serializable *
They are immutable / singleton Constants / Methods
Event handling with enum
Example - handling click event public void onClick(View v) {
swtich(v.getId()) { case R.id.button1: // do something... break; case R.id.button2: // do something... break; // ... } }
Example - handling click event
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ @Override public void handle(Context context) { /* ...*/ } }, BUTTON2(R.id.button2) { @Override public void handle(Context context) { /* ...*/ } }; // ... abstract public void handle(Context context); }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ @Override public void handle(Context context) { /* ...*/ } }, BUTTON2(R.id.button2) { @Override public void handle(Context context) { /* ...*/ } }; // ... abstract public void handle(Context context); }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ @Override public void handle(Context context) { /* ...*/ } }, BUTTON2(R.id.button2) { @Override public void handle(Context context) { /* ...*/ } }; // ... abstract public void handle(Context context); }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ /* ... */ }, BUTTON2(R.id.button2) { /* ... */ }; // ... public static ClickEventHandler valueOf(int resId) { for (ClickEventHandler handler : values()) { if (handler.getResId() == resId) { return handler; } } throw new IllegalArgumentException(); } }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ /* ... */ }, BUTTON2(R.id.button2) { /* ... */ }; // ... public static ClickEventHandler valueOf(int resId) { for (ClickEventHandler handler : values()) { if (handler.getResId() == resId) { return handler; } } throw new IllegalArgumentException(); } }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ /* ... */ }, BUTTON2(R.id.button2) { /* ... */ }; // ... public static ClickEventHandler valueOf(int resId) { for (ClickEventHandler handler : values()) { if (handler.getResId() == resId) { return handler; } } throw new IllegalArgumentException(); } }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ /* ... */ }, BUTTON2(R.id.button2) { /* ... */ }, UNKNOWN(-1) { @Override public void handle(Context context) {} }; // ... public static ClickEventHandler valueOf(int resId) { for (ClickEventHandler handler : values()) { if (handler.getResId() == resId) { return handler; } } return UNKNOWN; } }
Example - handling click event public enum ClickEventHandler { BUTTON1(R.id.button1)
{ /* ... */ }, BUTTON2(R.id.button2) { /* ... */ }, UNKNOWN(-1) { @Override public void handle(Context context) {} }; // NULL Object pattern // ... public static ClickEventHandler valueOf(int resId) { for (ClickEventHandler handler : values()) { if (handler.getResId() == resId) { return handler; } } return UNKNOWN; } }
Example - handling click event public void onClick(View v) {
ClickEventHandler.valueOf(v.getId()).handle(this); }
Example - handling click event public void onClick(View v) {
ClickEventHandler.valueOf(v.getId()).handle(this); }
* Delegates event handling from controller - We can avoid
fat controller !!!! * Improves testability - Because they are referentially transparent (?) * Other usecases - BLE (BluetoothGattCharacteristic), HTTP response, etc. Event handling with enum
Why don’t you use enum ?