<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="45446" data-post-id="45446">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Because it is very likely the pagination library should also be just using functions instead of a plug. I would rather do <code>query |&gt; Repo.paginate(params)</code> or <code>query |&gt; paginate(Repo)</code> than the plug or decorator approaches.</p>
<p>I can’t say about other languages but the fact validations in Ecto are not decorators makes them extremely easy to compose. If you need conditional validations, dynamic parameters, etc, you can just write Elixir code instead of finding the proper decorator incantation.</p>
<p>When it comes to composition and flow-control, there is no decorator that is going to be simpler and more readable than pattern matching, <code>with</code> and the <code>|&gt;</code> operator (for the simplest cases).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="45446" data-batch-url="/posts/batch_likers">
                        9
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-45446" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="45446"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #41"></div>
  </section>
</div>
    <div class="postbit" id="46347" data-post-id="46347">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="smueller" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/smueller/120/3857_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  smueller
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a> thanks for your feedback. I’m afraid I don’t know enough yet to understand your reasoning. Here’s what we’ve built for our realtime api (using phoenix channels):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defparams status_react_params %{
    status_id!: :integer,
    reaction_type!: :string
  }
  @decorate validate(&amp;status_react_params/1)
  def status_react(~m{status_id, reaction_type}, socket) do
    # ...
  end
</code></pre>
<p>The goal here is to make sure that the incoming params are present and valid. In this example, if <code>status_id</code> is missing or a string, then we handle the error response appropriately and we’ve used an <code>embedded_schema</code> for determining if things are valid or not.</p>
<p>What we eventually validate as part of the db query may be an entirely different ecto <code>schema</code>. In my opinion, this is pretty readable. It’s easy to construct <code>embedded_schema</code>s and the logic for how to handle incorrect params is centralized in one place (the <code>validate</code> decorator).</p>
<p>How would our code be measurably more readable and composable than what we have now? Is there perhaps a scenario of how things can break down that maybe we haven’t thought about?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="46347" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-46347" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="46347"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #42"></div>
  </section>
</div>
    <div class="postbit" id="46379" data-post-id="46379">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is the issue I see in your code. Imagine that the parameter you need to validate depend on some state in socket. Maybe it depends if the user is in their phone or if they are an admin. How are you going to apply this conditional validation? Maybe you need to add more sugar to defparams?Or maybe you need to define now two private functions with different behaviours that you’d call accordingly?</p>
<p>If this code was just Elixir code then the answer to how you would change any of this code is immediately obvious. You would handle it as any Elixir code. You would use pattern matching or conditionals, etc.</p>
<p>The type of conditional validation you are expressing can be done with functions and data types. There is no need for decorators or indirection:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @types %{
    status_id: :integer,
    reaction_type: :string
  }
  def status_react(params, socket) do
    with {:ok, ~m{status_id, reaction_type}} &lt;- validate_params(params, @types),
         do: ...
  end
</code></pre>
<p>You can use <code>with</code> or you can also use something like changesets, where you validate and annotate the types as you go.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="46379" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-46379" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="46379"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #43"></div>
  </section>
</div>
    <div class="postbit" id="156334" data-post-id="156334">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hrishi" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hrishi
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey, <a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a>  following up on your advice on not using decorators.</p>
<p>Is decorator a good implementation pattern when looking at it from a logging/instrumentation use-case?<br>
We’re looking at implementing logging/instrumentation for function entry/exit calls across controllers/web-sockets/contexts and to be honest using a <span class="mention">@decorator</span> makes the job much easier.</p>
<p>Are there any other alternate options to go about such logging functionality in elixir apps that I’m missing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="156334" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/45">Post #44</a>
	                </div>
	            </div>
              <div id="likers-container-156334" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="156334"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #44"></div>
  </section>
</div>
    <div class="postbit" id="156343" data-post-id="156343">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The decorators have problem in form that they do not work nicely with multiple function definitions in case of pattern matching. Is this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@decorate trace()
def sum([x | xs]), do: x + sum(xs)

def sum([]), do: 0
</code></pre>
<p>The same as this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@decorate trace()
def sum([x | xs]), do: x + sum(xs)

@decorate trace()
def sum([]), do: 0
</code></pre>
<p>And this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def sum([x | xs]), do: x + sum(xs)

@decorate trace()
def sum([]), do: 0
</code></pre>
<p>If yes, then how will you detect when to decorate function? If not then how it will not be confusing?</p>
<p>In case of logging and instrumentation I think the best solution will be just define function like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def trace(name, func) do
  ctx = start_trace(name)
  try do
    func.()
  after
    end_trace(ctx)
  end
end
</code></pre>
<p>And use it like that:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def my_traceable_func() do
  trace(:my_traceable_func, fn -&gt;
    # do thing
  end)
end
</code></pre>
<p>Alternatively you can use macro to extract some more information at the compile time.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="156343" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/nicest-way-to-emulate-function-decorators/2050/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-156343" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="156343"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>