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.3k
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
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
9
4.8k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.7k
connect-go で面倒くささと戦う / 2024-08-27 #newmo_layerx_go
izumin5210
2
880
コンパウンドプロダクト開発の質とスピードを支える Protobuf と Connect #アーキテクチャ_findy / Boosting Compound Product Development Efficiency with Protobuf and Connect
izumin5210
12
3.6k
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
1.3k
Next.js App Router を例に考える、技術選定・技術との距離感 #技術選定_findy / findy 2024-01-24
izumin5210
14
6.3k
雑なコード生成のすすめ #nihonbashijs 8 / zatsu-code-generation
izumin5210
0
100
React Server Components で複雑さに立ち向かう #コンポーネント_findy / findy 2023-10-04
izumin5210
10
3.4k
みんなで育てる GraphQL スキーマ, それを支える Protobuf / GraphQL and Protobuf #tech_stand
izumin5210
8
3.8k
Other Decks in Technology
See All in Technology
MC906491 を見据えた Microsoft Entra Connect アップグレード対応
tamaiyutaro
1
540
Helm , Kustomize に代わる !? 次世代 k8s パッケージマネージャー Glasskube 入門 / glasskube-entry
parupappa2929
0
250
Platform Engineeringは自由のめまい
nwiizo
4
2.1k
2025-02-21 ゆるSRE勉強会 Enhancing SRE Using AI
yoshiiryo1
1
240
SA Night #2 FinatextのSA思想/SA Night #2 Finatext session
satoshiimai
1
140
データマネジメントのトレードオフに立ち向かう
ikkimiyazaki
6
940
Datadog APM におけるトレース収集の流れ及び Retention Filters のはなし / datadog-apm-trace-retention-filters
k6s4i53rx
0
330
リアルタイム分析データベースで実現する SQLベースのオブザーバビリティ
mikimatsumoto
0
1.3k
飲食店予約台帳を支えるインタラクティブ UI 設計と実装
siropaca
7
1.7k
OpenID BizDay#17 KYC WG活動報告(法人) / 20250219-BizDay17-KYC-legalidentity
oidfj
0
240
Datadogとともにオブザーバビリティを布教しよう
mego2221
0
140
関東Kaggler会LT: 人狼コンペとLLM量子化について
nejumi
3
570
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
A better future with KSS
kneath
238
17k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
133
33k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
What's in a price? How to price your products and services
michaelherold
244
12k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
How to Ace a Technical Interview
jacobian
276
23k
It's Worth the Effort
3n
184
28k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Optimizing for Happiness
mojombo
376
70k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.2k
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 ?