<?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: Benjamin Muskalla</title>
    <description>The latest articles on DEV Community by Benjamin Muskalla (@bmuskalla).</description>
    <link>https://dev.to/bmuskalla</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%2F553854%2F7d5ec50d-d24a-4691-915f-9d14b3c8a904.jpeg</url>
      <title>DEV Community: Benjamin Muskalla</title>
      <link>https://dev.to/bmuskalla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bmuskalla"/>
    <language>en</language>
    <item>
      <title>Git Archeology</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Tue, 12 Jan 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/bmuskalla/git-archeology-1nl3</link>
      <guid>https://dev.to/bmuskalla/git-archeology-1nl3</guid>
      <description>&lt;p&gt;Working on a large codebase, it’s pretty normal to not remember every small or large subsystem or implementation choice. Be it whether you’re new to a codebase, or you’ve been focussed on a specific area of the codebase. When working on refactorings or bug fixes, I often end up in areas I might not be too familiar with. Generally not dramatic, it happens quite often that I’m stopping at what I’m doing because I need to decide on how to move forward. As software engineers, we have to make so many micro-decisions every minute that we’re pretty used to it. Depending on the code and domain knowledge, we often find ourselves in situations where we need more context to make an informed decision. Tests usually provide a pretty good context but depending on their granularity and naming, they might only tell is “what” is in the implementation but miss the “why”. And if we try to decide whether we can remove a certain check or rework a behavior, we need to understand why it was introduced in the first place. Usually, we resort to looking at the commit that introduced the change or the tickets associated with the commit.  &lt;/p&gt;

&lt;p&gt;Most people will start with using git blame (or the respective functionality within their IDE/editor). But on most non-trivial projects, you usually end up with a refactoring commit, a rename, or a trivial cross-project fix like switching to another assertion library. At first glance, we only see the most recent changes, not the most important ones. We need to carefully remove layer by layer of sand and dirt that has been swept over the real changes to unearth them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving Backwards In Time
&lt;/h2&gt;

&lt;p&gt;A simple &lt;code&gt;git blame&lt;/code&gt; shows us only the most recernt changes. Assuming it’s a simple move within the file or a whitespace fix, we can ignore those using the flags that &lt;code&gt;git blame&lt;/code&gt; offer us:&lt;/p&gt;

&lt;dl&gt;
  &lt;dt&gt;-w&lt;/dt&gt;
  &lt;dd&gt;ignores whitespace changes&lt;/dd&gt;
  &lt;dt&gt;-M&lt;/dt&gt;
  &lt;dd&gt;ignores changes if the line was just moved/copied within the same file&lt;/dd&gt;
  &lt;dt&gt;-C&lt;/dt&gt;
  &lt;dd&gt;ignores changes if the line was just moved/coped from other files&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;The above flags are already useful. Most of them are not only available on the command line though, they’re usually available in the various editors as well (feel free to show how it’s done in your editor and share &lt;a href="https://twitter.com/bmuskalla"&gt;on Twitter&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O2ovmQyG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/intellij_options.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O2ovmQyG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/intellij_options.png" alt="IntelliJ VCS Annotation Options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usually, this allows to hide some of the more trivial commits but requires &lt;code&gt;git&lt;/code&gt; to detect them as such. More often, we (as engineers) can easily gauge whether a commit is relevant for our search based on commit message, author, and coding practices. Luckily, this can also easily be achieved by the various editors by moving backward commit-wise. Looking at the history on GitHub, you can use the “View blame prior to this change” feature:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xfk9-b3g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/github_prior.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xfk9-b3g--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/github_prior.png" alt="GitHub prior commits"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, IntelliJ-based IDEs allow you to do the same using “Annotate Previous Revision”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2QE-wlMq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/annotate_previous.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2QE-wlMq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/annotate_previous.png" alt="IntelliJ Annotate Previous Commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case you’re using Visual Studio Code, the excellent “GitLense” extension offers similar commands:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HgDcWTPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/gitlense_previous.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HgDcWTPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/gitlense_previous.png" alt="Visual Studio Code Show Previous Commit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving Forward
&lt;/h2&gt;

&lt;p&gt;Depending on what we’re looking for, we may not want to uncover commit by commit until we find the initial commit that introduced something. Let’s say that we’re working with a customer who sees a specific error message on an older version of our product. On &lt;code&gt;main&lt;/code&gt;, this error message is not present anymore as somebody refactored the code already. To dig into this, we’ll have to find out when this error message got introduced. One way to pick through the whole history in the search for the holy grail..err..the error message is to use git pickaxe. Pickaxe is part of git log can be used as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log -S'errorMessage'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, it will spit out all commits that introdcued or removed the the string &lt;code&gt;errorMessage&lt;/code&gt; in any way.Especially with error messages, you may be looking for an error message template that has variables in there. Luckily, git has us covered: you can use &lt;code&gt;--pickaxe-regex&lt;/code&gt; and use &lt;code&gt;-S&amp;lt;regexp&amp;gt;&lt;/code&gt; to find matching commits. Be aware that using -S will only find commits that change the number of times a string is found (addition/deletion). If you’re also looking for a commit that changed the position of a string (e.g. a refactoring that moved the keyword around), you are better of using the &lt;code&gt;-G&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;git log -G"error.*"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Skipping History
&lt;/h2&gt;

&lt;p&gt;Whether you move forward or backward through history, there will always be the “big bang” refactorings that get in your way. Be it that your team decided to agree on certain line endings, a large split of the codebase or a rename to align the codebase with the &lt;a href="https://www.jamesshore.com/v2/books/aoad1/ubiquitous_language"&gt;ubiquitous language&lt;/a&gt;. So these changes are usually never interesting in the context of wading through the history for a specific line and Git has some neat features that allow us to skip those when looking at &lt;code&gt;git blame&lt;/code&gt; for a file. The simple approach is to pass all commit ids that we want to ignore using &lt;code&gt;--ignore-rev&lt;/code&gt; as the following example shows. Given this history for a file, consisting of the important change as well as a commit that just refactored the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ git log
commit 301b7eca0eb57737e160f5d2d16208f65c4156d6 (HEAD -&amp;gt; master)
Author: Benjamin Muskalla &amp;lt;bmuskalla@gradle.com&amp;gt;
Date: Tue Jan 12 11:38:40 2021 +0100

    Reformat all source files

commit bd3fca50ee1659e740e2f6744d95e737418f1f40
Author: Benjamin Muskalla &amp;lt;bmuskalla@gradle.com&amp;gt;
Date: Tue Jan 12 11:38:12 2021 +0100

    Important change

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, regular git blame shows the most recent changes on the file. This is the commit reformatting the file which is irrelevant for us. We want to see an important change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ git blame sourcefile.py
301b7eca 1) import random
301b7eca 2)
301b7eca 3) print(random.randint(0,9))
301b7eca 4)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So using the “ignore revisions” feature allows us to explicitly ignore specific commits, thus showing us the commit we’re interested in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git blame --ignore-rev 301b7ec sourcefile.py
^bd3fca5 1) import random
301b7eca 2)
^bd3fca5 3) print(random.randint(0,9))
301b7eca 4)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Throughout the lifetime of a project, this approach certainly doesn’t scale. There will always be commits that should be ignored in those cases. Based on the same approach, &lt;code&gt;git blame&lt;/code&gt; allows you to specify a file that contains a list of commits to be ignored. This is not only handy as you can check it in with all your other files, but it’s also easy to share this within your team so everybody can leverage this as well. In our example, let’s call it &lt;code&gt;.git-blame-ignored-revs&lt;/code&gt; and specify which commits should be ignored using their commit hash. You can use &lt;code&gt;#&lt;/code&gt; to add comments to the file as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# ignore pure formatting commits
301b7eca0eb57737e160f5d2d16208f65c4156d6

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now manually use this file as your source of filters using &lt;code&gt;--ignore-revs-file&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;❯ git blame -s --ignore-revs-file=.git-blame-ignored-revs sourcefile.py

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want this to be the default behavior, you can either create a git alias for this or even configure git to always use this file. With this configuration, git blame will always ignore the commits in &lt;code&gt;.git-blame-ignored-revs&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;❯ git config blame.ignoreRevsFile .git-blame-ignored-revs

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A nice side-effect of using &lt;code&gt;git config&lt;/code&gt; is that other tools and editors that rely on the command line tools behaviour just fall in line with these settings. For example, IntelliJ IDEA shows the “Important Change” when enabling annotations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6wWYE_4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/blame_ignore_intellij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6wWYE_4O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://bmuskalla.github.io/blog/git-archeology/blame_ignore_intellij.png" alt="IntelliJ Ignoring Changes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Recap
&lt;/h1&gt;

&lt;p&gt;Different problems require different approaches. Whether you’re looking for an error message that is long gone from the &lt;code&gt;main&lt;/code&gt; branch or try to peel back the layers for some new code, git offers a tremendous amount of tools for your needs. Sometimes it is enough to remember that certain tools or approaches exist so you can look them up if you decide which way you want to approach your search:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git blame&lt;/code&gt; with its various filters like &lt;code&gt;-M&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git log -S/-G&lt;/code&gt; to look for additions/deletions for specific keywords or patterns&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git blame --ignore-rev&lt;/code&gt; to hide known, noisy commits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope I was able to show the one or other interesting bit that can you add to your own toolbox and would be happy &lt;a href="https://twitter.com/bmuskalla"&gt;to hear about&lt;/a&gt; your approach to software archeology.&lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
      <category>github</category>
      <category>versioncontrol</category>
    </item>
    <item>
      <title>JIT 101</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Mon, 11 Jan 2021 20:19:08 +0000</pubDate>
      <link>https://dev.to/bmuskalla/jit-101-jh9</link>
      <guid>https://dev.to/bmuskalla/jit-101-jh9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;JIT compilation and HotSpot are with no doubt some very complex topics. This blog is a very short article touching on the topic in question; it was originally intended to be included in &lt;a href="http://shop.oreilly.com/product/0636920048824.do"&gt;97 Things Every Java Programmer Should Know&lt;/a&gt;. Contributions for the book had a limit on its length, thus a very short introduction into the HotSpot JIT. In the end, the book authors choose another piece of mine to be included but I thought this might be interesting for the one or other person out there.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Compared to other compilers, &lt;code&gt;javac&lt;/code&gt; avoids a lot of optimizations when compiling java source code to bytecode. While “Ahead-Of-Time” (AOT) compilation&lt;br&gt;
can do more heavyweight analysis of the source code, a dynamic compiler can take into account runtime statistics like the most used paths (hotspots)&lt;br&gt;
and advanced chipset features (e.g. which CPU instruction sets are available).&lt;/p&gt;

&lt;p&gt;Enter the “Just-In-Time” (JIT) compiler. That means over time, the behavior of what and how to compile bytecode to native code changes. Initially, most&lt;br&gt;
bytecode is actually just interpreted (tier 0) which is rather slow. Once a code path is “hot” enough, C1 compiler kicks in (most of us know this by&lt;br&gt;
the &lt;code&gt;-client&lt;/code&gt; flag). It is not as aggressive and allows for a faster initial startup. The C2 compiler (&lt;code&gt;-server&lt;/code&gt;) uses more comprehensive analysis and is&lt;br&gt;
meant for long-running processes. Since Java 7, the JVM uses a compilation mode called tiered compilation which seamlessly switches between the modes&lt;br&gt;
based on application behavior.&lt;/p&gt;

&lt;p&gt;Initially, the compilers insert profiling probes into the bytecode to determine which code paths are the hottest (e.g. by invocation count), invariants&lt;br&gt;
(which types are actually used), and branch prediction. Once enough analytics are collected, the compilers actually start to compile bytecode to native&lt;br&gt;
code once they are “hot enough” (&lt;code&gt;-XX:CompileThreshold&lt;/code&gt;), replacing the existing byte code step by step (mixed mode execution).&lt;/p&gt;

&lt;p&gt;Starting with the hot path, one of the first things the compiler tries to achieve is constant folding. Using partial evaluation and escape analysis,&lt;br&gt;
the compiler will try to determine if certain constructs can be reduced to constants (e.g. the expression &lt;code&gt;3 * 5&lt;/code&gt; can be replaced with &lt;code&gt;15&lt;/code&gt;). Another&lt;br&gt;
rather simple optimization is to avoid method calls by inlining methods into their call sites (if they are small enough).&lt;/p&gt;

&lt;p&gt;Virtual method calls present a more complex problem. Generally, the best case is a monomorphic call, a method call that can be translated to a direct&lt;br&gt;
jump in assembly. Compare that to polymorphic calls, like an instance method whose type is not known in advance. The type invariants collected previously&lt;br&gt;
by the probes can help tremendously to identify which types are most often encountered within a code path.&lt;/p&gt;

&lt;p&gt;The compiler optimizes aggressively using heuristics as well. In case a guess was actually wrong (e.g. the seemingly unused branch was called at some&lt;br&gt;
point), the compiler will deoptimize the code again and may revisit this path later using more profiling data. &lt;/p&gt;

&lt;p&gt;Depending on the architecture the JVM is running on, the bytecode may not even be used at all. The HotSpot JVM uses a concept called "intrinsics"&lt;br&gt;
which is a list of well-known methods that will be replaced with specific assembler instructions known to be fast. Good examples are the methods in&lt;br&gt;
&lt;code&gt;java.lang.Math&lt;/code&gt;, &lt;code&gt;System#arraycopy&lt;/code&gt; or &lt;code&gt;Object#getClass&lt;/code&gt; (see &lt;code&gt;@HotSpotIntrinsicCandidate&lt;/code&gt;). For a more in-depth explanation of instrinsics, I can highly recommend &lt;a href="https://alidg.me/blog/2020/12/10/hotspot-intrinsics"&gt;this article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Multi-threaded applications may as well benefit from the optimizations the JIT can do with synchronization locks. Depending on the locks used, the compiler may merge &lt;code&gt;synchronized&lt;/code&gt; blocks together (Lock Coarsening) or even remove them completely if escape analysis determines that nobody else&lt;br&gt;
can lock on those objects (Lock Elision).&lt;/p&gt;

&lt;p&gt;You can enable a lot of debug information about how the compiler decides what to do with your code using feature flags like&lt;br&gt;
&lt;code&gt;-XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining&lt;/code&gt;. If you want to dive deeper into the world of the Hotspot JIT Compiler, have a look at &lt;a href="https://github.com/AdoptOpenJDK/jitwatch"&gt;JITWatch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To have a real deep-dive into such topics, I can highly recommend &lt;a href="https://shipilev.net/"&gt;the posts by Aleksey Shipilëv&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>computerscience</category>
      <category>kotlin</category>
      <category>scala</category>
    </item>
    <item>
      <title>Java Syntax Puzzlers</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Mon, 11 Jan 2021 11:43:19 +0000</pubDate>
      <link>https://dev.to/bmuskalla/java-syntax-puzzlers-55lp</link>
      <guid>https://dev.to/bmuskalla/java-syntax-puzzlers-55lp</guid>
      <description>&lt;p&gt;Roughly 12 years ago, I started to contribute to the Eclipse ecosystem in various functions. One of the most interesting experiences to this date was to work on developer tooling and handling the edge cases so others don’t have to struggle. Though I stepped down as an Eclipse committer in the meantime, I’m still attached to working on productivity tooling nowadays as a member of the &lt;a href="https://github.com/gradle/gradle" rel="noopener noreferrer"&gt;Gradle Build Tool&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While working on Eclipse, I fondly remember working on various parts of the Java Tooling (JDT) and while working on refactorings and quick fixes. And not surprisingly, working on language-intensive pieces turned out to have the same hiccups as other non-trivial algorithsm - the transition from “this is gonna be easy” to “why am I up at 3am reading the Java Language Specification”.&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;Opening a bug report with snippet to reproduce: 10min. Working on a patch: 2h. Getting pinged whether you can finish that patch, 11 years later: priceless. I do miss working on &lt;a href="https://twitter.com/hashtag/eclipse?src=hash&amp;amp;ref_src=twsrc%5Etfw" rel="noopener noreferrer"&gt;#eclipse&lt;/a&gt; &lt;a href="https://twitter.com/hashtag/jdt?src=hash&amp;amp;ref_src=twsrc%5Etfw" rel="noopener noreferrer"&gt;#jdt&lt;/a&gt; though &lt;a href="https://t.co/vpLD73zOlC" rel="noopener noreferrer"&gt;&lt;/a&gt;&lt;a href="https://t.co/vpLD73zOlC" rel="noopener noreferrer"&gt;https://t.co/vpLD73zOlC&lt;/a&gt;&lt;/p&gt;— Benjamin Muskalla (&lt;a class="mentioned-user" href="https://dev.to/bmuskalla"&gt;@bmuskalla&lt;/a&gt;) &lt;a href="https://twitter.com/bmuskalla/status/1110923346441588737?ref_src=twsrc%5Etfw" rel="noopener noreferrer"&gt;March 27, 2019&lt;/a&gt;
&lt;/blockquote&gt; 

&lt;p&gt;Working on language-specific tooling exposes you to all kinds of edge cases and delicate details and language has to offer. Some of them are well known and generally seen "unprofessional" (hello &lt;code&gt;goto&lt;/code&gt;). Others are actually not known at all. And with all due respect, I quite enjoy discovering the edge cases of the language syntax - a lot of times to confuse my co-workers who think they know the Java Language Syntax ;) And given I love a good puzzle (especially the Java Puzzles), let’s try a puzzle but using the Java syntax only, without any runtime behavior. &lt;/p&gt;
&lt;h3&gt;
  
  
  Using Java for Phishing
&lt;/h3&gt;

&lt;p&gt;Let us start off with a widely known fact about Java source files. You’re allowed to use Unicode in most places of your code. While we can’t use the full range of &lt;a href="https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8" rel="noopener noreferrer"&gt;unicode in your class names&lt;/a&gt; (I still want to write &lt;code&gt;throw 🎂()&lt;/code&gt;), you can add enough Unicode to play some pranks on your co-workers. &lt;/p&gt;

&lt;p&gt;As an appetizer, in your next (remote) pairing session, just slip in a “Greek Question Mark” (&lt;code&gt;U+037E&lt;/code&gt;)  into the code and watch your co-worker trying to find out what is wrong with that simple semicolon. This technique is most often used by phishing emails to make a URL look like the real one but actually points &lt;a href="https://en.wikipedia.org/wiki/IDN_homograph_attack" rel="noopener noreferrer"&gt;to a very different domain&lt;/a&gt;.&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%2Flz6awz7sg04wwfeburx0.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%2Flz6awz7sg04wwfeburx0.png" alt="Greek Question Mark"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As it doesn’t even compile, that’s an easy one for your co-worker to recognize and fix. Let’s start to be a little more sneaky. &lt;/p&gt;

&lt;p&gt;Guess what the following program prints?&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;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="k"&gt;if&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="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// one down, one to go: \u000a\u007d\u007b&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="s"&gt;"1 is 2"&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;Yes, in the context of the post, you correctly guessed that it is printing “1 is 2”. Just...HOW? How is it possible to trick Java into thinking 1 == 2, even with unicode magic. INSIDE A COMMENT. Any guesses? It actually doesn’t change the expression. The following unicode characters were harmed in the process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;\u000a&lt;/code&gt; - the newline character &lt;code&gt;\n&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\u007d&lt;/code&gt; - a closing curly brace &lt;code&gt;}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\u007b&lt;/code&gt; - an opening curly brace &lt;code&gt;{&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the code we’re actually looking at is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="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="k"&gt;if&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="mi"&gt;2&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="s"&gt;"1 is 2"&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;Funny enough, most programmers would suspect something fishy with this comment when they see it. But what about indenting it so it’s not shown in your editor anymore? ;)&lt;/p&gt;

&lt;h3&gt;
  
  
  Blocks of Blocks
&lt;/h3&gt;

&lt;p&gt;Let’s move on to the &lt;a href="https://docs.oracle.com/javase/specs/" rel="noopener noreferrer"&gt;Java Language Specification&lt;/a&gt; and see what interesting bits of syntax we can find in there.&lt;/p&gt;

&lt;p&gt;Looking at the possibilities we have for implementing methods, Java defines method bodys to contain &lt;code&gt;Block&lt;/code&gt; elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MethodBody:
  Block

Block:
  { [BlockStatements] }

BlockStatement:
  LocalVariableDeclarationStatement
  ClassDeclaration
  Statement

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Taking a closer look at the definition of &lt;code&gt;Block&lt;/code&gt;, we learn that they can contain statements (so far so good) but also…&lt;code&gt;ClassDeclaration&lt;/code&gt;s. Now it gets interesting. Let’s see how deep the rabbit hole goes.&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;howDeepCanWeGo&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bar&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;helloFromBar&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// You musn't be afraid to dream a little bigger, darling.&lt;/span&gt;
                &lt;span class="o"&gt;}&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;Bar&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;helloFromBar&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="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="n"&gt;instance&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;Foo&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hello&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;Funnily enough, while this feature seems quite useless at first sight, it’s the only one I’ve been using in actual test code in the past. While working on a framework that heavily relied on reflection, the inline class definitions came in quite handy to define classes under test and keeping them with the test. The alternative of having a bunch of nested classes scattered alongside tests was a good reason to move them closer to the test. You can read me more about the quirks of local classes in &lt;a href="https://docs.oracle.com/javase/specs/jls/se15/html/jls-14.html#jls-14.3" rel="noopener noreferrer"&gt;JLS 14.3&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  This and That
&lt;/h3&gt;

&lt;p&gt;Moving away from classes and closer to the action. Let’s have a look at method parameters. As you may encounter several times yourself, you can’t name things the same as keywords. Well, let’s have a look at the following snippet.&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;KeywordParameter&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;KeywordParameter&lt;/span&gt; &lt;span class="n"&gt;someObject&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;KeywordParameter&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;someObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;callMe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;callMe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeywordParameter&lt;/span&gt; &lt;span class="k"&gt;this&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;foo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&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;So we’re creating a new instance of &lt;code&gt;KeywordParameter&lt;/code&gt; and calling the &lt;code&gt;callMe&lt;/code&gt; method on it. Passing the &lt;code&gt;int&lt;/code&gt; parameter. But wait, the method has two parameters. And one is even named after a keyword. That shouldn’t even compile, right? It actually does. Looking at the JLS 8.4 Method Declarations, we can find the definition for method declarations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MethodDeclarator:
  Identifier ( [ReceiverParameter ,] [FormalParameterList] ) [Dims]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see that the first parameter is a special, optional parameter not part of the formal parameter list. And it’s actually defined to always have the name “this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReceiverParameter:
  {Annotation} UnannType [Identifier .] this
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The so-called “receiver parameter” is an “optional syntactic device” that represents the object it is invoked on (so it’s really the same as what you’d expect from “this”). Its sole purpose is to be available in the source code to be annotated if necessary. Let’s assume we have an &lt;code&gt;@Immutable&lt;/code&gt; annotation in our project and for some reason, we want to ensure that our IDE (or other code analyzers) understand that &lt;code&gt;this&lt;/code&gt; in our current context represents an immutable data structure. With the explicit receiver parameter, we can annotate it correspondingly:&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;callMe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Immutable&lt;/span&gt; &lt;span class="nc"&gt;KeywordParameter&lt;/span&gt; &lt;span class="k"&gt;this&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;foo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  @Everywhere
&lt;/h3&gt;

&lt;p&gt;Talking about annotating things for the purposes of analyzing code. For the above snippets to work, the annotation needs to be targetable for &lt;code&gt;PARAMETER&lt;/code&gt;. Did you ever look up what other targets an annotation can have? Going through the most common ones, there are no surprises:&lt;br&gt;
&lt;code&gt;TYPE&lt;/code&gt;, &lt;code&gt;FIELD&lt;/code&gt;, &lt;code&gt;METHOD&lt;/code&gt;, &lt;code&gt;PARAMETER&lt;/code&gt;, &lt;code&gt;CONSTRUCTOR&lt;/code&gt;, &lt;code&gt;LOCAL_VARIABLE&lt;/code&gt;, &lt;code&gt;ANNOTATION_TYPE&lt;/code&gt;, &lt;code&gt;PACKAGE&lt;/code&gt;, &lt;code&gt;TYPE_PARAMETER&lt;/code&gt;, &lt;code&gt;MODULE&lt;/code&gt; (since Java 9) and &lt;code&gt;RECORD_COMPONENT&lt;/code&gt; (since Java 14). But there is one that’s not so obvious where to put it: &lt;code&gt;TYPE_USE&lt;/code&gt;. From the name, it sounds like it can be used everywhere a type is used. Let’s try and use it in some places:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@TypeAnnotationsEverywhere&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Immutable&lt;/span&gt; &lt;span class="c1"&gt;// ok, easy, similar to TYPE &lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TypeAnnotationsEverywhere&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;giveMeMoreTypes&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nd"&gt;@Immutable&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// errr what?&lt;/span&gt;
     &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nd"&gt;@Immutable&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// WHAT??????&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nd"&gt;@Immutable&lt;/span&gt; &lt;span class="nc"&gt;Function&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Indeed, using &lt;code&gt;TYPE_USE&lt;/code&gt; allows us to put the annotation in the most unusual spots. &lt;a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.11" rel="noopener noreferrer"&gt;JLS 4.11&lt;/a&gt; defines all the spots that are covered by a “type usage”. &lt;/p&gt;

&lt;p&gt;Which one of those syntaxes were you aware of? Got all of them? The code for the post can be found on &lt;a href="https://github.com/bmuskalla/java-syntax-puzzlers" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; as well. In the meantime, I’m still working on my museum of interesting cases of the language constructs, so please share anything you’ve encountered yourself. You can reach me on Twitter via &lt;a href="https://twitter.com/bmuskalla" rel="noopener noreferrer"&gt;@bmuskalla&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>watercooler</category>
      <category>security</category>
    </item>
    <item>
      <title>Refactoring towards speed reading</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Tue, 05 Jan 2021 13:56:43 +0000</pubDate>
      <link>https://dev.to/bmuskalla/refactoring-towards-speed-reading-3k4n</link>
      <guid>https://dev.to/bmuskalla/refactoring-towards-speed-reading-3k4n</guid>
      <description>&lt;p&gt;When was the last time you were in the &lt;a href="https://en.wikipedia.org/wiki/Flow_(psychology)"&gt;flow&lt;/a&gt; while working through some code? Reading code or doing code reviews is an activity that we do often, yet we don’t often get into the zone doing it. Too many times, we have to slow down, check that last line again or try to figure out the structure of the code. Even though we all follow the same principles for clean code, there is always that gut feel which tells you that one style is better than the other. While not conclusive, a part of this is the way we structure code for readability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Speed_reading"&gt;Speed reading&lt;/a&gt; is a technique many people have heard about. The casual reader usually reaches 150–200wpm (&lt;a href="https://en.wikipedia.org/wiki/Words_per_minute"&gt;words per minute&lt;/a&gt;) with a good comprehension rate. Auditory readers can read approximatly 400wpm while visual readers can reach up to 700wpm. Controversial world record holders in speed reading claim to reach up to 4200wpm. But don’t worry, we don’t need to set the new world record in speed reading to learn the basic concepts and apply them to another area: our code. We’ll look at 3 areas that are particular helpful when it comes to reading code: Skimming, meta guiding and visual fixation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lMzFasVW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/92qukrbmdpjtum7z9ntm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lMzFasVW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/92qukrbmdpjtum7z9ntm.gif" alt="Simple sentence @ 400wpm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what makes speed reading that fast? One of the first steps is to suppress subvocalication. Subvocalization? Exactly. That voice in your head that just tried to properly articulate that word. And yes, you’re now aware of that voice again. But don’t worry, it will be gone soon :) Subvocalization is something that an adult should be able to learn with relative ease as a first step to seriously improve reading speed, not only for code.&lt;/p&gt;

&lt;p&gt;In this post, I’m not going into the details of learning to speed read as others do a way better job at that. Be it books, articles or software like &lt;a href="https://www.spreeder.com/cx/"&gt;spreeder&lt;/a&gt; or &lt;a href="http://spritzinc.com/"&gt;spritz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lets get to our first example that we want to refactor to improve readability. Here is a simple function which takes 3 parameters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yxgNZ2ln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nkkxa5ywqqt97q74xa1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yxgNZ2ln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nkkxa5ywqqt97q74xa1a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I suspect it is not the first time for you reading code so I can safely assume that you didn’t even try to read it word by word. The first thing that happens when trying to read this code is that we try to skim over it and find the relevant bits of what is happening based our previous expierence. One likely way to read the code is to follow where and how the input parameters are used. Using &lt;a href="https://en.wikipedia.org/wiki/Eye_movement_in_reading"&gt;eye tracking&lt;/a&gt;, what was likely happening when you read the code is this eye movement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VVdoybvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kasraqx3sozck0v2sos.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VVdoybvt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kasraqx3sozck0v2sos.png" alt="Eye movement when tracking the parameter usage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After locating name, we actually have to break our visual fixation and find the next parameter which requires us to look down and left. We can start with a very simple refactoring to help a bit with this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mZ2TX5yn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hgfmuvx2rmm0ah4rt73e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mZ2TX5yn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hgfmuvx2rmm0ah4rt73e.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Better. But we’re not there yet. Looking at the semantic meaning of the three lines of code here suggests that they’re in fact not too different. We do validate all three arguments, not just age as the method name suggests. While many people these days strive for clean code, the trigger to extract a method is usually the number of lines that can be extracted. By extracting methods for these cases even helps us to guide the eye, allowing future engineers to actually skim over the code and let them decide whether they need to stop here or actually go on depending on what they’re looking for.&lt;/p&gt;

&lt;p&gt;By the way, were you able to read the animated sentence in orange above? That was 400wpm. That is already very fast. The fact that we can rely on visual fixation to avoid additional eye movement helps us to keep up. If you’re not used to this reading speed, comprehension usually suffers when reading a longer text in that speed. You can usually try it with speed reading apps, &lt;a href="https://chrome.google.com/webstore/detail/sprint-reader-speed-readi/kejhpkmainjkpiablnfdppneidnkhdif?hl=en"&gt;browser extensions&lt;/a&gt; or this &lt;a href="https://www.spreeder.com/bookmarklet.php"&gt;bookmarklet&lt;/a&gt; if you’re curious.&lt;/p&gt;

&lt;p&gt;Meta guiding is another principle in speed reading that is commonly suggested when starting to learn speed reading. Instead of trying to read word by word in a book, you try to capture the whole line at once. Kids usually do that by using their finger to keep track of the word they’re reading. As adults, we usually stopped doing that at some point. But using a finger or another device helps us to keep moving forward and avoids jumping back a word or two. Funny enough, code itself can act as such a device as it has an inherit structure that we can leverage to guide our eye.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xyupCbP_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b9h88jtaz3q6xiacrirt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xyupCbP_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b9h88jtaz3q6xiacrirt.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How many items are in the list? One, two, three, four! Five. Maybe more. Oops, missed that zeros argument, too?. The structure that should actually help us gets in our way. While we have allowed our “reader” to be guided by the alignment of the add methods, we totally misguided the eye and missed the constructor argument. Rewriting this to match the pattern of the other add calls allows the reader to follow the guide easily without missing any important information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1F6UouAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/83zx9yrj1obzku9ze2q1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1F6UouAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/83zx9yrj1obzku9ze2q1.png" alt="Help guiding the eye of your reader"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now all the methods that actually add something to the list are in line and easy to spot without looking back and forth.&lt;/p&gt;

&lt;p&gt;What are your code snippets that you broke your flow of reading in the past? Because most of the time, parsing the code with our eyes is what we do. The trick is not to become a better parser or a better reader. The goal should be to write our code in a way that it helps guide the reader towards the important aspects of the code. A reader needs to be able to skim over the code, ignore the boilerplate that does not add to the semantic understanding and still have a high comprehension. But in the end, it goes a long way to write code that is not only readable but can actually be digested in a really fast manner.&lt;/p&gt;

&lt;p&gt;Next time you write some piece of code, see if you can actually speed read it.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://images.unsplash.com/photo-1506782664677-3e16d3382641?ixlib=rb-1.2.1&amp;amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;amp;auto=format&amp;amp;fit=crop&amp;amp;w=600&amp;amp;q=80"&gt;Cover Photo by Fischer Twins on Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>flow</category>
      <category>speedreading</category>
    </item>
    <item>
      <title>Merging nested Lists or Arrays with Java 8</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Tue, 15 Nov 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/bmuskalla/merging-nested-lists-or-arrays-with-java-8-27em</link>
      <guid>https://dev.to/bmuskalla/merging-nested-lists-or-arrays-with-java-8-27em</guid>
      <description>&lt;p&gt;When accessing 3rd party APIs with pagination, we tend to see the same pattern over and over again. Usually, a response (represented as POJO) looks something 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;class Result {
  public List&amp;lt;Item&amp;gt; getItems() {
    ...
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be it from a third party service or your own APIs, after retrieving all results of the API call, we end up with something like a &lt;code&gt;List&amp;lt;Result&amp;gt;&lt;/code&gt;. Great. We don’t really care about the response itself, we’re actually interested in all the items of the responses. Let’s say we want all the items, filter some of them out and transform them into a &lt;code&gt;TransformedItem&lt;/code&gt;. People usually start writing something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Result&amp;gt; results = ...
List&amp;lt;TransformedItem&amp;gt; newItems = results.stream()
     .map(result -&amp;gt; result.getItems())
     .filter(item -&amp;gt; item.isValid())
     .map(TransformedItem::new)
     .collect(toList());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ooops, this doesn’t even compile. The problem is that the first map doesn’t return a &lt;code&gt;Stream&amp;lt;Item&amp;gt;&lt;/code&gt; but actually a &lt;code&gt;Stream&amp;lt;List&amp;lt;Item&amp;gt;&amp;gt;&lt;/code&gt;. In order to actually merge/flatten all those nested lists, you can use &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#flatMap-java.util.function.Function-"&gt;Stream#flatMap&lt;/a&gt;. The difference is quite simple. &lt;code&gt;#map&lt;/code&gt; allows you to transform an element in the stream into another element. On the other hand, &lt;code&gt;#flatMap&lt;/code&gt; allows you to convert a single element to many (or no) elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List&amp;lt;Result&amp;gt; results = ...
List&amp;lt;TransformedItem&amp;gt; newItems = results.stream()
     .map(result -&amp;gt; result.getItems())
     .flatMap(List::stream)
     .map(TransformedItem::new)
     .collect(toList());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just in case you’re working with a 3rd party API that returns something ugly as &lt;code&gt;List&amp;lt;Item[]&amp;gt;&lt;/code&gt;, you can use the same pattern, just choose the corresponding the flatMap function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class QueryResponse {
  public Item[] getItems() {
    ...
  }
}

...

List&amp;lt;TransformedItem&amp;gt; newItems = results.stream()
     .map(result -&amp;gt; result.getItems())
     .flatMap(Arrays::stream)
     .map(TransformedItem::new)
     .collect(toList());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Have fun with &lt;code&gt;#flatMap&lt;/code&gt; and let me know &lt;a href="https://twitter.com/bmuskalla"&gt;on Twitter&lt;/a&gt; how you used &lt;code&gt;#flatMap&lt;/code&gt; in your scenarios. A great explanation of how to compose streams and some concepts behind streams can be found in Martin Fowler &lt;a href="http://martinfowler.com/articles/collection-pipeline/"&gt;Collection Pipeline&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>WHERE_TO_PUT.CONSTANTS</title>
      <dc:creator>Benjamin Muskalla</dc:creator>
      <pubDate>Thu, 06 Oct 2016 00:00:00 +0000</pubDate>
      <link>https://dev.to/bmuskalla/wheretoput-constants-4obe</link>
      <guid>https://dev.to/bmuskalla/wheretoput-constants-4obe</guid>
      <description>&lt;p&gt;When writing software, some conversations happen over and over again. Over the years, I’ve seen different solutions to the same problem that I’d like to share with you. One of those conversations go something like this, even though the answer might differ depending on the team or component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bob:&lt;/strong&gt; Hey, I have some string literals here and I was told in the code review that I should extract those into constants. But where would I put those?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Alice:&lt;/strong&gt; Ah, sure. Just put them into our MyModuleConstants class. We put all constants into that class so they are easy to find.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MyConstants {

  public static final String V0 = "v0";

  public static final String LABEL_STATUS_OPEN = "Open";

  public static final String UNUSED = "butNobodyWillKnow";

  public static final Long MAX_SIZE = 13498368L;

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Soooo. Constant classes, eh? Let me share some of the patterns I’ve found (and used myself) over the years and see if we can somehow adapt or improve existing patterns when and how to use constants…and when not to. Take it a bit like “Refactoring away from constants”. First things first, I’m not going to talk about constant interfaces here. Please refer to &lt;a href="https://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683"&gt;Item 17: Use interfaces only to define types&lt;/a&gt; why that’s a bad idea.&lt;/p&gt;

&lt;p&gt;First up, naming constants. Easy, right? Not at all. It’s as hard as naming classes and methods to make the intention really clear. Otherwise, Bad Things™ will happen. Let’s take a look at our first constant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static final String V0 = "v0";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It seems to be defining some kind of version number. Let’s assume our project is interacting with a 3rd party API, this seems to be the version number of their REST API. Now, taking a step back, why do we extract constants in the first place? Usually, it’s for two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make the intention of a value clear (&lt;a href="https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants"&gt;magic numbers&lt;/a&gt; as one example)&lt;/li&gt;
&lt;li&gt;To make the code easier to maintain by factoring out common constants so they can be easily changed in the future&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jH2cI7Ws--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://imgs.xkcd.com/comics/int_pi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jH2cI7Ws--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://imgs.xkcd.com/comics/int_pi.png" alt="https://xkcd.com/1275/"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the above example, the intention is not clear at all. While we can guess it’s a version string, we don’t know how it is supposed to be used. Is it so all API calls use the same version of the REST API? Or is it to determine whether we’re working with the v0 version which requires some special handling? If we now want to upgrade to v1 of the REST API, it’s not as easy as just changing the constant as other code might rely on this exact version. Without proper naming, people tend to reuse constants for different scenarios that actually require two different constructs (which might happen to have the same value for now). Assuming we had the two scenarios above, we slightly better way would be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static final String REST_API_V0 = "v0";

public static final String REST_API_CURRENT = V0;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not really great either. We still try to group out constants together using a common prefix (REST_API). Usually when you start using common prefixes, that group usually shares some kind of responsibility (see also this &lt;a href="http://www.cse.ohio-state.edu/~rountev/presto/pubs/icsm07.pdf"&gt;paper&lt;/a&gt; and a &lt;a href="https://github.com/khatchad/Constants-to-Enum-Eclipse-Plugin"&gt;research prototype&lt;/a&gt; for an automated refactoring). Let’s try and extract these constants out into their own entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public enum RestApiVersion {

  V0("v0"),

  V1("v1");

  private String apiPath;

  public static RestApiVersion getCurrentApiVersion() {
    return V1;
  }

  private RestApiVersion(String version) {
    this.apiPath = version;
  }

  private String getApiPath() {
    return apiPath;
  }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think the main takeaway from this is that people tend to mix up literals and magic numbers. While everyone is trying to follow the “Extract magic numbers into constants” refactoring, they tend to see every literal as “magic number”. Be aware of the fact that constants usually have no context and thus may form cohesion where none exists. The example doesn’t cover the full story though. One nice side-effect is that we gain compile-safety by using a proper type (be it an enum or a class with the strategy pattern). While enums are usually preferred over constants, take a step back and see if there is even a need for a “list of constant things” or if a higher abstraction actually makes more sense. For example, if there is a need to dynamically influence the current version (e.g. for testing/mocking) or if there is more to handling different versions, I recommend to check out the &lt;a href="https://en.wikipedia.org/wiki/Strategy_pattern"&gt;Strategy design pattern&lt;/a&gt; which I’ll leave as an exercise for the reader.&lt;/p&gt;




&lt;p&gt;Defining constants by constants is another pattern I’ve seen quite a few times. Take the example above of the RestApiVersion. A slightly different incarnation I’ve seen many times is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public enum RestApiVersion {

  V0(MyConstants.REST_VERSION_V0), // no, no, no

  ....
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This again goes back to treating every literal as a magic string. There is no additional value gained by defining &lt;code&gt;REST_VERSION_V0&lt;/code&gt; as a separate constant. The opposite is actually true. How do consumers decide when to use the proper facade like &lt;code&gt;RestApiVersion&lt;/code&gt; and when to use the “backdoor” by just using the &lt;code&gt;REST_VERSION_V0&lt;/code&gt; constant itself. With the constant exposed as a constant value, there is also no chance for the maintainer of RestApiVersion to dynamically adapt to future needs. With the whole concept modeled with a proper API, we do have a chance to change the implementation without breaking consumers.&lt;/p&gt;




&lt;p&gt;Whenever you’re about to create a *Constants class, take a step back, go for a walk, or grab a coffee with a fellow engineer. Try to explain why you want to create such a class and if that is really the right abstraction or if the next engineers on your component might be able to see the patterns easier with another abstraction. There are a lot topics we haven’t touched on yet. When should we actually use constants? What about literals with placeholders (I look at you &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html"&gt;MessageFormat&lt;/a&gt;). And are there any tools to detect unused constants? Let me know in the comments.&lt;sup id="fnref:1"&gt;1&lt;/sup&gt;&lt;/p&gt;




&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Originally published on &lt;a href="https://www.tasktop.com/blog/where_to_put-constants/"&gt;Tasktop Blog&lt;/a&gt; ↩︎&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

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