# Writing idiomatic Elixir code

**URL:** <https://forum.elixirforum.com/t/writing-idiomatic-elixir-code/2365>\
**Category:** Questions\
**Tags:** code-style, idiomatic-elixir\
**Created:** [November 13, 2016, 12:53pm UTC](https://forum.elixirforum.com/t/writing-idiomatic-elixir-code/2365 "2016-11-13T12:53:29Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![michalmuskala](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/michalmuskala/32/20288_2.png) [@michalmuskala](https://forum.elixirforum.com/u/michalmuskala)\
**Post date:** [November 13, 2016, 1:16pm UTC](https://forum.elixirforum.com/t/writing-idiomatic-elixir-code/2365/3 "2016-11-13T13:16:03Z")

</div>

You can do that by using `if` as an expression and assembling list of values finally concatenating them together:

```elixir
IO.iodata_to_binary([
  if(data["a"] > 0, do: "text1", else: ""),
  if(data["b"] > 0, do: "text2", else: ""),
  if(data["c"] > 0, do: "text3", else: "")
])

```

You could even extract the `if` to a separate function, if the pattern if very repeating:

```elixir
defp if_positive(value, text), do: if(value > 0, do: text, else: "")

IO.iodata_to_binary([
  if_positive(data["a"], "text1"),
  if_positive(data["b"], "text2"),
  if_positive(data["c"], "text3")
])

```

---

_[View the full topic](https://forum.elixirforum.com/t/writing-idiomatic-elixir-code/2365)._
