Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Format Your Elixir Code Now

Format Your Elixir Code Now

Elixir master now contains a formatter that automatically formats your code to match a community style. Which might lead you to ask: Why should I use it? How do I set it up? What is it going to do to my code?

Wonder no more! In this talk, we’ll seek answers to these questions. We’ll review the best arguments for and against autoformatting, locate Elixir’s moment in the history of this idea, explain setup on different systems, learn how the formatter works, and witness how it changes a real OSS Elixir/Phoenix application.

Elixir mixologists of every experience level will leave this talk with a better understanding of this important tool, and a deeper grasp of the persistent debate around code style.

A9689bb4f705b8bede56deddf66c87ef?s=128

Jake Worth

March 01, 2018
Tweet

Transcript

  1. Format Your Elixir Code Now Jake Worth, Hashrocket

  2. 2

  3. "Elixir master now includes a code formatter that automatically formats

    your code according to a consistent style. We want to convert Elixir's codebase to use the formatter on all files. We understand this means a lot of code churn now but, on the positive side, it means we can automatically enforce and/or format future contributions, making it easier for everyone to contribute to Elixir."1 1 José Valim, Github Elixir-Lang Issue #6643, https://github.com/elixir-lang/elixir/issues/6643. 3
  4. Refactor Stats » 425 files changed » 214 pull requests

    » 84 committers » 368 commits » 4 days to complete! 4
  5. @jwworth Hashrocket 5

  6. 6

  7. A Changing Community » Before v1.6: style is subjective »

    After v1.6: style conforms to a community style guide 7
  8. Questions » What is autoformatting? » Why should I care?

    » What is it going to do to my code? 8
  9. Format your code now. It's where Elixir is headed. 9

  10. Agenda 1. History of Autoformatting 2. Arguments For/Against 3. Practical

    Applications 10
  11. Slides » https://speakerdeck.com/jwworth (PDF) 11

  12. 1. History of Autoformatting 12

  13. Autoformat: to cause the layout of a document to be

    created or edited without further effort by the execution of a program.2 2 Your Dictionary, http://www.yourdictionary.com/, s.v. "Autoformat". 13
  14. Linting vs. Autoformatting Tool Target Action Linter Code smells Recommends

    changes Autoformatter Inconsistent style Recommends + changes code 14
  15. GNU Indent » Unix utility that formats C and C++

    » Oldest copyright 1989 15
  16. Theory: Autoformatting solves a modern problem 16

  17. Go + Autoformatting » Go created in 2009 » gofmt

    released in 2013 » Overview: https:// blog.golang.org/go-fmt-your- code 17
  18. Autoformatted Go Easier to write: never worry about minor formatting

    concerns while hacking away 18
  19. Autoformatted Go Easier to read: when all code looks the

    same you need not mentally convert others' formatting style into something you can understand 19
  20. Autoformatted Go Easier to maintain: mechanical changes to the source

    don't cause unrelated changes to the file's formatting; diffs show only the real changes 20
  21. Autoformatted Go Uncontroversial: never have a debate about spacing or

    brace position ever again! 21
  22. Elixir + Autoformatting » Elixir created in 2011 » mix

    format released in Elixir 1.6 (January 2018) » Overview: José Valim's ElixirConf 2017 keynote 22
  23. 23

  24. We don't talk about these anymore 24

  25. 2. Arguments For/ Against 25

  26. Arguments For/Against For Against Productivity But My Preferences! Consistency Friendliness

    to Newcomers Execution 26
  27. For: Productivity Bikeshedding [originally BSD, now common] Technical disputes over

    minor, marginal issues conducted while more serious ones are being overlooked. The implied image is of people arguing over what color to paint the bicycle shed while the house is not finished.4 » Most style preferences don't matter 4 The Jargon File (version 4.4.7), http://www.catb.org/, s.v. "Bikeshedding". 27
  28. For: Consistency » Elixir should have "a consistent style" »

    Distinct style guides separate us » We need "collective ownership" of the community's code 28
  29. 29

  30. Eliminate Trivial Decisions 30

  31. For: Friendliness to Newcomers » New devs shouldn't have to

    learn "how things are done here" on every team » Elixir code should be exemplary » Feedback on your code need not always come from a human 31
  32. For: Execution Elixir Autoformatter's Guidelines:5 1. It does not change

    code semantics by default 2. Minimizes configuration 3. No special cases 5 José Valim, "Elixir Conf Keynote 2017", https://www.youtube.com/watch?v=Wa7Ipc0yo (Accessed 7 Feb 2018) 32
  33. Against: But My Preferences! » Elixir's formatter always removes "trailing

    commas" » "No trailing comma (ever)."6 6 José Valim, 'Elixir Style Guide Pull Request #46', https://github.com/lexmag/elixir-style-guide/pull/46. 33
  34. Trailing Comma Controversy [ 1, 2 ] [ 1, 2,

    ] 34
  35. Trailing Comma Controversy [ 1, 2 ] [ 1, 2,

    ] 35
  36. I came, I saw, I conquered. 36

  37. I came, I saw, I conquered,. 37

  38. Trailing Comma Controversy [ 1, 2 ] ‑ [ 1,

    2, 3 ] 38
  39. Trailing Comma Controversy [ 1, 2 ] ‑ [ 1,

    2, 3 ] 39
  40. Trailing Comma Controversy [ 1, 2, ] ‑ [ 1,

    2, 3, ] 40
  41. Trailing Comma Controversy [ 1, 2, ] ‑ [ 1,

    2, 3, ] 41
  42. Against: But My Preferences! Potential Benefit: If successful, it ends

    persistent debates 42
  43. Any code of your own that you haven't looked at

    for six or more months might as well have been written by someone else. — Eagleson's Law 43
  44. 3. Practical Applications 44

  45. 45

  46. 46

  47. TIL Stats » 90 Total (non-dependency) Elixir files » 31

    files (35%) passed the formatter » Conclusion: churn is coming 47
  48. # lib/tilex_web/controllers/post_controller.ex plug :load_channels when action in [:new, :create, :edit,

    :update] 48
  49. # lib/tilex_web/controllers/post_controller.ex plug :load_channels when action in [:new, :create, :edit,

    :update] ‑ plug(:load_channels when action in [:new, :create, :edit, :update]) 49
  50. # lib/tilex_web/controllers/post_controller.ex page = params |> Map.get("page", "1") |> String.to_integer

    50
  51. # lib/tilex_web/controllers/post_controller.ex page = params |> Map.get("page", "1") |> String.to_integer

    ‑ page = params |> Map.get("page", "1") |> String.to_integer() 51
  52. # lib/tilex_web/controllers/post_controller.ex render(conn, "search_results.html", posts: posts, posts_count: posts_count ) 52

  53. ‑ render( conn, "search_results.html", posts: posts, posts_count: posts_count ) 53

  54. # lib/tilex_web/controllers/post_controller.ex post = case current_user.admin do false -> current_user

    |> assoc(:posts) |> Repo.get_by!(slug: slug) true -> Repo.get_by!(Post, slug: slug) end 54
  55. ‑ post = case current_user.admin do false -> current_user |>

    assoc(:posts) |> Repo.get_by!(slug: slug) true -> Repo.get_by!(Post, slug: slug) end 55
  56. # lib/tilex_web/router.ex @auth_controller Application.get_env(:tilex, :auth_controller) @cors_origin Application.get_env(:tilex, :cors_origin) 56

  57. # lib/tilex_web/router.ex @auth_controller Application.get_env(:tilex, :auth_controller) @cors_origin Application.get_env(:tilex, :cors_origin) ‑ @auth_controller

    Application.get_env(:tilex, :auth_controller) @cors_origin Application.get_env(:tilex, :cors_origin) 57
  58. # config/config.exs config :guardian, Guardian, allowed_algos: ["HS512"], # optional 58

  59. # config/config.exs config :guardian, Guardian, allowed_algos: ["HS512"], # optional ‑

    config :guardian, Guardian, # optional allowed_algos: ["HS512"], 59
  60. CI Integration # .circleci/config.yml steps: - mix format --check-formatted 60

  61. Text Editor Integrations Vim: " ~/.vimrc autocmd BufWritePost *.exs,*.ex silent

    :!mix format --check-equivalent % 61
  62. Conclusion: Format Your Code 62

  63. @jwworth Hashrocket 63