Refactor: Big Function Alert

Our parse_args function is waving two red flags. First, it contains conditional logic. Second, it is too long. Let’s split it up.

 def​ parse_args(argv) ​do
  OptionParser.parse(argv, ​switches:​ [ ​help:​ ​:boolean​],
 aliases:​ [ ​h:​ ​:help​ ])
  |> elem(1)
  |> args_to_internal_representation()
 end
 
 def​ args_to_internal_representation([user, project, count]) ​do
  { user, project, String.to_integer(count) }
 end
 
 def​ args_to_internal_representation([user, project]) ​do
  { user, project, @default_count }
 end
 
 def​ args_to_internal_representation(_) ​do​ ​# bad arg or --help
 :help
 end

And run the tests:

 issues$ ​​mix​​ ​​test
 ......
 
 Finished ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.