<?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: Sylvain Magnier</title>
    <description>The latest articles on DEV Community by Sylvain Magnier (@sourcemancer).</description>
    <link>https://dev.to/sourcemancer</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%2F266914%2F0adb86c1-8685-413c-857e-c7529a7e92ec.jpeg</url>
      <title>DEV Community: Sylvain Magnier</title>
      <link>https://dev.to/sourcemancer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sourcemancer"/>
    <language>en</language>
    <item>
      <title>Some nice laws to consider in your "coding philosophy"</title>
      <dc:creator>Sylvain Magnier</dc:creator>
      <pubDate>Thu, 10 Nov 2022 10:07:38 +0000</pubDate>
      <link>https://dev.to/sourcemancer/some-nice-laws-to-consider-in-your-coding-philosophy-4aif</link>
      <guid>https://dev.to/sourcemancer/some-nice-laws-to-consider-in-your-coding-philosophy-4aif</guid>
      <description>&lt;h3&gt;
  
  
  Hofstadter's law
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;It always takes longer than you expect, even when you take into account Hofstadter's Law.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You better keep in mind that all of your estimates may be wrong to some extends.&lt;br&gt;
Avoid spending too much time to refine those estimate again and again, don't take them for granted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linus' law
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;given enough eyeballs, all bugs are shallow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This one invites you to maximise the amount of peoples checking code through methods like pair programming, code reviews, etc.&lt;br&gt;
The more time you spend to write and to refactor some code samples, the more you're biased on its behavior, and your vision of it become narrower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Murphy's law
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Anything that can go wrong will go wrong.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This one is well known, but easy to miss too.&lt;br&gt;
Maximise your testing efforts through unit tests, integration tests, try to use TDD for guidance.&lt;/p&gt;

&lt;p&gt;Try to test as close as possible to the runtime conditions. For example uses TestContainers instead of H2 for database integration test, this way you'll be able to test against the target stack the soonest in your pipeline.&lt;br&gt;
If you use a database migration tool like Flyway or Liquibase, with TestContainers it can be part of your test too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Postel's law, or the robustness principle
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;be conservative in what you do, be liberal in what you accept from others.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This law suggests that the network may contains malevolent entities which could send anything unexpected to your application. &lt;br&gt;
It invites you to never trust the input, and to be strict regarding the data you send.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Easy time tracing in RxJava and Reactor</title>
      <dc:creator>Sylvain Magnier</dc:creator>
      <pubDate>Thu, 29 Apr 2021 23:12:13 +0000</pubDate>
      <link>https://dev.to/sourcemancer/easy-time-tracing-in-rxjava-and-reactor-2de4</link>
      <guid>https://dev.to/sourcemancer/easy-time-tracing-in-rxjava-and-reactor-2de4</guid>
      <description>&lt;p&gt;There's a lot of ways to track performance issues and unexpectedly time consuming methods, from the quick and simple time measurement from the start to the end of the method, to the more complete (and complex) &lt;a href="http://brendangregg.com/FlameGraphs/cpuflamegraphs.html"&gt;CPU flamegraph&lt;/a&gt;, or even the classics as the thread dump, the cpu profiling, etc.&lt;/p&gt;

&lt;p&gt;In many cases just working with the trick of timestamps and comparison of them will do the job for debugging. &lt;br&gt;
But with the asynchronous paradigm, this may be a too naive way.&lt;/p&gt;

&lt;p&gt;For instance, in a RxJava context, this kind of statements in a method:&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="nc"&gt;Instant&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nc"&gt;Flowable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;itemRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findAllById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idList&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Duration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;between&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;now&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;promise&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;won't really logs the time it took to fetch the repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make old new again
&lt;/h3&gt;

&lt;p&gt;Fortunately, RxJava come with a convenient operator to measure how much time an observable took since its subscription until its completion: &lt;a href="http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Flowable.html#timeInterval--"&gt;&lt;code&gt;timeInterval()&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
(Reactor has a similar operator that I'll detail later)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;timeInterval()&lt;/code&gt; doc explains that it will measure the duration in milliseconds and wrap the previously emitted value in an object containing both the value and the measured delay. With the previous example, &lt;code&gt;Flowable&amp;lt;Item&amp;gt;&lt;/code&gt; would become &lt;code&gt;Flowable&amp;lt;Timed&amp;lt;Item&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thus we can write our code like this:&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;itemRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findAllById&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idList&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;timeInterval&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timed&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;time&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
                    &lt;span class="n"&gt;timed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&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;This time, we effectively log the duration to fetch the items.&lt;/p&gt;

&lt;p&gt;But there's still a subtility: doing so on a Flowable may fatally give you a log per element, not for the overall. What if I perform API calls for a bunch of item ?&lt;br&gt;
That's it:&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="nc"&gt;Flowable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromIterable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idList&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;apiClient:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;getItemInfo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;timeInterval&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timed&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;time&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;timed&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;value&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;We'll get a log per id as we have many API call and as much subscription of the &lt;code&gt;.map(apiClient::getItemInfo)&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  You talked about Reactor too
&lt;/h3&gt;

&lt;p&gt;Reactor provides its own operator called &lt;a href="https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#elapsed--"&gt;&lt;code&gt;elapsed()&lt;/code&gt;&lt;/a&gt;.&lt;br&gt;
It pretty similar as its return a &lt;code&gt;Flux&amp;lt;Tuple2&amp;lt;Long,T&amp;gt;&lt;/code&gt;, the Tuple2 having the methods &lt;code&gt;getT1()&lt;/code&gt; (get left value, here the duration in milliseconds) and &lt;code&gt;getT2()&lt;/code&gt; (get right value, the value emitted by the previous call).&lt;/p&gt;

&lt;p&gt;Thus the above code would look like this:&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="nc"&gt;Flux&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromIterable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idList&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;apiClient:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;getItemInfo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;elapsed&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
            &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tuple&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getT1&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getT2&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;Now imagine that you put some static LongSummaryStatistics somewhere and you're ready to quickly export min/max, average and counts regarding a complex set of operations like, for example, calling two APIs, combining their results and putting some info in a Redis cache.&lt;/p&gt;

</description>
      <category>java</category>
      <category>tracing</category>
      <category>rxjava</category>
      <category>reactive</category>
    </item>
  </channel>
</rss>
