<?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: Satyarth Agrahari</title>
    <description>The latest articles on DEV Community by Satyarth Agrahari (@satylogin).</description>
    <link>https://dev.to/satylogin</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%2F513557%2F76e6244c-796f-4065-a574-c60cba5b6371.jpeg</url>
      <title>DEV Community: Satyarth Agrahari</title>
      <link>https://dev.to/satylogin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satylogin"/>
    <language>en</language>
    <item>
      <title>[Rust] Generic Segment Tree</title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Sat, 23 Apr 2022 05:16:54 +0000</pubDate>
      <link>https://dev.to/satylogin/rust-generic-segment-tree-3cng</link>
      <guid>https://dev.to/satylogin/rust-generic-segment-tree-3cng</guid>
      <description>&lt;p&gt;I wanted to write and keep a segment tree implementation for myself, that I could use more generally (all reasonable data types, and all reasonable operations). I finally decided to write one and add it to &lt;a href="https://github.com/satylogin/cp-lib"&gt;cp-lib&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The core struct for now is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;SegTree&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Clone&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Copy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;Fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;op&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;F&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;And usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SegTree&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cmp&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="nf"&gt;.insert&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;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="nf"&gt;.query&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;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="nf"&gt;.insert_range&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;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="nf"&gt;.query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where op is the operation that the segment tree performs (min, max, gcd, or something custom), and default is the value the should be returned as query default and gets stored during initialization.&lt;/p&gt;

&lt;p&gt;The major benefit is that it allows for me to write more complicated segtree rather easily,&lt;br&gt;
eg:&lt;br&gt;
for problem: &lt;a href="https://codeforces.com/contest/1557/problem/D"&gt;https://codeforces.com/contest/1557/problem/D&lt;/a&gt;&lt;br&gt;
my solution (&lt;a href="https://codeforces.com/contest/1557/submission/154901530"&gt;https://codeforces.com/contest/1557/submission/154901530&lt;/a&gt;) uses&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;seg_tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;SegTree&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;points&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)|&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="na"&gt;.1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="na"&gt;.1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;right&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;Sharing the &lt;a href="https://github.com/satylogin/cp-lib/blob/main/src/ds/range/seg_tree.rs"&gt;full implementation&lt;/a&gt; here in case someone needs it.&lt;/p&gt;

&lt;p&gt;I will keep on modifying it as needed. In case anyone do decide to use it and find some bugs, or have a feature request, do let me know.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>datastructures</category>
      <category>competitiveprogramming</category>
    </item>
    <item>
      <title>Releasing rss-update-0.1-beta.2</title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Thu, 19 Aug 2021 01:51:48 +0000</pubDate>
      <link>https://dev.to/satylogin/releasing-rss-update-0-1-beta-2-511b</link>
      <guid>https://dev.to/satylogin/releasing-rss-update-0-1-beta-2-511b</guid>
      <description>&lt;p&gt;I recently setup neovim editor as my sole IDE. It was fun setting up an IDE with only the things that you care about, and get excellent speedups for both autocomplete and type hints. If you are interested in my current neovim conf, you can check it out at &lt;a href="https://github.com/satylogin/UbuntuSetupHelpers/blob/main/init.vim"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since I had my IDE setup, I wanted to take it out to a test drive, so I started writing a CLI util for something that I wanted to have for few months now. A way to track updates on blogs that I follow with no upper cap on number on blogs. And I recently learned rust, so it was time that I built something using both.&lt;/p&gt;

&lt;p&gt;Presenting &lt;a href="https://github.com/satylogin/rss-update"&gt;rss-update&lt;/a&gt; cli, an util for tracking rss/atom feeds and get updates on them, when you want to. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To add new feeds: &lt;code&gt;rss-update add --feed &amp;lt;FEED&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To fetch new posts in feed: &lt;code&gt;rss-update&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To show unread posts: &lt;code&gt;rss-update unread&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To mark posts as read: &lt;code&gt;rss-feed read --post &amp;lt;URL&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To list tracking feeds: &lt;code&gt;rss-feed tracking&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To remove feed from tracking: &lt;code&gt;rss-feed remove --feed &amp;lt;FEED&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case you try it out and have some feed-back / request, feel free to open an issue or reach out to me.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>programming</category>
      <category>productivity</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Indirect Coupling Story</title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Wed, 21 Jul 2021 09:46:53 +0000</pubDate>
      <link>https://dev.to/satylogin/indirect-coupling-story-bpo</link>
      <guid>https://dev.to/satylogin/indirect-coupling-story-bpo</guid>
      <description>&lt;p&gt;At some point or the other, we all read about importance of decoupling components while writing code. The coupling doesn't just exists in low level code, but one can also observer it at architecture levels. With the exploding popularity of micro services, and the lack of knowledge around building them (or rather the lack of curiosity of actually reading about them), has lead to creeping the same coupling problems between improperly designed micro services. &lt;/p&gt;

&lt;p&gt;But let's not go into complex issues, and look at something which is much more simple and obvious ones you notice it. Its about coupling that is created from using a certain version of library dependency across services, and poor API design that doesn't abstract its effect from leaking out. &lt;/p&gt;

&lt;p&gt;We have around 30-40 different micro services that just our team owns. Recently we started doing some dependency upgrades for our serde libraries that is used to pass object states across different services, and realised that a supposedly simple upgrade is proving to be non trivial. &lt;/p&gt;

&lt;p&gt;The issue arose from the fact that we have plethora of micro services which all share states between each other, and used a serde to achieve the same. The serde however releases changes which are backward incompatible even across minor version upgrades (doesn't follows semver) and so there exists the possibility of breaking api calls between services since the API leaks the serialised objects as API's request / response arguments. &lt;/p&gt;

&lt;p&gt;The way the services communicated also makes upgrading dependency iteratively difficult as they follow somewhat mesh structure, and thus we are stuck in a situation where:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;one has to upgrade it for all required services in one go. 2. pause the traffic. &lt;/li&gt;
&lt;li&gt;verify that we have covered all services by testing over a traffic slice.&lt;/li&gt;
&lt;li&gt;Activate the traffic flow. 
In case something goes wrong, find the missing upgrade, and start with step 1 in loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope that I could say these are legacy services, but sadly they are services that were recently developed.&lt;/p&gt;

&lt;p&gt;The important thing here is the learning that we get from the incident, majorly making micro services truly decoupled, and to achieve that, I guess at least one important thing is designing APIs in a way that doesn't leak effects from their dependencies.&lt;/p&gt;

&lt;p&gt;Oh and also choosing 3p libraries that follow semver in case they are available.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>programming</category>
    </item>
    <item>
      <title>Starship, because it's too damn cool </title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Sun, 09 May 2021 05:31:27 +0000</pubDate>
      <link>https://dev.to/satylogin/starship-because-it-s-too-damn-cool-3nfc</link>
      <guid>https://dev.to/satylogin/starship-because-it-s-too-damn-cool-3nfc</guid>
      <description>&lt;p&gt;This is more of an appreciative post for &lt;a href="https://starship.rs/"&gt;starship&lt;/a&gt;, an amazingly cool and customizable prompt for any shell. And the reason why it is so amazing is because even by default it has too many cool features while being super fast.&lt;/p&gt;

&lt;p&gt;Just to show how it looks by default for rust and python:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3dodcSuF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnwrb9f19g7k07jvnwcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3dodcSuF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnwrb9f19g7k07jvnwcu.png" alt="Default"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that it identifies what kind of project it is while giving information about which branch we are on, and which version of toolchain we are using.&lt;/p&gt;

&lt;p&gt;It is not just limited to what comes by default, and its extensibility and ease of use is what is amazing about it. I use it for my personal projects as well as in my office system to inject information into prompt (like getting to know which versionset I am using).&lt;/p&gt;

&lt;p&gt;And special thanks to Chris Rose for introducing me to this.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>cli</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
    <item>
      <title>Playing the generator game</title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Fri, 26 Mar 2021 05:56:14 +0000</pubDate>
      <link>https://dev.to/satylogin/playing-the-generator-game-2bk4</link>
      <guid>https://dev.to/satylogin/playing-the-generator-game-2bk4</guid>
      <description>&lt;p&gt;A while back I wrote a post on &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/satylogin" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F513557%2F76e6244c-796f-4065-a574-c60cba5b6371.jpeg" alt="satylogin"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/satylogin/how-i-misunderstood-lombok-1k44" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;How I misunderstood Lombok&lt;/h2&gt;
      &lt;h3&gt;Satyarth Agrahari ・ Nov 13 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#java&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;So This time around, I didn't wanted to make the same mistake, which brings us back to this post.&lt;/p&gt;

&lt;p&gt;Let's start with a simple exercise for people familiar with &lt;code&gt;lombok&lt;/code&gt; and &lt;code&gt;dependency injection&lt;/code&gt;. What do you think should be the generated code from the following block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onConstructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@__&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Inject&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@NonNull&lt;/span&gt; &lt;span class="nd"&gt;@Named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testString"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think most people would guess (who have seen generated code by lombok) that this would translate to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Generated&lt;/span&gt;
    &lt;span class="nd"&gt;@Inject&lt;/span&gt;
    &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@NonNull&lt;/span&gt; &lt;span class="nd"&gt;@Named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testString"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testString&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NullPointerException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testString is marked non-null but is null"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;testString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if it would have been the case, then we wouldn't be here, would we...&lt;br&gt;
So lets see what it actually generates&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Generated&lt;/span&gt;
    &lt;span class="nd"&gt;@Inject&lt;/span&gt;
    &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@NonNull&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testString&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NullPointerException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testString is marked non-null but is null"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;testString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;   
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But that's strange, I can clearly see that I have annotated my field with @Named, and so how would it even find the correct dependency to inject, if it will not have the name discovery during dependency injection.&lt;/p&gt;

&lt;p&gt;Well as it turns out, lombok doesn't respects all annotations, since it gets too complicated to implement that behaviour. You can go through the github issues for details &lt;a href="https://github.com/rzwitserloot/lombok/issues/1634" rel="noopener noreferrer"&gt;1&lt;/a&gt; &lt;a href="https://github.com/rzwitserloot/lombok/issues/1570" rel="noopener noreferrer"&gt;2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now that we know that this is the case, the question is how to resolve this. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;in newer versions of lombok one could add &lt;code&gt;lombok.copyableAnnotations += com.google.inject.name.Named&lt;/code&gt; in &lt;code&gt;lombok.config&lt;/code&gt; file and lombok would try and copy them when it generates the constructor.&lt;/li&gt;
&lt;li&gt;Go with our old fashioned constructor based injection and write
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Inject&lt;/span&gt;
    &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@NonNull&lt;/span&gt; &lt;span class="nd"&gt;@Named&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testString"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;testString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;testString&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I prefer 2, because of couple of reasons&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Putting a conf in lombok.config that determines what gets copied hides too much information and is frankly just an accident waiting to happen, when someone see this code sample and tries to copy it and it somehow works because there was no collision.&lt;/li&gt;
&lt;li&gt;It only copies the annotation which have copyable implemented for them, so in case we come across something that is not copyable, we are back to implementing 2 anyways.&lt;/li&gt;
&lt;li&gt;I like the 2nd one since its more expressive, and I like writing codes that are more expressive since eventually we are writing them for humans, and its better if we do not hide too many things and keep things simple so that the person reading code can focus more on business logic rather than spending time understanding why or why not their injection works.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>lombok</category>
      <category>programming</category>
      <category>dependencyinjection</category>
    </item>
    <item>
      <title>How I misunderstood Lombok</title>
      <dc:creator>Satyarth Agrahari</dc:creator>
      <pubDate>Fri, 13 Nov 2020 07:38:24 +0000</pubDate>
      <link>https://dev.to/satylogin/how-i-misunderstood-lombok-1k44</link>
      <guid>https://dev.to/satylogin/how-i-misunderstood-lombok-1k44</guid>
      <description>&lt;p&gt;I was working on a project, and I wrote some code which I expected to work fine. The snippet was like&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;But to my surprise, I got a &lt;code&gt;Medium Priority Warnings&lt;/code&gt; from &lt;code&gt;findbugs&lt;/code&gt;: &lt;strong&gt;&lt;code&gt;RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE: Nullcheck of value previously dereferenced&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Basically it started complaining that I have non null check on a name on line &lt;code&gt;19&lt;/code&gt; after it is being de-referenced at line &lt;code&gt;22&lt;/code&gt; (the numbers are correct, bear with me for a while), so if the value was null, I would get NPE before even reaching this point.&lt;/p&gt;

&lt;p&gt;This error in itself was super confusing, atleast for me. From what I understood here, we are creating Concrete, and checking if both the input are non null. Before it is initialized, we are passing this value super where we have non null check on name, and it is set as value. &lt;/p&gt;

&lt;p&gt;So, I went and looked at the decompiled .class file. It was expanded as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Concrete(
    @NonNull final String name,
    @NonNull final String address
) {
    super(name);
    if (name == null) {
        throw new NullPointerException("name");
    }
    this.address = address;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What was happening here was, the non null check was applied after calling the super class constructor, as opposed to before calling it, which is not what I would have expected. I thought that non null check will happen, before anything is executed in body, but I was definitely wrong. &lt;/p&gt;

&lt;p&gt;Lombok is a super helpful tool, and it has made java much much (a couple of thousands much) less painful. But this incident reminded me again that no matter what, we should never just assume something while writing code.&lt;/p&gt;

&lt;p&gt;This was a great learning for me.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
