Skip to content

Instantly share code, notes, and snippets.

@klazuka
Last active May 23, 2018 03:09
Show Gist options
  • Save klazuka/fdd18790ce50ee291a1a210c63ef48f7 to your computer and use it in GitHub Desktop.
Save klazuka/fdd18790ce50ee291a1a210c63ef48f7 to your computer and use it in GitHub Desktop.
comparing single line if-then-else for elm-format

All examples from a 20k LOC Elm code base that has been in production for 2 years now.

{-
Examples which I think are improved by single-line formatting of `if-then-else`
-}
-- status quo
viewSave { isSaving } =
div []
[ button
[ onClick Save, disabled isSaving ]
[ text <|
if isSaving then
"Saving..."
else
"Save"
]
]
-- single-line
viewSave { isSaving } =
div []
[ button
[ onClick Save, disabled isSaving ]
[ text (if isSaving then "Saving..." else "Save")
]
]
-- status-quo
viewFeedbackPicker suggestions =
let
prompt =
"or pick one..."
toFeedback label =
if label == prompt then
""
else
label
in
Util.Html.dropdown prompt
(prompt :: suggestions)
toFeedback
-- single-line
viewFeedbackPicker suggestions =
let
prompt =
"or pick one..."
toFeedback label =
if label == prompt then "" else label
in
Util.Html.dropdown prompt
(prompt :: suggestions)
toFeedback
{-
Examples which I think are made worse (or not much better) by single-line formatting of `if-then-else`
-}
-- status-quo
adjustSelection index =
if index == numScreens - 1 then
index - 1
else
index
-- single-line
adjustSelection index =
if index == numScreens - 1 then index - 1 else index
-- status-quo
themeSelector index =
if index == model.currentScreenIndex then
"btn-success"
else
"btn-default"
-- single-line
themeSelector index =
if index == model.currentScreenIndex then "btn-success" else "btn-default"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment