<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pavol Rajzak</title>
    <description>The latest articles on DEV Community by Pavol Rajzak (@rapasoft).</description>
    <link>https://dev.to/rapasoft</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F708%2F4bbcc6a5-bca7-4b06-af29-b604d55b1ae8.jpg</url>
      <title>DEV Community: Pavol Rajzak</title>
      <link>https://dev.to/rapasoft</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rapasoft"/>
    <language>en</language>
    <item>
      <title>Coding on iPad</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Wed, 25 Nov 2020 20:03:41 +0000</pubDate>
      <link>https://dev.to/rapasoft/coding-on-ipad-4f9o</link>
      <guid>https://dev.to/rapasoft/coding-on-ipad-4f9o</guid>
      <description>&lt;p&gt;Well, this is what I’ve been thinking about ever since I bought one. In general they are devices for consuming, not producing content. But since new Apple MacBooks are switching to ARM (same CPU architecture as iPads) we could see more development tools being also available for tablets.&lt;/p&gt;

&lt;p&gt;I have seen some people using it for Python and also some applications for learning programming, but would like to know whether it could be used also for JavaScript or even JVM stuff :). &lt;/p&gt;

&lt;p&gt;Does anyone have any experience? Would like to read about it in comments. &lt;/p&gt;

</description>
      <category>ipad</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to hack unsecure web application</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Wed, 18 Nov 2020 20:43:38 +0000</pubDate>
      <link>https://dev.to/rapasoft/how-to-hack-unsecure-web-application-371c</link>
      <guid>https://dev.to/rapasoft/how-to-hack-unsecure-web-application-371c</guid>
      <description>&lt;p&gt;In this article I would like to demonstrate how easy it is to get confidential information from a web application that... well, doesn't pay too much attention to security. What I will be working with is a sample Java application that I created in Spring Boot for demonstration purposes. But the attacks performed are valid for any other programming language or framework. Some vulnerabilities and security issues that will be demonstrated include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SQL &lt;a href="https://owasp.org/www-project-top-ten/2017/A1_2017-Injection" rel="noopener noreferrer"&gt;Injection&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure" rel="noopener noreferrer"&gt;Sensitive data exposure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/www-project-top-ten/2017/A10_2017-Insufficient_Logging%2526Monitoring" rel="noopener noreferrer"&gt;Insufficient logging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://owasp.org/www-project-top-ten/2017/A2_2017-Broken_Authentication" rel="noopener noreferrer"&gt;Broken authentication&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;We will be working with a fictional web application that allows you to login as user which is a part of some group. Based on user role, he can view either basic or confidential data. In this case, you will see a list of employees in a company, their status, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv6p9aqbg26mfh9461frv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv6p9aqbg26mfh9461frv.png" alt="Page overview"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can find the sources to this application in my Github: &lt;a href="https://github.com/rapasoft/hackme" rel="noopener noreferrer"&gt;https://github.com/rapasoft/hackme&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Attack #1: Man in the middle attack
&lt;/h2&gt;

&lt;p&gt;Imagine you are sitting in a restaurant and would like to check something in the application above. You connect to restaurant's Wi-Fi, open the login page, enter credentials and obtain information you want (e.g. the list of specific employees). What you might not now, is that your credentials have been compromised. Let's examine the login page closely:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ffzpbjxstpjlthhsoym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4ffzpbjxstpjlthhsoym.png" alt="Login page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This application does not use SSL/TLS (e.g. no &lt;code&gt;https://&lt;/code&gt; connection). This means, that any information sent can be viewed as plain text by anyone with access to communication between the client and server.&lt;/p&gt;

&lt;p&gt;This is exactly what is happening in so called &lt;em&gt;Man in the middle attack&lt;/em&gt;. More specifically, in this case &lt;a href="https://en.wikipedia.org/wiki/ARP_spoofing" rel="noopener noreferrer"&gt;ARP spoofing&lt;/a&gt; has been used as a way to poison whole network so that attacker is aware of anything that goes on and can sniff on all packets. I am not going to go into details on how ARP spoofing works, but will demonstrate it on this video:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=YD-Glf1jDt8" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimg.youtube.com%2Fvi%2FYD-Glf1jDt8%2F0.jpg" alt="Youtube video of MITM attack"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You might need to open this Youtube video in a new window and full-screen to see more details&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What you can see above is that I have started &lt;a href="https://www.ettercap-project.org/" rel="noopener noreferrer"&gt;Ettercap&lt;/a&gt;, which is a comprehensive suite for MITM attacks, and performed ARP spoofing in my local network. In the meantime, when user (me via iPad) enters the credentials, they are visible both in Ettercap and in &lt;a href="https://www.wireshark.org/" rel="noopener noreferrer"&gt;Wireshark&lt;/a&gt; (tool for network traffic analysis).&lt;/p&gt;

&lt;h2&gt;
  
  
  Attack #2: SQL Injection
&lt;/h2&gt;

&lt;p&gt;Continuing our role as attacker of this application, we now have the credentials for user &lt;code&gt;hgraves&lt;/code&gt; with password &lt;code&gt;P4SSW0RD&lt;/code&gt;. You can see that not only this application does not use TLS, it has a very weak password policy enabling users chose really awful passwords.&lt;/p&gt;

&lt;p&gt;We log in using the credentials above and can see one input field with option to search for list of people. When pressing &lt;code&gt;Search&lt;/code&gt; we will get the full list of people. If we put "Fra" for instance we will get filtered results based on search. What if we put there some illegal character? It is obvious, that there's some database behind this application, so let's try the &lt;code&gt;'&lt;/code&gt; character:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fas1k7yv7edmnqwythyvl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fas1k7yv7edmnqwythyvl.jpg" alt="SQL Injection - logging"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only did this fail, but we can see that &lt;code&gt;POST&lt;/code&gt; request has failed with very specific error message that expose the structure of whole SQL statement. We can see that &lt;code&gt;WHERE P.NAME LIKE '%'%'&lt;/code&gt; is used at the end. &lt;/p&gt;

&lt;p&gt;Skilled hacker does not need more. Adjusting the search string can after few attempts yield these results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhtzj7dqh2aynb6n9fl39.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhtzj7dqh2aynb6n9fl39.png" alt="SQL Injection - results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The injection in this case created union to select statement that fetches usernames and passwords together with their role. Not only this was successful, we've managed to display it in the original client application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have now credentials of all users, and their roles (displayed by the integer in the "Age" column). If &lt;code&gt;hframe&lt;/code&gt; has role 1, &lt;code&gt;rgraves&lt;/code&gt; must therefore be more important user with role 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attack #3: Attacking weakly hashed passwords
&lt;/h2&gt;

&lt;p&gt;We have learned in the first attack, that there probably isn't any password policy set for this application. This would give us hope that &lt;code&gt;rgraves&lt;/code&gt; does not pay too much attention on security either.&lt;/p&gt;

&lt;p&gt;The easiest way to crack hash of password that uses insufficient hashing algorithm (like MD5 in this case) is to perform &lt;a href="https://en.wikipedia.org/wiki/Dictionary_attack" rel="noopener noreferrer"&gt;dictionary&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Rainbow_table" rel="noopener noreferrer"&gt;rainbow table&lt;/a&gt; attack. There are many possibilities, but the easiest one is to use &lt;a href="https://crackstation.net/" rel="noopener noreferrer"&gt;internet services&lt;/a&gt; that already have large database of password-to-hash combinations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F850ff4so8rd1mkxuw63i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F850ff4so8rd1mkxuw63i.png" alt="Cracked hashes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First one is &lt;code&gt;hframe&lt;/code&gt;, second one is &lt;code&gt;rgraves&lt;/code&gt;. &lt;code&gt;smorgan&lt;/code&gt; seems to be using stronger passwords than others, since it is not in the dictionary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can see that password for &lt;code&gt;rgraves&lt;/code&gt; is &lt;a href="https://www.imdb.com/title/tt0244244/" rel="noopener noreferrer"&gt;swordfish&lt;/a&gt;, which means that he doesn't now how important it is to choose non-dictionary password ;). Now, nothing stops us from logging in as this user and discovering the "admin" section and confidential information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jngfkx6hw0pki7j8d01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jngfkx6hw0pki7j8d01.png" alt="Admin section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=FBWr1KtnRcI" rel="noopener noreferrer"&gt;Do not read this if you're German :)&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to fix this?
&lt;/h2&gt;

&lt;p&gt;You might say that all of this could be easily prevented. You might say that these were &lt;em&gt;rookie mistakes&lt;/em&gt;. Well, this might be true, but what's also true is that &lt;a href="https://owasp.org/" rel="noopener noreferrer"&gt;The Open Web Application Security Project&lt;/a&gt; lists &lt;em&gt;Injection&lt;/em&gt; as #1 vulnerability in web applications (and it has been like this for several years).&lt;/p&gt;

&lt;p&gt;So, if you found yourself wondering how to prevent all of this, let's recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Always use SSL/TLS when sending something to server&lt;/strong&gt;. It doesn't have to be credentials, it is currently considered as good practice in general. Even if you don't have any inputs on your page at all! The easiest way is to provide certificate via services like &lt;a href="https://letsencrypt.org/" rel="noopener noreferrer"&gt;Let's Encrypt&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Remember that self-signed certificates might mitigate the issue of data exposure, but do not prevent from MITM attacker to fake the whole site with his own self-signed certificate altogether.&lt;/li&gt;
&lt;li&gt;When dealing with SQL queries &lt;strong&gt;always escape parameters obtained from client&lt;/strong&gt;. The easiest way to do that is use capabilities of your framework, e.g. in Spring you can use &lt;a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html" rel="noopener noreferrer"&gt;NamedParameterJdbcTemplate&lt;/a&gt; which is good for readability as well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be paranoid.&lt;/strong&gt; Validate any user input. Again in Java/Spring world you can add validation rules using &lt;code&gt;JSR-380&lt;/code&gt; bean validation implementation like &lt;a href="https://hibernate.org/validator/" rel="noopener noreferrer"&gt;Hibernate validator&lt;/a&gt; and annotate parameters, e.g. &lt;code&gt;@Pattern(regexp = "[a-z|A-Z]*") String name&lt;/code&gt; would prevent entering &lt;code&gt;'&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Log any attempts to pass in invalid input in the application. Moreover, in case of error &lt;strong&gt;do not expose any implementation-specific information&lt;/strong&gt; to the client.&lt;/li&gt;
&lt;li&gt;Improve your authentication. 

&lt;ul&gt;
&lt;li&gt;If possible use at least &lt;a href="https://en.wikipedia.org/wiki/Multi-factor_authentication" rel="noopener noreferrer"&gt;2-factor authentication&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Do not implement custom password storage or encryption algorithms. It is extremely difficult to do on your own and you will definitely fail. There're proven solutions like &lt;a href="https://en.wikipedia.org/wiki/Bcrypt" rel="noopener noreferrer"&gt;BCrypt&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Read and bookmark &lt;a href="https://owasp.org/www-project-top-ten/" rel="noopener noreferrer"&gt;OWASP TOP 10&lt;/a&gt;. Start with there and then go through their list of best practices.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;I hope you found this article not only informative but entertaining as well. Looking forward to your comments!&lt;/p&gt;

</description>
      <category>security</category>
      <category>java</category>
    </item>
    <item>
      <title>Common Java myths and misconceptions</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Thu, 30 Jul 2020 12:14:48 +0000</pubDate>
      <link>https://dev.to/rapasoft/common-java-myths-and-misconceptions-3knk</link>
      <guid>https://dev.to/rapasoft/common-java-myths-and-misconceptions-3knk</guid>
      <description>&lt;p&gt;There're a lot of incorrect opinions among developers - some root in misunderstanding, lack of knowledge or bad experience from past. Java developers are usually up to date with the changes, but I often hear from non-Java developers and general IT world that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java is slow&lt;/li&gt;
&lt;li&gt;Java is buggy/not secure&lt;/li&gt;
&lt;li&gt;Java is not free, so you need to pay a lot to Oracle for licenses&lt;/li&gt;
&lt;li&gt;Java is verbose, outdated language, with API that is awkward to use&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I understand that without proper context, any information can be misinterpreted, so I will try to glue it together so that it makes sense :).&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Java slow?
&lt;/h2&gt;

&lt;p&gt;Short answer: &lt;strong&gt;No&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I remember back at my university days, I was trying to get into competetive programming. It is basically all about algorithms and data structures and you write code to solve some problem. The verification is done on a remote server which tests your solution against input dataset. Only language I knew back then was Java, and the server supported assesment of Java written solutions. The main source of loathing from the guys writing in C was that Java solutions took significantly more time to execute and that's why time limit had to be increased for Java.*&lt;/p&gt;

&lt;p&gt;Fast-forward to 2020, &lt;strong&gt;Java is one of the most popular languages in the world&lt;/strong&gt; and JVM-based solutions dominate the world of data-intensive computation. Apache Kafka, one of the leading platforms for high-speed stream processing is running on JVM. This fact itself, should disprove the statetment of Java being slow.&lt;/p&gt;

&lt;p&gt;When it comes to performance, however, rarely something beats a compiled code. But it is like comparing apples and oranges, since Java bytecode runs on JVM - which gives it one of its main advantages: portability. Once C code is compiled against specific platform, it can be run only on that platform. But Java compiled bytecode can be run on almost any platform.&lt;/p&gt;

&lt;p&gt;Other thing is that since its inception, JVM has evolved and there are also multiple implementations which are focusing on increasing the throughput and reduce latency, like &lt;a href="https://www.graalvm.org/"&gt;GraalVM&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;slowness&lt;/em&gt; usually comes from the experience with working with badly written, outdated applications (e.g. GUI based apps in Swing). Unfortunately, Java gives a lot of freedom to create applications and it might happen that while something works, it does not necessarily need to perform well. I think &lt;a href="https://stackoverflow.com/a/2163570/1471785"&gt;this StackOverflow answer&lt;/a&gt; summarizes it the best.&lt;/p&gt;

&lt;p&gt;*By the way the slowness was caused by JVM being started so that code can run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Java buggy or not secure?
&lt;/h2&gt;

&lt;p&gt;Short answer: &lt;strong&gt;No&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Again, there are many proven examples of Java-based solutions, with high focus on security: payment processing, digital signature, embedded devices (ATMs) and so on. Java has wide support for cryptography, authentication and authorization, SSL etc., so it gives you the tools to build secure applications. However, with any codebase bugs can happen, and JDK is not free of those. Luckily, there're frequent updates which fix the critical issues.&lt;/p&gt;

&lt;p&gt;The main source of "bugginess" as perceived by users, other than the application code itself, might come from the fact, that the Java runtime (JRE) is not &lt;strong&gt;up to date&lt;/strong&gt; on the machine. If the issue happens with application running on server, it is the fault of infrastructure maintainer for not keeping the JRE updated. And the same is with users. Do you remember these annoying pop-ups telling you that there's new version of Java? Do you remember also closing and ignoring it? Well, you just made your computer vulnerable.&lt;/p&gt;

&lt;p&gt;Maybe the issue is with additional component (JRE) that you need to maintain on your computer, but most of the Java applications nowadays are bundled with JRE and do not require users to have one installed. This reduces the issue one degree down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Java free?
&lt;/h2&gt;

&lt;p&gt;Short answer: &lt;strong&gt;Yes&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Where does the confusion come from? In 2019 &lt;a href="https://www.oracle.com/java/technologies/javase/jdk-faqs.html"&gt;Oracle announnced changes in licensing model&lt;/a&gt;, that require buying subscription if you are using Oracle Java in production. This means that either you have a server based apps that run Java or you have a desktop application that you sell with bundled JRE. Having Oracle Java SE subscription will give you access to extended support.&lt;/p&gt;

&lt;p&gt;That being said, if you don't need to have this premium support, you can still opt out and switch to &lt;a href="https://github.com/openjdk"&gt;OpenJDK&lt;/a&gt; or any other providers. There are many stable and open-source implementations of JDK, that most of the time will give you the same experience as with Oracle JDK. So the long answer is also &lt;strong&gt;yes&lt;/strong&gt;, since you have a choice to select different vendor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Java is verbose, "write-only" and obsolete language
&lt;/h2&gt;

&lt;p&gt;Short answer: &lt;strong&gt;No&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Java has a long history and it was missing some constructs in older versions, but as any language it keeps evolving and improving. Let's take this snippet of code written in Java 7:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;java7&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Luke Warm"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John New"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Elsa Snow"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mary Rose"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ADULT&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ADULT&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;adults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
                    &lt;span class="n"&gt;adults&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ADULT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;adults&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;containsKey&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHILD&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHILD&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
                    &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                    &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHILD&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is unnecessarily long and difficult to read. There've been many improvements since Java 7, so the code above can be now written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;java11&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Luke Warm"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John New"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Elsa Snow"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mary Rose"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;persons&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collectors&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;groupingBy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                        &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ADULT&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Maturity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHILD&lt;/span&gt;
                &lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;adultAndChildrenMap&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Current Java release cycle is &lt;strong&gt;6 months&lt;/strong&gt; and while version 11 is the LTS (long term support), current version is 14, which brings even more improvements either as preview or stable features. The problem with Java is that it rolls a big "ball of legacy", which needs to support (e.g. &lt;a href="https://stackoverflow.com/a/25784475/1471785"&gt;the existence of default methods in interfaces introduced in Java 8&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Since Java 8, the most notable Java update which was released in 2013, &lt;a href="https://advancedweb.hu/new-language-features-since-java-8-to-14/"&gt;there have been many updates&lt;/a&gt; which aim to reduce verbosity and increase readability, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improved factory methods for Collection API (&lt;code&gt;Map.of()&lt;/code&gt;, &lt;code&gt;Set.of()&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Stream API improvements (&lt;code&gt;takeWhile/dropWhile&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Local variable type inference (&lt;code&gt;var list = new ArrayList&amp;lt;String&amp;gt;();&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Improved &lt;code&gt;String&lt;/code&gt; utility functions (&lt;code&gt;repeat()&lt;/code&gt;, &lt;code&gt;isBlank()&lt;/code&gt;, &lt;code&gt;indent()&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Multiline &lt;code&gt;String&lt;/code&gt;s:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"""
&amp;lt;html&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;p&amp;gt;Hello, world&amp;lt;/p&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
"""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://openjdk.java.net/jeps/325"&gt;Switch expressions&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numLetters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;MONDAY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FRIDAY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SUNDAY&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;TUESDAY&lt;/span&gt;&lt;span class="err"&gt;               &lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;THURSDAY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;SATURDAY&lt;/span&gt;&lt;span class="err"&gt;    &lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;WEDNESDAY&lt;/span&gt;&lt;span class="err"&gt;             &lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://openjdk.java.net/jeps/305"&gt;&lt;code&gt;instanceof&lt;/code&gt; pattern matching&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="c1"&gt;// can use s here without explicit casting&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;   &lt;/span&gt; &lt;span class="c1"&gt;// can't use s here&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://openjdk.java.net/jeps/359"&gt;Records&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;Point&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As with any language, in order to use it effectively you need to learn all the constructs. For some, the first code example written in Java 7 will be more readable than the second one, but that's just because they have not learned the new ways of writing the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There's a lot to add to each of these topics and it is worth mentioning that sometimes it's not black-and-white situation (especially with the performance). I also know, that similar false statements are out there for other languages (Python being slow, C#/.Net copying Java or JavaScript invented by aliens that are waiting for us to go mad while writing application using it and then taking over the planet).&lt;/p&gt;

&lt;p&gt;I hope that you enjoyed this article and feel free to add your most beloved myths about Java or any other language in the comments!&lt;/p&gt;

</description>
      <category>java</category>
      <category>myths</category>
    </item>
    <item>
      <title>Migrating from Oracle DB pipe messaging to Kafka</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Wed, 22 Apr 2020 12:35:49 +0000</pubDate>
      <link>https://dev.to/rapasoft/migrating-from-oracle-db-pipe-messaging-to-kafka-1ee3</link>
      <guid>https://dev.to/rapasoft/migrating-from-oracle-db-pipe-messaging-to-kafka-1ee3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;First of all, I would like to state that I am not an Oracle DB expert. I am just a regular full-stack developer, that knows his way around in the DB world and can make things work. I've been involved in several projects that were using Oracle DB and I can honestly say that every one of them could've survive on any other open-source DB engine. Every time, it's usage was unjustified and statements like "it's been there forever" and "it is hard to move away from it" just underlined the impossibility of replacement.&lt;/p&gt;

&lt;p&gt;Nevertheless, I had to use it, and most of the time it was more of a fight. Things that were essential in every other engine were a nightmare to configure (&lt;a href="https://stackoverflow.com/questions/3726758/is-there-any-boolean-type-in-oracle-databases"&gt;the simplest ones&lt;/a&gt;). If something usable was included, you had to pay extra for it (like the absence of partitioning in the standard edition). Anyway, Oracle DB is still the synonym for "enterprise-ready database solution" so Oracle marketing team is doing their job well!&lt;/p&gt;

&lt;h2&gt;
  
  
  Database-centric messaging model
&lt;/h2&gt;

&lt;p&gt;The (simplified version of) application that underwent transformation to Kafka can be described with producer/consumer model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UIYV-9g0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YjgykJ1/oracle-solution.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UIYV-9g0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YjgykJ1/oracle-solution.png" alt="Oracle solution" width="501" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simply put: There are &lt;code&gt;1 .. n&lt;/code&gt; producers and &lt;code&gt;1&lt;/code&gt; consumer. The producers are writing data in the database tables, which have &lt;code&gt;ON UPDATE&lt;/code&gt; triggers to send information to Oracle Pipe (&lt;code&gt;DBMS_PIPE&lt;/code&gt; package). The client then reads information from the pipe and processes it. &lt;/p&gt;

&lt;p&gt;Oracle Pipes are low-level mechanism to communicate between two (or more) sessions in similar scenarios as above. They work similarly as UNIX pipes, with all of their limitations, and are actually quite fast. This solution has one very important problem - it does not scale. Imagine that there will be &lt;code&gt;1 .. gazillion&lt;/code&gt; producers, you can probably add some extra power and RAM, but you still have a single bottleneck to maintain - database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Kafka
&lt;/h2&gt;

&lt;p&gt;Apache Kafka needs no introduction, since it already has a steady place in a world of high-load/high-availability solutions. It is a streaming platform that enables you to build scalable distributed applications with not much effort. If you don't have experience with Kafka, &lt;a href="https://kafka.apache.org/intro"&gt;this post is everything you need to know to understand it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Kafka somehow fits naturally into our use-case. There already are producers and consumers, so we could just take them, wire it all together using Kafka and skip the DB part! In simple world, yes, that would be the case. But our application has important business logic related to calculation and messaging melted in the database, remember?&lt;/p&gt;

&lt;p&gt;Until we are in an ideal world, where database is purely a storage facility, Kafka has a tool that could help us to transition to pure producer-consumer Kafka solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kafka JDBC Source Connector
&lt;/h2&gt;

&lt;p&gt;Using &lt;a href="https://kafka.apache.org/documentation/#connect"&gt;kafka-connect API&lt;/a&gt;, we can create a (source) connector for the database, that would read the changes in tables that were previously processed in database triggers and PL/SQL procedures. These changes would then be sent to intermediate component, that would do the computation itself.&lt;/p&gt;

&lt;p&gt;You can use either JDBC-based or log-based solution, but since the latter one would cost more money (licenses for tools like Oracle GoldenGate or XStream), we will use JDBC-based one. Setting up JDBC source connector is relatively easy, &lt;a href="https://www.confluent.io/blog/kafka-connect-deep-dive-jdbc-source-connector/"&gt;here's a good article that describes it in a succint way&lt;/a&gt;. If you don't want to read the article, here's what it does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Polls specific table (either based on query or just the table name)&lt;/li&gt;
&lt;li&gt;When change is made, it's JSON representation is sent to Kafka&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HL2Fd_0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/ssWCmYf/kafka-solution.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HL2Fd_0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/ssWCmYf/kafka-solution.png" alt="Oracle solution" width="531" height="352"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are certain caveats and limitations which I will list for future reference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you can specific the mode in which the updates will be checked. I've used &lt;code&gt;timestamp&lt;/code&gt;, which is using comparison of timestamp column values. In order to set this up, timestamp column must be:

&lt;ul&gt;
&lt;li&gt;present in the table (duh)&lt;/li&gt;
&lt;li&gt;not null (nullability can be enabled, but what's the point of having that?)&lt;/li&gt;
&lt;li&gt;updated on each change either in the application or in the database trigger (since Oracle does not natively support &lt;code&gt;ON UPDATE TIMESTAMP&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;using correct timezone (which can be configured in the connector settings)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;JDBC connector will use polling, so polling timeout must be fine-tuned in a way that it does not swamp the database. Also, the query used to fetch data must be executed fast.&lt;/li&gt;
&lt;li&gt;Database triggers have the ability to compare old and new date (e.g. before and after update). This would not be possible with JDBC connector, so you need to keep this in mind.&lt;/li&gt;
&lt;li&gt;You would probably also need to format the result in the query, for instance &lt;code&gt;number(*)&lt;/code&gt; (unbounded numeric type) will be transformed to a byte array. It can be easily solved by casting it specific value, e.g. &lt;code&gt;number(12)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What's really great about this solution is how easily it scales up. The &lt;code&gt;processing logic&lt;/code&gt; module can be spun up several times within the same Kafka group, which means the load will be distributed amongst consumers "&lt;a href="https://kafka.apache.org/intro#intro_consumers"&gt;the Kafka way&lt;/a&gt;". This would practically off-load the pressure on the database and enable us to scale up the processing part.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7AWX1Gcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/grxB7Fk/kafka-scaled-solution.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7AWX1Gcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/grxB7Fk/kafka-scaled-solution.png" alt="Oracle solution" width="531" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The implementation of processing logic can be done using kafka-streams or Kafka's Producer/Consumer API, depending on the nature of transformations. I haven't exposed too many details, which was on purpose, so if you have more questions feel free to leave a comment and contact me!&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>oracle</category>
      <category>database</category>
      <category>java</category>
    </item>
    <item>
      <title>Using RxJava for creating reactive JavaFx UI</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Fri, 17 Jan 2020 07:01:33 +0000</pubDate>
      <link>https://dev.to/rapasoft/using-rxjava-for-creating-reactive-javafx-ui-2dj9</link>
      <guid>https://dev.to/rapasoft/using-rxjava-for-creating-reactive-javafx-ui-2dj9</guid>
      <description>&lt;p&gt;In this article, I would like to show you the benefits of RxJava in practical example - desktop JavaFx GUI application. If you are developing Android or any other apps that "compute and render content" at the same time read on!&lt;/p&gt;

&lt;h2&gt;
  
  
  An intro you can skip if you know JavaFx
&lt;/h2&gt;

&lt;p&gt;If you never heard of JavaFx before don't feel bad. On the other hand, if you thought JavaFx project is long dead, well... I don't blame you. But believe it or not it's &lt;a href="https://openjfx.io/" rel="noopener noreferrer"&gt;alive and kickin'&lt;/a&gt;. Since it was open-sourced and separated from JDK it has become the only reasonable choice for building desktop apps in Java. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why on Earth am I writing about technology for creating desktop applications in Java in 2020?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I was firstly introduced to JavaFx in 2013, when it was still part of JDK and it was a proper replacement for Swing, the former UI library, that was... let just say unpopular. But JavaFx was like a fresh breeze and version 2.0 introduced concept of &lt;code&gt;FXML&lt;/code&gt; files, which allowed you to define the looks and style of your components in similar fashion as HTML and CSS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why on Earth would anyone want to build desktop applications in 2020 anyway?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are several reasons. First of all some users still prefer it to web-based services. If you don't have internet connection, you cannot access the site (if it is not offline-enabled). Also, these kind of applications are perfect for communicating with filesystem or underlying OS (e.g. we had an utility that connected through ssh and executed a script, whose results were displayed in the UI). I think there are a lot of benefits there, but this is not the topic for today.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do you want to develop an app?
&lt;/h2&gt;

&lt;p&gt;Let's say you're writing simple UI in JavaFx. You define your layout, create first components, start adding behavior to them and you expect results. Everything works, technically, but sometimes it &lt;em&gt;does not feel right&lt;/em&gt;. The UI is lagging when operations are performed and you get the impression you are doing something wrong.&lt;/p&gt;

&lt;p&gt;To illustrate this I created a simple application with just two components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Combobox&lt;/strong&gt; (a.k.a. SelectBox), which would perform some background tasks whenever user selects different value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;List view&lt;/strong&gt; of results from these long running tasks. This list will show the name of the task and time taken to execute it (up to 1s) in order of execution. And if time is higher than 500 ms it will mark it as &lt;code&gt;slow&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code to execute this task is fairly simple (ignore the &lt;code&gt;Result&lt;/code&gt; class for now, it's just a simple POJO):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private Result runTask(Integer i) {
    long currentTime = System.currentTimeMillis();

    String name = "Task" + i;
    long sleepDuration = (long) (Math.random() * 1000);

    try {
        Thread.sleep(sleepDuration);
        return new Result(name, sleepDuration);
    } catch (Exception e) {
        return new Result("-", 0);
    } finally {
        System.out.println(name + " took " + (System.currentTimeMillis() - currentTime) + " ms");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The long running task will be fairly simple. We define the &lt;code&gt;NUMBER_OF_TASKS&lt;/code&gt; that should be executed and collect results in an &lt;code&gt;ObservableList&lt;/code&gt; that will be used as backing collection for the &lt;code&gt;ListView&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For those not familiar with JavaFx, the &lt;code&gt;ListView&lt;/code&gt; works with a special collection wrapper (&lt;code&gt;ObservableList&lt;/code&gt;) that is bound with the component. So whenever its content is changed, it is also changed in the UI. Simple as that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And hey! Since it is 2020 we're going to use &lt;code&gt;Stream&lt;/code&gt;s, which highly increase readability of what's happening:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void runTasksJavaFx(ObservableList&amp;lt;String&amp;gt; observableList) {
    IntStream.range(1, NUMBER_OF_TASKS) // Stream API way of iterating
            .mapToObj(this::runTask) // Execute and map the results of our long-running task
            .map(result -&amp;gt; result.time &amp;gt; 500 ? new Result(result.name + " (slow)", result.time) : result) // "Annotate" those that took too long
            .forEach(result -&amp;gt; observableList.add(result.toString())); // And push them to result list so that they are displayed in UI
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This looks nice, but you can probably &lt;em&gt;feel&lt;/em&gt; that it's not right. When you try it this is what you'll get:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Fjavafx-blocking.gif%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Fjavafx-blocking.gif%3Fraw%3Dtrue" alt="Blocking"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As you can see when item is selected the whole GUI freezes until computation is finished (you can see that in the console output on the right).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Use the threads Luke
&lt;/h2&gt;

&lt;p&gt;This is not an issue limited to JavaFx and it surely was a nightmare in Swing as well. The problem here is that the application code is running on the same thread as UI code, which means whenever there's some &lt;em&gt;long running task&lt;/em&gt; it will block updates of UI until it is finished. And that's exactly what's happening above.&lt;/p&gt;

&lt;p&gt;There're, of course, ways to fix this. One of them is to use JavaFx's &lt;code&gt;Platform.runLater()&lt;/code&gt; method that takes standard &lt;code&gt;Runnable&lt;/code&gt; as a single parameter. It will schedule updates to "some time in the future". Second is to use JavaFx's &lt;code&gt;Task&lt;/code&gt; and together with &lt;code&gt;ExecutorsService&lt;/code&gt; (and related). To keep it simple, we'll use first one:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void runTasksLaterJavaFx(ObservableList&amp;lt;String&amp;gt; observableList) {
    IntStream.range(1, NUMBER_OF_TASKS) // Still Java 8, yaaay!
            .forEach(i -&amp;gt; Platform.runLater(() -&amp;gt; { // We're using lambda for Runnable, so we cannot map the result
                Result result = runTask(i); // So we go one Java version down with the code style
                if (result.time &amp;gt; 500) {
                    result = new Result(result.name + " (slow)", result.time);
                }
                observableList.add(result.toString());
            }));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Ok, there're few things here and the most important one is that since Runnable returns &lt;code&gt;void&lt;/code&gt;, we need to process results "the old fashioned way". My personal opinion: it's ugly. And it will get uglier if the logic is more complex.&lt;/p&gt;

&lt;p&gt;And then there's this nice note in the &lt;code&gt;Platform.runLater&lt;/code&gt; method's documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: applications should avoid flooding JavaFX with too many pending Runnables. Otherwise, the application may become unresponsive. Applications are encouraged to batch up multiple operations into fewer runLater calls. Additionally, long-running operations should be done on a background thread where possible, freeing up the JavaFX Application Thread for GUI operations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's not very encouraging, isn't it? So when it gets complex, you need to introduce logic for batching the updates. You could use the Task/Executors solution, but that's even more code and if there's one thing that developers hate is reading huge amounts of code to understand what's happening.&lt;/p&gt;

&lt;p&gt;But okay, let's see what it does when we execute this method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Fjavafx-non-blocking.gif%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Fjavafx-non-blocking.gif%3Fraw%3Dtrue" alt="Non-Blocking-Old"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The improvement here is that we don't block the UI, but as you can see, the updates are periodically displayed to the console output, but they are rendered once everything is finished. This can be of course improved, but at what cost?&lt;/p&gt;

&lt;h2&gt;
  
  
  You know what Luke? Let's revisit this threads idea again
&lt;/h2&gt;

&lt;p&gt;There are, of course, better options than fighting threads. Since we want to push our code to the third decade of 21st century, we will use Reactive Extensions (RX)!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With RX it is as with anything that's currently trending like Cloud computing or Blockchain. The concept is not new, it's just a matter of putting everything together to solve a specific problem. The main marketing pitch for RX is &lt;em&gt;The Observer pattern done right&lt;/em&gt;. I won't go into too much details about RX so I suggest you to read a thing or two on &lt;a href="http://reactivex.io/" rel="noopener noreferrer"&gt;reactivex.io&lt;/a&gt; website.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Observer pattern in a nutshell is about creating an object that would emit changes (&lt;code&gt;Observable&lt;/code&gt;) and registering some handler that would execute action whenever needed (&lt;code&gt;Observer&lt;/code&gt;). RX goes further with that since it &lt;em&gt;is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming&lt;/em&gt;. There are multiple implementations of RX in various languages, so we're going to use &lt;a href="https://github.com/ReactiveX/RxJava" rel="noopener noreferrer"&gt;RxJava&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'll go ahead and post a code example, which I will explain in detail line by line:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void runTasksRxJavaFx(ObservableList&amp;lt;String&amp;gt; observableList) {
    Observable.range(1, NUMBER_OF_TASKS) // 1
            .subscribeOn(Schedulers.computation()) // 2
            .map(this::runTask) // 3
            .map(result -&amp;gt; result.time &amp;gt; 500 ? new Result(result.name + " (slow)", result.time) : result) // 3
            .observeOn(JavaFxScheduler.platform()) // 4
            .forEach(result -&amp;gt; observableList.add(result.toString())); // 5
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Line &lt;code&gt;// 1&lt;/code&gt; looks very similar to what we have in first example (the blocking one). But instead of using regular &lt;code&gt;IntStream&lt;/code&gt; we are using one of RxJava's utility classes to generate an observable collection. In this case it will &lt;code&gt;emit&lt;/code&gt; each iterated element, so that observers are notified.&lt;/li&gt;
&lt;li&gt;Line &lt;code&gt;// 2&lt;/code&gt; is probably the most difficult concept to understand here. This is because RxJava is &lt;strong&gt;not multi-threaded by default&lt;/strong&gt;. To enable multi-threading, you need to use &lt;code&gt;Schedulers&lt;/code&gt;, to off-load the execution. &lt;code&gt;subscribeOn&lt;/code&gt; method is used to describe how you want to schedule your background processing. There are &lt;a href="https://www.baeldung.com/rxjava-schedulers" rel="noopener noreferrer"&gt;multiple schedulers you can use&lt;/a&gt; based on the type of work. Specifically &lt;code&gt;Schedulers.computation()&lt;/code&gt; will use bounded thread-pool with the size of up to number of available cores.&lt;/li&gt;
&lt;li&gt;Lines marked with &lt;code&gt;// 3&lt;/code&gt; are the same as in first example, see?&lt;/li&gt;
&lt;li&gt;Line &lt;code&gt;// 4&lt;/code&gt; is similar to &lt;code&gt;subscribeOn&lt;/code&gt;, but &lt;code&gt;observeOn&lt;/code&gt; instead declares where you want to schedule your updates. If you look at the flow of the code, at this point, we would like to &lt;code&gt;emit&lt;/code&gt; changes back to the UI thread. In this case, we will use special type of scheduler which is part of (additional) &lt;a href="https://github.com/ReactiveX/RxJavaFX" rel="noopener noreferrer"&gt;RxJavaFx&lt;/a&gt; library and it uses - guess what - the Java FX GUI thread.&lt;/li&gt;
&lt;li&gt;Line &lt;code&gt;// 5&lt;/code&gt; is also same as in the first example, but in this step we are actually instructing what should be done after &lt;code&gt;observeOn&lt;/code&gt; was called.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And it finally looks like an application users want to use:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Frxjavafx-non-blocking.gif%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Frapasoft%2FJavaFxSkeletonApp%2Fblob%2Fexample-app%2Frxjavafx-non-blocking.gif%3Fraw%3Dtrue" alt="Non-Blocking-rxjava"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There's of course more to it. RxJavaFx project has nice API for creating RX-ready components. Truth to be told the learning curve might be "gentle", especially when it comes to RxJava itself. But when it is used right, you can tune it to handle large amounts of changes without sacrificing UX or performance of your application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This was just a fly-by of what you can do with RxJava and I omitted a lot of details (including the setup of JavaFx project), so for those of you that read it until here I have prepared a &lt;a href="https://github.com/rapasoft/JavaFxSkeletonApp" rel="noopener noreferrer"&gt;Git repository&lt;/a&gt; with template project that you can use for developing JavaFx applications and a &lt;a href="https://github.com/rapasoft/JavaFxSkeletonApp/tree/example-app" rel="noopener noreferrer"&gt;branch with fully working example&lt;/a&gt; of what I just described here.&lt;/p&gt;

</description>
      <category>rxjava</category>
      <category>javafx</category>
      <category>reactive</category>
      <category>java</category>
    </item>
    <item>
      <title>Kotlin</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Fri, 05 Oct 2018 11:02:31 +0000</pubDate>
      <link>https://dev.to/rapasoft/kotlin--ld9</link>
      <guid>https://dev.to/rapasoft/kotlin--ld9</guid>
      <description>&lt;p&gt;In this year's &lt;a href="https://kotlinconf.com/"&gt;KotlinConf&lt;/a&gt; keynote Andrey Breslav mentioned how pleasantly surprised he was by the results of Google search for "Kotlin love" keywords. Developers love working with it, that's why it &lt;a href="https://insights.stackoverflow.com/survey/2018#most-loved-dreaded-and-wanted"&gt;ranked 2nd in "Most loved language" category of StackOverflow Developer survey&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The question is: why is that? What makes the language lovable?&lt;/p&gt;

&lt;p&gt;It's really hard to pick up the best features of Kotlin, since one can easily end up writing about everything. But it would just mean rewriting their &lt;a href="https://kotlinlang.org/docs/reference/idioms.html"&gt;awesome documentation&lt;/a&gt; :). The most important is what can actually be used in solving &lt;em&gt;real world issues™&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I was working on a internal time-tracking application. Backend is written in Java and it has a fairly complex logic when it comes to operations with time. For instance, the working day is defined as 8 hours, and anything that's above it is considered an overtime. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: This is actually a simplification of this problem, but let's go with it for the sake of this example&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Data classes
&lt;/h3&gt;

&lt;p&gt;In Java, it is implemented using various libraries and utility functions on top of complex structures. What I really like about Kotlin, is that it's very expressive, while it stays readable. For instance, if we would like to define the structure for &lt;code&gt;Time&lt;/code&gt;, we could write a simple &lt;strong&gt;data class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will give you an immutable data structure, with &lt;code&gt;equals&lt;/code&gt;/&lt;code&gt;hashCode&lt;/code&gt;/&lt;code&gt;toString&lt;/code&gt;/&lt;code&gt;getters&lt;/code&gt; and &lt;code&gt;setters&lt;/code&gt; (and other) functions implemented out of the box. It's something Java people would call a POJO, but defined in one line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Operators overloading
&lt;/h3&gt;

&lt;p&gt;The next thing, useful in our scenario would be comparing one Time to other. For instance &lt;code&gt;1:30&lt;/code&gt; is more than &lt;code&gt;0:45&lt;/code&gt; and so on. In Java, you can implement &lt;code&gt;Comparable&lt;/code&gt; and override &lt;code&gt;compareTo&lt;/code&gt; function. The same works in Kotlin, but with one big difference - you are able to compare instances by using equality operators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also &lt;strong&gt;override other operators&lt;/strong&gt;. For instance we would need the &lt;code&gt;minus&lt;/code&gt; operator. You can just add this to &lt;code&gt;Time&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;minus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;convertToTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convertToDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="nf"&gt;convertToDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Time(hours=0, minutes=45)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Companion objects
&lt;/h3&gt;

&lt;p&gt;You can see that I've used some utility functions &lt;code&gt;convertToTime&lt;/code&gt; and &lt;code&gt;convertToDouble&lt;/code&gt;. If you are working with Java, you are used to create your own &lt;code&gt;XYZUtil&lt;/code&gt; classes, so instead of creating &lt;code&gt;TimeUtil&lt;/code&gt; class, we can use the &lt;strong&gt;companion object&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;@JvmStatic&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convertToTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// implementation&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nd"&gt;@JvmStatic&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convertToDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// implementation&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Java interoperability
&lt;/h3&gt;

&lt;p&gt;As you can see, I've annotated the methods with &lt;code&gt;@JvmStatic&lt;/code&gt; annotation. Kotlin does not have a &lt;code&gt;static&lt;/code&gt; methods, but to provide &lt;strong&gt;perfect interoperability with Java&lt;/strong&gt;, it can make them visible by using this annotation. Thus, you can call this in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;JavaClass&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;convertToTime&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.75&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can expose also constants and other fields, that would be visible in Java.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null safety
&lt;/h3&gt;

&lt;p&gt;Kotlin does not allow you to use null values by default. However, if you for some reason (e.g. Java inter-op) want to allow them, you can define the parameter types with &lt;code&gt;?&lt;/code&gt;. Let's define another constructor in &lt;code&gt;Time&lt;/code&gt; that accepts only hours:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hours&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define it using &lt;code&gt;constructor&lt;/code&gt; keyword and specifying parameter as &lt;code&gt;Int?&lt;/code&gt;. But if this value is &lt;code&gt;null&lt;/code&gt;, we would like to provide zero as a default. You can use so called &lt;strong&gt;elvis operator&lt;/strong&gt; &lt;code&gt;?:&lt;/code&gt; which will evaluate the expression and if it is null, it will use the right hand value as a fallback. &lt;/p&gt;

&lt;h3&gt;
  
  
  Smart casting
&lt;/h3&gt;

&lt;p&gt;In Java, it is very annoying to do the "check-than-cast" routine, where you first check whether object instance is &lt;code&gt;instanceof&lt;/code&gt; some class and then manually cast it. Let's look at the Kotlin implementation of &lt;code&gt;convertToTime&lt;/code&gt; function defined above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;convertToTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;fullHours&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fullHours&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;fullHours&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Float&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;convertToTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDouble&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might not know this, but function &lt;code&gt;Math.floor&lt;/code&gt; is expecting &lt;code&gt;Double&lt;/code&gt; as a parameter. We are not casting it explicitly, but since we're doing it in on of the branches of &lt;code&gt;when&lt;/code&gt; (i.e. &lt;code&gt;switch&lt;/code&gt; in Java), it is automatically &lt;strong&gt;smart casted&lt;/strong&gt; to &lt;code&gt;Double&lt;/code&gt;. And IDE will highlight this for you, so that you are aware of it! The same goes to call &lt;code&gt;is Int -&amp;gt; Time(time)&lt;/code&gt; which is smart casted to &lt;code&gt;Int&lt;/code&gt; and uses the constructor defined in the previous section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extension functions
&lt;/h3&gt;

&lt;p&gt;We've talked about writing utility functions on top of our newly created classes by using companion objects. But what if we would like to add functionality to existing classes?&lt;/p&gt;

&lt;p&gt;For instance, we would like to provide a &lt;code&gt;toString&lt;/code&gt; implementation for our Time class, that would print &lt;code&gt;00:45&lt;/code&gt; instead of the data class default &lt;code&gt;Time(hours=0, minutes=45)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"${hours.padZeros(2)}:${minutes.padZeros(2)}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;hours&lt;/code&gt; and &lt;code&gt;minutes&lt;/code&gt; are &lt;code&gt;Int&lt;/code&gt; properties of Time class, but function &lt;code&gt;padZeros&lt;/code&gt; does not exist in &lt;code&gt;Int&lt;/code&gt; class. If you really want to use this across the whole project, you can define an application-wide &lt;strong&gt;extension function&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padZeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"${if (this.sign &amp;lt; 0) "&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="s"&gt;" else ""}${this.absoluteValue.toString().padStart(length, '0')}"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;this&lt;/code&gt; in the function above refers to instance on which the function is invoked, so you can use it however you like.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: As you see, Kotlin allows powerful String templating using syntax known from other languages: &lt;code&gt;"${someVar}"&lt;/code&gt; :)&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Ranges
&lt;/h3&gt;

&lt;p&gt;Let's put it all together in a next example. We would like to have a class for &lt;code&gt;Workday&lt;/code&gt;, which can tell us when the person started and concluded his day at work. One of the specialties of this class, would be determining, whether person was at work at specific time.&lt;/p&gt;

&lt;p&gt;We can achieve this by using a &lt;code&gt;ClosedRange&lt;/code&gt; interface, which allows us to use this syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nc"&gt;Workday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The implementation is pretty straight-forward thanks to conventions used for &lt;code&gt;ClosedRange&lt;/code&gt;. It expects that the implementation provides the &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;endInclusive&lt;/code&gt; properties of specified type (which in our case is &lt;code&gt;Time&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Workday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;endInclusive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ClosedRange&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One important note is that this works because &lt;code&gt;ClosedRange&lt;/code&gt; expects that the type is implementing &lt;code&gt;Comparable&lt;/code&gt; interface, which our &lt;code&gt;Time&lt;/code&gt; class does.&lt;/p&gt;

&lt;h3&gt;
  
  
  Higher order functions
&lt;/h3&gt;

&lt;p&gt;Kotlin supports some idioms of functional programming by treating functions as a first-class citizens in language. This means, that you can use them as either parameters, return types or declare them as variables.&lt;/p&gt;

&lt;p&gt;For instance, we would like to add functionality which will check the constructor parameters once the class is initialized:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Workday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;endInclusive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ClosedRange&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// higher order function&lt;/span&gt;
        &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Range `${this.start} to ${this.endInclusive}` does not define a valid workday."&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;endInclusive&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are several things going on here at once. First, we define a function &lt;code&gt;isValid&lt;/code&gt; which validates that start and end times are within a day range. Then, in &lt;code&gt;init&lt;/code&gt; block (which is called after constructing instance) a &lt;code&gt;require&lt;/code&gt; function is called, which is defined in Kotlin &lt;em&gt;stdlib&lt;/em&gt; as this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;lazyMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words, it will evaluate a first parameter, and if it is &lt;code&gt;false&lt;/code&gt; it will invoke a second parameter which is a function (declared using &lt;a href="https://kotlinlang.org/docs/reference/lambdas.html#instantiating-a-function-type"&gt;lambda expression syntax&lt;/a&gt;) that defines the message used in &lt;code&gt;IllegalArgumentException&lt;/code&gt;. So this call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;Workday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will end up like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Exception in thread "main" java.lang.IllegalArgumentException: Range `-01:00 to 12:00` does not define a valid workday.
    at Workday.&amp;lt;init&amp;gt;(WorkingTime.kt:62)
    at WorkingTimeKt.main(WorkingTime.kt:83)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: In Kotlin, if the last parameter is a lambda expression, you can write it after the function call (as in our example). But it is the same as &lt;code&gt;require(isValid(), { "Range ${this.start} to ${this.endInclusive} does not define a valid workday." })&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;There are many hidden gems, that you will discover when working with Kotlin. It enables you to write concise and readable code while working with known technologies (e.g. Java, JVM and it's frameworks). If you would like to discover them, you can start by going through &lt;a href="https://try.kotlinlang.org"&gt;Kotlin koans&lt;/a&gt;, a set of exercises that go through most of the language features - and you can do it in browser!&lt;/p&gt;

&lt;p&gt;Edit: Here's a &lt;a href="https://gist.github.com/rapasoft/2925b6dbf18b78b5e9b4913b707852f9"&gt;Gist to a full source code&lt;/a&gt; used in this article. ;)&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>jvm</category>
      <category>java</category>
    </item>
    <item>
      <title>Pains of Java generics solved in Kotlin</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Thu, 06 Sep 2018 05:54:41 +0000</pubDate>
      <link>https://dev.to/rapasoft/pains-of-java-generics-solved-in-kotlin-4g4a</link>
      <guid>https://dev.to/rapasoft/pains-of-java-generics-solved-in-kotlin-4g4a</guid>
      <description>&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Why does this not compile in Java?&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optionalInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optionalNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalInteger&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalNumber&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am usually very conservative when using generics in Java. When I try to "be smart" I usually over-complicate things and end up with &lt;code&gt;@Suppress("Unchecked")&lt;/code&gt; annotations all over the code. Programming language should always be both easy to write and read. So why is it not possible to do the cast in the example above?&lt;/p&gt;

&lt;p&gt;I would expect the above to work because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Number&lt;/code&gt; is parent to &lt;code&gt;Integer&lt;/code&gt; class in Java&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;optionalNumber&lt;/code&gt; is a producing &lt;code&gt;Number&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Java "solution"
&lt;/h2&gt;

&lt;p&gt;It can be solved by using the &lt;code&gt;extends&lt;/code&gt;/&lt;code&gt;super&lt;/code&gt; keywords to specify the upper bound.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt; &lt;span class="kd"&gt;super&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optionalInteger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optionalNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="n"&gt;optionalInteger&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalNumber&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work, but we will get &lt;code&gt;Unchecked cast&lt;/code&gt; warning on the second line, which &lt;em&gt;does&lt;/em&gt; make sense in the world of Java generics, but it's still ugly. Also, what does it say to reader when &lt;code&gt;&amp;lt;? super Integer&amp;gt;&lt;/code&gt; is used?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Kotlin solution
&lt;/h2&gt;

&lt;p&gt;Kotlin has solved this with &lt;a href="https://kotlinlang.org/docs/reference/generics.html#declaration-site-variance"&gt;&lt;strong&gt;declaration-site variance&lt;/strong&gt;&lt;/a&gt; using keywords &lt;code&gt;in&lt;/code&gt;/&lt;code&gt;out&lt;/code&gt;. We will simulate the &lt;code&gt;Optional&lt;/code&gt; class (since there isn't one in Kotlin) with &lt;code&gt;get()&lt;/code&gt; function for this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;optionalInteger&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;optionalNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalInteger&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By annotating the &lt;code&gt;T&lt;/code&gt; parameter with &lt;code&gt;out&lt;/code&gt; keyword we will specify that &lt;code&gt;T&lt;/code&gt; will always be used as output value (e.g. produced), thus it is safe to perform above cast (since the produced value is safe to cast). Also, if you try adding member function &lt;code&gt;fun set(t: T)&lt;/code&gt; you will get a compiler warning, that &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;out&lt;/code&gt; parameter but occurs in &lt;code&gt;in&lt;/code&gt; position. It is easy to write, read and understand.&lt;/p&gt;

&lt;p&gt;As I mentioned, Kotlin does not have separate &lt;code&gt;Optional&lt;/code&gt; class. Instead it has some &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html"&gt;cool language features&lt;/a&gt; for null checking that you can find in any other modern language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;haveReadArticle&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;giveItA&lt;/span&gt;&lt;span class="err"&gt;❤️&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;😎&lt;/p&gt;

</description>
      <category>java</category>
      <category>kotlin</category>
      <category>generics</category>
      <category>covariance</category>
    </item>
    <item>
      <title>On immutability in Kotlin</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Thu, 21 Jun 2018 13:27:27 +0000</pubDate>
      <link>https://dev.to/rapasoft/on-immutability-in-kotlin-251k</link>
      <guid>https://dev.to/rapasoft/on-immutability-in-kotlin-251k</guid>
      <description>&lt;p&gt;Kotlin has direct support for immutable data structures. They are called &lt;a href="https://kotlinlang.org/docs/reference/data-classes.html"&gt;data classes&lt;/a&gt; and they have some really nice features.&lt;/p&gt;

&lt;p&gt;First of all, the data class creation is really simple. For instance, this &lt;code&gt;User&lt;/code&gt; data class will create class with one full-args constructor, &lt;code&gt;hashCode&lt;/code&gt;, &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt; methods generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's additional advantage here, that by using &lt;code&gt;val&lt;/code&gt; the references to primitive properties are final - thus, cannot be changed. You also need to define all constructor parameters when creating new instance, e.g. &lt;code&gt;User("John", "Doe", 50)&lt;/code&gt;. That's a good start for building immutable data structures on your own!&lt;/p&gt;

&lt;p&gt;The most basic operation you'll want to do with immutable data structure is creating new instance out of existing one and changing (some of) its parameters. This is nicely supported in Kotlin by &lt;code&gt;copy()&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;Let's start by simply copying the instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="c1"&gt;// referential equality = false&lt;/span&gt;
&lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt;  &lt;span class="c1"&gt;// structural equality = true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that it works as expected - objects are equal, but instances are not the same. What if we want to change one property?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will copy the instance and change &lt;code&gt;age&lt;/code&gt; parameter to 20. Compared to Java (in which cloning/copying without 3rd party library is nearly impossible) this is really awesome!&lt;/p&gt;

&lt;p&gt;Let's look at a bit more complex example where we don't use only primitive types but reference to a object instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;user&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jane"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;system1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Blog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;system2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;system1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;system1&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="n"&gt;system2&lt;/span&gt;             &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;system1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;===&lt;/span&gt; &lt;span class="n"&gt;system2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;   &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can see &lt;code&gt;copy()&lt;/code&gt; is not &lt;em&gt;deep&lt;/em&gt;, which means that parameters that are referring to instances will only copy their references. So basically, it's a syntactic sugar over creating new instance via it's constructor and passing the parameters of original instnace.&lt;/p&gt;

&lt;p&gt;But that's okay, since user is an immutable data class and it's properties cannot be changed. It would only be problematic if &lt;code&gt;User&lt;/code&gt; would have properties defined as &lt;code&gt;var&lt;/code&gt;. Then you would be able to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;system2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;system1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;system1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;     &lt;span class="c1"&gt;// not possible if user is defined as `val`&lt;/span&gt;

&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;system2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// prints "13"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bottom line is, if you keep using &lt;code&gt;val&lt;/code&gt; definitions consistently you should be safe with immutability!&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>immutability</category>
    </item>
    <item>
      <title>Frontend development stack: Introducing complexity to reduce complexity</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Mon, 14 May 2018 12:12:28 +0000</pubDate>
      <link>https://dev.to/rapasoft/frontend-development-stack-introducing-complexity-to-reduce-complexity-41dl</link>
      <guid>https://dev.to/rapasoft/frontend-development-stack-introducing-complexity-to-reduce-complexity-41dl</guid>
      <description>&lt;p&gt;As a Java developer, I am not a big fan of JavaScript as a language. In it's essence it is really primitive and simplistic. While this is fine for simpler scripting (the original intention of JavaScript) it is not usable for developing a large application. It has a lot of inconsistencies, tricky parts and in general it will make you pull your hair sometimes.&lt;/p&gt;

&lt;p&gt;But, as in every language there are tools, libraries and frameworks that will ease your pain. I am really surprised how mature is the ecosystem around JavaScript and that you don't have to understand most of it. There's a handlebar for almost everything and - if we take it to the extreme - you might not know you are developing a JavaScript application at all. You can build a full-blown single page application, without even writing a single line of JavaScript - using TypeScript, for instance.&lt;/p&gt;

&lt;p&gt;"How's that?" you might ask, and the answer is in the hidden complexity those "handlebars" bring in. There are plenty of tasks that are done in the background, from writing source code up to creating a "production bundle" that you can deploy.&lt;/p&gt;

&lt;p&gt;In this article you'll find answers to these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I just want to build web! Should I look into some more complex technologies other than HTML, CSS and plain JavaScript?&lt;/li&gt;
&lt;li&gt;There are so many tools and libraries, which ones should I pick?&lt;/li&gt;
&lt;li&gt;Does it have to be so hard to set everything up?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...and some other, hopefully.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project
&lt;/h2&gt;

&lt;p&gt;Let's work with some specific, running example so that the concepts are more clear. We will develop a frontend application for a blogging platform. Simple UI that will display posts from various authors and categories.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwz7rcqry5iuv87f92ksi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwz7rcqry5iuv87f92ksi.PNG" alt="Blog!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Naive approach
&lt;/h2&gt;

&lt;p&gt;You all remember the good old days, when you wrote HTML, some CSS and then to add some spice into it you've added some JavaScript effects? Well, this is where we are going to start. But the world has changed a bit since then, and now everything is written in JavaScript. It's mostly because you can define reusable parts of code and then just re-use them. For instance, this will be a title (&lt;code&gt;title.js&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const title = `&amp;lt;h1&amp;gt;👨‍👨‍👦‍👦 Friends blog 🌟&amp;lt;/h1&amp;gt;`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is how we are going to use it (&lt;code&gt;main.js&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const main = `
    &amp;lt;div class="main container"&amp;gt;
        &amp;lt;div class="title"&amp;gt;${title}&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in order to use this, we need to put it in some main file, for instance &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById("app").innerHTML = main;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And use it in &lt;code&gt;index.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;

    &amp;lt;title&amp;gt;Blogging platform!&amp;lt;/title&amp;gt;

    &amp;lt;script type="text/javascript" src="components/title.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script type="text/javascript" src="components/main.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;div id="app"&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;script type="text/javascript" src="index.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have three &lt;code&gt;.js&lt;/code&gt; files, that need to be imported in specific order, because the constants are defined in global scope. Well this is a problem when we'll have additional components defined in other &lt;code&gt;.js&lt;/code&gt; files. First, you need to manually add them into &lt;code&gt;index.html&lt;/code&gt; and then you need to maintain specific order. What if you define variable with same name? It will overwrite the previous one. Also, we have jumped in it too quickly and are using features from ES6 ("version" of JavaScript) &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals" rel="noopener noreferrer"&gt;like string templates&lt;/a&gt; which isn't supported by older browsers. So, no IE9 support :(.&lt;/p&gt;

&lt;p&gt;And we have nothing but title, yet. In a normal world, you want to add more components, styles, some layouting framework (for instance Bootstrap), add specific logic, helper classes and... bundle it all together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bundling it all together
&lt;/h2&gt;

&lt;p&gt;Why is this needed? If you have significant amount of &lt;code&gt;.js&lt;/code&gt; files, you will need to send a HTTP request for each of them (like in our example &lt;code&gt;title.js&lt;/code&gt;, &lt;code&gt;main.js&lt;/code&gt; and &lt;code&gt;index.js&lt;/code&gt;) which is by nature always slower as if you would put it into one file (&lt;code&gt;bundle.js&lt;/code&gt;). The orchestration around one HTTP request/response roundtrip is just too expensive.&lt;/p&gt;

&lt;p&gt;We can automate this &lt;em&gt;task&lt;/em&gt; by delegating it to a tool! Not too much time ago, there were 2 main competitors and that's &lt;a href="https://gruntjs.com/" rel="noopener noreferrer"&gt;Grunt&lt;/a&gt; and &lt;a href="https://gulpjs.com/" rel="noopener noreferrer"&gt;Gulp&lt;/a&gt;. You can define a specific list of tasks that should be executed in specific sequence in order to build a project, for instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;clean build directory from previous runs&lt;/li&gt;
&lt;li&gt;iterate over list of source &lt;code&gt;.js&lt;/code&gt; files

&lt;ul&gt;
&lt;li&gt;and copy them into one &lt;code&gt;bundle.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and compress it's contents&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;iterate over list of source &lt;code&gt;.css&lt;/code&gt; files

&lt;ul&gt;
&lt;li&gt;and copy them into one &lt;code&gt;bundle.css&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and compress it's contents&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;generate and copy &lt;code&gt;index.html&lt;/code&gt; that will include link to built &lt;code&gt;bundle.js&lt;/code&gt; and &lt;code&gt;bundle.css&lt;/code&gt; files&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Gulp and Grunt are somewhat similar in what they do, but they use different approach. While with Grunt it's more declarative, in Gulp you are defining procedures that are piped together. Take this config (called &lt;code&gt;gulpfile.js&lt;/code&gt;) for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... (gulp plugin definition) ...

gulp.task('buildJs', function () {
    gulp.src([
        './src/components/title.js',
        './src/components/main.js',
        './src/index.js'
    ])
        .pipe(concat('build.js'))
        .pipe(uglify())
        .pipe(gulp.dest('target'))
});

gulp.task('buildCss', function () {
    gulp.src('./src/styles/*.css') // path to your file
        .pipe(minifyCss())
        .pipe(concat('build.css'))
        .pipe(gulp.dest('target'));
});

gulp.task('cleanTarget', function () {
    return gulp.src('target')
        .pipe(clean({force: true}))
});

gulp.task('copyIndexHtml', function () {
    return gulp.src('src/index.html')
        .pipe(gulp.dest('target'));
});

gulp.task('default', gulpSequence('cleanTarget', 'buildJs', 'buildCss', 'copyIndexHtml'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is pretty much clear what it does, running &lt;code&gt;gulp&lt;/code&gt; in command line will run the &lt;code&gt;default&lt;/code&gt; task which combines everything together.&lt;/p&gt;

&lt;p&gt;How Gulp works? It's a &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;node.js&lt;/a&gt; application, so in order to use it properly you have to have node.js installed. You need to transform your project to node.js project as well (via &lt;code&gt;package.json&lt;/code&gt; configuration) and install all the plugins which gulp will used (via &lt;code&gt;npm&lt;/code&gt; - &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;node package manager&lt;/a&gt;). So we need to install not one, but two tools to automate tasks.&lt;/p&gt;

&lt;p&gt;Of course, both gulp and grunt have many plugins that will help you achieve almost everything there's needed. But they are mostly 3rd party plugins that are of various quality and difficulty in terms of setup.&lt;/p&gt;

&lt;p&gt;In our setup, we haven't yet tackled the problem with order of &lt;code&gt;.js&lt;/code&gt; files (which still needs to be explicit) and also haven't mentioned another problem which is a dependency management.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some kind of dependency management
&lt;/h2&gt;

&lt;p&gt;There's Bootstrap, jQuery, moment.js, lodash and more cool libraries and frameworks you would like to use in your project. In ideal world what you would do is to download or point to a &lt;code&gt;bootstrap.css&lt;/code&gt; and link it in &lt;code&gt;index.html&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
    ...
    &amp;lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" /&amp;gt;
    ...
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you're done! But nothing is ideal in frontend world nowadays and the problems will appear as soon as you start adding files. You will need to solve problems like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is library A compatible with specific version of library B?&lt;/li&gt;
&lt;li&gt;Why adding jQuery into project disabled all my fancy pop-up menus?&lt;/li&gt;
&lt;li&gt;Why do we have 4 libraries for creating fancy pop-up menus anyway?&lt;/li&gt;
&lt;li&gt;I have linked an external library that is not available at this link anymore :(&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will soon realize, you need at least some kind of &lt;code&gt;dependency management&lt;/code&gt;. Something that will find the correct version of library you need and link it into project. Not so long time ago these problems were solved by using &lt;a href="https://bower.io/" rel="noopener noreferrer"&gt;Bower&lt;/a&gt;. Bower is a package manager, which will use it's configuration file (&lt;code&gt;bower.json&lt;/code&gt;) to define, download and later install dependencies for your use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "blogging-platform-frontend",
  "description": "The blog!",
  "main": "index.js",
  "authors": ["Pavol Rajzak"],
  "license": "ISC",
  "keywords": ["javascript", "gulp"],
  "homepage": "",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components"
  ],
  "dependencies": {
    "bootstrap": "^4.0.0"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As for Gulp/Grunt you need to install it via &lt;code&gt;npm&lt;/code&gt;, then you can run &lt;code&gt;bower&lt;/code&gt; command which will create a &lt;code&gt;bower_components&lt;/code&gt; directory with specific libraries. You can then use gulp/grunt plugins to wire the dependencies into your project. Sweet! It seems so, but not everything is ideal. Bower is now a de-facto discontinued project, because of several design problems it contained. &lt;a href="https://gofore.com/en/stop-using-bower-and-how-to-do-it/" rel="noopener noreferrer"&gt;You should not use it for new development&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another point of view could be like this: for building the project and maintaining dependencies we need these tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;node.js - runner of &lt;code&gt;.js&lt;/code&gt; scripts&lt;/li&gt;
&lt;li&gt;npm - package manager&lt;/li&gt;
&lt;li&gt;grunt/gulp - task runner&lt;/li&gt;
&lt;li&gt;bower - package manager&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, I tried to make it obvious, that there's some duplication we could avoid. Why do we need 2 task runners and 2 separate dependency management tools? The answer is - we don't.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing webpack
&lt;/h2&gt;

&lt;p&gt;So, we have got rid of gulp/grunt and bower and replaced it with pure npm/node.js solution for frontend part. What can we do with it? Well, not much. The main task - bundling it all together - still needs to be replaced by some more advanced, dedicated tool. &lt;strong&gt;An application bundler&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's where we introduce &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; into our story. It's a bundler capable of crawling through your application, finding sources and creating application bundle based on defined dependencies. The main and biggest difference here is that it takes advantage of &lt;em&gt;modular approach&lt;/em&gt;, which can be implemented in several ways. The ES6 way is using &lt;code&gt;import&lt;/code&gt;/&lt;code&gt;export&lt;/code&gt; statements.&lt;/p&gt;

&lt;p&gt;Let's look at our changed example from naive approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... (title.js) ...
export const title = `&amp;lt;h1&amp;gt;👨‍👨‍👦‍👦 Friends blog 🌟&amp;lt;/h1&amp;gt;`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... (main.js) ...
import {title} from "./title";

export const main = `
    &amp;lt;div class="main container"&amp;gt;
        &amp;lt;div class="title"&amp;gt;${title}&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... (index.js) ...
import {main} from "./src/components/main";

document.getElementById("app").innerHTML = main;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this is a huge improvement and it solves the problem of having all component definition in global namespace. Now that you can define your dependencies explicitly, you don't have to worry about any namespace clashes, since webpack will bundle it all in safe way.&lt;/p&gt;

&lt;p&gt;Webpack can be configured (&lt;code&gt;webpack.config.js&lt;/code&gt;) like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['babel-preset-env', 'react'],
                        plugins: [
                            require('babel-plugin-transform-object-rest-spread')
                        ]
                    }
                }
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
        ]
    },
    plugins: [
        new WebpackCleanupPlugin('target', {verbose: true}),
        new CopyWebpackPlugin([{from: 'index.html', to: 'index.html'}])
    ]
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is doing very similar thing as gulp/grunt was doing, but instead of tasks/plugins there are &lt;strong&gt;loaders&lt;/strong&gt; used for processing files and &lt;em&gt;plugins&lt;/em&gt; for additional tasks.&lt;/p&gt;

&lt;p&gt;Most importantly, you define and trigger the build using &lt;code&gt;npm&lt;/code&gt; / node.js , so we have now only three very specific tools that will do the job. Here's for instance the &lt;code&gt;package.json&lt;/code&gt; configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "blogging-platform-frontend",
  "version": "1.0.0",
  "description": "The simplest UI you can imagine",
  "main": "index.js",
  "scripts": {
    "start": "webpack --watch index.js target/build.js",
    "build": "webpack index.js target/build.js",
  },
  "keywords": [
    "javascript"
  ],
  "author": "Pavol Rajzak",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-jest": "^22.2.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-add-react-displayname": "0.0.5",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "copy-webpack-plugin": "^4.5.1",
    "css-loader": "^0.28.9",
    "style-loader": "^0.20.1",
    "url-loader": "^0.6.2",
    "webpack": "^3.10.0",
    "webpack-cleanup-plugin": "^0.5.1"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might've spotted one addition in the configuration - we are using &lt;code&gt;babel&lt;/code&gt;. It is a transpiler (language-to-language compiler) which will compile our ES6 code to a more supported ES5 version of JavaScript. We can now support our application also on IE9, woohoo! &lt;code&gt;&amp;lt;IronyOff /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also, webpack is able to natively work in &lt;code&gt;watch&lt;/code&gt; mode, which will automatically pick-up changes and re-build the bundle. It also supports multiple configurations (development / production), it has numerous plugins and it get's better each version! As opposed to gulp/grunt the configuration process is definitely more deterministic.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's all Greek to me!
&lt;/h2&gt;

&lt;p&gt;The lifespan of these tools is very short, I am not talking about decades of evolution, but a few years at most. How to track what's going on? If you are not directly employed/involved in frontend development it is very hard. If now &lt;code&gt;webpack&lt;/code&gt; is the cool thing to use, in some months it can be &lt;a href="https://github.com/facebook/prepack" rel="noopener noreferrer"&gt;prepack&lt;/a&gt;. Or &lt;code&gt;npm&lt;/code&gt; is now being deprecated in favor of &lt;a href="https://yarnpkg.com/lang/en/" rel="noopener noreferrer"&gt;yarn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Luckily, there are tools/libraries/frameworks even for this! If you want to build React applications, you can start with &lt;a href="https://github.com/facebook/create-react-app" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;, for Angular you have &lt;a href="https://cli.angular.io/" rel="noopener noreferrer"&gt;angular-cli&lt;/a&gt;. Everything you might need is already pre-configured for you. You still need to understand the notion of build and dependency management, but at least the burden is a lighter this way.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bottom line
&lt;/h2&gt;

&lt;p&gt;I personally think, that being a frontend developer means you need to keep track of changes in JS ecosystem even more frequently. While on backend, you can basically jump back in even after few years without studying new trends (which you shouldn't), it is nearly impossible in frontend. If you do, however, lose track of what is the "standard" frontend stack, you can just use boilerplate or generators directly targeting specific frameworks.&lt;/p&gt;

</description>
      <category>npm</category>
      <category>grunt</category>
      <category>gulp</category>
      <category>webpack</category>
    </item>
    <item>
      <title>Preparing a course for junior developers</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Tue, 03 Apr 2018 07:07:40 +0000</pubDate>
      <link>https://dev.to/rapasoft/preparing-a-course-for-junior-developers-4fnf</link>
      <guid>https://dev.to/rapasoft/preparing-a-course-for-junior-developers-4fnf</guid>
      <description>&lt;p&gt;So, we have decided to create a course for junior developers that would go on for several weeks. The point is to introduce them to newest technologies that are used on real projects and show them the best practices in software engineering. While previously I had several presentations and short-term workshops, I am still a bit struggling about how to conduct it so that it will bring value to the participants.&lt;/p&gt;

&lt;p&gt;I have identified several things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;prepare all prerequisites (environment, materials, Git repository)&lt;/li&gt;
&lt;li&gt;work with smaller groups (or have additional help with overcoming impediments)&lt;/li&gt;
&lt;li&gt;don't over-flood people with information&lt;/li&gt;
&lt;li&gt;if possible select participants (so that everyone starts on the same level)&lt;/li&gt;
&lt;li&gt;be friendly, try to motivate instead of just stating facts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anyhow, I would like to start a discussion on what &lt;em&gt;you&lt;/em&gt; think are the key points for successful course. If you have any tips to share, please don't hesitate ;).&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>training</category>
      <category>course</category>
      <category>developers</category>
    </item>
    <item>
      <title>Just do it! With AWS Lambda</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Sun, 25 Mar 2018 17:31:59 +0000</pubDate>
      <link>https://dev.to/rapasoft/just-do-it-with-aws-lambda-4nla</link>
      <guid>https://dev.to/rapasoft/just-do-it-with-aws-lambda-4nla</guid>
      <description>&lt;h2&gt;
  
  
  The prequel
&lt;/h2&gt;

&lt;p&gt;Last year I had a presentation about AWS Lambda. It's not something I am using in my daily work, but I wanted to explore this technology further and present it to public. When I was preparing code examples for this talk, I was having a problem of coming up with some real-world usage that would go beyond standard "Hello World" or some other peoples' examples. The talk was okay, but it was missing that "Wow!" moment when audience realize that this is the game changer.&lt;/p&gt;

&lt;p&gt;Fast forward to present: Our company is finally adopting Slack as our primary communication tool. It's a huge improvement over traditional Skype for Business, mostly because we can create specialized channels. And with that, the most frequently used channel is... the discussion about where to go for a lunch!&lt;/p&gt;

&lt;h2&gt;
  
  
  The struggle
&lt;/h2&gt;

&lt;p&gt;You see, this is quite a thing, here in Slovakia, since restaurants offer daily lunch menus for a reduced price. Each day different menu for several restaurants around our office. There are many options, so you see why the lunch discussion is an ongoing one.&lt;/p&gt;

&lt;p&gt;The first idea (before Slack era) was to create a web application that would scrape information about menus from nearby restaurants. Either from their webpages or from similar services that provide this information. Thus, Luncher was born!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe35k6fjjox98tl8ilf32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe35k6fjjox98tl8ilf32.png" alt="Luncher"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And so, with Slack our lunch-flow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a screenshot from Luncher&lt;/li&gt;
&lt;li&gt;Post it to Slack &lt;code&gt;#lunch&lt;/code&gt; channel&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;/poll&lt;/code&gt; where to go for lunch.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's not very Slack-ish (if that's a word).&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea
&lt;/h2&gt;

&lt;p&gt;Luckily, I designed Luncher to expose a REST API which provides a list of daily menus in JSON format. My first idea was to create a slack command and use this API. Without any prior knowledge, I started to explore how things work. &lt;/p&gt;

&lt;p&gt;In Slack, it is fairly easy to create a command. You just create new app within your organization, and then provide a functionality to it. In my case, the intention was to create so called slash command, which starts with forward slash followed by command name and parameters. I called this command &lt;code&gt;/obed&lt;/code&gt; (meaning lunch in Slovak). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkojjx49sjfck5m2zif5a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkojjx49sjfck5m2zif5a.png" alt="Implementing obed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The twist
&lt;/h2&gt;

&lt;p&gt;The rules to create slash commands are pretty simple: you use &lt;code&gt;/obed&lt;/code&gt; command, it will call REST API to fetch the data and list the results in Slack. Well that's easy, but the problem (here comes the twist) is that the resulting JSON that is going to be read by Slack needs to be in specific format, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "text": "Here's the main text!",
    "attachments": [
        {
            "text":"Here be some additional text!"
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Luncher sends responses in a very different format.&lt;/p&gt;

&lt;p&gt;The easiest thing would be just to add additional endpoint that would send responses in Slack-friendly way, but here's the real problem. Luncher is deployed to Azure as a WAR file (it's Kotlin + Spring Boot) and it's a bit painful to change anything there, since I am not responsible for the deployments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;I didn't think long when I decided to try implementing a AWS Lambda function that would call Luncher API and transform the output to Slack format. The whole implementation was really easy, since I created and deployed the Lambda function using awesome &lt;a href="http://serverless.com" rel="noopener noreferrer"&gt;Serverless&lt;/a&gt; framework.&lt;/p&gt;

&lt;p&gt;Basically, when &lt;code&gt;/obed&lt;/code&gt; is executed it will call an endpoint defined in AWS API Gateway which will point to Lambda function. This function will call the Luncher REST API, retrieve data, transform them in Slack JSON response and return it back. As a result you get this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flkqrh3rho73on4jic3oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flkqrh3rho73on4jic3oj.png" alt="Ta-daaa"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final words
&lt;/h2&gt;

&lt;p&gt;The most awesome thing about this is that it was super easy. I managed to do this within one hour or so. For now it is simple, but it can be enhanced so that it accepts parameters and formats output in a more fancy way. Also, now I have a real-world example for my next FaaS related topics.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>node</category>
      <category>slack</category>
    </item>
    <item>
      <title>Gerrit notifications: How to tame them?</title>
      <dc:creator>Pavol Rajzak</dc:creator>
      <pubDate>Mon, 22 Jan 2018 13:24:11 +0000</pubDate>
      <link>https://dev.to/rapasoft/gerrit-notifications-how-to-tame-them-806</link>
      <guid>https://dev.to/rapasoft/gerrit-notifications-how-to-tame-them-806</guid>
      <description>&lt;p&gt;We are currently required to use Gerrit for our code reviews. While in general I am okay with the "one-review-per-commit" mechanism and I actually like how the review is done.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;strong&gt;the user experience provided by Gerrit web UI is just terrible&lt;/strong&gt;. For example you cannot even sort the changes by specific columns in table, which does not have even/odd lines in different colors (yes, I need to use my pen to track the information on one line on my 24" screen).&lt;/p&gt;

&lt;p&gt;But the main productivity killer for us is the notification system. &lt;strong&gt;Only push-like notifications are sent via email&lt;/strong&gt;, and they cannot be fine tuned (well, you can switch between plain-text and HTML or "all comments" and "comments left by others"). Therefore, you get a lot of SPAM which you need to manually go through.  And it is not even aggregated like in other tools (e.g. BitBucket). While you can write scripts'n'filters in your favorite e-mail client, Outlook, for instance, is not my favorite client to tweak.&lt;/p&gt;

&lt;p&gt;Therefore, I would really like to know, &lt;strong&gt;are there any alternatives to get meaningful push-like notifications&lt;/strong&gt;?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>gerrit</category>
    </item>
  </channel>
</rss>
