Last active
March 20, 2022 06:12
-
-
Save takezoux2/9a7dcef0e51294c7462e37275200bae7 to your computer and use it in GitHub Desktop.
ScalaMatsuri2022 第2回ひどいコード選手権
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 通知設定を取得し、ユーザーに通知を送るかどうかを判断するプログラム | |
*/ | |
object Main { | |
def main(args: Array[String]): Unit = { | |
// フラグ立ってるのが通知してOKなのかどうかがもはやわからない | |
getConfig("User1") match { | |
case Some(config) if config.flag => { | |
// do nothing | |
} | |
case _ => { | |
notifyToUser("User1", "Message") | |
} | |
} | |
} | |
def getConfig(userId: String) = { | |
// Read from db | |
Some(NotificationConfig(userId, "OnReceivePrivateMessage", false)) | |
} | |
def notifyToUser(userId: String, message: String) = { | |
// Send notification to user | |
println(s"Send email to ${userId}") | |
} | |
} | |
case class NotificationConfig(userId: String, notificationType: String, flag: Boolean) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Success(message: String) | |
case class UnknownError(message: String) | |
/* | |
実際はレイヤードアキテクチャーにより、何段か深い部分で例外が握りつぶされており、かつ同じように握りつぶしている場所が多数なので、 | |
例外発生箇所の特定が不可能になってます。 | |
*/ | |
object Main { | |
def main(args: Array[String]): Unit = { | |
// 例外起きてもどこで起きているか、何が起きているかが全くわからない | |
val result = doAnyAction() | |
println(result) | |
} | |
def doAnyAction(): Success | UnknownError = { | |
try { | |
val r = coreProcess() | |
Success(r) | |
} catch { | |
// 握り潰した上でエラーを変更している | |
// ログも出ていない | |
case err => UnknownError("Unknown error occured") | |
} | |
} | |
def coreProcess = () => { | |
throw new Exception("Error on Database") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment