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.

Jake Worth

March 01, 2018
Tweet

More Decks by Jake Worth

Other Decks in Programming

Transcript

  1. 2

  2. "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
  3. Refactor Stats » 425 files changed » 214 pull requests

    » 84 committers » 368 commits » 4 days to complete! 4
  4. 6

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

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

    » What is it going to do to my code? 8
  7. 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
  8. Linting vs. Autoformatting Tool Target Action Linter Code smells Recommends

    changes Autoformatter Inconsistent style Recommends + changes code 14
  9. Go + Autoformatting » Go created in 2009 » gofmt

    released in 2013 » Overview: https:// blog.golang.org/go-fmt-your- code 17
  10. 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
  11. 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
  12. Elixir + Autoformatting » Elixir created in 2011 » mix

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

  14. 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
  15. For: Consistency » Elixir should have "a consistent style" »

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

  17. 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
  18. 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
  19. 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
  20. 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
  21. 45

  22. 46

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

    files (35%) passed the formatter » Conclusion: churn is coming 47
  24. # 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
  25. # 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
  26. # 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
  27. ‑ 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
  28. # config/config.exs config :guardian, Guardian, allowed_algos: ["HS512"], # optional ‑

    config :guardian, Guardian, # optional allowed_algos: ["HS512"], 59