TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Elixir Anti-Patterns

66 pointsby lobo_tuerto11 months ago

5 comments

Terr_11 months ago
&gt; Complex else clauses in with<p>This has proven controversial, but sometimes the Else behavior is very sensitive to <i>which</i> of the With expressions failed (especially for better logging) and I maintain that the <i>least terrible</i> approach involves augmenting the With clauses using atoms to identify each expression when it fails, ex:<p>For example:<p><pre><code> with {_, {:ok, key} } &lt;- {:phase_key, get_key() }, {_, {:ok, door}} &lt;- {:phase_unlock, unlock(door) }, {_, %Thingy{} } &lt;- {:phase_thingy, fetch_thingy(door)}, {_, %Widget{} } &lt;- {:phase_widget, fetch_widget(door)} do else {:phase_key, {:error, _msg} = err} -&gt; # I can tell the key was missing, rather than the lock broke {:phase_unlock, {:error, _msg} = err} -&gt; # I can tell the lock broke, rather than missing a key {:phase_thingy, nil } -&gt; # I can tell this nil value is a missing thingy, not a widget {:phase_widget, nil } -&gt; # I can tell this nil value is a missing widget, not a thingy end </code></pre> As a big bonus, when something crazy-unexpected is emitted to cause a WithClauseError--like &quot;hello world&quot;--you have a much better chance of quickly diagnosing what happened from log messages, instead of inspecting a dozen different logical branching or trying to blindly repro it.<p>Pre-buttals:<p>&quot;Just rewrite those 4 functions to return better somehow&quot; -&gt; No, that&#x27;s not always practical or even possible. In the above example, the signatures of some of these functions are already perfectly fine <i>on their own</i>. The ambiguity comes when they get used near others in different ways, and trying to assign <i>globally</i> unique features to match on is a bad idea.<p>&quot;Create 4 private functions to wrap things&quot; -&gt; Ugh, now the person reading the code has an even harder time, since they need to refer to functions elsewhere in the file, any of which might be hiding unexpected bonus behavior. Those 4 private functions might not even be useful in <i>other</i> with-statements in the same module, causing you to make even more private functions... and all of it still falls apart the moment your Else logging&#x2F;cleanup needs to record or revert something from a prior (successful) step.
评论 #40823798 未加载
评论 #40823671 未加载
评论 #40833289 未加载
评论 #40838614 未加载
Terr_11 months ago
Anti-pattern: Overuse and misuse of macros. (Or perhaps not a &quot;pattern&quot;, since the manifestations are so varied?)<p>For non-Elixir folks, imagine something that <i>looks</i> like a regular function-call where expressions are evaluated and the results are passed in... but in reality all arguments are symbolic inputs to something that emits new code.<p>For example, you might see this unassuming line in your editor:<p><pre><code> describe_combo(a(foo),b(bar)) </code></pre> But what might actually be compiled is:<p><pre><code> # What might actually get compiled # Runtime results like &quot;a(1)+b(2)=3&quot; &quot;a(&quot; &lt;&gt; to_string(foo) &lt;&gt; &quot;)+b(&quot; &lt;&gt; to_string(bar) &lt;&gt; &quot;)=&quot; to_string(a(foo)+b(bar)) </code></pre> And if someone in your codebase is an evil misanthrope:<p><pre><code> foo = &quot;haha I changed your local variable&quot; IO.puts(&quot;surprise, suckers&quot;) # Prints to console kick_puppies() </code></pre> Macros are actually part of the core DNA of the language and underpin how some of its syntax works... but with great power comes great potential for WTF.
评论 #40825450 未加载
mervz11 months ago
Elixir is one language I REALLY want to use as the solo developer working on internal infrastructure tools; but I&#x27;m very hesitant as I don&#x27;t want to be cruel to the person who eventually replaces me and has to maintain these applications written in this seemingly (awesome) niche language.<p>Instead, I&#x27;m stuck writing applications in Go which is a language I absolutely despise but can&#x27;t deny it&#x27;s ability to get shit done regardless of the insane amount of boilerplate code I have to write.<p>Any predictions on whether Elixir will eventually gain more traction? The language and platform are rock solid and it&#x27;s the best developer experience I&#x27;ve encountered. Phoenix is by far my favorite web framework and just feels very intuitive and complete. The lack of a good IDE is one huge pain point that I can&#x27;t seem to overcome (I don&#x27;t care how much people praise it, I know it&#x27;s the best but VSCode is awful for Elixir development).<p>I just wish this language was more popular because it deserves to be!
评论 #40823301 未加载
评论 #40823432 未加载
评论 #40824030 未加载
评论 #40823834 未加载
评论 #40823183 未加载
评论 #40823287 未加载
评论 #40825789 未加载
评论 #40823484 未加载
fouc11 months ago
Jose Valim gave an overview of this in last month&#x27;s ElixirEU Conf <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=agkXUp0hCW8" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=agkXUp0hCW8</a>
mjoin11 months ago
Unpopular opinion given all the craze around Elixir these days. I find the syntax extremely verbose and hard to read compared to e.g. Python. I was told great things about it by engineers I really respect. But each time I start reading Elixir code bases to get a good look at what an actual Elixir project looks like, I find the code hard to read and understand. Is it just me?
评论 #40823440 未加载
评论 #40823609 未加载
评论 #40823531 未加载
评论 #40823278 未加载
评论 #40823996 未加载
评论 #40824299 未加载
评论 #40823892 未加载
评论 #40823259 未加载
评论 #40823458 未加载