Skip to main content
Code Review

Return to Question

edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Can Fetching environment variables — can I rewrite this withuse pattern matching?

added tags to attract programmers of other languages
Link
Tweeted twitter.com/#!/StackCodeReview/status/442067755265957888
Fixing code formatting
Source Link

I've got the following Elixir code:

defmodule Envvar do
 def exists?(env) do :os.getenv(env) != false end
 def get(env) do 
 if exists?(env) do
 {env, :os.getenv(env)}
 else 
 {env, nil}
 end 
 end
end

Two things:

  1. Am I using the ? correctly here? I'm assuming that any predicate function should have the ? on the end of the name by convention.
  2. Is there a way to code the get function as a pattern match rather than a if/`elseelse? I'm asking here because I'm guessing that a pattern match would be more idiomatic Elixir code.

I've got the following Elixir code:

defmodule Envvar do
 def exists?(env) do :os.getenv(env) != false end
 def get(env) do 
 if exists?(env) do
 {env, :os.getenv(env)}
 else 
 {env, nil}
 end 
 end
end

Two things:

  1. Am I using the ? correctly here? I'm assuming that any predicate function should have the ? on the end of the name by convention.
  2. Is there a way to code the get function as a pattern match rather than a if/`else? I'm asking here because I'm guessing that a pattern match would be more idiomatic Elixir code.

I've got the following Elixir code:

defmodule Envvar do
 def exists?(env) do :os.getenv(env) != false end
 def get(env) do 
 if exists?(env) do
 {env, :os.getenv(env)}
 else 
 {env, nil}
 end 
 end
end

Two things:

  1. Am I using the ? correctly here? I'm assuming that any predicate function should have the ? on the end of the name by convention.
  2. Is there a way to code the get function as a pattern match rather than a if/else? I'm asking here because I'm guessing that a pattern match would be more idiomatic Elixir code.
deleted 20 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 4 characters in body
Source Link
Loading
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /