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
izumin5210
August 11, 2015
Technology
3.5k
2
Share
Why don't you use enum ?
【第20回】potatotips(iOS/Android開発Tips共有会) 発表資料
http://connpass.com/event/17624/
izumin5210
August 11, 2015
More Decks by izumin5210
See All by izumin5210
開発体験を左右するライブラリの API 設計 - GraphQL スキーマ構築ライブラリから考える #tskaigi
izumin5210
1
110
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
2
830
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
8
2.7k
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
6
2.1k
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.7k
Web エンジニアが JavaScript で AI Agent を作る / JSConf JP 2025 sponsor session
izumin5210
4
3.3k
AI Coding Meetup #3 - 導入セッション / ai-coding-meetup-3
izumin5210
0
3.7k
Web フロントエンドエンジニアに開かれる AI Agent プロダクト開発 - Vercel AI SDK を観察して AI Agent と仲良くなろう! #FEC余熱NIGHT
izumin5210
3
1.2k
TypeScript を活かしてデザインシステム MCP を作る / #tskaigi_after_night
izumin5210
5
920
Other Decks in Technology
See All in Technology
Databricks 月刊サービスアップデートまとめ 2026年04月号
tyosi1212
0
130
How to learn AWS Well-Architected with AWS BuilderCards: Security Edition
coosuke
PRO
0
150
AIAgentと取り組むKaggle
508shuto
2
240
アプリブロック機能のつくりかたと、AIとHTMLの不合理な相性の良さについて
kumamotone
1
260
Gaussian Splattingの実用化 - 映像制作への展開
gpuunite_official
0
200
いつの間にかデータエンジニア以外の業務も増えていたけど、意外と経験が役に立ってる
zozotech
PRO
0
650
写真で見るAWS Summit Singapore 2026
k_adachi_01
0
110
Swift Sequence の便利 API 再発見
treastrain
1
290
Every Conversation Counts
kawaguti
PRO
0
250
"うちにはまだ早い"は本当? ─ 小さく始めるPlatform Engineering入門
harukasakihara
6
630
"スキルファースト"で作る、AIの自走環境
subroh0508
0
590
Gaussian Splattingの表現力を拡張する — 高周波再構成とインタラクションへのアプローチ —
gpuunite_official
0
190
Featured
See All Featured
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
510
4 Signs Your Business is Dying
shpigford
187
22k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
410
WENDY [Excerpt]
tessaabrams
10
37k
Building Adaptive Systems
keathley
44
3k
Speed Design
sergeychernyshev
33
1.7k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
780
Writing Fast Ruby
sferik
630
63k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
530
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Google's AI Overviews - The New Search
badams
0
1k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
110
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 ?