<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anonymous Duck</title>
    <description>The latest articles on DEV Community by Anonymous Duck (@dandeduck).</description>
    <link>https://dev.to/dandeduck</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1947941%2F35c708a6-6411-4c72-aaf2-8b957893c19e.png</url>
      <title>DEV Community: Anonymous Duck</title>
      <link>https://dev.to/dandeduck</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dandeduck"/>
    <language>en</language>
    <item>
      <title>Zig First Impressions from a JS Dev</title>
      <dc:creator>Anonymous Duck</dc:creator>
      <pubDate>Sat, 24 Aug 2024 13:02:59 +0000</pubDate>
      <link>https://dev.to/dandeduck/zig-first-impressions-from-a-js-dev-28k4</link>
      <guid>https://dev.to/dandeduck/zig-first-impressions-from-a-js-dev-28k4</guid>
      <description>&lt;p&gt;I've been learning zig for my game development project, read more about it &lt;a href="https://dev.to/dandeduck/un-frameworking-myself-12a9"&gt;here&lt;/a&gt;. Those are my initial (mostly positive) impressions of the language, coming from a mostly JS/TS recent experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error handling
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Errors are values&lt;/strong&gt; - It's quite a popular opinion at this point that exceptions aren't the best. They create a hidden control flow, and in JavaScript they can't even be declared; which makes your applications much more unstable.&lt;/p&gt;

&lt;p&gt;Zig uses error enums and nice syntactical sugar for easy and fun error handling. For example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;failingFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;error&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="py"&gt;MyError&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;failingFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above we declare an error &lt;code&gt;MyError&lt;/code&gt; (This can also be done separately) and return it.&lt;br&gt;
The &lt;code&gt;try&lt;/code&gt; means "if this returns an error, return it here" as in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="n"&gt;failingFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I believe this approach is a great combination and saves us from the endless &lt;code&gt;if (err != nil)&lt;/code&gt; in Go land.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Other highlights&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Errors are explicit, all types have to be declared and handled&lt;/li&gt;
&lt;li&gt;Handling is done right then and there, not on the block level&lt;/li&gt;
&lt;li&gt;Thanks to payload capturing, errors are typed correctly and autocompeleted, making it easy to use something like a switch expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;!void&lt;/code&gt; syntax&lt;/strong&gt; - &lt;code&gt;!&lt;/code&gt; is used to create a union between the return type and the error types. Zig supports not adding any errors prior to the &lt;code&gt;!&lt;/code&gt;, which is supposed to create a union of all the errors that you actually return from the function.&lt;/p&gt;

&lt;p&gt;In practice, I find this syntax unhelpful. At least with my IDE experience I don't get any intellisense in this case, and it makes the function less clear. &lt;em&gt;Just tell me what you are gonna return!&lt;/em&gt;&lt;br&gt;
I only see it being useful on the &lt;code&gt;main()&lt;/code&gt; function.&lt;/p&gt;
&lt;h2&gt;
  
  
  Payload capturing
&lt;/h2&gt;

&lt;p&gt;You know how in TS you might have a type like &lt;code&gt;number | undefined&lt;/code&gt;? You might use an &lt;code&gt;if&lt;/code&gt; or some logic to narrow down the type to what you need, and TS automatically displays the new type correctly.&lt;br&gt;
While it's easy, there are problems with this approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If types can change throughout a function, it's harder to follow&lt;/li&gt;
&lt;li&gt;In some cases you still have to do a cast&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Zig, you do this with "Payload Capturing". You can "capture" aka create a new immutable variable with the resulting type. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;maybe_num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="kt"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// `?` Means it can be `null`&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maybe_num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// Use num&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's very clear what's happening! Moreover, the variable is immutable, but if you &lt;em&gt;really&lt;/em&gt; need to change it, you can capture a pointer to the value instead.&lt;/p&gt;

&lt;p&gt;It's also worth mentioning that this mechanism can be used throughout the language, including: &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;switch&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt;, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comptime shenanigans
&lt;/h2&gt;

&lt;p&gt;Admittedly I didn't yet grasp the full possibilities of comptime. But in short, you can run regular code during compilation. You may create whole functions that are only used during this time, and can return compilation errors if necessary.&lt;/p&gt;

&lt;p&gt;It suits Zig quite well, because it's a very malleable language. Even types are values, meaning you can create, change, and get information about types (Especially in comptime).&lt;/p&gt;

&lt;p&gt;A basic example of this &lt;a href="https://zig.guide/master/language-basics/comptime" rel="noopener noreferrer"&gt;from the Zig Guide&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;// When a number type isn't specified, it defaults to comptime_int&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;f32&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c"&gt;// b: f32 after compilation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Editor experience
&lt;/h2&gt;

&lt;p&gt;I'm using VSCode with the official Zig plugin (which uses &lt;code&gt;zls&lt;/code&gt;). The intellisense and errors I see in the editor leave much to be desired.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"detectable illegal behavior"&lt;/em&gt; aka illegal things that will result in a compilation error aren't typically displayed in the editor. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="mi"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c"&gt;// Index out of bounds error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm on the 0.14 (dev) master branch version, if it's supposed to work let me know in the comments!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EDIT:&lt;/code&gt; &lt;br&gt;
I was able to enable this type of error checking in VSCode by enabling the "Enable Build On Save" option under the zig extension settings.&lt;br&gt;
Which is exactly what it sounds like, saving the file will rebuild the app and show any errors resulting from compilation on the correct lines. 🤦&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>zig</category>
    </item>
    <item>
      <title>Un-frameworking myself</title>
      <dc:creator>Anonymous Duck</dc:creator>
      <pubDate>Tue, 20 Aug 2024 10:28:25 +0000</pubDate>
      <link>https://dev.to/dandeduck/un-frameworking-myself-12a9</link>
      <guid>https://dev.to/dandeduck/un-frameworking-myself-12a9</guid>
      <description>&lt;p&gt;Recently my Youtube feed has been filled with videos talking about how &lt;em&gt;"people rely too much on frameworks"&lt;/em&gt; or &lt;em&gt;"devs don't really know how things work anymore"&lt;/em&gt; etc. And it has gotten me thinking, are we (especially web developers) just monkeys learning to connect one super abstract tool with another to fit our use-case? Seemingly throwing existing libraries and solutions at the problem until one sticks? &lt;/p&gt;

&lt;p&gt;Maybe.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;I'm a gen z developer, I started programming from Java and C, and only in my first "real" job I began using JavaScript seriously. Because of that, I felt a bit "superior" to my fellow junior devs, as if knowing what is a pointer was the same thing as writing your own compiler or OS.&lt;/p&gt;

&lt;p&gt;And yet, having worked pretty much exclusively with JS/TS for the past 2.5 years, I feel like my brain has rotten. Any new project I make uses TS by default, with tons of cool and shiny libraries and tools piled on top.&lt;/p&gt;

&lt;p&gt;Because &lt;em&gt;you can't just use &lt;strong&gt;node&lt;/strong&gt;&lt;/em&gt;, you gotta use &lt;em&gt;Bun&lt;/em&gt;! and maybe &lt;em&gt;elysia&lt;/em&gt; for the api, connected &lt;em&gt;to EdgeDB&lt;/em&gt;; Perhaps &lt;em&gt;NextJS&lt;/em&gt; for the website, with additional dependencies like &lt;em&gt;React Query&lt;/em&gt;, &lt;em&gt;Redux&lt;/em&gt;, &lt;em&gt;Tailwind&lt;/em&gt; and so on. Then you might use something like &lt;em&gt;Turborepo&lt;/em&gt; or &lt;em&gt;NX&lt;/em&gt; to put everything in a monorepo...&lt;/p&gt;

&lt;p&gt;When me and our company's architect were arguing about this topic, he actually said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In every case, ever, if there is a library for it, we should use it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;I feel like this is just insane...&lt;/strong&gt;&lt;br&gt;
Sure, existing libraries have already been &lt;em&gt;written&lt;/em&gt;, and they are more standard, maintainable*, and so on. And in many cases I agree, we shouldn't re-invent the wheel, especially when it comes to standards.&lt;/p&gt;

&lt;p&gt;But using all those tools often eventually leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Worse performance&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;INCREASED&lt;/em&gt; complexity&lt;/li&gt;
&lt;li&gt;Lower security
and overall lowers developer satisfaction and skill. (Not to mention that we're still stuck on Material UI 4 with no prospects of upgrading)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't get me wrong, I'm not advocating for inventing your own standards, or (god forbid) using XML. I just feel like doing away with some of our libraries or even excluding big extensive frameworks, could bring really nice benefits to many companies and solo devs.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Plan
&lt;/h2&gt;

&lt;p&gt;I want to do something stupid, I want to undergo a "trial by fire" of sorts to purge my brain, and train my hands to build complex things without relying on code I don't know or don't understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Making a game from scratch
&lt;/h3&gt;

&lt;p&gt;I figured that making a game engine (from as much scratch as I can manage) is a pretty stupid idea. Many have already said that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want to make a game - &lt;strong&gt;do not make an engine&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I figure building an engine is a complex thing, that actually necessitates using a low-level language like C/C++ or Zig. Which will give me plenty of opportunity to suffer, and implement basic things, very terribly, just to prove that I can (...or can't).&lt;/p&gt;

&lt;p&gt;So that's the plan, build a 2/2.5D game (&lt;em&gt;currently in Zig&lt;/em&gt;) while using as little dependencies as I possibly can, and share the process with you guys ❤️.&lt;/p&gt;

&lt;h2&gt;
  
  
  Signs that things are getting better
&lt;/h2&gt;

&lt;p&gt;Despite the mess that most JS/TS startups and developers find themselves in today, I feel like many are realizing it, and the industry is moving in the right direction. More and more companies are switching to greener pastures like Go and Rust. While frameworks are moving things to the backend and trying to simplify your infrastructure.&lt;/p&gt;

&lt;p&gt;But I feel like as long as people keep using JavaScript (and they will), our dependency trees will continue growing and flourishing.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Because when all you know is JS, everything looks like a JS problem.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
