Variables name convention

Whether you like Hungarian Notation or not (there can be arguments made for its proper use), variable naming convention in Elixir is that you use snake_case, so your example would be:

def request(:get, b_uri, b_user, b_password, m_http_headers \\ %{}) ...

With a simple typespec, and avoiding to re-state that information in the variable names, it could look like this instead:

@spec request(:get, String.t, String.t, String.t, %{optional(any) => any}) :: list
def request(:get, uri, user, password, http_headers \\ %{}) ...

Now it can also be properly checked by Dialyzer (via Dialyxir for example).

A more involved use of typespecs would be to define your own types, be more specific with allowed map / list contents, etc… maybe something like this:

@type uri :: String.t
@type user :: String.t
@type password :: String.t
@type http_headers :: %{optional(String.t) => String.t)
@type response :: [headers: http_headers, body: String.t]

# ... more code that uses the types we defined ...

@spec request(:get, uri, user, password, http_headers) :: response
def request(:get, uri, user, password, http_headers \\ %{}) ...
4 Likes