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
6
1.7k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
connect-go で面倒くささと戦う / 2024-08-27 #newmo_layerx_go
izumin5210
2
760
コンパウンドプロダクト開発の質とスピードを支える Protobuf と Connect #アーキテクチャ_findy / Boosting Compound Product Development Efficiency with Protobuf and Connect
izumin5210
12
2.7k
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
1.2k
Next.js App Router を例に考える、技術選定・技術との距離感 #技術選定_findy / findy 2024-01-24
izumin5210
14
6.1k
雑なコード生成のすすめ #nihonbashijs 8 / zatsu-code-generation
izumin5210
0
84
React Server Components で複雑さに立ち向かう #コンポーネント_findy / findy 2023-10-04
izumin5210
10
3.3k
みんなで育てる GraphQL スキーマ, それを支える Protobuf / GraphQL and Protobuf #tech_stand
izumin5210
8
3.7k
Other Decks in Technology
See All in Technology
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
1
190
強いチームと開発生産性
onk
PRO
34
11k
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
Introduction to Works of ML Engineer in LY Corporation
lycorp_recruit_jp
0
110
オープンソースAIとは何か? --「オープンソースAIの定義 v1.0」詳細解説
shujisado
7
820
[FOSS4G 2024 Japan LT] LLMを使ってGISデータ解析を自動化したい!
nssv
1
210
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
380
Application Development WG Intro at AppDeveloperCon
salaboy
0
190
サイバーセキュリティと認知バイアス:対策の隙を埋める心理学的アプローチ
shumei_ito
0
380
第1回 国土交通省 データコンペ参加者向け勉強会③- Snowflake x estie編 -
estie
0
130
OCI Vault 概要
oracle4engineer
PRO
0
9.7k
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
150
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
693
190k
How GitHub (no longer) Works
holman
310
140k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Happy Clients
brianwarren
98
6.7k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Six Lessons from altMBA
skipperchong
27
3.5k
Building an army of robots
kneath
302
43k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
We Have a Design System, Now What?
morganepeng
50
7.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 ?