<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lalit Vatsal on Medium]]></title>
        <description><![CDATA[Stories by Lalit Vatsal on Medium]]></description>
        <link>https://medium.com/@lprakashv?source=rss-841731c92c5e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wUD7thKivzw0A1nMRI5VJw.jpeg</url>
            <title>Stories by Lalit Vatsal on Medium</title>
            <link>https://medium.com/@lprakashv?source=rss-841731c92c5e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:03:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lprakashv/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Intro to OpenTelemetry]]></title>
            <link>https://lprakashv.medium.com/intro-to-opentelemetry-a140add00283?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a140add00283</guid>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[opencensus]]></category>
            <category><![CDATA[opentracing]]></category>
            <category><![CDATA[opentelemetry-collector]]></category>
            <category><![CDATA[opentelemetry]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Wed, 18 Dec 2024 14:30:22 GMT</pubDate>
            <atom:updated>2024-12-18T14:30:22.033Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.lprakashv.com/posts/2022-08-24_intro-to-opentelemetry/">Originally posted</a> on my blog (Aug 24, 2022)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/0*ZFFC_bDYCs25ufAn" /></figure><p>To understand the OpenTelemetry, we need to understand the foundational concept of <strong>Observability</strong> first.</p><h3>Observability</h3><p>In the modern day distributed micro-service applications, it is increasingly becoming difficult for developers to monitor and systems with multiple dependencies due the sheer scale and diversity of technologies involved in the system.</p><p>This brings the need for a standard way to understand the running system in a more scalable way.</p><blockquote><em>Observability is a methodology to understand a system from the outside without knowing its inner workings.</em></blockquote><p>The system is called <strong>“observable”</strong>, if it the application code is instrumented to emit traces, metrics, and logs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/0*JngUhIhgXmBBiCqq.png" /></figure><h3>Background History of Observability and OpenTelemetry</h3><p>In the past, there as no standard way in which code was instrumented. And each Observability back-end (Ziplin, Jaeger, Prometheus etc.) had its own instrumentation libraries and agents for emitting data to the tools.</p><p>This meant:</p><ol><li><strong>Lack of Standardized Format:</strong> There was no standardized data format for sending data to an Observability back-end.</li><li><strong>Lack of Portability:</strong> If a user chose to switch Observability back-ends, they would have to re-instrument their code and configure new agents to be able to emit telemetry data to the new tool of choice.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EOL4BUnaFhxOFEbJ.png" /></figure><p>Recognizing the need for standardization and portability, the cloud community came together, and two open-source projects were born:</p><ol><li><strong>OpenTracing</strong> (a Cloud Native Computing Foundation (CNCF) project): This provided a vendor-neutral API for sending telemetry data over to an Observability back-end; however, it relied on developers to implement their own libraries to meet the specification.</li><li><strong>OpenCensus</strong> (a Google Open Source community project): This provided a set of language-specific libraries that developers could use to instrument their code and send to any one of their supported back-ends.</li></ol><p>To standardize things further, in May 2019, <strong>OpenCensus and OpenTracing were merged to form “OpenTelemetry”</strong> (OTel for short). As a CNCF incubating project, OpenTelemetry takes the best of both worlds, and then some.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NG52kxRG_OwVGlxK.png" /></figure><h3>OpenTelemetry</h3><p>OpenTelemetry is the mechanism by which application code is instrumented, to help make a system observable.</p><p>OTel’s goal is to provide a set of standardized vendor-agnostic SDKs, APIs, and tools for ingesting, transforming, and sending data to an Observability back-end (i.e. open-source or commercial vendor).</p><h3>OpenTelemetry Signals</h3><p>In OpenTelemetry, a <strong>Signal</strong> refers to the categories of telemetry that are supported by the specification. It currently supports the Signals below, and more may be added in the future.</p><p><strong>Traces</strong> — Traces give us the big picture of what happens when a request is made by user or an application.</p><p><strong>Metrics</strong> — A metric is a measurement about a service, captured at runtime.</p><p><strong>Logs</strong> — A log is a timestamped text record, either structured (recommended) or unstructured, with metadata.</p><p><strong>Baggage</strong> — Baggage refers to contextual information that’s passed between spans</p><h3>Language specific Instrumentation Libraries (vendor-agnostic)</h3><ul><li><a href="https://opentelemetry.io/docs/instrumentation/">OpenTelemetry code instrumentation is supported for many popular programming languages</a></li><li>Some programming languages support both auto-instrumentation and manual instrumentation while others may support only one of them based on language limitations.</li></ul><p>Checkout <a href="https://opentelemetry.io/docs/concepts/instrumenting/">instrumenting</a> for more details.</p><h3>Collector (vendor-neutral)</h3><p>OpenTelemetry Collector (otel-collector) is a vendor-neutral component which allows telemetry data (signals) to be collected, processed upon and exported to an observability backend in their respective formats.</p><p>OpenTelemetry Collector can be customized using its configuration, an example configuration would look like:</p><pre>receivers:<br>  otlp:<br>    protocols:<br>      grpc:<br>      http:</pre><pre>processors:<br>  batch:</pre><pre>exporters:<br>  otlp:<br>    endpoint: otelcol:4317</pre><pre>extensions:<br>  health_check:<br>  pprof:<br>  zpages:</pre><pre>service:<br>  extensions: [health_check,pprof,zpages]<br>  pipelines:<br>    traces:<br>      receivers: [otlp]<br>      processors: [batch]<br>      exporters: [otlp]<br>    metrics:<br>      receivers: [otlp]<br>      processors: [batch]<br>      exporters: [otlp]<br>    logs:<br>      receivers: [otlp]<br>      processors: [batch]<br>      exporters: [otlp]</pre><p>We can specify “pipelines” in the otel-collector, each for <strong>metrics</strong>, <strong>traces</strong> and <strong>logs</strong> which are comprised of:</p><ol><li><strong>Receivers:</strong> A receiver accepts data in a specified format, translates it into the internal format and passes it to processors and exporters defined in the applicable pipelines</li><li><strong>Processors:</strong> A processor pre-processes data before it is exported (e.g. modify attributes or sample) or helps ensure that data makes it through a pipeline successfully (e.g. batch/retry).</li><li><strong>Exporters:</strong> An exporter translates the internal format into another defined format.</li></ol><p><strong>Extensions:</strong> There is another type of component called “extensions”, which provides capabilities on top of the primary functionality of the collector. Generally, extensions are used for implementing components that can be added to the Collector, but which do not require direct access to telemetry data and are not part of the pipelines (like receivers, processors or exporters)</p><p>There are 2 flavors of otel-collector present:</p><ul><li><a href="https://github.com/open-telemetry/opentelemetry-collector">Core otel-collector</a>: This is the standard collector provided by the open-telemetry and have certain receivers, processors, exporters and extensions built-in for any generic usage.</li><li><a href="https://github.com/open-telemetry/opentelemetry-collector-contrib">otel-collector-contrib</a>: This is a superset of standard otel-collector, this includes open source community contributed components for vendor specific data-formats. Use this if you want to have any vendor-specific observability backends or data sources (instrumentation, applications etc.) to support in your system.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R-MuTnMcWyXmsm7i.png" /></figure><h3>Conclusion</h3><p>To conclude this, OpenTelemetry provides robust standards, rich set of libraries for various programming languages, and tools for instrumenting, collecting, transforming and exporting telemetry data to make the system observable.</p><p>Here is the general OpenTelemetry workflow diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oBRPeLKVTtF5X9S8.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a140add00283" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Better Switch-Case for Java!]]></title>
            <link>https://lprakashv.medium.com/better-switch-case-for-java-b3b09f14cebc?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3b09f14cebc</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[pattern-matching]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Tue, 25 May 2021 11:30:29 GMT</pubDate>
            <atom:updated>2021-05-25T18:08:24.717Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wEzfzGBe9OCzVDxUIB1_kQ.png" /></figure><p>Let’s start with the question, what is wrong with the switch-case anyway? Here’s a simple example of a switch-case in Java:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WP655OTAIKvcOA_AugLrDA.png" /></figure><p>Can you see the problem here? It is the unnecessary “ceremony” of writing the “break” statement on each case!</p><p>You can argue that we can just return after each case-block, while this is a brilliant idea but, then the ceremony would be of writing “return” in each block 😜 you can even miss that!</p><p>In fact, the return statement at every case-block means that every block should <strong>always</strong> “return” or “evaluate” to something! Which is the most fundamental idea in Functional Programming, where <strong><em>every expression must return or evaluate something</em></strong> just like a mathematical “function”.</p><pre>value := <strong>F(x)</strong> &lt;--- always returns a value!</pre><p>In, fact this switch-case with always returning case blocks is omnipresent in all the FP languages as <strong>“Pattern Matching”</strong>, here is a simple determistic example code in Clojure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xMQervJJ974xYvI7pnGnVw.png" /></figure><p>Some languages like Scala, Haskell take this idea even further to allow partial match or even destructured matches on the input argument:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctjT0yZU5G49hvL5behsPQ.png" /></figure><p>Enough features of other languages now! Now let’s come back to our hero here “<strong>JAVA</strong>”, can we do similar things in Java?</p><blockquote>Yes and No.</blockquote><p>The standard library doesn’t have any similar feature but there are a few libraries supporting this, one among those is <a href="https://docs.vavr.io/#_pattern_matching"><strong>Vavr</strong></a> check it out!</p><p>However, I am going to shamelessly endorse my own library for this <a href="https://github.com/lprakashv/patternmatcher4j">job: <strong>“patternmatcher4j”</strong></a>.<strong> </strong>You can start by adding the dependency:</p><p><strong><em>Maven:</em></strong></p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;io.github.lprakashv&lt;/groupId&gt;<br>    &lt;artifactId&gt;patternmatcher4j&lt;/artifactId&gt;<br>    &lt;version&gt;0.2.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p><strong><em>Gradle:</em></strong></p><pre>implementation &#39;io.github.lprakashv:patternmatcher4j:0.2.0&#39;</pre><p>Here is our simple case-match with the enum:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AxMqm_qXIpr2r7s-ZWgsiA.png" /></figure><p>Now, let’s see some advanced match-cases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VqSytQVQv5UZ8LFOi1Wshg.png" /></figure><p>Ins’nt this awesome? Short explanation of the library code-test above:</p><p><strong>PMatcher&lt;T, R&gt;:</strong></p><p>This is like an actual switch-case block, we initialize it using a constructor taking an object to be matched. This is a parameterized class where T = input matched object type and R = return type of the block.</p><p><strong>Match Cases:</strong></p><p>We can create a match-case using:</p><ol><li>.matchValue(value) — to match the matched-object with the passed value using Java’s <strong><em>equals()</em></strong> method,</li><li>.matchRef(ref) — to matched the reference of the original matched-object with the passed ref.</li><li>.matchCase(..) — advanced matching!</li></ol><p><strong>Advanced matchCase():</strong></p><p>We can match using 3 different types of cases:</p><ol><li>Predicate match: To match the matched-object using a predicate. — .matchCase(p -&gt; methodReturningBoolean(p))</li><li>Class match: To match the class of the matched-object — .matchCase(SomeClass.class)</li><li>De-structured match: To match matched-object’s fields (which internally uses Reflection API). <em>You can even use the recursive match with the </em><strong><em>MField</em></strong><em> objects passed to the matchCase passing any of predicate, class, further de-structured matches </em>— .matchCase(MField.with(&quot;field1&quot;, ...), MField.withValue(&quot;field2&quot;, field2Value), MField.with(&quot;field3&quot;, Field3.class))</li></ol><p><strong>Actions:</strong></p><p>Each of the matchCase, matchValue and matchRef produces an object of class “CaseActionAppender” which has 3 “action” methods which you can use to define action on the preceding match-case.</p><ol><li>.thenReturn(R returnValue) — to return the value on the preceding match-case.</li><li>.thenSupply(Supplier&lt;R&gt; supplier) — to evaluate the return value lazily using a supplier.</li><li>.thenTransform(Function&lt;T,R&gt; fn) — to evaluate the return value by performing transformation operation on the matched-object.</li></ol><h3>Fin.</h3><p>Please use the library and provide your valuable feedback, you can even raise a PR and I will look into it. This was my very first maven-central deployment so any suggestion will be welcomed :)</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3b09f14cebc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bulk RDBMS Upserts with Spring]]></title>
            <link>https://medium.com/analytics-vidhya/bulk-rdbms-upserts-with-spring-506edc9cea19?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/506edc9cea19</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[bulk-data]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[upsert]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Fri, 28 Aug 2020 21:08:07 GMT</pubDate>
            <atom:updated>2020-09-08T08:30:51.128Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTwZq7ceSHClNuAconBNig.jpeg" /></figure><p>Upsert is a fairly common terminology in databases, meaning <strong><em>Up</em></strong>date if the record exists or In<strong><em>sert</em></strong> the new record. Upserts make more sense in case of simple object save requests with new information.</p><h3>Why Bulk?</h3><p>If we talk about any <strong><em>data sync</em></strong>, <strong><em>data migration</em></strong> or <strong><em>bulk data update jobs</em></strong>, we are bound to have a bulk upsert scenario to update whatever we have in the database and insert all the new rows.</p><h3>Solutions</h3><p>We will be discussing about the solutions present in the Spring Boot environment and inferences we make out of them.</p><p>For the testing, I will be using MS SQL Server as the database and will be limited to it’s functionality but the concepts are fairly generalisable.</p><h4>#1 Standard `saveAll()` Solution using Spring JPA</h4><p>Consider we have a price table with a composite unique key/constraints having like with structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gAhF3sDKiEo8tZqZ_5VkDA.png" /></figure><p>Then, our entity’s composite primary key and entity classes would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iEx6e5V_4Hy9Dq7lAtGR9Q.png" /></figure><p>Assume we are consuming a stream of record batches to upsert. To mock this, we have a supplier to generate a random Price1 object batches of size 1000. The ingestion code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4lDrZepN3aEoA2JOh1CFA.png" /></figure><p>Let’s see the results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TM0vPLHqxsFiiCkwekmufA.png" /></figure><p>Now, on looking closely the time taken to persist each batch increases as the table get filled up! What is this?</p><p>The culprit is the the method .save() of SimpleJPARepository class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*Wa-CB925OwTJtnbpPxQAaw.png" /></figure><p>Since, the save performs both the insert and update operation, it has to check whether an entity is “new” or not. For which, it has to either check in the persistence context or query the database, which will get complex in time as the table get filled up.</p><h4>#2 Optimisations for Bulk save</h4><p>Our bottleneck in the previous approach was continuous reads from the DB for a primary key combination to check whether to perform .persist() / insert or .merge() / update.</p><p>To avoid additional querying on the table for .isNew() we could have another auto generated unique field (row) which is independent of the business logic. So that, every new object will have a unique id and will always do .persist() for them.</p><p>Let’s do the modifications on a completely new table (keeping the business columns as they are), with an additional auto-incremented “identity” column :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xW-IXit0NUpStjsRbC56Bw.png" /></figure><p>Here is our entity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v-_LZt_TYUkivGn5ViKKBw.png" /></figure><p>Testing out similar ingestion code will yield the result like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-WtKO4AMfIi9nK0Olh-HtA.png" /></figure><p>Hooray, Constant time and faster inserts! But wait,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2bDAsDu9qJhecCz0NLcyMw.png" /></figure><p>There are now duplicate rows with upc, store_id combinations</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RjW5H6mqO8anN95d8I7Jfw.png" /></figure><p>Which is understandable, as we are doing no updates.</p><h4>#3 Plain inserts then merge</h4><p>The above 2 experiments have encouraged us to keep the insert model for faster inserts and somehow merge (do updates) later.</p><p>To achieve this, we can have a “stage” table having a unique auto-generated id for inserts, separate from our target main table. And a pos-ingestion job for merging the records after de-duplicating.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bnq1-uR0W5hDaNEjlvyxlw.png" /></figure><p>Plain inserts can go as the done above, we will now try to write a “merge” step. This step can very well be database stored procedure due to following reasons:</p><ol><li>To avoid movement of data and process the bulk data where it resides.</li><li>Database specific optimisations are built-in.</li></ol><p>Following is an example naive implementation of a merge stored procedure for MS SQL, similar merge query features are present most of the mainstream databases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qA9C1Oo2T4pHOqPS78iaoA.png" /></figure><p>Executing this took less than a second!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*Ck-xzwu6sKaeKfFGkrPk_A.png" /></figure><p><strong>Important Notes:</strong> The above stored procedure implementation is a very naive approach just to demonstrate and misses on a lot of the aspects to be taken care of in a production environment, like:</p><ul><li>Over utilising transaction log size due to huge data in the merge statement, <strong><em>batch-wise merging with id range </em></strong>could be implemented here.</li><li>Monitoring and logging procedure failures, a TRY-CATCH based procedure with logging failures in a <strong><em>procedure-audit logging table</em></strong> could be used.</li><li>A successfully merged batch-range delete instead of truncating the stage table.</li></ul><h4>#Extra: Further Improving batch inserts</h4><p>If we try to log the hibernate generated SQL statements for our .saveAll() operation, we will get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4Uor_n-ssZOd5Knzv7WYQ.png" /></figure><p>Here, we are firing single insert statements with values to insert, each going over the network.</p><p>There are some improvements that can be done here:</p><ol><li>Batch the queries and fire in call to the database over network.</li><li>Rewrite the single queries into a form of single multi-row query.</li></ol><p>For first, we can make use of hibernate properties:</p><pre>spring.jpa.properties.hibernate.jdbc.batch_size=1000</pre><pre>spring.jpa.properties.hibernate.order_inserts=true</pre><p>For second, there are bulk (multi-row) insert query options available in most of the mainstream database solutions (Postgres, MySQL, Oracle). With syntax like:</p><pre>insert into myschema.my_table (col1, col2, col3) <br>values<br>(val11, val12, val13),<br>(val21, val22, val23),<br>....<br>(valn1, valn2, valn3);</pre><p>While, Postgres and MySQL do support this features with the help of JDBC flag: <strong><em>reWriteBatchedInserts=true</em></strong></p><p>But unfortunately, according to <a href="https://docs.microsoft.com/en-us/sql/connect/jdbc/use-bulk-copy-api-batch-insert-operation?view=sql-server-ver15">this resource</a>, ms-sql JDBC driver does not support the multi-row rewrite of the queries. So, if we want to do this, we would have to write the insert queries manually.</p><p>Manual insert query creation could look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KniRb0UL9QZV2swv1TG9mA.png" /></figure><p>The ingestion code would use entityManager.createNativeQuery() method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uipYQIloPkK2kcXtPUebQg.png" /></figure><p>Let’s test it out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-wW3Tp9XjkOPjdrSYJDvmw.png" /></figure><p>Wow!! Those batches of 1000 records took less than a second, let’s look at the milli seconds now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RugMAxqQfFcdvlImRaCtWw.png" /></figure><h3>Conclusion</h3><p>We can safely infer from our above experiments that:</p><ol><li>Achieving bulk Upserts are really complicated in relational databases.</li><li>Spring JPA and Hibernate provided out-of-the-box save methods won’t scale for huge loads.</li><li>A decoupled (from business columns/fields) independent primary key will help improve the insert performance of the inserts, but will eventually insert duplicate records.</li><li>Separating insert-only and read (actual) table can be used to improve ingestion performance.</li><li>A post insert, merge strategy can be used for de-duplicating inserted records. Stored procedures work well in this scenario.</li><li>Native SQL queries will give the most performant result, use solutions which are as close to the database as possible where performance is critical.</li></ol><h4>Testing Environment:</h4><ul><li>Macbook Pro 2016 model 15&quot; with 16 GB RAM.</li><li>Application uses Java 1.8 with Spring Boot 2.3.3.</li><li>MS SQL server 2017 database in a Docker container.</li></ul><p>Thanks for Reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=506edc9cea19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/bulk-rdbms-upserts-with-spring-506edc9cea19">Bulk RDBMS Upserts with Spring</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Going Reactive!]]></title>
            <link>https://medium.com/swlh/going-reactive-f6c22aa10597?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6c22aa10597</guid>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[pub-sub]]></category>
            <category><![CDATA[reactive]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Thu, 16 Apr 2020 10:47:47 GMT</pubDate>
            <atom:updated>2020-05-28T19:41:05.072Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*3pJ3uV2SIfFTmhV7i4HbXA.png" /><figcaption>Logo courtesy: <a href="http://reactivex.io/">http://reactivex.io/</a></figcaption></figure><p>There is a lot of hype around this shiny new thing in web applications development world called being “Reactive”! There are many frameworks popping up with this slogan, e.g., Vert.x, Akka, Spring Webflux, Play, RxJava, RxJS etc. to name a few. What does it mean? and why is this hype? is it worth your time? Let’s explore…</p><h3>What does Reactive mean exactly?</h3><p>Being reactive means <strong><em>ability to “react” to “events/changes”.</em></strong> One very simple example is how a column in excel-sheet gets updated (reacts) if the column which it depends upon changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*9YNyZlApPID0oit-CEp78g.gif" /></figure><p>This is a known design pattern and <strong><em>can be implemented using “Observer pattern” or a “Publisher-Subscriber pattern”</em></strong>, where there is a publisher and one or more subscribers to it.</p><p>There are two distinct things which are Reactive, [a] <strong>Reactive programming</strong>, which uses the reactive programming principles (including the above stated observer pattern) and [b] <strong>Reactive system</strong> where an entire system follows the guidelines of the “<a href="http://www.reactivemanifesto.org/">reactive manifesto</a>”. The latter one in fact is much bigger picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GRUgjeKco0kOnK-vrFOOzg.png" /><figcaption>Courtesy: <a href="https://www.lightbend.com/white-papers-and-reports/reactive-programming-versus-reactive-systems">https://www.lightbend.com/white-papers-and-reports/reactive-programming-versus-reactive-systems</a></figcaption></figure><p>There is a really <a href="https://www.lightbend.com/white-papers-and-reports/reactive-programming-versus-reactive-systems">cool post</a> about this difference from Lightbend, do check it out! <strong><em>We will however stick to the “reactive-programming” for the scope of this article</em></strong>.</p><h3>Reactive Programming</h3><p>We just saw that we can implement reactive using our plain-old Observer pattern or a Publisher-Subscriber (PubSub) pattern.</p><blockquote>So, is this it?</blockquote><p>Try to fit every piece of your code with a Pub-Sub pattern, you would need certain disciplinary guidelines to be able to make use of it without pulling your hairs out! In fact there are more than just PubSub and Observer pattern to implement reactive e.g., event-loop, actor model etc.</p><p>There are some fundamental building block of the reactive programming model:</p><p><strong><em>Non-blocking:</em></strong> Execution of a task shouldn’t block another task’s execution.</p><blockquote>In our PuSub context, any event should be handled without blocking the execution of upcoming events.</blockquote><p><strong><em>Asynchronous:</em></strong> Tasks happen asynchronously/separately and may notify after they’re done. Most popular programming pattern seen with this model is callbacks.</p><p>CompletableFuture is a very good example for asynchronous programming in Java:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4515e9baac0ca8feefdc5beaf35c6f31/href">https://medium.com/media/4515e9baac0ca8feefdc5beaf35c6f31/href</a></iframe><p>The above code is asynchronous as well as it won’t block! Most often than not, <strong><em>non-blocking and asynchronous are the same thing</em></strong>. If a piece of code is asynchronous, it has to be non-blocking and vice-versa.</p><p>There is one major problem with the callbacks, and that is the <a href="http://callbackhell.com/"><strong><em>Callback Hell</em></strong></a> when we have more than 2 nested callbacks, the code starts to look obscure and becomes unmaintainable very quickly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ede6b1c90dec2f99c54bc303288475cf/href">https://medium.com/media/ede6b1c90dec2f99c54bc303288475cf/href</a></iframe><p><strong><em>Functional/Declarative:</em></strong> To overcome the callback hell and be able to compose (or chain) the operations on the events subscribed, we should use the functional/declarative style. Suppose there is a high level construct of Publisher and Subscriber such that we could use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/027d4b80e8e6e89b2d1d36aa8e3f7f9e/href">https://medium.com/media/027d4b80e8e6e89b2d1d36aa8e3f7f9e/href</a></iframe><p>The above code looks more elegant even though it performs a lot of things in steps with a merge chaining of functions. It was just an example taken from Spring Webflux’s Publisher/Subscriber API. There are other reactive libraries and frameworks where this type of functional composition and chaining is possible.</p><p>Java 9 also has introduced <a href="https://www.reactive-streams.org/">Reactive Streams</a> and it would be exciting to see how this evolves over time.</p><h3>OK, what to I get from being Reactive?</h3><p>In a traditional blocking code, a thread may get blocked and awaits for the result of some other task. While waiting, it is simply doing nothing! But since, the thread is there, it will consume the precious CPU cycles, waking up checking if the execution is completed and going back to sleep if the result has not arrived.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PQcwErAAaeimgYCTWJRKBw.png" /></figure><p>While, non-blocking code usually implemented using either,</p><p><strong><em>Event-loop</em></strong>: I is essentially just a single thread! This event-loop executes/notifies the process to be run/invoked next without getting blocked. Hence, instead of multiple threads waiting for the resource, there’s just this one thread which handles all the I/O and resource allocation to different requests. Just like a waiter does for multiple customers. Example implementations are Vert.x, nodejs, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*zC4vZWgRB27v7RpJ0b8JGg.png" /></figure><p><strong><em>Event driven thread-pool backed mechanism</em></strong>: Event driven mechanism involving very small number of threads usually employs some virtual process units (actors, verticles, queue etc.) on top of a thread-pool framework (Fork-Join) making an efficient use of it. Example implementations are Akka Actor model, Reactor, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J1Eef_R00BQR7InyrcYYWA.png" /></figure><p>In addition to that, subscribers being notified (push based) on events by the publisher means that subscribers don’t have to keep polling for the events, saving further more CPU resources.</p><p>Ultimately this all leads to better resource management and this is why the reactive model can work with even lesser number threads.</p><h3>Why the sudden Need?</h3><p>We live in the era of “clouds” and “scale”. And by scale, I mean massive scale with thousands to hundreds of thousands users concurrently using your application.</p><p>Before the “reactive” hype, every web application out there used to work on a synchronous/blocking <strong><em>“1 thread per request model”</em></strong>. Which means that <strong>a thread is allocated for every request</strong>! This model served perfectly well for us in past, but in today’s age of mobile apps cheaper and accessible internet, the number of users a “viral” app has to handle can grow really fast.</p><p>This massive scale demands resources (larger or more machines) and getting resources in cloud costs money! So, we desperately needed a model which could scale even with lesser resources. In our case lesser number of threads for a highly concurrent application.</p><h3>Use cases</h3><p>As we discussed about this reactive model of programming, you might have had the impression of this being the holy grail for modern web programming. This does in fact also add some noticeable complexity to the system. As they say,</p><blockquote>If you consider all you have is as a hammer, everything else will start looking like a nail!</blockquote><p>There is a lot, which is required from a developer to develop truly reactive applications:</p><p><strong><em>Discipline is Needed:</em></strong> If you are using a reactive framework and the database driver is blocking, you will end up blocking threads, same applies to every other block of you system. Hence, to have a proper reactive application, you have no option but to go <strong><em>“all reactive”</em></strong>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fj3KXqE52HwpQcatFj5%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fj3KXqE52HwpQcatFj5%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fj3KXqE52HwpQcatFj5%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f958faac7fa21f5456625b279866016/href">https://medium.com/media/6f958faac7fa21f5456625b279866016/href</a></iframe><p>This adds even more complexity and does influence many critical design decisions that have to be made, such as:</p><blockquote>1. Which DB to use, does it have reactive drivers? <br>2. Does the libraries I am using support reactive?<br>3. Is there any blocking operation that I am doing within my system?<br>4. Do I have written appropriate adapter/wrappers over all the non-reactive components?</blockquote><p>So, better stick to the traditional frameworks unless there is a need otherwise. Some such scenarios could be:</p><ul><li>Highly concurrent application.</li><li>With limited resources.</li><li>Handling request spikes (back-pressure).</li></ul><p>This is all from me and hope you got some reactive knowledge out of this post.</p><p>Thanks for Reading and have a Good day :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6c22aa10597" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/going-reactive-f6c22aa10597">Going Reactive!</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Akka Streams in Java Spring Boot!]]></title>
            <link>https://medium.com/analytics-vidhya/akka-streams-in-java-spring-boot-f7749cafb7f5?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7749cafb7f5</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[akka-streams]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[alpakka]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Sun, 12 Apr 2020 10:45:58 GMT</pubDate>
            <atom:updated>2020-09-08T08:29:58.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*PZWmwJFrF3_hMnCciD7mOw.png" /></figure><p>Streaming data from a Source to Sink is a very trivial task in today’s data processing and data pipelining systems. Ergo, there are many streaming solutions out there like: Kafka Stream, Spark Streaming, Apache Flink etc.</p><p>All of them in one way or another either need an infrastructure to be setup to be able to fully take advantage of them (e.g., HDFS, Spark cluster, Kafka streaming setup etc.) or we need some kind of orchestration among the streaming jobs (e.g., Apache Airflow).</p><h3>Akka Streams</h3><p>Akka streams stands out in this battle and have this advantage of being totally application driven. Akka stream is build on top of the Akka’s celebrated Actor model (which in fact is inspired from Erlang’s actor model). Hence, Akka streams can leverage its battle tested <a href="https://www.lightbend.com/blog/why-do-we-need-a-reactive-manifesto">resiliency, elastic, event-driven and responsive</a> (see <a href="https://www.reactivemanifesto.org/">reactive manifesto</a>) capabilities.</p><p><strong>Problems with Akka</strong></p><ol><li>Java developer community has been staying away from the “made-for and built-on scala” Akka platform.</li><li>Not much documentation and support for the most popular Java framework, “Spring”.</li></ol><p>I’m here to tell you otherwise! Inspite of lack of resources available on the internet, we can in-fact do akka-streams in Java and do it with ease.</p><p>In this post we will build an Akka stream application <strong>in Java</strong> and with <strong>Spring Boot!</strong> And we will analyse the out of the box benefits we can get using Akka streams. So, let’s ge started…</p><h3>Problem Statement</h3><p>We need a simple realtime stream which consumes all the updates published on a Kafka topic and persists the events in a SQL server database after parsing. And we only want to commit the Kafka offset after the record has been inserted into the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rGNbm-gnpZJA7sL7Pib2A.png" /></figure><p><strong>Lets start a fresh Spring boot project from Spring Initialzr.</strong></p><p><a href="https://start.spring.io/">Spring Initializr</a></p><p>We will make the project organised like the tree below which is a pretty standard maven directory structure (since we have chosen to use maven here). You could go with gradle also if you like.</p><pre>.<br>├── pom.xml<br>└── src<br>    ├── main<br>    │   ├── java<br>    │   │   └── com<br>    │   │       └── lprakashv<br>    │   │           └── springalpakka<br>    │   │               ├── SpringCommandLineApplication.java<br>    │   │               ├── configs<br>    │   │               │   ├── AkkaConfig.java<br>    │   │               │   └── StreamConfig.java<br>    │   │               ├── dtos<br>    │   │               │   └── Event.java<br>    │   │               ├── services<br>    │   │               │   └── StreamService.java<br>    │   │               └── utils<br>    │   │                   └── StreamUtils.java<br>    │   └── resources<br>    │       ├── application.yml<br>    │       └── stream.conf<br>    └── test<br>        └── java<br>            └── com<br>                └── lprakashv<br>                    └── springalpakka</pre><p>Don’t bother about the code files in the tree, we will shortly talk about those.</p><p><strong>Add all the required dependencies:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMSapi8oZTX_e650DxiO5A.png" /></figure><p><strong>Setup Foundational Akka configurations</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c8d41e2cfc55f4bbe9229cf20dbca0e/href">https://medium.com/media/9c8d41e2cfc55f4bbe9229cf20dbca0e/href</a></iframe><p>In every Akka/Akka-Stream application the very basic components needed are the Akka’s <strong>ActorSystem</strong> and <strong>Materializer</strong>. These are needed for a lot of things in this eco-system like, spawning actors, creating stream components, running streams, materializing streams etc.</p><p>In the above code, we made sure:</p><ol><li>We have only one instance beans of both ActorSystem and Materializer throughout our application.</li><li>Will be instantiated only if <strong><em>akka.stream.javadsl.Source</em></strong> is in scope.</li></ol><p><strong>Our Kafka Event DTO to consume:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81f60ffb78b6e801be5f4b8cd895e0a6/href">https://medium.com/media/81f60ffb78b6e801be5f4b8cd895e0a6/href</a></iframe><p><strong>Let’s write our Source (Kafka)</strong>: We would like to commit the offsets later hence using a <a href="https://doc.akka.io/api/alpakka-kafka/2.0.2/akka/kafka/scaladsl/Consumer$.html#plainSource[K,V](settings:akka.kafka.ConsumerSettings[K,V],subscription:akka.kafka.Subscription):akka.stream.scaladsl.Source[org.apache.kafka.clients.consumer.ConsumerRecord[K,V],akka.kafka.scaladsl.Consumer.Control]">committableSource</a>. We will create a Bean out of our committable source to be autowired in our service classes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf589fa13d1a0c6047802e3172c474cf/href">https://medium.com/media/cf589fa13d1a0c6047802e3172c474cf/href</a></iframe><p><strong>Now let’s write our Flow (Slick):</strong> We could have cerated a Sink if we were to not bother about persist result and commit Kafka offset anyway (using a “<a href="https://doc.akka.io/api/alpakka-kafka/2.0.2/akka/kafka/scaladsl/Consumer$.html#plainSource[K,V](settings:akka.kafka.ConsumerSettings[K,V],subscription:akka.kafka.Subscription):akka.stream.scaladsl.Source[org.apache.kafka.clients.consumer.ConsumerRecord[K,V],akka.kafka.scaladsl.Consumer.Control]">plainSource</a>”). We are using flow instead of sink because we want to propagate the committable-offset even after the database stage. We will add the following code in our same config class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a89011266f689262f02c00e8bebd6288/href">https://medium.com/media/a89011266f689262f02c00e8bebd6288/href</a></iframe><p>You might be wondering what is in the <strong><em>stream.conf</em></strong> file and where did that <strong><em>committableMesssageToDTO</em></strong> and <strong><em>insertEventQuery</em></strong> came from?</p><p>Slick needs a configuration to create a session which will execute all our DB queries. This config needs to follow the below structure in a <strong><em>.conf</em></strong> file (which is the standard way to access configs in the typesafe/lightbend world).</p><pre>event-sqlserver {<br>  profile = &quot;slick.jdbc.SQLServerProfile$&quot;<br>  db {<br>    dataSourceClass = &quot;slick.jdbc.DriverDataSource&quot;<br>    properties {<br>      driver = &quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;<br>      url = &quot;your-db-url!&quot;<br>      user = &quot;your-db-user!&quot;<br>      password = &quot;your-db-pass&quot;<br>    }<br>  }<br>}</pre><p>While, the <strong><em>committableMesssageToDTO</em></strong> and <strong><em>insertEventQuery</em></strong> are functions which will convert our CommittableMessage to DTO (our record class) and then to SQL insert query.</p><p>We can write a Utils class with static functions to generate the SQL like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eb25a57828f62f75ffcf2bf1f133269/href">https://medium.com/media/5eb25a57828f62f75ffcf2bf1f133269/href</a></iframe><p><strong>Now, let’s piece them together and build a stream:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/528e6365b0dc776c648923818a42b73b/href">https://medium.com/media/528e6365b0dc776c648923818a42b73b/href</a></iframe><p>This is it! We can now invoke the startKafkaToDatabaseStream() method from anywhere and it will do our job.</p><p>There were a lot of highlighting features there in a single “terse” chain! Let me explain each:</p><ol><li><strong><em>.buffer(size, overflowStrategy)</em></strong>: This will add a fixed-<strong><em>size</em></strong> buffer into our “flow” which will protect the downstream system from a faster upstream source. We can use the strategies for discarding the messages if the size is full: <strong><em>backpressure</em></strong> — will down the consumption, <strong><em>drophead/droptail/dropbuffer</em></strong> — will drop the messages and won’t backpressure and <strong><em>fail</em></strong> — fails the stream on buffer full.</li><li><strong><em>.idleTimeout(duration)</em></strong>: It will throw a <strong><em>java.util.concurrent.TimeoutException</em></strong> on idle timeout duration which can be handled using recover/recoverWith.</li><li><strong><em>.recoverWith(ThrowableClass, fallbackSourceConsumer)</em></strong>: Whenever a ThrowableClass.class exception is intercepted, the original source is replaced by a Source received from the fallbackSourceConsumer.</li><li><strong><em>.throttle(elements, per, maximumburst, mode)</em></strong>: Sends elements downstream with a speed limited to (<strong><em>elements/per</em></strong>), mode=Shaping — makes pauses before emitting to meet the throttle rate, mode=Enforcing — fails with exception when upstream is faster than the throttle rate.</li><li><strong><em>.mapAsync(parallelism, () -&gt; CompletionStage(value))</em></strong>: To process the stage in asynchronous mode with parallelism defined.</li><li><strong><em>CommitterSettings.create(actorSystem)</em></strong>: Creates a default committer settings.</li></ol><p>The DrainingControl can be used as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f66cb34250d219582317646d23c2a9e/href">https://medium.com/media/7f66cb34250d219582317646d23c2a9e/href</a></iframe><p>There are a lot of other features of akka stream which were not in scope of this post but still worth to explore. Please checkout <a href="https://doc.akka.io/docs/akka/current/stream/index.html"><strong>akka-stream</strong></a> and <a href="https://doc.akka.io/docs/alpakka/current/index.html"><strong>alpakka’s</strong></a> documentations.</p><h3>Conclusion</h3><p>We saw that we could easily integrate Akka streams into our Spring Boot projects and leverage Spring’s dependency injection to manage our Akka/Stream beans easily.</p><p>There are a lot of streaming features like back-pressure and throttling to name a few which would easily take up a lot of developer hours and brains. And yet, we would have missed some corner cases if not tested thoroughly. Those things we got out-of-the-box in akka-stream’s toolkit.</p><p>All in all, Akka-streams along with Alpakka are great tools to have in a data platform stack.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7749cafb7f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/akka-streams-in-java-spring-boot-f7749cafb7f5">Akka Streams in Java Spring Boot!</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling Nulls in nested objects (Java)]]></title>
            <link>https://medium.com/swlh/handling-nulls-in-nested-objects-java-7079b9413ec9?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7079b9413ec9</guid>
            <category><![CDATA[optionals]]></category>
            <category><![CDATA[lambda-function]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Sun, 29 Mar 2020 09:14:32 GMT</pubDate>
            <atom:updated>2022-04-04T10:14:18.585Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTbmkpD3ejhzaPCYQnN0Cw.png" /></figure><p>Handling NullPointerException and keeping track of all the nullable values has always been a pain for Java developers.</p><p>This is even worse when you are working with deeply nested objects and handling all the null values grows exponentially with each nullable nested value. This is visible by all the statements like:</p><pre>if (x.y().z().. != null) {…}</pre><p>In this article, we will explore some of the techniques starting with the naive approach to a more advanced one for handling such scenarios.</p><p>So, are you ready? Let’s dive in…</p><p>Suppose we have a simple but nested object structure like:</p><pre>root {<br>   first-level {<br>     second-level {<br>       third-level {<br>         name: &quot;string&quot;<br>       }<br>     }<br>   }<br> }</pre><p>where each object and/or value can be nullable. Say our objective is to create a greeting for the name nested three level deep behind root object.</p><p>Here are our test objects with our testing code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a6240a5ee74666ba9fdb868be9e2c89/href">https://medium.com/media/8a6240a5ee74666ba9fdb868be9e2c89/href</a></iframe><p>Let’s start with the solutions:</p><h3><strong>Unsafe Approach:</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa8cc354a33ae5f85f89eb60957d4bce/href">https://medium.com/media/fa8cc354a33ae5f85f89eb60957d4bce/href</a></iframe><p>This can result in a NullPointerException!</p><p>Result:</p><pre> Failed to greet for r1 !<br> Failed to greet for r2 !<br> Failed to greet for r3 !<br> Greeting for r4 : Hello null<br> Greeting for r5 : Hello Lalit</pre><h3><strong>Naive Safe Approach:</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0943c86b066aee294a9787f8a99f89b6/href">https://medium.com/media/0943c86b066aee294a9787f8a99f89b6/href</a></iframe><p>Result:</p><pre> Greeting for r1 : Hey There!<br> Greeting for r2 : Hey There!<br> Greeting for r3 : Hey There!<br> Greeting for r4 : Hey There!<br> Greeting for r5 : Hello Lalit</pre><p>Look at the indentation increasing with the increasing nesting level. Although, we could combine the null checks, but if we wanted to do other operation on each nested object then we would have to adopt the above approach.</p><h3><strong>The Optional Approach</strong></h3><p>Now, we will jump into the functional territory to solve this problem. Java 8’s Optional&lt;T&gt; comes to our rescue:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3645121db472a898d9b0fc6fcde3148c/href">https://medium.com/media/3645121db472a898d9b0fc6fcde3148c/href</a></iframe><p>This works like a charm! The only problem is we cannot control and determine which stage failed or is null, and cannot have failover for each stage.</p><p>To implement that in Optional, we have to do like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3592975df41d097e21503029c5d87bfa/href">https://medium.com/media/3592975df41d097e21503029c5d87bfa/href</a></iframe><p>Result:</p><pre>Greeting for r1 : Hello first level is null<br>Greeting for r2 : Hello second level null<br>Greeting for r3 : Hello third level is null<br>Greeting for r4 : Hey There!<br>Greeting for r5 : Hello Lalit</pre><p>This will work, but we had to write a lot of orElse statements to handle a particular null case and we have to be careful as to where we want to put them.</p><p>What if there was a way to pass a default at each map stage…?</p><h3><strong>Custom Wrapper Approach</strong></h3><p>Let’s write our own NullableWrapper with mapper taking a default value at each map stage (We’ll consider null otherwise). Let’s call our object as NullableWrapper&lt;T&gt; which:</p><ol><li>Takes a <strong>supplier</strong> returning a value of type T (denoting lazy computation) or a value of type T.</li><li><strong>May take</strong> a <strong>default value</strong> of type T (if the supplier or value results in null) which is by default set to null.</li><li>Can <strong>map</strong> the current wrapper&lt;T&gt; to new wrapper&lt;R&gt; by providing a transforming function Function&lt;T,R&gt; and <strong>may take</strong> a <strong>new default value</strong> of type R which is otherwise set to null.</li><li>Can <strong>get</strong> the value by providing a <strong>new default value</strong> of type T which is otherwise set to null.</li></ol><p>Now we have all the ingredients for the recipe, let’s start cooking!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0efa4e40782b819c32d0d814193ff9c2/href">https://medium.com/media/0efa4e40782b819c32d0d814193ff9c2/href</a></iframe><p>Now, let’s taste it! I mean test it with the <strong>mapWithDefault</strong> feature (the other map will work exactly like Optional).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/929f6ea0370c34248fff5679cb1b50b5/href">https://medium.com/media/929f6ea0370c34248fff5679cb1b50b5/href</a></iframe><p>This will also work just like we expected, and the code looks more elegant.</p><p>One more benefit of this is that <strong>we don’t have to use the getOrElse every time as NullableWrapper’s .get() will return a default value or null</strong> (in case default not provided) instead of blowing up:</p><pre>// will blow up!<br>Optional.ofNullable(null).map(x -&gt; &quot;Hello &quot; + x).get();</pre><pre>// will return null<br>new NullableWrapper(() -&gt; null).map(x -&gt; &quot;Hello &quot; + x).get();</pre><p>Now, let’s see the NullableWrapper&lt;&gt;() from the point of view of a library user. It asks for a supplier or a value of type T and a default value.</p><p>This makes me think that the <strong>supplier evaluation must also be safe</strong> as it would be lazily evaluated inside the wrapper! Let’s try it out:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1ac08f5541d193eab537249b06cfe59/href">https://medium.com/media/b1ac08f5541d193eab537249b06cfe59/href</a></iframe><p>Result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C1-mmvhI1fxpP7wUjyu82w.png" /></figure><p>Uh oh! What happened there?</p><p>This means the .get() operation is not that safe as we thought! How to make it safe? Let’s try to fix this by gulping all the NPEs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d182f9cf1bf3143e06c5bba7efb2c2ad/href">https://medium.com/media/d182f9cf1bf3143e06c5bba7efb2c2ad/href</a></iframe><p>Now, we also want to do mapping by ignoring NPEs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28801b992492d1abf2cc33cce7a73244/href">https://medium.com/media/28801b992492d1abf2cc33cce7a73244/href</a></iframe><p>Let’s try that again:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c4b7e2ce3444c3f8a7cf7edaff600ea/href">https://medium.com/media/9c4b7e2ce3444c3f8a7cf7edaff600ea/href</a></iframe><p>Result:</p><pre>Hey There!<br>Hi Lalit</pre><h3><strong>Conclusion</strong></h3><p>We saw how we can utilise the power of Optionals and functional programming concepts to get the safety of the null checks as well as the control over the outcome with a default value.</p><p>Our own NullableWrapper gives following advantages over builtin Optional:</p><ul><li>Mapping with a default value at any stage.</li><li>Defining initial default value.</li><li>Super safe lazy computation by ignoring NPEs.</li><li>Safer than NullableWrapper’s get() is more safer than Optional’s get() as it will return null or a default value as the final result and won’t just blow up.</li></ul><p>Hope you liked it. Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7079b9413ec9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/handling-nulls-in-nested-objects-java-7079b9413ec9">Handling Nulls in nested objects (Java)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Circuit Breaker pattern in Scala]]></title>
            <link>https://lprakashv.medium.com/circuit-breaker-pattern-in-scala-185994512c07?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/185994512c07</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[resilience]]></category>
            <category><![CDATA[circuit-breaker]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Sat, 28 Mar 2020 20:30:28 GMT</pubDate>
            <atom:updated>2020-06-19T19:21:42.614Z</atom:updated>
            <content:encoded><![CDATA[<h3>Implementing “Circuit Breaker” in Scala</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2g4mfcArhRwJ1puTY5PeGQ.jpeg" /></figure><p><a href="https://martinfowler.com/bliki/CircuitBreaker.html"><strong>Circuit breaker pattern</strong></a> is a common microservice resiliency pattern to make system responsive after series of failures and have a fallback mechanism.</p><p>[NOTE] Spoiler — This would be a simple implementation and the Scala code would be stateful and have side-effects.</p><h3>The Need</h3><p>Doing a remote call or executing a task which is outside the domain boundary of the system is very common in modern applications. Especially in microservice world, we are bound to make calls to other microservices.</p><p>In such scenarios, we need to consider eventual failures. If some failures are too continuous and consistent in nature, they can hog too much of the system resources resulting into failures. We can avoid such cases using the circuit-breaker pattern.</p><h3>The Concept</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*5We6Tx9nEw3cSkxRWYwMWQ.png" /><figcaption>courtesy: <a href="https://martinfowler.com/bliki/CircuitBreaker.html">https://martinfowler.com/bliki/CircuitBreaker.html</a></figcaption></figure><p>The concept is simple, the circuit represents the connection between the caller and the callee. It can only have 3 states: <strong>closed</strong>, <strong>open</strong> and <strong>half-open</strong>. The system starts with the “closed” circuit initially.</p><p>We monitor the consecutive (or within a time window) failures from the callee and when the failures exceed a threshold we trip the circuit to “open” and all the further calls from the caller won’t reach the callee and rather caller is returned with a default fallback result.</p><blockquote>— can be implemented using a <strong>failure result counter.</strong></blockquote><p>We also keep monitoring the time since opening the circuit and when a certain timeout is reached, we put the circuit to “half-open” meaning, only one ‘probe’ call will pass through and its result will determine if the circuit will be closed or opened based on its success or failure.</p><blockquote>— can be implemented using a <strong>background timer.</strong></blockquote><h3>Implementation</h3><p><strong>CircuitState</strong> — a trait extended by possible states</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5c63361d787ac4a29f98b5a2ccad269/href">https://medium.com/media/d5c63361d787ac4a29f98b5a2ccad269/href</a></iframe><p><strong>CircuitResult[T]</strong> — a trait extended by Success and Failure results of the Circuit with success return type T.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3b6e2c845aa2ab5fc94b0b1fb9de169/href">https://medium.com/media/c3b6e2c845aa2ab5fc94b0b1fb9de169/href</a></iframe><p><strong>Circuit[R]</strong> — a class denoting a circuit where the call returns the result of type Future[R], future denoting an async call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32f890f374984c2a0c146f8568de52e3/href">https://medium.com/media/32f890f374984c2a0c146f8568de52e3/href</a></iframe><p>States of the Circuit</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca594f0ab991c50645ac1d9c17fe0c34/href">https://medium.com/media/ca594f0ab991c50645ac1d9c17fe0c34/href</a></iframe><p>Transition of states</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20ca210eed872c73ce4d1c6a471dd6bb/href">https://medium.com/media/20ca210eed872c73ce4d1c6a471dd6bb/href</a></iframe><p><strong>Main block execution handler</strong> — It acts as a router and invokes other handlers based on the Circuit’s state. After state transition, the thread request may come back here to be routed again based on the updated state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11b57bf70a32a6c9a4a15ebbde015f97/href">https://medium.com/media/11b57bf70a32a6c9a4a15ebbde015f97/href</a></iframe><p><strong>Handling failure response from the Callee</strong> —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/468a4bad10f7aebdca2491c3e7363c2c/href">https://medium.com/media/468a4bad10f7aebdca2491c3e7363c2c/href</a></iframe><p><strong>Handling closed circuit </strong>— Executing the provided block of code and yield a success or failure as the result asynchronously, while maintaining/updating the count of consecutive failures.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5aa7b10c8e500db14cfeb406231bfa82/href">https://medium.com/media/5aa7b10c8e500db14cfeb406231bfa82/href</a></iframe><p><strong>Handling Half-Open Circuit</strong> —</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33a0a850af82e8a86afd331240347e07/href">https://medium.com/media/33a0a850af82e8a86afd331240347e07/href</a></iframe><p><strong>Handling open circuit </strong>— return the successful future with CircuitSuccess wrapping the default value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab1b3d432cbd701f3fc30f07e4842bcd/href">https://medium.com/media/ab1b3d432cbd701f3fc30f07e4842bcd/href</a></iframe><p><strong>Half-Opener Background Timer</strong> — Will trip the circuit to half-open after timeout reached since opening the circuit.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caf7bbee840345b34cb3b3da3660127d/href">https://medium.com/media/caf7bbee840345b34cb3b3da3660127d/href</a></iframe><p><strong>CircuitImplicits</strong> — Implicits making the use of our Circuit class a breeze!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85859e5117e11494ad461ed7f80e2f20/href">https://medium.com/media/85859e5117e11494ad461ed7f80e2f20/href</a></iframe><p>We can now do something like:</p><pre>implicit val sampleCircuit: Circuit[Int] = new Circuit[Int](<br>    &quot;sample-circuit&quot;, <br>    5,                // max allowed consecutive closed failures<br>    5.seconds,        // half-opener timeout<br>    1,                // max allowed consecutive half-open failures<br>    -1,               // invalid success result<br>    <em>println<br></em>)</pre><pre>//this will use the above created circuit to execute<br>Future.successful(2 + 2).executeAsync</pre><p><strong>Testing it out</strong> — You can find the test cases <a href="https://github.com/lprakashv/scala-utils/blob/master/src/test/scala/com/lprakashv/resiliency/CircuitTest.scala">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HsozSONY9KxDhEnkaqUuPg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=185994512c07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to keep your sanity working with Scala Implicit!]]></title>
            <link>https://lprakashv.medium.com/how-to-keep-your-sanity-working-with-scala-implicit-fda9ffc33162?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/fda9ffc33162</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[implicit]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Thu, 17 Oct 2019 08:14:10 GMT</pubDate>
            <atom:updated>2019-10-17T10:25:16.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tPL7VedpJjKplVYMRwKfew.jpeg" /><figcaption>Courtesy: <a href="https://www.pexels.com/photo/calm-daylight-evening-grass-267967/">https://www.pexels.com/</a></figcaption></figure><p>Scala Implicits provide a lot of power and flexibility to express the code in such beautiful ways that development sometimes feel like and art and are nothing short of a “<a href="https://en.wikipedia.org/wiki/Brahmastra">Brahmastra</a>” in the hand of a library developer.</p><p>And, as Spider-man’s uncle said,</p><blockquote>“With great power, comes greater responsibility!”</blockquote><p>The abuse of such a powerful feature has made working with them a nightmare sometimes for the end users. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*HYDv-I8MSPBbLkGwAkBqYQ.gif" /></figure><p>You will keep you head scratching over the above script until you find an implicit conversion (from Int to String) and an extension method (toHindiDigits) coming from just two imports!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*7AZLhsP3xZE252fjvxyM3w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rbdkg7aqrP-noPBIp9d1tA.png" /></figure><p>Even <a href="https://en.wikipedia.org/wiki/Martin_Odersky">Martin Odersky</a> ( 🙌), the creator of Scala has admitted that he has made quite a few mistakes with the Implicits while designing the Scala language 😱!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1h8xNBykZqM%3Fstart%3D1679%26feature%3Doembed%26start%3D1679&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1h8xNBykZqM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1h8xNBykZqM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/21832d450d45607a202fdf5b0c9fc12b/href">https://medium.com/media/21832d450d45607a202fdf5b0c9fc12b/href</a></iframe><p>While this can be really disheartening, this is not enough of a reason to move away from an amazing programming language, just because of a really abused feature! After all, there are other languages with much more horrible warts!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*8fbaronc6zfM76zm_c7YDQ.png" /><figcaption>Courtesy: https://me.me</figcaption></figure><p>And JS is the most popular language right now 😉</p><p>So, coming onto our topic, keeping the sanity of our mind! For that, we can follow some tips:</p><p><strong><em>Use an IDE: </em></strong>Personal choice, IntelliJ IDEA with Scala plugin.</p><p>Look at features from <a href="https://blog.jetbrains.com/scala/">JetBrain’s Scala plugin blog</a>:</p><p>You can show implicitly passed parameters in a function, using “show implicit hints” feature, so that there’ll be <strong><em>no surprises</em></strong> as what was passed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gYLO87ttPSJ6Oi7Ocf0jgg.gif" /></figure><p>Similar example,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QY0WMvdN_PaRJ2wer6NhuQ.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1RyRlZDmvpU4N28fsbYRvQ.gif" /></figure><p><strong><em>Avoid implicit conversions/defs: </em></strong>This can become a complete nightmare if a String or any type gets converted to another on the fly without even telling us and we spend hours debugging the issue.</p><p>Even Scala compiler will warn you if you use implicit conversion without a specific import ( <strong>scala.language.implicitConversions</strong> ):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QMWfJOv4CgUUe47yCS6PrQ.png" /></figure><p>So, the tip is, <strong><em>avoid implicit conversions at all costs</em></strong>. When you absolutely need a conversion in your code in a convenient manner, please use <a href="https://medium.com/@lprakashv/making-ordinary-classes-rich-scala-ab7f991d690">implicit classes with extension method</a> like:</p><pre>trait A</pre><pre>trait B</pre><pre>//avoid this:<br>//implicit def aToB(a: A): B = ???</pre><pre>//do this instead:<br>implicit class RichA(a: A) {</pre><pre>  def toB = ???<br>}</pre><pre>//use it like this:<br>val someA: A = ???<br>val someB: SomeB = someA.toB</pre><pre>//so that, this will be avoided:<br>//val someB: SomeB = someA</pre><p><strong><em>Ditch the type inference and annotate the required types.</em></strong></p><p>Simple logic: Avoid surprises 😅 and let compilation fail on mismatches!</p><p><strong><em>Check the imports!</em></strong></p><p>Any underscore imports may contain implicit conversions, parameters and/or somewhat safe implicit classes.</p><p><strong><em>Conclusion:</em></strong></p><p>So, these were my highly opinionated rules to avoid surprises and headaches while working with Scala Implicit! They are really powerful tools for writing really awesome code, please use them, don’t abuse them :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fda9ffc33162" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making ordinary classes Rich! (Scala)]]></title>
            <link>https://lprakashv.medium.com/making-ordinary-classes-rich-scala-ab7f991d690?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab7f991d690</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[oop]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Mon, 07 Oct 2019 17:24:21 GMT</pubDate>
            <atom:updated>2019-10-07T17:24:21.805Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5KwlqHv87gtrlFErPArGA.jpeg" /></figure><p>Let me break it to you, this post has nothing to do with charity! Now, let’s start with a question:</p><blockquote><strong>How do we enrich something?</strong></blockquote><p>By making it abundant, rich in features and resourceful!</p><p>In simple OOP terms, we add additional feature methods to the class!!</p><p>Let’s try to enrich a Person class, starting with the basic class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f6d0e9fa30ce004722672ad766095af/href">https://medium.com/media/0f6d0e9fa30ce004722672ad766095af/href</a></iframe><p>Now, to make a person speak French, we have to <strong>add another method</strong> to our Person class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/858035ffbe7582641267b053f23a86a8/href">https://medium.com/media/858035ffbe7582641267b053f23a86a8/href</a></iframe><p>This is perfectly fine! Just add the additional feature to the existing class and make it rich.</p><p>But… <strong><em>there are cases when we cannot add features/methods to classes, like, library classes </em></strong>(java.util.Date, String, etc).</p><p>To add features to such classes we have to make<strong><em> “wrapper classes”</em></strong>. Suppose we want to add the “spell the digits” feature in the standard libraries Int class, we would have to make a wrapper class over Int.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45df22dc2ae1a9b4daf5a606a505ccc1/href">https://medium.com/media/45df22dc2ae1a9b4daf5a606a505ccc1/href</a></iframe><p>Problem with this wrapper class approach is, if the desired class’ objects are used in a lot of places, then <strong><em>refactoring the class objects to our Rich-Wrapper class objects becomes a real pain!</em></strong></p><blockquote><strong>Solution: Implicit classes and extension methods</strong></blockquote><p>We will now make slight changes to our wrapper class:</p><ol><li>Add an <strong><em>implicit</em></strong> keyword in front of the class.</li><li>Put the implicit class in a container object, which has a reason but also is generally a good practice. This way, we can import the “rich” feature at will!</li><li>Import contents of the container object.</li><li>Use the method on simple objects directly!</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/112badd313d36b5855c7d3e6e7bbb357/href">https://medium.com/media/112badd313d36b5855c7d3e6e7bbb357/href</a></iframe><p>This pattern of creating an implicit class to enhance a library’s features has a funny name</p><blockquote>“Pimp My Library Pattern”</blockquote><p>The example we shown above was a very basic example of Implicit class and its extension methods. The application of Implicit classes reach far beyond this!</p><p>One such example, is creating a generic method time profiler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e9b2c41f071571fe96ce2e02d8c5531/href">https://medium.com/media/0e9b2c41f071571fe96ce2e02d8c5531/href</a></iframe><p>Then, what are your waiting for? Since, now you’ve learned to “pimp” any library, go make the mundane library objects rich with features you like!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F4483207&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fthemoreyouknow-more-know-gif-4483207&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F3407d1f3b2eed279d759caeef945016d%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/316b1f33abea3e1d3dcc88d3064c3dbf/href">https://medium.com/media/316b1f33abea3e1d3dcc88d3064c3dbf/href</a></iframe><p><em>fin.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab7f991d690" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cushing Design Patterns! — Unnecessary Patterns.]]></title>
            <link>https://lprakashv.medium.com/cushing-design-patterns-unnecessary-patterns-dda76bfdca07?source=rss-841731c92c5e------2</link>
            <guid isPermaLink="false">https://medium.com/p/dda76bfdca07</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Lalit Vatsal]]></dc:creator>
            <pubDate>Mon, 30 Sep 2019 05:16:43 GMT</pubDate>
            <atom:updated>2019-10-01T06:47:39.491Z</atom:updated>
            <content:encoded><![CDATA[<h3>Cushing Design Patterns! — Unnecessary Patterns.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gmr0_w1jq2VzZp0Ba7Er6Q.jpeg" /></figure><p>This is the part-II in the continuation of the “Crushing Design Patterns” series (<a href="https://medium.com/@lprakashv/crushing-design-patterns-with-scala-chain-of-responsibility-7d705ebe12b5">Part-I</a>) where we debunk the idea of having to use the design patterns to design the system.</p><p>They are either just so natural that we have been using them unconsciously or there are better ways in which we don’t have to focus on ceremoniously writing them.</p><p>Here we go.</p><p><strong>1. Builder Pattern:</strong></p><blockquote>The intent of the Builder design pattern is to separate the construction of a complex object from its representation.</blockquote><blockquote>— Wikipedia</blockquote><p>Meaning in plain english: <em>A way to create objects with a flexibility to use any number of parameters for its creation.</em></p><p>Question: what is wrong with the constructors?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac7428f593e6310be945ef4a79a26d27/href">https://medium.com/media/ac7428f593e6310be945ef4a79a26d27/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F10244582&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fbird-blocks-gif-10244582&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F873bd28db47bcd7ce2bd0080732d55cd%2Ftenor.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/883d6ab42622f76d1b8c28c4908f44e1/href">https://medium.com/media/883d6ab42622f76d1b8c28c4908f44e1/href</a></iframe><p>Counter question: How many would you make? To answer that, I’ll have show off my high school mathematics:</p><blockquote>If there are <strong><em>n</em></strong> optional parameters/fields, there will be <strong><em>2^n</em></strong> possible combinations of them, that means we’ll have to create those many constructors.</blockquote><p>Turns out it will be 64 for our 6 optional veggies. Oh God, Java will make me die unhealthy!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F4651500&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fcookie-monster-vegetables-veggies-diet-pissed-gif-4651500&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F9212acaa071f227079ad4cd3f3c04c8d%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/a505da879ad37a2ad18714eef078c3f3/href">https://medium.com/media/a505da879ad37a2ad18714eef078c3f3/href</a></iframe><p>Here comes the Builder pattern for our rescue,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/feba2b2d758557d0e236637c0a245c3b/href">https://medium.com/media/feba2b2d758557d0e236637c0a245c3b/href</a></iframe><p>But hey, this is 2019 and remember, we don’t need to do it like this! Behold, Java has a saviour now, <a href="https://projectlombok.org/features/all"><strong><em>lombok</em></strong></a><strong><em>! </em></strong>Let’s rewrite our lombok-y Sandwich class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bdebeca82ddb966fbb358e2ef3c1082/href">https://medium.com/media/3bdebeca82ddb966fbb358e2ef3c1082/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F5183369&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fjake-adventure-time-eyes-sparkle-sparkling-gif-5183369&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2Fc888671f00fede4fd5e0d113b428c087%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd6b837a061dc35e876b6e0c441d04a2/href">https://medium.com/media/cd6b837a061dc35e876b6e0c441d04a2/href</a></iframe><p>That is it! Heck, we didn’t even need write the getters and setters ourselves, lombok does it all for you ! How do we use it, if you ask. Simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fabe2aa11e9e7f8b67287549ac0106c6/href">https://medium.com/media/fabe2aa11e9e7f8b67287549ac0106c6/href</a></iframe><blockquote><strong>Pro Tip:</strong> If you are a Java dev, you should definitely check out the <a href="https://projectlombok.org/features/all"><strong>Project Lombok</strong></a>, it offer dozens of cool modern features with simple annotations. <strong>All the lombok annotated code simply de-sugars at the compile time into the ugly Java we don’t wanna write!</strong></blockquote><p><strong><em>Verdict: Builder Pattern is unnecessary in Java due to lombok! Some languages provide features like named and default arguments (Scala, Clojure, Python etc) making the object building easier.</em></strong></p><p><strong>2. Iterator Pattern:</strong></p><blockquote>The <strong>iterator pattern</strong> is a design pattern in which an iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers.</blockquote><blockquote>— Wikipedia</blockquote><p>Meaning:<em> It’s a pattern where </em><strong><em>an abstract container</em></strong><em> providing a </em><strong><em>next()</em></strong><em> method and in some cases hasNext() method as well.</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F5047266&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fnext-britney-spears-next-one-up-next-gif-5047266&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F53753ecabeb39f9168edc197eed0ef0a%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6ed52d8edcc08f68832d615e2b2bf47/href">https://medium.com/media/f6ed52d8edcc08f68832d615e2b2bf47/href</a></iframe><p>At this point, if you are a programmer, your brain should be screaming right now. <strong>We already have those in every mainstream programming language!!</strong>.</p><ul><li><strong>Java</strong> has <strong>Iterator&lt;T&gt;</strong> interface.</li><li><strong>Python</strong> objects provides <strong>__next__()</strong> method.</li><li><strong>Scala and other functional languages</strong> provide <strong>sequences </strong>and <strong>lazy sequences</strong>.</li></ul><p><strong>3. Command Pattern:</strong></p><blockquote>Here, object is used to encapsulate all information needed to perform an action or trigger an event at a later time.</blockquote><blockquote>— Wikipedia</blockquote><p>Meaning: Every command implementation does what it is meant for!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F7295202&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fyour-wish-is-my-command-geenie-kiss-gif-7295202&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F0c85d3016045408310c223dfe02dbdd1%2Ftenor.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd2ceab28c0928d18b430b0d95f4269e/href">https://medium.com/media/cd2ceab28c0928d18b430b0d95f4269e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8c43c02ad531bca2a2e2b1707be3d8b/href">https://medium.com/media/b8c43c02ad531bca2a2e2b1707be3d8b/href</a></iframe><p>Usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50a072f60ba5b535e45815d1d1d289f6/href">https://medium.com/media/50a072f60ba5b535e45815d1d1d289f6/href</a></iframe><p>You got the point right! Now, I want to you to pay attention and see that “<em>we only care about the encapsulated execute() method”</em>. So, in a nutshell, the <strong><em>Command pattern is just a function/method!</em></strong></p><p>This becomes very intuitive with Java-8 lambdas, we don’t need to create LoginCommand and LogoutCommand wrapper implementations of the Command interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6c8e07a608637d16bea6f6012404a21/href">https://medium.com/media/b6c8e07a608637d16bea6f6012404a21/href</a></iframe><p>BTW, this is very easy in functional languages, let’s take Scala for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/139e9d72febd446a48d0b13f18509785/href">https://medium.com/media/139e9d72febd446a48d0b13f18509785/href</a></iframe><p><strong>4. Strategy Pattern:</strong></p><blockquote>S<strong>trategy pattern</strong> (also known as the <strong>policy pattern</strong>) enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.</blockquote><blockquote>— Wikipedia</blockquote><p>Meaning: <strong><em>Injecting the algorithm/strategy for some computation at runtime.</em></strong></p><p>For example, suppose we are running a country named LaLaLand and are running out of cash. What would be the best way to fill up our stash?</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F4998183&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Ftaxes-tax-money-robin-hood-prince-john-gif-4998183&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F6c229f3bb3b819f589e43fe2207d5cd8%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/b108ce7190676a27883e9f016e5c92ef/href">https://medium.com/media/b108ce7190676a27883e9f016e5c92ef/href</a></iframe><p>We start making a taxing strategy, let’s tax a quarter of the income of everybody.</p><pre># quarter-taxing-strategy:</pre><pre>tax = user.income / 4</pre><p>But , when we think about the good people running a kitten orphanage NGO, we feel bad for them. Also, we want to tax more the black money holders now. <strong>We need more strategies</strong>!</p><pre># non-profit-taxing-strategy:</pre><pre>tax = 0</pre><pre># half-taxing-strategy:</pre><pre>tax = user.income / 2</pre><p>Here, <strong><em>the algorithm is the taxing strategy</em></strong> to compute the taxes from users. More strategies mean, more algorithms (functions). Let’s start with good old Java way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4de16d11d79a7a6ec935d780d07937e/href">https://medium.com/media/e4de16d11d79a7a6ec935d780d07937e/href</a></iframe><p>For every new strategy, we need to create another implementation class! Usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3feaf3c050ac58eb3f655d0882ce0d8/href">https://medium.com/media/c3feaf3c050ac58eb3f655d0882ce0d8/href</a></iframe><p>Smart ones among you might already have guessed that <strong><em>we don’t need the wrapper implementation classes, we just need functions as strategies and pass them around arguments</em></strong>!</p><p>Let’s do it in functional way (no need to create multiple implementations of Strategy interface):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/000e7f6488731de4e93dbab1c9f4e25c/href">https://medium.com/media/000e7f6488731de4e93dbab1c9f4e25c/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F5495543&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fcool-coolbeans-gif-5495543&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F3268c83a854ad44b3afc20e51de64df5%2Fraw&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/59cf09fc5acaefb74f89f54e6fa3b6a1/href">https://medium.com/media/59cf09fc5acaefb74f89f54e6fa3b6a1/href</a></iframe><p>In functional languages (Scala):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1748777934cdeade679aff9e840cd40f/href">https://medium.com/media/1748777934cdeade679aff9e840cd40f/href</a></iframe><p><strong>Conclusion</strong></p><p>We have discussed 4 pattern in this post: Builder, Iterator, Command and Strategy. Each one can be used without writing any pattern, simply by just intuitively using language features (even in Java). Each pattern has been right in front of us hiding in the language!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F14314567&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fright-in-front-of-us-in-front-open-your-eyes-gaming-archonaut-gif-14314567&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2Fe659eb5da47b0f7541a20a192238dfc8%2Ftenor.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c7380088a8d89cd3fcf06df4ac10a68/href">https://medium.com/media/2c7380088a8d89cd3fcf06df4ac10a68/href</a></iframe><p>In summary:</p><ol><li><em>Builder — Lombok’s Builder annotation in Java, named and default parameters in other languages (Scala, Clojure, Python etc).</em></li><li><em>Iterator — Readily available interfaces/abstractions in many languages.</em></li><li><em>Command — Just pure functions.</em></li><li><em>Strategy — passing functions as strategy at runtime.</em></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dda76bfdca07" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>