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.5k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.6k
connect-go で面倒くささと戦う / 2024-08-27 #newmo_layerx_go
izumin5210
2
840
コンパウンドプロダクト開発の質とスピードを支える Protobuf と Connect #アーキテクチャ_findy / Boosting Compound Product Development Efficiency with Protobuf and Connect
izumin5210
12
2.9k
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
99
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
Docker Desktop で Docker を始めよう
zembutsu
PRO
0
170
生成AIのビジネス活用
seosoft
0
110
完全自律型AIエージェントとAgentic Workflow〜ワークフロー構築という現実解
pharma_x_tech
0
350
三菱電機で社内コミュニティを立ち上げた話
kurebayashi
1
360
DMMブックスへのTipKit導入
ttyi2
1
110
Building Scalable Backend Services with Firebase
wisdommatt
0
110
AWS re:Invent 2024 recap in 20min / JAWSUG 千葉 2025.1.14
shimy
1
100
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
2.4k
Cloudflareで実現する AIエージェント ワークフロー基盤
kmd09
0
290
AWS Community Builderのススメ - みんなもCommunity Builderに応募しよう! -
smt7174
0
180
comilioとCloudflare、そして未来へと向けて
oliver_diary
6
440
Kotlin Multiplatformのポテンシャル
recruitengineers
PRO
2
150
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
A better future with KSS
kneath
238
17k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
The Pragmatic Product Professional
lauravandoore
32
6.4k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Code Reviewing Like a Champion
maltzj
521
39k
Navigating Team Friction
lara
183
15k
Scaling GitHub
holman
459
140k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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 ?