<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://kornafeld.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://kornafeld.com/" rel="alternate" type="text/html" /><updated>2026-01-30T16:15:48-05:00</updated><id>https://kornafeld.com/feed.xml</id><title type="html">Bit More</title><subtitle>Adam&apos;s thoughts on software and other amusing topics
</subtitle><author><name>Adam Kornafeld</name></author><entry><title type="html">The one that brings the Dalí Clock to life</title><link href="https://kornafeld.com/2026/01/14/dali-clock.html" rel="alternate" type="text/html" title="The one that brings the Dalí Clock to life" /><published>2026-01-14T00:00:00-05:00</published><updated>2026-01-14T00:00:00-05:00</updated><id>https://kornafeld.com/2026/01/14/dali-clock</id><content type="html" xml:base="https://kornafeld.com/2026/01/14/dali-clock.html"><![CDATA[<p>When I was looking for a cover image for the <a href="https://kornafeld.com/2021/04/10/on-naming.html">very first post</a> I almost instinctively reached out to one of my favorite paintings: <a href="https://en.wikipedia.org/wiki/The_Persistence_of_Memory">The Persistence of Memory</a> by Dalí. You might know it by the name: The Melting Clocks. It’s only now, years later, that it dawned on me why.</p>

<p>The story says Dalí got his inspiration for the painting from the surreal way he once saw a piece of runny Camembert cheese melting in the Sun. The melting clocks represent the <em>omnipresence</em> of time, and identify its <em>mastery</em> over human beings. I feel lucky to have been able to witness this painting in real life at the <a href="https://www.moma.org/">MoMA</a> in New York City.</p>

<h2 id="-how-it-all-started">🎥 How it all started</h2>

<p>I am fascinated by the concept of time. Don’t really know (yet) why, but I always have been. Back when I was a freshman at the university - Budapest University of Technology and Economics or <a href="https://www.bme.hu/?language=en">BME</a> that is - the gateway drug language into software engineering they taught us was <em>Pascal</em>. I know, right?! I feel old. During the first semester everyone had to pick an idea and implement it as their take home assignment over the course of those few months. When people think about Pascal, command line applications usually first pop in mind. Not for me though. Excited to get my hands dirty with <em>real</em> software engineering - after having been hacking at it on my own well before university - I wanted to jump in the deep.</p>

<p>So I picked a <em>graphics</em> problem. An idea that I had for some time that found its roots in Dalí’s painting and that I lacked the know-how on how to execute it up until that time. See, mechanical clocks, watches and timepieces have a major limitation in my eyes. The shape of them are mainly influenced by the length of their hour, minute and second hands. The longest hand sets the minimum of the radius the clock face can have. Anything smaller and the face would not be able to complete a full circle without bumping into the wall of the clock face. Or worse run off the clock face. Sure, you see rectangular clock faces every now and then. But that’s usually the farthest the imagination of the creator goes. Enter Dalí. He was first to break down this barrier raised by the physical limitations presented by watch hands in his painting. However, he was cheating or more like lucky in a sense that through his painting he was able to freeze time so he didn’t have to worry about the length of the clock hands.</p>

<h2 id="-free-style-clock-face">⏰ Free-style clock face</h2>

<p>For the take home assignment my idea was to take Dalí’s concept one notch further leveraging the creative freedom provided to us by computers. What if the hands could <em>breathe</em>, extending and contracting as they sweep around the face? The original Pascal implementation is lost to time - perhaps fitting for a project about time itself - but the concept lives on, now reborn in JavaScript and React.</p>

<p>The algorithm is deceptively simple:</p>
<ul>
  <li>Draw an arbitrary closed shape</li>
  <li>Place the center point of the clock somewhere inside that shape</li>
  <li>For each hand, calculate the distance from the center to the boundary in the direction the hand is pointing</li>
  <li>Draw the hand with that calculated length</li>
  <li>Repeat sixty times per second</li>
</ul>

<p>Let’s dig into the implementation, shall we?</p>

<h2 id="-ray-casting">📐 Ray Casting</h2>

<p>The heart of the algorithm is ray casting. Given a point (the center) and an angle (where the hand is pointing), we need to find where a ray in that direction intersects our custom boundary. Here’s the core function:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">getIntersectionDistance</span><span class="p">(</span><span class="nx">center</span><span class="p">,</span> <span class="nx">angle</span><span class="p">,</span> <span class="nx">boundaryPoints</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="nx">radians</span> <span class="o">=</span> <span class="p">(</span><span class="nx">angle</span> <span class="o">-</span> <span class="mi">90</span><span class="p">)</span> <span class="o">*</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span> <span class="o">/</span> <span class="mi">180</span><span class="p">;</span>
  <span class="kd">const</span> <span class="nx">direction</span> <span class="o">=</span> <span class="p">{</span> <span class="na">x</span><span class="p">:</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">radians</span><span class="p">),</span> <span class="na">y</span><span class="p">:</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">radians</span><span class="p">)</span> <span class="p">};</span>
  
  <span class="kd">let</span> <span class="nx">minDistance</span> <span class="o">=</span> <span class="kc">Infinity</span><span class="p">;</span>
  
  <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">boundaryPoints</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">const</span> <span class="nx">p1</span> <span class="o">=</span> <span class="nx">boundaryPoints</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
    <span class="kd">const</span> <span class="nx">p2</span> <span class="o">=</span> <span class="nx">boundaryPoints</span><span class="p">[</span><span class="nx">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">];</span>
    
    <span class="kd">const</span> <span class="nx">distance</span> <span class="o">=</span> <span class="nx">raySegmentIntersection</span><span class="p">(</span><span class="nx">center</span><span class="p">,</span> <span class="nx">direction</span><span class="p">,</span> <span class="nx">p1</span><span class="p">,</span> <span class="nx">p2</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">distance</span> <span class="o">!==</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="nx">distance</span> <span class="o">&lt;</span> <span class="nx">minDistance</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">minDistance</span> <span class="o">=</span> <span class="nx">distance</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>
  
  <span class="k">return</span> <span class="nx">minDistance</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>For each segment of our boundary polygon, we check if the ray from the center intersects it. We keep track of the closest intersection - that’s how long our hand should be. The math here is good old linear algebra: solving for the intersection of a ray and a line segment. You might be more familiar with ray casting’s flashier cousin: <a href="https://en.wikipedia.org/wiki/Ray_tracing_(graphics)">ray tracing</a>. While ray tracing follows rays as they bounce around a scene to simulate realistic lighting and reflections, ray casting is simpler - we just need to find the first thing a ray hits and stop there.</p>

<h2 id="-drawing-the-boundary">🎯 Drawing the Boundary</h2>

<p>What makes this implementation interactive is the ability to draw your own clock face. Users can sketch any closed shape they like, and the clock adapts. The drawing logic tracks mouse or touch events to collect points as the user draws, then validates that the shape forms a proper closed loop around the clock center:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">useBoundaryDrawing</span><span class="p">()</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">drawingPoints</span><span class="p">,</span> <span class="nx">setDrawingPoints</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">([]);</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">isDrawing</span><span class="p">,</span> <span class="nx">setIsDrawing</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
  <span class="kd">const</span> <span class="nx">startPoint</span> <span class="o">=</span> <span class="nx">useRef</span><span class="p">(</span><span class="kc">null</span><span class="p">);</span>

  <span class="kd">const</span> <span class="nx">startDrawing</span> <span class="o">=</span> <span class="p">(</span><span class="nx">point</span><span class="p">,</span> <span class="nx">center</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">distance</span><span class="p">(</span><span class="nx">point</span><span class="p">,</span> <span class="nx">center</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nx">MIN_DISTANCE</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
    <span class="nx">startPoint</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="nx">point</span><span class="p">;</span>
    <span class="nx">setDrawingPoints</span><span class="p">([</span><span class="nx">point</span><span class="p">]);</span>
    <span class="nx">setIsDrawing</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
  <span class="p">};</span>

  <span class="kd">const</span> <span class="nx">continueDrawing</span> <span class="o">=</span> <span class="p">(</span><span class="nx">point</span><span class="p">,</span> <span class="nx">center</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">isDrawing</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
    <span class="c1">// Keep points outside the minimum radius from center</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">distance</span><span class="p">(</span><span class="nx">point</span><span class="p">,</span> <span class="nx">center</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nx">MIN_DISTANCE</span><span class="p">)</span> <span class="p">{</span>
      <span class="kd">const</span> <span class="nx">angle</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">atan2</span><span class="p">(</span><span class="nx">point</span><span class="p">.</span><span class="nx">y</span> <span class="o">-</span> <span class="nx">center</span><span class="p">.</span><span class="nx">y</span><span class="p">,</span> <span class="nx">point</span><span class="p">.</span><span class="nx">x</span> <span class="o">-</span> <span class="nx">center</span><span class="p">.</span><span class="nx">x</span><span class="p">);</span>
      <span class="nx">point</span> <span class="o">=</span> <span class="p">{</span>
        <span class="na">x</span><span class="p">:</span> <span class="nx">center</span><span class="p">.</span><span class="nx">x</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">angle</span><span class="p">)</span> <span class="o">*</span> <span class="nx">MIN_DISTANCE</span><span class="p">,</span>
        <span class="na">y</span><span class="p">:</span> <span class="nx">center</span><span class="p">.</span><span class="nx">y</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">angle</span><span class="p">)</span> <span class="o">*</span> <span class="nx">MIN_DISTANCE</span>
      <span class="p">};</span>
    <span class="p">}</span>
    <span class="nx">setDrawingPoints</span><span class="p">(</span><span class="nx">prev</span> <span class="o">=&gt;</span> <span class="p">[...</span><span class="nx">prev</span><span class="p">,</span> <span class="nx">point</span><span class="p">]);</span>
  <span class="p">};</span>

  <span class="kd">const</span> <span class="nx">endDrawing</span> <span class="o">=</span> <span class="p">(</span><span class="nx">center</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">isDrawing</span> <span class="o">||</span> <span class="o">!</span><span class="nx">startPoint</span><span class="p">.</span><span class="nx">current</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span>
    <span class="kd">const</span> <span class="nx">lastPoint</span> <span class="o">=</span> <span class="nx">drawingPoints</span><span class="p">[</span><span class="nx">drawingPoints</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
    <span class="c1">// Close the loop if end point is near start point</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">drawingPoints</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;=</span> <span class="nx">MIN_POINTS</span> <span class="o">&amp;&amp;</span> 
        <span class="nx">distance</span><span class="p">(</span><span class="nx">lastPoint</span><span class="p">,</span> <span class="nx">startPoint</span><span class="p">.</span><span class="nx">current</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="nx">CLOSE_THRESHOLD</span><span class="p">)</span> <span class="p">{</span>
      <span class="kd">const</span> <span class="nx">closedShape</span> <span class="o">=</span> <span class="p">[...</span><span class="nx">drawingPoints</span><span class="p">,</span> <span class="nx">startPoint</span><span class="p">.</span><span class="nx">current</span><span class="p">];</span>
      <span class="k">if</span> <span class="p">(</span><span class="nx">isPointInsidePolygon</span><span class="p">(</span><span class="nx">center</span><span class="p">,</span> <span class="nx">closedShape</span><span class="p">))</span> <span class="p">{</span>
        <span class="nx">createBoundary</span><span class="p">(</span><span class="nx">closedShape</span><span class="p">);</span>
      <span class="p">}</span>
    <span class="p">}</span>
    <span class="nx">setIsDrawing</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
    <span class="nx">setDrawingPoints</span><span class="p">([]);</span>
  <span class="p">};</span>

  <span class="k">return</span> <span class="p">{</span> <span class="nx">startDrawing</span><span class="p">,</span> <span class="nx">continueDrawing</span><span class="p">,</span> <span class="nx">endDrawing</span><span class="p">,</span> <span class="nx">drawingPoints</span><span class="p">,</span> <span class="nx">isDrawing</span> <span class="p">};</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The key validation here is <code class="language-plaintext highlighter-rouge">isPointInsidePolygon</code> - we only accept shapes that actually contain the clock center. Drawing a shape that excludes the center would result in hands pointing into the void, which wouldn’t make much sense for a clock.</p>

<h2 id="-caching-for-performance">🔄 Caching for Performance</h2>

<p>One optimization worth mentioning: recalculating ray intersections for every angle, every frame would be wasteful. Instead, when a boundary is drawn, we pre-calculate the maximum hand length for all 360 integer degrees and store them in a Map:</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">calculateAngleToRadius</span><span class="p">(</span><span class="nx">points</span><span class="p">,</span> <span class="nx">center</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="nx">angleToRadius</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Map</span><span class="p">();</span>
  
  <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">angle</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">angle</span> <span class="o">&lt;</span> <span class="mi">360</span><span class="p">;</span> <span class="nx">angle</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">const</span> <span class="nx">radius</span> <span class="o">=</span> <span class="nx">getIntersectionDistance</span><span class="p">(</span><span class="nx">center</span><span class="p">,</span> <span class="nx">angle</span><span class="p">,</span> <span class="nx">points</span><span class="p">);</span>
    <span class="nx">angleToRadius</span><span class="p">.</span><span class="kd">set</span><span class="p">(</span><span class="nx">angle</span><span class="p">,</span> <span class="nx">radius</span><span class="p">);</span>
  <span class="p">}</span>
  
  <span class="k">return</span> <span class="nx">angleToRadius</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>

<p>At runtime, we interpolate between adjacent cached values for smooth animation. This turns an O(n) operation per hand per frame into a simple Map lookup with some basic math.</p>

<h2 id="️-try-it-yourself">🖱️ Try It Yourself</h2>

<p>The best way to understand this clock is to play with it. Head over to <a href="https://kornafeld.com/clock/">kornafeld.com/clock</a> and draw your own clock face. Sketch a star, a heart, or Dalí’s melting blob itself. Drag the center point around and watch the hands stretch and shrink to fill the space.</p>

<p>What fascinates me about this project is how an idea conceived in a Pascal assignment decades ago finds new life in modern web technologies. The core algorithm hasn’t changed - it’s still ray casting and intersection math. But the delivery mechanism has transformed from a university assignment running on a local machine to an interactive web experience accessible to anyone with a browser.</p>

<h2 id="-connecting-the-dots">🔘 Connecting the dots</h2>

<p>It is indeed funny how life gives you dots and it’s up to you to connect them. <em>Or not</em>. This is me having a <em>great time</em> realizing how I subconsciously used Dalí’s painting as the cover of the first post of my blog in which I focus <em>mainly</em> on software engineering. Only to realize later that how that very painting ties back into the early days of my software endeavors at BME.</p>

<p>Also fascinating that it seems to me that this idea has not been leveraged much yet with the dawn of all the smart watches out there that have the necessary screens built in to support watch faces of dynamic shape. A missed opportunity?!</p>

<p>Dalí saw melting clocks in a piece of Camembert cheese warming in the sun. I saw them in the logical flexibility of computer graphics. Different inspirations, same liberation from the rigid march of mechanical time. And that wraps our coding session for today. Happy coding!</p>]]></content><author><name>Adam Kornafeld</name></author><category term="javascript" /><category term="react" /><category term="clock" /><category term="UI" /><category term="Dalí" /><category term="Salvador" /><category term="pascal" /><summary type="html"><![CDATA[When I was looking for a cover image for the very first post I almost instinctively reached out to one of my favorite paintings: The Persistence of Memory by Dalí. You might know it by the name: The Melting Clocks. It’s only now, years later, that it dawned on me why. The story says Dalí got his inspiration for the painting from the surreal way he once saw a piece of runny Camembert cheese melting in the Sun. The melting clocks represent the omnipresence of time, and identify its mastery over human beings. I feel lucky to have been able to witness this painting in real life at the MoMA in New York City. 🎥 How it all started I am fascinated by the concept of time. Don’t really know (yet) why, but I always have been. Back when I was a freshman at the university - Budapest University of Technology and Economics or BME that is - the gateway drug language into software engineering they taught us was Pascal. I know, right?! I feel old. During the first semester everyone had to pick an idea and implement it as their take home assignment over the course of those few months. When people think about Pascal, command line applications usually first pop in mind. Not for me though. Excited to get my hands dirty with real software engineering - after having been hacking at it on my own well before university - I wanted to jump in the deep. So I picked a graphics problem. An idea that I had for some time that found its roots in Dalí’s painting and that I lacked the know-how on how to execute it up until that time. See, mechanical clocks, watches and timepieces have a major limitation in my eyes. The shape of them are mainly influenced by the length of their hour, minute and second hands. The longest hand sets the minimum of the radius the clock face can have. Anything smaller and the face would not be able to complete a full circle without bumping into the wall of the clock face. Or worse run off the clock face. Sure, you see rectangular clock faces every now and then. But that’s usually the farthest the imagination of the creator goes. Enter Dalí. He was first to break down this barrier raised by the physical limitations presented by watch hands in his painting. However, he was cheating or more like lucky in a sense that through his painting he was able to freeze time so he didn’t have to worry about the length of the clock hands. ⏰ Free-style clock face For the take home assignment my idea was to take Dalí’s concept one notch further leveraging the creative freedom provided to us by computers. What if the hands could breathe, extending and contracting as they sweep around the face? The original Pascal implementation is lost to time - perhaps fitting for a project about time itself - but the concept lives on, now reborn in JavaScript and React. The algorithm is deceptively simple: Draw an arbitrary closed shape Place the center point of the clock somewhere inside that shape For each hand, calculate the distance from the center to the boundary in the direction the hand is pointing Draw the hand with that calculated length Repeat sixty times per second Let’s dig into the implementation, shall we? 📐 Ray Casting The heart of the algorithm is ray casting. Given a point (the center) and an angle (where the hand is pointing), we need to find where a ray in that direction intersects our custom boundary. Here’s the core function: function getIntersectionDistance(center, angle, boundaryPoints) { const radians = (angle - 90) * Math.PI / 180; const direction = { x: Math.cos(radians), y: Math.sin(radians) }; let minDistance = Infinity; for (let i = 0; i &lt; boundaryPoints.length - 1; i++) { const p1 = boundaryPoints[i]; const p2 = boundaryPoints[i + 1]; const distance = raySegmentIntersection(center, direction, p1, p2); if (distance !== null &amp;&amp; distance &lt; minDistance) { minDistance = distance; } } return minDistance; } For each segment of our boundary polygon, we check if the ray from the center intersects it. We keep track of the closest intersection - that’s how long our hand should be. The math here is good old linear algebra: solving for the intersection of a ray and a line segment. You might be more familiar with ray casting’s flashier cousin: ray tracing. While ray tracing follows rays as they bounce around a scene to simulate realistic lighting and reflections, ray casting is simpler - we just need to find the first thing a ray hits and stop there. 🎯 Drawing the Boundary What makes this implementation interactive is the ability to draw your own clock face. Users can sketch any closed shape they like, and the clock adapts. The drawing logic tracks mouse or touch events to collect points as the user draws, then validates that the shape forms a proper closed loop around the clock center: function useBoundaryDrawing() { const [drawingPoints, setDrawingPoints] = useState([]); const [isDrawing, setIsDrawing] = useState(false); const startPoint = useRef(null); const startDrawing = (point, center) =&gt; { if (distance(point, center) &lt; MIN_DISTANCE) return; startPoint.current = point; setDrawingPoints([point]); setIsDrawing(true); }; const continueDrawing = (point, center) =&gt; { if (!isDrawing) return; // Keep points outside the minimum radius from center if (distance(point, center) &lt; MIN_DISTANCE) { const angle = Math.atan2(point.y - center.y, point.x - center.x); point = { x: center.x + Math.cos(angle) * MIN_DISTANCE, y: center.y + Math.sin(angle) * MIN_DISTANCE }; } setDrawingPoints(prev =&gt; [...prev, point]); }; const endDrawing = (center) =&gt; { if (!isDrawing || !startPoint.current) return; const lastPoint = drawingPoints[drawingPoints.length - 1]; // Close the loop if end point is near start point if (drawingPoints.length &gt;= MIN_POINTS &amp;&amp; distance(lastPoint, startPoint.current) &lt;= CLOSE_THRESHOLD) { const closedShape = [...drawingPoints, startPoint.current]; if (isPointInsidePolygon(center, closedShape)) { createBoundary(closedShape); } } setIsDrawing(false); setDrawingPoints([]); }; return { startDrawing, continueDrawing, endDrawing, drawingPoints, isDrawing }; } The key validation here is isPointInsidePolygon - we only accept shapes that actually contain the clock center. Drawing a shape that excludes the center would result in hands pointing into the void, which wouldn’t make much sense for a clock. 🔄 Caching for Performance One optimization worth mentioning: recalculating ray intersections for every angle, every frame would be wasteful. Instead, when a boundary is drawn, we pre-calculate the maximum hand length for all 360 integer degrees and store them in a Map: function calculateAngleToRadius(points, center) { const angleToRadius = new Map(); for (let angle = 0; angle &lt; 360; angle++) { const radius = getIntersectionDistance(center, angle, points); angleToRadius.set(angle, radius); } return angleToRadius; } At runtime, we interpolate between adjacent cached values for smooth animation. This turns an O(n) operation per hand per frame into a simple Map lookup with some basic math. 🖱️ Try It Yourself The best way to understand this clock is to play with it. Head over to kornafeld.com/clock and draw your own clock face. Sketch a star, a heart, or Dalí’s melting blob itself. Drag the center point around and watch the hands stretch and shrink to fill the space. What fascinates me about this project is how an idea conceived in a Pascal assignment decades ago finds new life in modern web technologies. The core algorithm hasn’t changed - it’s still ray casting and intersection math. But the delivery mechanism has transformed from a university assignment running on a local machine to an interactive web experience accessible to anyone with a browser. 🔘 Connecting the dots It is indeed funny how life gives you dots and it’s up to you to connect them. Or not. This is me having a great time realizing how I subconsciously used Dalí’s painting as the cover of the first post of my blog in which I focus mainly on software engineering. Only to realize later that how that very painting ties back into the early days of my software endeavors at BME. Also fascinating that it seems to me that this idea has not been leveraged much yet with the dawn of all the smart watches out there that have the necessary screens built in to support watch faces of dynamic shape. A missed opportunity?! Dalí saw melting clocks in a piece of Camembert cheese warming in the sun. I saw them in the logical flexibility of computer graphics. Different inspirations, same liberation from the rigid march of mechanical time. And that wraps our coding session for today. Happy coding!]]></summary></entry><entry><title type="html">DIY Software Development Kit</title><link href="https://kornafeld.com/2022/09/19/gls_sdk.html" rel="alternate" type="text/html" title="DIY Software Development Kit" /><published>2022-09-19T00:00:00-04:00</published><updated>2022-09-19T00:00:00-04:00</updated><id>https://kornafeld.com/2022/09/19/gls_sdk</id><content type="html" xml:base="https://kornafeld.com/2022/09/19/gls_sdk.html"><![CDATA[<p>Today we are gonna implement an SDK in Python. The motivation being is that a close relative needed to integrate a web shop with the label printing service of GLS Parcel. GLS has a REST-like API, but they only publish sample code in a handful of languages that excludes Python. I thought this would be a perfect opportunity to demonstrate how to go from <em>zero to hero</em> in a situation that a service provider publishes an <a href="https://api.mygls.hu/index_en.html">API</a>, but no SDK in your favorite language.</p>

<h2 id="-api-documentation">📄 API Documentation</h2>

<p>GLS provides a decent <a href="https://api.mygls.hu/docs/mygls_api_20220617.pdf">documentation</a> for their API. They also provide sample code in Java, PHP and C#. Personally, I always wonder why a service provider would stop at publishing only sample code. It should not take too much effort to turn that sample code into a proper SDK and delight their users. 
Anyhow, reading through the docs it seems fairly straightforward. The API publishes a couple endpoints useful to create shipping labels. Those labels could then be fetched as a PDF to be printed on self adhesive paper.</p>

<p>A couple things to note that seem to be custom to the GLS API and not following RESTful specification or industry best practices:</p>
<ul>
  <li>all endpoints use the POST verb, even ones that fetch data</li>
  <li>all model properties use <a href="https://wiki.c2.com/?UpperCamelCase">upper camel case</a></li>
  <li>authentication is custom, included in the body of every request</li>
  <li>the label PDF is returned embedded in a JSON response encoded as an integer list of byte values</li>
</ul>

<h2 id="️-authentication">🗝️ Authentication</h2>

<p>Authentication takes care of the “Who are you” question when it comes to interacting with APIs. The counterpart to authentication is authorization that answers the “What can you do” question. As I mentioned, GLS solves the authentication in a custom manner. Every API request need to include key-value pairs for the <code class="language-plaintext highlighter-rouge">Username</code> and the <code class="language-plaintext highlighter-rouge">Password</code> as part of the request body. I guess this explains why does every resource use the <em>POST</em> verb. <em>GET</em> requests cannot have a request body. A better practice would be to supply the authentication data as a request header. However, being on the client side of the API our hands are tied and we can only use what the server gives us.</p>

<p>At least the password is not submitted as plain text. Rather, an SHA-512 hash needs to be calculated and the byte values of the hash digest make up the password value as a list of integers. We can implement the mapping logic with a helper method:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import hashlib

def calculate_password(self, plaintext: str) -&gt; list[int]:
    """
    Calculates password for API authentication
    """
    sha = hashlib.sha512()
    sha.update(plaintext.encode("utf-8"))
    return list(sha.digest())
</code></pre></div></div>

<h2 id="-timestamps">⏳ Timestamps</h2>

<p>There is no shipping without pickup and delivery dates. The GLS server expects each date and time in a custom format represented as timestamps, e.g.: midnight of September 19, 2022 for example is represented by <code class="language-plaintext highlighter-rouge">/Date(1663538400000)/</code>. That is the unix timestamp multiplied by 1000. I guess they wanted the flexibility to represent sub-second times, but alas never needed it so far. Following the single responsibility principle - one of the five <a href="https://en.wikipedia.org/wiki/SOLID">SOLID</a> principles - we can implement two small helper methods. One that takes care of the date to timestamp conversion and another that wraps the timestamp with the <code class="language-plaintext highlighter-rouge">/Date()/</code> marker:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>def convert_to_timestamp(date: datetime) -&gt; int:
    """
    Converts date object to timestamp
    """
    return int(datetime.timestamp(date)) * 1000

def convert_to_datefield(date: datetime) -&gt; str:
    """
    Converts date object to API format: /Date(timestamp)/
    """
    return f"/Date({convert_to_timestamp(date)})/"
</code></pre></div></div>

<p>One benefit of following the single responsibility principle is unit testing becomes a breeze:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import unittest
from datetime import datetime

class Test(unittest.TestCase):

    def test_convert_to_timestamp(self):
        date = datetime(2022, 9, 19, 0, 0)
        self.assertEqual(1663538400000, convert_to_timestamp(date))

    def test_convert_to_datefield(self):
        date = datetime(2022, 9, 19, 0, 0)
        self.assertEqual("/Date(1663538400000)/", convert_to_datefield(date))
</code></pre></div></div>

<p>One thing to mention about testing is that we should include test cases for edge cases. For example, what if the input date is <code class="language-plaintext highlighter-rouge">None</code>? Since we are using type hints, we can attribute a <code class="language-plaintext highlighter-rouge">null</code> input as user error. In production code, though, it is good practice to handle the unexpected.</p>

<h2 id="️-model-classes">🏛️ Model Classes</h2>

<p>To represent real world objects we need to implement some model classes. When it comes to shipping address and parcel would be two great examples. In python, a dictionary can be thought as a general purpose model implementation. However, when implementing an SDK it makes good sense to ensure that data is in the right format, so we would definitely add validation to the mix. Can be done manually, but why reinvent the wheel when there are great libraries out there, like <a href="https://docs.pydantic.dev/">pydantic</a> that already solves this problem.</p>

<h3 id="address">Address</h3>

<p>The following class describes an address, taking advantage of pydantic’s drop in replacement for data classes. The reason I opted for pydantic vs vanilla dataclasses is that the latter does not have built in support for serialization, while the former does. As you can see, the property names of an address are straightforward and so are their types.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from pydantic.dataclasses import dataclass
from typing import Optional


@dataclass
class Address:
    City: str
    ContactEmail: Optional[str]
    ContactName: Optional[str]
    ContactPhone: Optional[str]
    CountryIsoCode: str
    HouseNumber: str
    Name: str
    Street: str
    ZipCode: str
    HouseNumberInfo: Optional[str]
</code></pre></div></div>

<h3 id="parcel">Parcel</h3>

<p>A parcel model captures all pieces of data that go on a printed label necessary for not only successful pickup, but a flawless delivery.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>from pydantic.dataclasses import dataclass
from typing import Optional
from .address import Address
from .service import Service


@dataclass
class Parcel:
    ClientNumber: int
    ClientReference: Optional[str]
    Count: int
    DeliveryAddress: Address
    PickupAddress: Address
    PickupDate: str
    ServiceList: list[Service]
    Content: Optional[str] = ""
    CODAmount: Optional[float] = 0
    CODReference: Optional[str] = ""
</code></pre></div></div>

<p>There are some additional model classes here that I won’t include in this article, but you can find them in the GitHub <a href="https://github.com/adamkornafeld/mygls-python">repo</a>.</p>

<h2 id="️-rest-call">🖥️ REST call</h2>

<p>Once we have our model classes and can represent a parcel it is time to create that label. This operation will involve two network calls and persisting the downloaded PDF on disk:</p>
<ul>
  <li>prepare label</li>
  <li>get printed label</li>
  <li>save PDF
For the network I/O I will be using the popular <a href="https://requests.readthedocs.io/en/latest/">requests</a> library.</li>
</ul>

<h3 id="prepare-label">Prepare Label</h3>

<p>To prepare a label, we have to send a POST request to the <code class="language-plaintext highlighter-rouge">/PrepareLabels</code> endpoint and include at least one parcel in the body of the request:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>def prepare_labels(self, parcels: list[Parcel]) -&gt; PrepareLabelsResponse:
    """
    Prepares labels for printing
    """
    payload = self._request_payload()
    payload["ParcelList"] = [asdict(p) for p in parcels]
    response = requests.post(
        f"{self.settings.api_root}/PrepareLabels",
        data=json.dumps(payload),
        headers=HEADERS,
        timeout=self.settings.timeout_seconds,
    )
    return PrepareLabelsResponse.__pydantic_model__.parse_raw(response.text)
</code></pre></div></div>

<h3 id="get-printed-label">Get Printed Label</h3>

<p>To download the PDF, we have to send a POST request to the <code class="language-plaintext highlighter-rouge">/GetPrintedLabels</code> endpoint and include the parcel ids in the body of the request that we got from the prepare labels step before:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>def get_printed_labels(
    self,
    parcel_ids: list[int],
    printer_type: PrinterType = PrinterType.THERMO,
    print_position: int = 1,
    show_dialog: bool = False,
) -&gt; PrintedLabelsResponse:
    """
    Gets printed labels
    """
    payload = self._request_payload()
    payload["ParcelIdList"] = parcel_ids
    payload["PrintPosition"] = print_position
    payload["ShowPrintDialog"] = 1 if show_dialog else 0
    payload["TypeOfPrinter"] = printer_type.value
    response = requests.post(
        f"{self.settings.api_root}/GetPrintedLabels",
        data=json.dumps(payload),
        headers=HEADERS,
        timeout=self.settings.timeout_seconds,
    )
    return PrintedLabelsResponse.__pydantic_model__.parse_raw(response.text)
</code></pre></div></div>

<h3 id="save-pdf">Save PDF</h3>

<p>Once we fetched the labels PDF using the <code class="language-plaintext highlighter-rouge">get_printed_labels</code> function, we can extract the PDF data from the response JSON and proceed with converting it to binary data and saving it to disk:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>...
printer_type: PrinterType = PrinterType.THERMO
labels = get_printed_labels(parcel_ids, printer_type)
label_data = labels.Labels
save_pdf(pdf_path, label_data)

def save_pdf(pdf_path: str, byte_list: list[int]) -&gt; None:
    data = bytes(byte_list)
    with open(pdf_path, "wb") as f:
        f.write(data)
    log.info(f"Saved parcel label to {pdf_path}")
</code></pre></div></div>

<p>Here is an example of how the resulting PDF would look like using the address of my alma mater as the pickup and delivery addresses:</p>

<p align="center">
  <img src="https://raw.githubusercontent.com/adamkornafeld/mygls-python/main/parcel.png" />
</p>

<h2 id="-publishing-the-sdk">📦 Publishing the SDK</h2>

<p>The main goal of our SDK is to make it very easy for someone to start using it. The standard Python package repository is <a href="https://pypi.org/">pypi.org</a>. We need to create a couple config files that describe our Python package and how it should be built and published.</p>

<p>Using the built in <code class="language-plaintext highlighter-rouge">setuptools</code> package, the old way of doing this was to create a <code class="language-plaintext highlighter-rouge">setup.py</code> file in the root of the project. The new approach is to include a <code class="language-plaintext highlighter-rouge">setup.cfg</code> file with the project description that effectively reduces the <code class="language-plaintext highlighter-rouge">setup.py</code> to a minimum:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import setuptools

if __name__ == "__main__":
    setuptools.setup()
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">setup.cfg</code> file would include all the necessary metadata like author, description, version, license, dependencies, project URLs, etc. In our case it would look something like <a href="https://github.com/adamkornafeld/mygls-python/blob/main/setup.cfg">this</a>. Finally, a <code class="language-plaintext highlighter-rouge">project.toml</code> file describes how to build the project:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[build-system]
requires = ["setuptools&gt;=61.0", "wheel"]
build-backend = "setuptools.build_meta"
</code></pre></div></div>

<p>Once the configuration files are in place, we can build the project using</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python -m build
</code></pre></div></div>

<p>The build artifacts are created under the <code class="language-plaintext highlighter-rouge">dist</code> folder and we can use <code class="language-plaintext highlighter-rouge">twine</code> to publish it to the Python Package Repository:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>twine upload -r pypi dist/*
</code></pre></div></div>

<p>Before targeting the live package index, it is good practice to first publish to the test index and double check whether everything looks good. Once you publish in the live index, the metadata can only be updated by publishing a new version.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>twine upload -r testpypi dist/*
</code></pre></div></div>

<h2 id="-conclusion">🏁 Conclusion</h2>

<p>The goal of this article is to give you an idea how the client side of an API - in our case the <a href="https://api.mygls.hu/index_en.html">MyGLS API</a> - can be turned into an SDK for a delightful user experience. You can find the full source code in <a href="https://github.com/adamkornafeld/mygls-python">this</a> GitHub repo and you can <code class="language-plaintext highlighter-rouge">pip install mygls-rest-client</code> from the pypi <a href="https://pypi.org/project/mygls-rest-client/">package repo</a>. Happy coding!</p>]]></content><author><name>Adam Kornafeld</name></author><category term="python" /><category term="sdk" /><category term="api" /><category term="rest" /><category term="parcel" /><category term="package" /><summary type="html"><![CDATA[Today we are gonna implement an SDK in Python. The motivation being is that a close relative needed to integrate a web shop with the label printing service of GLS Parcel. GLS has a REST-like API, but they only publish sample code in a handful of languages that excludes Python. I thought this would be a perfect opportunity to demonstrate how to go from zero to hero in a situation that a service provider publishes an API, but no SDK in your favorite language. 📄 API Documentation GLS provides a decent documentation for their API. They also provide sample code in Java, PHP and C#. Personally, I always wonder why a service provider would stop at publishing only sample code. It should not take too much effort to turn that sample code into a proper SDK and delight their users. Anyhow, reading through the docs it seems fairly straightforward. The API publishes a couple endpoints useful to create shipping labels. Those labels could then be fetched as a PDF to be printed on self adhesive paper. A couple things to note that seem to be custom to the GLS API and not following RESTful specification or industry best practices: all endpoints use the POST verb, even ones that fetch data all model properties use upper camel case authentication is custom, included in the body of every request the label PDF is returned embedded in a JSON response encoded as an integer list of byte values 🗝️ Authentication Authentication takes care of the “Who are you” question when it comes to interacting with APIs. The counterpart to authentication is authorization that answers the “What can you do” question. As I mentioned, GLS solves the authentication in a custom manner. Every API request need to include key-value pairs for the Username and the Password as part of the request body. I guess this explains why does every resource use the POST verb. GET requests cannot have a request body. A better practice would be to supply the authentication data as a request header. However, being on the client side of the API our hands are tied and we can only use what the server gives us. At least the password is not submitted as plain text. Rather, an SHA-512 hash needs to be calculated and the byte values of the hash digest make up the password value as a list of integers. We can implement the mapping logic with a helper method: import hashlib def calculate_password(self, plaintext: str) -&gt; list[int]: """ Calculates password for API authentication """ sha = hashlib.sha512() sha.update(plaintext.encode("utf-8")) return list(sha.digest()) ⏳ Timestamps There is no shipping without pickup and delivery dates. The GLS server expects each date and time in a custom format represented as timestamps, e.g.: midnight of September 19, 2022 for example is represented by /Date(1663538400000)/. That is the unix timestamp multiplied by 1000. I guess they wanted the flexibility to represent sub-second times, but alas never needed it so far. Following the single responsibility principle - one of the five SOLID principles - we can implement two small helper methods. One that takes care of the date to timestamp conversion and another that wraps the timestamp with the /Date()/ marker: def convert_to_timestamp(date: datetime) -&gt; int: """ Converts date object to timestamp """ return int(datetime.timestamp(date)) * 1000 def convert_to_datefield(date: datetime) -&gt; str: """ Converts date object to API format: /Date(timestamp)/ """ return f"/Date({convert_to_timestamp(date)})/" One benefit of following the single responsibility principle is unit testing becomes a breeze: import unittest from datetime import datetime class Test(unittest.TestCase): def test_convert_to_timestamp(self): date = datetime(2022, 9, 19, 0, 0) self.assertEqual(1663538400000, convert_to_timestamp(date)) def test_convert_to_datefield(self): date = datetime(2022, 9, 19, 0, 0) self.assertEqual("/Date(1663538400000)/", convert_to_datefield(date)) One thing to mention about testing is that we should include test cases for edge cases. For example, what if the input date is None? Since we are using type hints, we can attribute a null input as user error. In production code, though, it is good practice to handle the unexpected. 🏛️ Model Classes To represent real world objects we need to implement some model classes. When it comes to shipping address and parcel would be two great examples. In python, a dictionary can be thought as a general purpose model implementation. However, when implementing an SDK it makes good sense to ensure that data is in the right format, so we would definitely add validation to the mix. Can be done manually, but why reinvent the wheel when there are great libraries out there, like pydantic that already solves this problem. Address The following class describes an address, taking advantage of pydantic’s drop in replacement for data classes. The reason I opted for pydantic vs vanilla dataclasses is that the latter does not have built in support for serialization, while the former does. As you can see, the property names of an address are straightforward and so are their types. from pydantic.dataclasses import dataclass from typing import Optional @dataclass class Address: City: str ContactEmail: Optional[str] ContactName: Optional[str] ContactPhone: Optional[str] CountryIsoCode: str HouseNumber: str Name: str Street: str ZipCode: str HouseNumberInfo: Optional[str] Parcel A parcel model captures all pieces of data that go on a printed label necessary for not only successful pickup, but a flawless delivery. from pydantic.dataclasses import dataclass from typing import Optional from .address import Address from .service import Service @dataclass class Parcel: ClientNumber: int ClientReference: Optional[str] Count: int DeliveryAddress: Address PickupAddress: Address PickupDate: str ServiceList: list[Service] Content: Optional[str] = "" CODAmount: Optional[float] = 0 CODReference: Optional[str] = "" There are some additional model classes here that I won’t include in this article, but you can find them in the GitHub repo. 🖥️ REST call Once we have our model classes and can represent a parcel it is time to create that label. This operation will involve two network calls and persisting the downloaded PDF on disk: prepare label get printed label save PDF For the network I/O I will be using the popular requests library. Prepare Label To prepare a label, we have to send a POST request to the /PrepareLabels endpoint and include at least one parcel in the body of the request: def prepare_labels(self, parcels: list[Parcel]) -&gt; PrepareLabelsResponse: """ Prepares labels for printing """ payload = self._request_payload() payload["ParcelList"] = [asdict(p) for p in parcels] response = requests.post( f"{self.settings.api_root}/PrepareLabels", data=json.dumps(payload), headers=HEADERS, timeout=self.settings.timeout_seconds, ) return PrepareLabelsResponse.__pydantic_model__.parse_raw(response.text) Get Printed Label To download the PDF, we have to send a POST request to the /GetPrintedLabels endpoint and include the parcel ids in the body of the request that we got from the prepare labels step before: def get_printed_labels( self, parcel_ids: list[int], printer_type: PrinterType = PrinterType.THERMO, print_position: int = 1, show_dialog: bool = False, ) -&gt; PrintedLabelsResponse: """ Gets printed labels """ payload = self._request_payload() payload["ParcelIdList"] = parcel_ids payload["PrintPosition"] = print_position payload["ShowPrintDialog"] = 1 if show_dialog else 0 payload["TypeOfPrinter"] = printer_type.value response = requests.post( f"{self.settings.api_root}/GetPrintedLabels", data=json.dumps(payload), headers=HEADERS, timeout=self.settings.timeout_seconds, ) return PrintedLabelsResponse.__pydantic_model__.parse_raw(response.text) Save PDF Once we fetched the labels PDF using the get_printed_labels function, we can extract the PDF data from the response JSON and proceed with converting it to binary data and saving it to disk: ... printer_type: PrinterType = PrinterType.THERMO labels = get_printed_labels(parcel_ids, printer_type) label_data = labels.Labels save_pdf(pdf_path, label_data) def save_pdf(pdf_path: str, byte_list: list[int]) -&gt; None: data = bytes(byte_list) with open(pdf_path, "wb") as f: f.write(data) log.info(f"Saved parcel label to {pdf_path}") Here is an example of how the resulting PDF would look like using the address of my alma mater as the pickup and delivery addresses: 📦 Publishing the SDK The main goal of our SDK is to make it very easy for someone to start using it. The standard Python package repository is pypi.org. We need to create a couple config files that describe our Python package and how it should be built and published. Using the built in setuptools package, the old way of doing this was to create a setup.py file in the root of the project. The new approach is to include a setup.cfg file with the project description that effectively reduces the setup.py to a minimum: import setuptools if __name__ == "__main__": setuptools.setup() The setup.cfg file would include all the necessary metadata like author, description, version, license, dependencies, project URLs, etc. In our case it would look something like this. Finally, a project.toml file describes how to build the project: [build-system] requires = ["setuptools&gt;=61.0", "wheel"] build-backend = "setuptools.build_meta" Once the configuration files are in place, we can build the project using python -m build The build artifacts are created under the dist folder and we can use twine to publish it to the Python Package Repository: twine upload -r pypi dist/* Before targeting the live package index, it is good practice to first publish to the test index and double check whether everything looks good. Once you publish in the live index, the metadata can only be updated by publishing a new version. twine upload -r testpypi dist/* 🏁 Conclusion The goal of this article is to give you an idea how the client side of an API - in our case the MyGLS API - can be turned into an SDK for a delightful user experience. You can find the full source code in this GitHub repo and you can pip install mygls-rest-client from the pypi package repo. Happy coding!]]></summary></entry><entry><title type="html">Cloud, meet Desktop - a system design challenge</title><link href="https://kornafeld.com/2022/07/08/breaking_free_from_cloud.html" rel="alternate" type="text/html" title="Cloud, meet Desktop - a system design challenge" /><published>2022-07-08T00:00:00-04:00</published><updated>2022-07-08T00:00:00-04:00</updated><id>https://kornafeld.com/2022/07/08/breaking_free_from_cloud</id><content type="html" xml:base="https://kornafeld.com/2022/07/08/breaking_free_from_cloud.html"><![CDATA[<p>Let’s look at an interesting system design challenge that I recently came across. Given an ordinary cloud system comprised of multiple services. These services communicate over HTTP using RESTful APIs. Your mission - <a href="https://youtu.be/KlyLtJd-ArY?t=173">should you choose to accept it</a> - is to integrate the services of an external provider with the system. The provider had two offerings to integrate with. A modern RESTful API over HTTP and a desktop app providing a socket API.</p>

<p>It goes without question that from the perspective of a cloud system, the desktop app presents challenges that make the REST API route be the one without much consideration. Or so I thought…</p>

<h2 id="-sdk-vs-api">📦 SDK vs API</h2>

<p>As an online service provider, one has a handful of options to cater for customers who wish to interact with your service in a programmatic fashion. Two common approaches are an application programming interface (API) or a software development kit (SDK). Both have benefits and tradeoffs. Let’s take a look at them one by one.</p>

<p>The benefits of an API is that you only have to implement it in a single programming language. Therefore, it is usually the more economical choice. The downside, on the other hand, is that the clients wishing to interact with the API will have to implement the client side part for themselves. Quality documentation as well as a well tested implementation is essential. Otherwise, your help desk will be flooded with questions or worse your API users will look for another provider.</p>

<p>The benefits of an SDK is that your clients will <em>thank you</em>. It is the white glove service of the internet. You implement and give client side software to the users of your API or services that turn complex network I/O, authentication, data representation and transfer into simple function calls. Coupled with decent documentation, you will be lightyears ahead of your competition that only provides an API but no SDK. The downside, is that it takes more effort. You have to select the client side software languages you want to support and implement an SDK for <em>all</em> of them.</p>

<h2 id="️-desktop-api">🖥️ Desktop API</h2>

<p>…continuing my story of the integration with the external provider. For reasons outside my reach it turned out that the integration via the RESTful API is a dead end. So what do you do when the task at hand suddenly demands to integrate the services of a cloud provider with your own cloud system via a <em>desktop app</em>?! Since your whole system runs in the cloud, the first thought would be to:</p>
<ul>
  <li>deploy a virtual server with a desktop OS on it in the cloud</li>
  <li>install the desktop app on it</li>
  <li>open the right ports</li>
  <li>and call it a day.</li>
</ul>

<p>However, what if a user has to interact with the desktop app, say regularly log in? Remote desktop, VPN and all the extra hoops and complexity. Can we do something simpler?</p>

<h2 id="-desktop-vs-server">🆚 Desktop vs. Server</h2>

<p>Desktop and server are both just fancy terms for computers. There is usually one crucial difference between the two. When you think about your laptop as a desktop, you run around with it all day long. Connecting to the internet from a myriad of locations: home, office, coffee shop. In all these locations, chances are your laptop is assigned a new IP address. Even if your computer is a true desktop, chances are your home internet provider did not assign you a permanent IP address. A server, on the other hand, usually earns the title <em>server</em> by having a permanent IP address assigned to it. If you know the address, you can connect to that server.</p>

<p>Why do I call out this difference? If we can deploy a piece of software on the desktop, it can initiate a connection to the server on one side. On the other side, it can establish a local socket connection with the desktop app of the service provider we are integrating with. This piece of software acts as a simple proxy between our two components.</p>

<h2 id="-bi-directional-communication">🔁 Bi-directional Communication</h2>

<p>The only remaining question at hand is: what type of connection should the proxy app use to connect to the cloud system? As usual, it depends. And the question we have to answer is: How will data flow between the service provider (A) and our cloud system (B)? Since this is analogous with a cable there are really only two options. From A to B or from B to A. If the service provider sends data to the proxy app we can forward that data via simple HTTP requests. This makes sense since we mentioned that our cloud system is already using a RESTful communication. 
The more interesting scenario is the reverse one. What if we want to send data from our cloud system to the service provider? One approach is HTTP <a href="https://en.wikipedia.org/wiki/Push_technology#Long_polling">long polling</a>, but that is kind of an emulation. A more flexible approach is establishing a websocket connection. This gives us the option to communicate in both directions. The proxy app can initiate the connection to the cloud server and once the connection is established, the cloud server can send data to the proxy on demand. Here is a quick sketch of the setup:</p>

<p><img src="/assets/images/websocket_proxy.png" alt="WebSocket Proxy" /></p>

<h2 id="-wrap-up">🎁 Wrap up</h2>

<p>In hindsight this design seems trivial, but at the time it did not feel like one - when you code cloud server components all day long, the idea of a desktop app can seem far fetched - and definitely required some outside the box thinking. My favorite kind of thinking. Happy coding!</p>]]></content><author><name>Adam Kornafeld</name></author><category term="system" /><category term="design" /><category term="cloud" /><category term="sdk" /><category term="api" /><category term="server" /><category term="desktop" /><category term="websocket" /><summary type="html"><![CDATA[Let’s look at an interesting system design challenge that I recently came across. Given an ordinary cloud system comprised of multiple services. These services communicate over HTTP using RESTful APIs. Your mission - should you choose to accept it - is to integrate the services of an external provider with the system. The provider had two offerings to integrate with. A modern RESTful API over HTTP and a desktop app providing a socket API. It goes without question that from the perspective of a cloud system, the desktop app presents challenges that make the REST API route be the one without much consideration. Or so I thought… 📦 SDK vs API As an online service provider, one has a handful of options to cater for customers who wish to interact with your service in a programmatic fashion. Two common approaches are an application programming interface (API) or a software development kit (SDK). Both have benefits and tradeoffs. Let’s take a look at them one by one. The benefits of an API is that you only have to implement it in a single programming language. Therefore, it is usually the more economical choice. The downside, on the other hand, is that the clients wishing to interact with the API will have to implement the client side part for themselves. Quality documentation as well as a well tested implementation is essential. Otherwise, your help desk will be flooded with questions or worse your API users will look for another provider. The benefits of an SDK is that your clients will thank you. It is the white glove service of the internet. You implement and give client side software to the users of your API or services that turn complex network I/O, authentication, data representation and transfer into simple function calls. Coupled with decent documentation, you will be lightyears ahead of your competition that only provides an API but no SDK. The downside, is that it takes more effort. You have to select the client side software languages you want to support and implement an SDK for all of them. 🖥️ Desktop API …continuing my story of the integration with the external provider. For reasons outside my reach it turned out that the integration via the RESTful API is a dead end. So what do you do when the task at hand suddenly demands to integrate the services of a cloud provider with your own cloud system via a desktop app?! Since your whole system runs in the cloud, the first thought would be to: deploy a virtual server with a desktop OS on it in the cloud install the desktop app on it open the right ports and call it a day. However, what if a user has to interact with the desktop app, say regularly log in? Remote desktop, VPN and all the extra hoops and complexity. Can we do something simpler? 🆚 Desktop vs. Server Desktop and server are both just fancy terms for computers. There is usually one crucial difference between the two. When you think about your laptop as a desktop, you run around with it all day long. Connecting to the internet from a myriad of locations: home, office, coffee shop. In all these locations, chances are your laptop is assigned a new IP address. Even if your computer is a true desktop, chances are your home internet provider did not assign you a permanent IP address. A server, on the other hand, usually earns the title server by having a permanent IP address assigned to it. If you know the address, you can connect to that server. Why do I call out this difference? If we can deploy a piece of software on the desktop, it can initiate a connection to the server on one side. On the other side, it can establish a local socket connection with the desktop app of the service provider we are integrating with. This piece of software acts as a simple proxy between our two components. 🔁 Bi-directional Communication The only remaining question at hand is: what type of connection should the proxy app use to connect to the cloud system? As usual, it depends. And the question we have to answer is: How will data flow between the service provider (A) and our cloud system (B)? Since this is analogous with a cable there are really only two options. From A to B or from B to A. If the service provider sends data to the proxy app we can forward that data via simple HTTP requests. This makes sense since we mentioned that our cloud system is already using a RESTful communication. The more interesting scenario is the reverse one. What if we want to send data from our cloud system to the service provider? One approach is HTTP long polling, but that is kind of an emulation. A more flexible approach is establishing a websocket connection. This gives us the option to communicate in both directions. The proxy app can initiate the connection to the cloud server and once the connection is established, the cloud server can send data to the proxy on demand. Here is a quick sketch of the setup: 🎁 Wrap up In hindsight this design seems trivial, but at the time it did not feel like one - when you code cloud server components all day long, the idea of a desktop app can seem far fetched - and definitely required some outside the box thinking. My favorite kind of thinking. Happy coding!]]></summary></entry><entry><title type="html">How low can you go?</title><link href="https://kornafeld.com/2022/04/27/how-low-can-you-go.html" rel="alternate" type="text/html" title="How low can you go?" /><published>2022-04-27T00:00:00-04:00</published><updated>2022-04-27T00:00:00-04:00</updated><id>https://kornafeld.com/2022/04/27/how-low-can-you-go</id><content type="html" xml:base="https://kornafeld.com/2022/04/27/how-low-can-you-go.html"><![CDATA[<p>When it comes to having fun at work, simple games like rock, paper, scissors often get pulled out to put a twist on making decisions. Burrito or gyros for lunch? Who gets the ticket to tonight’s big game - <em>go Celtics</em> - offered up by a colleague who can’t make it? Today I am gonna talk about my favorite such game and we will also implement it in python. As of the writing of this article I am using python <code class="language-plaintext highlighter-rouge">3.8</code>.</p>

<h2 id="-the-game">🎱 The game</h2>

<p>The name of the game is <em>How low can you go</em>? Can be played by an arbitrary number of players that makes it suitable to be played in an office setting. The rules are dead simple: the lowest positive unique integer entry wins.
Let’s digest that sentence a bit.</p>
<ul>
  <li><em>Integer entry</em>: the game is played by picking numbers</li>
  <li><em>Lowest positive</em>: the lowest number you can pick is 1</li>
  <li><em>Unique</em>: two players picking the same number get eliminated</li>
</ul>

<p>What I love about this game is that the rules are so simple that first time players usually skimp over the details, and haphazardly pick 1 as their entry and end up losing. See, the biggest emphasis is on unique. If two or more players pick 1 as their entry, they all get eliminated. Picking a winner would continue to the next lowest entries and continue up until the first unique entry is found. What is especially funny about this game is that technically you can be the winner with an entry that does not fit into the category of low at all. You could enter the googol number - that is 1 followed by 100 zeros - and still be the winner, if all other players eliminated each other by picking shared numbers as their entries.</p>

<h2 id="️-example-round">▶️ Example round</h2>

<p>Let’s say there are 5 players. Each of them pick 1 random positive number. Player one picks 3, player two picks 1, player three picks 1, player four picks 2, player five picks 3. Player two and three picked the lowest numbers, however, their entries are not unique, so they eliminated each other. Player four picked 2 and no other player picked it that makes player four the winner. Players one and five both picked 3. Should player four picked a larger number, they would have eliminated each other.</p>

<h2 id="-code">🕹 Code</h2>

<p>Let’s try to code this simple game in python. As you will soon see, this makes a perfect pair coding problem in a job interview setting because the logic is very simple, yet there are some nuances that still make it an interesting coding challenge.</p>

<p>Let’s start with some scaffolding. To keep it simple this will be a console game, making the user interface text based. Now, choosing the console does not mean that the app does not need to consider basic <a href="https://developer.apple.com/design/human-interface-guidelines/patterns/onboarding">human interface guidelines</a>. To the contrary, the amount of thought one puts into the applications user interface can easily make or break the end result.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    <span class="k">def</span> <span class="nf">how_low_can_you_go</span><span class="p">(</span><span class="n">intro</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">'How low can you go'</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">'------------------'</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">intro</span><span class="p">:</span>
          <span class="k">print</span><span class="p">(</span><span class="s">"How to play: "</span><span class="p">)</span>
          <span class="k">print</span><span class="p">(</span><span class="s">"Lowest positive unique integer entry wins."</span><span class="p">)</span>
</code></pre></div></div>

<p>The method will be the entry point for the game. The game welcomes the player by printing the name of the game. Setting the boolean <code class="language-plaintext highlighter-rouge">intro</code> argument <code class="language-plaintext highlighter-rouge">true</code>, the game also prints a basic help on how to play. The flag comes in handy for returning players. They have played before, so seeing the rules again might not be of much use to them anymore. We will make use of this flag at the very end, where we ask the players if they would like to play another round. The game tends to be addictive.</p>

<h2 id="-ready-player-one">👾 Ready Player One</h2>

<p>In the first iteration, we will implement a single player game vs. the computer. This allows us to keep it super simple while also put some rudimentary artificial intelligence in place to make it not so easy but fun to play.</p>

<p>First, we have to figure out how many players there will be. <em>n</em> being the number of player, <em>n-1</em> players will be played by the computer and the human player will be player <em>n</em>. Input and output of console applications tend to be boilerplate so it makes sense to look for an existing solution. My goto choice in python is the <a href="https://click.palletsprojects.com/en/8.1.x/">Click</a> library. The name can be a bit confusing at first, as the mouse is usually not at hand in a console setting. However, once you learn that click is an acronym that stands for <em>Command Line Interface Creation Kit</em> it all makes good sense.</p>

<p>We prompt for the number of players and let click handle the chore of validating user input. Zero or negative number of players does not make much sense, ain’t that true?</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="kn">import</span> <span class="nn">click</span>

    <span class="p">...</span>
    <span class="n">num_players</span> <span class="o">=</span> <span class="n">click</span><span class="p">.</span><span class="n">prompt</span><span class="p">(</span><span class="s">"Number of players?"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">click</span><span class="p">.</span><span class="n">IntRange</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
</code></pre></div></div>

<p>Once we know how many players there are, we need to collect the guesses of the computer players. A simple array will do the trick.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">sleep</span>

    <span class="p">...</span>
    <span class="n">guesses</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">collect_guesses</span><span class="p">(</span><span class="n">num_players</span><span class="p">,</span> <span class="n">guesses</span><span class="p">)</span>
</code></pre></div></div>

<p>Method <code class="language-plaintext highlighter-rouge">collect_guesses</code> is where things start to get interesting. Following HIG, it makes sense to distinguish the case of 2 players vs. more players. For the 2 player case, there is only one computer player, whereas for more than two players, there are more than two computer players. For every computer player, we call the <code class="language-plaintext highlighter-rouge">create_guess_tuple</code> method that will generate a random entry for a player. For more than two players, we can spice up the user experience a bit by creating the illusion that the computer players are thinking super deep when they are picking their entries. To achieve this, we rely on the <code class="language-plaintext highlighter-rouge">sleep</code> method of the built in <code class="language-plaintext highlighter-rouge">time</code> package and the <code class="language-plaintext highlighter-rouge">progressbar</code> feature of click. We simply sleep a little for every computer player and show a progress bar in the meantime.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">def</span> <span class="nf">collect_guesses</span><span class="p">(</span><span class="n">num_players</span><span class="p">,</span> <span class="n">guesses</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">num_players</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> 
        <span class="n">guesses</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_guess_tuple</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
        <span class="k">print</span><span class="p">(</span><span class="sa">f</span><span class="s">"Player #1 has made their guess."</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">num_players</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">with</span> <span class="n">click</span><span class="p">.</span><span class="n">progressbar</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">num_players</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="sa">f</span><span class="s">"Players #1-#</span><span class="si">{</span><span class="n">num_players</span> <span class="o">-</span> <span class="mi">1</span><span class="si">}</span><span class="s"> guessing"</span><span class="p">,</span> <span class="n">show_eta</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">show_percent</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">players</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">player</span> <span class="ow">in</span> <span class="n">players</span><span class="p">:</span>
                <span class="n">guesses</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_guess_tuple</span><span class="p">(</span><span class="n">player</span><span class="p">))</span>
                <span class="n">sleep</span><span class="p">(</span><span class="mi">3</span> <span class="o">/</span> <span class="n">num_players</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="sa">f</span><span class="s">"Players #1-#</span><span class="si">{</span><span class="n">num_players</span> <span class="o">-</span> <span class="mi">1</span><span class="si">}</span><span class="s"> have made their guesses."</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="sa">f</span><span class="s">"You are player #</span><span class="si">{</span><span class="n">num_players</span><span class="si">}</span><span class="s">."</span><span class="p">)</span>
</code></pre></div></div>

<p>The central point of the game logic is implemented in method <code class="language-plaintext highlighter-rouge">create_guess_tuple</code>. The first observation we can make is that we need to generate a random number. However, to make the game more challenging to play, any random number will not do. We would like to achieve a distribution that is skewed towards lower numbers. Lucky for us, we chose python as our language for this exercise and it comes with third party packages to make solving any problem a breeze. Packages like <a href="https://numpy.org/">NumPy</a> certainly has skew functions that we could use here. However, NumPy is not the lightest of packages so at the same time it feels wrong to whip out a big gun for such a small thing as our little game at hand. Instead, we will achieve the skewing with a clever little trick. We will make the computer players play a different strategy using their indexes. Player 1 will pick a random number from smaller set of numbers, than player 2, 3 and so on. This simple logic will guarantee that some computer players will always pick low numbers close to 1. At the same time, we also make all computer players follow a different strategy that helps us avoid the situation of computer players constantly eliminating each other. Using the constant of <code class="language-plaintext highlighter-rouge">GUESS_MULTIPLIER</code> we can control how tight the computer players pick their entries. E.g.: 3 computer players, with a constant of 4. Computer player 1 will pick a random entry from the set of (1, 4). Computer player 2 will pick a random entry from the set of (1, 8). Computer player 3 will pick a random entry from the set of (1, 12). As you see, the chance of 3 computer players picking entries from 1 to 4 is triple that of numbers larger than 4, thus we have achieved skewing without having to rely on complex math and heavy external libraries.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">randint</span>
  <span class="n">GUESS_MULTIPLIER</span> <span class="o">=</span> <span class="mi">4</span>

  <span class="k">def</span> <span class="nf">create_guess_tuple</span><span class="p">(</span><span class="n">player</span><span class="p">):</span>
      <span class="k">return</span> <span class="p">(</span><span class="n">player</span><span class="p">,</span> <span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">player</span> <span class="o">*</span> <span class="n">GUESS_MULTIPLIER</span><span class="p">))</span>
</code></pre></div></div>

<p>Once computer players made their guesses, it is time to collect the human player’s entry:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="n">human_guess</span> <span class="o">=</span> <span class="n">click</span><span class="p">.</span><span class="n">prompt</span><span class="p">(</span><span class="s">"Your guess"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="n">click</span><span class="p">.</span><span class="n">IntRange</span><span class="p">(</span><span class="nb">min</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
  <span class="n">human_player</span> <span class="o">=</span> <span class="p">(</span><span class="n">num_players</span><span class="p">,</span> <span class="n">human_guess</span><span class="p">)</span>
  <span class="n">guesses</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">human_player</span><span class="p">)</span>
</code></pre></div></div>

<p>By this point, we have all the entries collected in the array <code class="language-plaintext highlighter-rouge">guesses</code>. All we have to do now is to evaluate the entries and pick a winner. First, we sort the entries ascending and offload the chore of picking the winner to method <code class="language-plaintext highlighter-rouge">evaluate_winner</code>.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="n">guesses</span><span class="p">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
  <span class="n">winner</span> <span class="o">=</span> <span class="n">evaluate_winner</span><span class="p">(</span><span class="n">guesses</span><span class="p">)</span>
</code></pre></div></div>

<p>To evaluate the winner, we have to consider all pairs of entries. For this we make use of the <code class="language-plaintext highlighter-rouge">combinations</code> function of the built-in <code class="language-plaintext highlighter-rouge">itertools</code> package, passing <code class="language-plaintext highlighter-rouge">2</code> as the second argument. We also rely on the power of a <code class="language-plaintext highlighter-rouge">Set</code> data structure to collect unique guesses and see if the entry under consideration has already been entered. For some reason people new to the software engineering game tend to have more difficulty grasping the power of the set data structure. Given its similarities with a <code class="language-plaintext highlighter-rouge">List</code> both falling under the category of collections, they tend to use <code class="language-plaintext highlighter-rouge">List</code> as their silver bullet when in need of a collection data structure. A <code class="language-plaintext highlighter-rouge">Set</code> by definition can only contain a certain element once, so if we pass in a list of <code class="language-plaintext highlighter-rouge">Set([1,2,2,1,1,2])</code> to a set constructor, the resulting set will be <code class="language-plaintext highlighter-rouge">{1,2}</code> thus achieving uniqueness. There is a big difference in the time complexity of lookup operations like <code class="language-plaintext highlighter-rouge">contains</code> when it comes to <code class="language-plaintext highlighter-rouge">Set</code> vs <code class="language-plaintext highlighter-rouge">Array</code>. The former can do the trick in constant time, making it the default choice when it comes to solving for the problem of ‘is this element a member of this collection’?</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">combinations</span>

  <span class="k">def</span> <span class="nf">evaluate_winner</span><span class="p">(</span><span class="n">guesses</span><span class="p">):</span>
      <span class="n">unique_guesses</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
      <span class="n">winner</span> <span class="o">=</span> <span class="bp">None</span>
      <span class="k">for</span> <span class="p">(</span><span class="n">guess_left</span><span class="p">,</span> <span class="n">guess_right</span><span class="p">)</span> <span class="ow">in</span> <span class="n">combinations</span><span class="p">(</span><span class="n">guesses</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
          <span class="k">if</span> <span class="n">guess_left</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">guess_right</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">guess_left</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">in</span> <span class="n">unique_guesses</span><span class="p">:</span>
              <span class="n">winner</span> <span class="o">=</span> <span class="n">guess_left</span>
              <span class="k">break</span>
          <span class="n">unique_guesses</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">guess_left</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
      <span class="k">if</span> <span class="ow">not</span> <span class="n">winner</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">guesses</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="ow">in</span> <span class="n">unique_guesses</span><span class="p">:</span> <span class="n">winner</span> <span class="o">=</span> <span class="n">guesses</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
      <span class="k">return</span> <span class="n">winner</span>
</code></pre></div></div>

<p>Once we have a winner, all that is left is to make a big announcement. The final nuance of the game that I have not mentioned yet is that in rare circumstances all players can be eliminated in which case the game is a draw.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    <span class="p">...</span>
    <span class="n">show_result</span><span class="p">(</span><span class="n">guesses</span><span class="p">,</span> <span class="n">winner</span><span class="p">,</span> <span class="n">human_player</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">show_result</span><span class="p">(</span><span class="n">guesses</span><span class="p">,</span> <span class="n">winner</span><span class="p">,</span> <span class="n">human</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">""</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">guess</span> <span class="ow">in</span> <span class="n">guesses</span><span class="p">:</span>
            <span class="n">win</span> <span class="o">=</span> <span class="n">guess</span> <span class="o">==</span> <span class="n">winner</span>
            <span class="n">result</span> <span class="o">=</span> <span class="s">"wins"</span> <span class="k">if</span> <span class="n">win</span> <span class="k">else</span> <span class="s">"lost"</span>
            <span class="k">print</span><span class="p">(</span><span class="sa">f</span><span class="s">"Player #</span><span class="si">{</span><span class="s">'</span><span class="si">{:</span> <span class="o">&gt;</span><span class="mi">2</span><span class="si">}</span><span class="s">'.format(guess[0]) if len(guesses) &gt; 9 else guess[0]</span><span class="si">}</span><span class="s"> guess of </span><span class="si">{</span><span class="s">'</span><span class="si">{:</span> <span class="o">&gt;</span><span class="mi">2</span><span class="si">}</span><span class="s">'.format(guess[1])</span><span class="si">}</span><span class="s"> </span><span class="si">{</span><span class="n">result</span><span class="si">}</span><span class="s">"</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">winner</span><span class="p">:</span>
            <span class="k">print</span><span class="p">(</span><span class="s">"Game is a rare draw"</span><span class="p">)</span>
</code></pre></div></div>

<p>Finally, to cater for the game addicts and to frame my article with what I hinted at in the beginning we can offer the player to play one more round.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>    <span class="k">if</span> <span class="n">click</span><span class="p">.</span><span class="n">confirm</span><span class="p">(</span><span class="s">"Play another round?"</span><span class="p">)</span>
       <span class="n">how_low_can_you_go</span><span class="p">(</span><span class="n">false</span><span class="p">)</span>
</code></pre></div></div>

<p>Happy coding!</p>]]></content><author><name>Adam Kornafeld</name></author><category term="python," /><category term="fun," /><category term="game" /><summary type="html"><![CDATA[When it comes to having fun at work, simple games like rock, paper, scissors often get pulled out to put a twist on making decisions. Burrito or gyros for lunch? Who gets the ticket to tonight’s big game - go Celtics - offered up by a colleague who can’t make it? Today I am gonna talk about my favorite such game and we will also implement it in python. As of the writing of this article I am using python 3.8. 🎱 The game The name of the game is How low can you go? Can be played by an arbitrary number of players that makes it suitable to be played in an office setting. The rules are dead simple: the lowest positive unique integer entry wins. Let’s digest that sentence a bit. Integer entry: the game is played by picking numbers Lowest positive: the lowest number you can pick is 1 Unique: two players picking the same number get eliminated What I love about this game is that the rules are so simple that first time players usually skimp over the details, and haphazardly pick 1 as their entry and end up losing. See, the biggest emphasis is on unique. If two or more players pick 1 as their entry, they all get eliminated. Picking a winner would continue to the next lowest entries and continue up until the first unique entry is found. What is especially funny about this game is that technically you can be the winner with an entry that does not fit into the category of low at all. You could enter the googol number - that is 1 followed by 100 zeros - and still be the winner, if all other players eliminated each other by picking shared numbers as their entries. ▶️ Example round Let’s say there are 5 players. Each of them pick 1 random positive number. Player one picks 3, player two picks 1, player three picks 1, player four picks 2, player five picks 3. Player two and three picked the lowest numbers, however, their entries are not unique, so they eliminated each other. Player four picked 2 and no other player picked it that makes player four the winner. Players one and five both picked 3. Should player four picked a larger number, they would have eliminated each other. 🕹 Code Let’s try to code this simple game in python. As you will soon see, this makes a perfect pair coding problem in a job interview setting because the logic is very simple, yet there are some nuances that still make it an interesting coding challenge. Let’s start with some scaffolding. To keep it simple this will be a console game, making the user interface text based. Now, choosing the console does not mean that the app does not need to consider basic human interface guidelines. To the contrary, the amount of thought one puts into the applications user interface can easily make or break the end result. def how_low_can_you_go(intro: bool): print('How low can you go') print('------------------') if intro: print("How to play: ") print("Lowest positive unique integer entry wins.") The method will be the entry point for the game. The game welcomes the player by printing the name of the game. Setting the boolean intro argument true, the game also prints a basic help on how to play. The flag comes in handy for returning players. They have played before, so seeing the rules again might not be of much use to them anymore. We will make use of this flag at the very end, where we ask the players if they would like to play another round. The game tends to be addictive. 👾 Ready Player One In the first iteration, we will implement a single player game vs. the computer. This allows us to keep it super simple while also put some rudimentary artificial intelligence in place to make it not so easy but fun to play. First, we have to figure out how many players there will be. n being the number of player, n-1 players will be played by the computer and the human player will be player n. Input and output of console applications tend to be boilerplate so it makes sense to look for an existing solution. My goto choice in python is the Click library. The name can be a bit confusing at first, as the mouse is usually not at hand in a console setting. However, once you learn that click is an acronym that stands for Command Line Interface Creation Kit it all makes good sense. We prompt for the number of players and let click handle the chore of validating user input. Zero or negative number of players does not make much sense, ain’t that true? import click ... num_players = click.prompt("Number of players?", type=click.IntRange(min=1)) Once we know how many players there are, we need to collect the guesses of the computer players. A simple array will do the trick. from time import sleep ... guesses = [] collect_guesses(num_players, guesses) Method collect_guesses is where things start to get interesting. Following HIG, it makes sense to distinguish the case of 2 players vs. more players. For the 2 player case, there is only one computer player, whereas for more than two players, there are more than two computer players. For every computer player, we call the create_guess_tuple method that will generate a random entry for a player. For more than two players, we can spice up the user experience a bit by creating the illusion that the computer players are thinking super deep when they are picking their entries. To achieve this, we rely on the sleep method of the built in time package and the progressbar feature of click. We simply sleep a little for every computer player and show a progress bar in the meantime. def collect_guesses(num_players, guesses): if num_players == 2: guesses.append(create_guess_tuple(1)) print(f"Player #1 has made their guess.") elif num_players &gt; 2: with click.progressbar(range(1, num_players), label=f"Players #1-#{num_players - 1} guessing", show_eta=False, show_percent=False) as players: for player in players: guesses.append(create_guess_tuple(player)) sleep(3 / num_players) print(f"Players #1-#{num_players - 1} have made their guesses.") print(f"You are player #{num_players}.") The central point of the game logic is implemented in method create_guess_tuple. The first observation we can make is that we need to generate a random number. However, to make the game more challenging to play, any random number will not do. We would like to achieve a distribution that is skewed towards lower numbers. Lucky for us, we chose python as our language for this exercise and it comes with third party packages to make solving any problem a breeze. Packages like NumPy certainly has skew functions that we could use here. However, NumPy is not the lightest of packages so at the same time it feels wrong to whip out a big gun for such a small thing as our little game at hand. Instead, we will achieve the skewing with a clever little trick. We will make the computer players play a different strategy using their indexes. Player 1 will pick a random number from smaller set of numbers, than player 2, 3 and so on. This simple logic will guarantee that some computer players will always pick low numbers close to 1. At the same time, we also make all computer players follow a different strategy that helps us avoid the situation of computer players constantly eliminating each other. Using the constant of GUESS_MULTIPLIER we can control how tight the computer players pick their entries. E.g.: 3 computer players, with a constant of 4. Computer player 1 will pick a random entry from the set of (1, 4). Computer player 2 will pick a random entry from the set of (1, 8). Computer player 3 will pick a random entry from the set of (1, 12). As you see, the chance of 3 computer players picking entries from 1 to 4 is triple that of numbers larger than 4, thus we have achieved skewing without having to rely on complex math and heavy external libraries. from random import randint GUESS_MULTIPLIER = 4 def create_guess_tuple(player): return (player, randint(1, player * GUESS_MULTIPLIER)) Once computer players made their guesses, it is time to collect the human player’s entry: human_guess = click.prompt("Your guess", type=click.IntRange(min=1)) human_player = (num_players, human_guess) guesses.append(human_player) By this point, we have all the entries collected in the array guesses. All we have to do now is to evaluate the entries and pick a winner. First, we sort the entries ascending and offload the chore of picking the winner to method evaluate_winner. guesses.sort(key = lambda x: x[1]) winner = evaluate_winner(guesses) To evaluate the winner, we have to consider all pairs of entries. For this we make use of the combinations function of the built-in itertools package, passing 2 as the second argument. We also rely on the power of a Set data structure to collect unique guesses and see if the entry under consideration has already been entered. For some reason people new to the software engineering game tend to have more difficulty grasping the power of the set data structure. Given its similarities with a List both falling under the category of collections, they tend to use List as their silver bullet when in need of a collection data structure. A Set by definition can only contain a certain element once, so if we pass in a list of Set([1,2,2,1,1,2]) to a set constructor, the resulting set will be {1,2} thus achieving uniqueness. There is a big difference in the time complexity of lookup operations like contains when it comes to Set vs Array. The former can do the trick in constant time, making it the default choice when it comes to solving for the problem of ‘is this element a member of this collection’? from itertools import combinations def evaluate_winner(guesses): unique_guesses = set() winner = None for (guess_left, guess_right) in combinations(guesses, 2): if guess_left[1] != guess_right[1] and not guess_left[1] in unique_guesses: winner = guess_left break unique_guesses.add(guess_left[1]) if not winner and not guesses[-1][1] in unique_guesses: winner = guesses[-1] return winner Once we have a winner, all that is left is to make a big announcement. The final nuance of the game that I have not mentioned yet is that in rare circumstances all players can be eliminated in which case the game is a draw. ... show_result(guesses, winner, human_player) def show_result(guesses, winner, human): print("") for guess in guesses: win = guess == winner result = "wins" if win else "lost" print(f"Player #{'{: &gt;2}'.format(guess[0]) if len(guesses) &gt; 9 else guess[0]} guess of {'{: &gt;2}'.format(guess[1])} {result}") if not winner: print("Game is a rare draw") Finally, to cater for the game addicts and to frame my article with what I hinted at in the beginning we can offer the player to play one more round. if click.confirm("Play another round?") how_low_can_you_go(false) Happy coding!]]></summary></entry><entry><title type="html">Earn, learn and have fun!</title><link href="https://kornafeld.com/2021/12/14/earn-learn-fun.html" rel="alternate" type="text/html" title="Earn, learn and have fun!" /><published>2021-12-14T00:00:00-05:00</published><updated>2021-12-14T00:00:00-05:00</updated><id>https://kornafeld.com/2021/12/14/earn-learn-fun</id><content type="html" xml:base="https://kornafeld.com/2021/12/14/earn-learn-fun.html"><![CDATA[<p>There is a mantra - earn, learn and have fun - that I heard a while ago that I have been using ever since as a leveling gauge. The idea being that as an employee one should consider whether working for a given company makes sense or it is time to move on. It provides this simple barometer that as long as you earn a living, learn something every day at work while you also enjoy it as in you are having fun then you are at the right place. However, if any of those three conditions makes you feel lacking then probably it is time to look around what the job market might have in store for you.</p>

<h2 id="-earn">💵 Earn</h2>

<p>Stating the obvious - the world we live in is material. Unless you hit the jackpot, you have to make a living. When you decide to work for an employer, you are technically selling the time you have on your hand (no pun intended). Regardless of the job requirements or the qualifications one has, at the core every employee is conducting a business transaction of selling time.</p>

<h2 id="-learn">🧑‍🎓 Learn</h2>

<p>While most occupations I can think of entail some level of learning, I feel lucky having chosen software engineering as my profession. It is widely regarded as a quickly evolving industry where you <em>have</em> to keep up with it, lest it will leave your outdated knowledge in the dust. 
A more interesting aspect of it is the choice one eventually has to make. Become an expert in a specific sub-domain or try to maintain an all rounded but not necessarily the deepest insight into a broader spectrum of the industry. As with most difficult questions in life, this is not a left or right one. There is no right answer. I for one, like to maintain an open attitude towards software engineering in general. At the end of the day, this profession is about solving problems. There are lots of tools out there to help you out. However, no one tool is a <a href="https://en.wikipedia.org/wiki/Silver_bullet">silver bullet</a> that will help you yield an ideal solution to any problem. For that reason, I keep learning every day and try hard not to use a hammer when it comes to driving screws.</p>

<h2 id="-have-fun">🕹 Have Fun</h2>

<p>I like to <a href="https://youtu.be/eJUnA0DKsvg?t=27">put</a> some skin in the game every once in a while. I usually challenge my colleagues for a pizza. Large, name your toppings. Some were skeptical in the beginning, but after I had that pie delivered to a colleague on a different continent it became a hallmark in no time.
My utmost favorite pastime at work is <a href="https://en.wikipedia.org/wiki/Pair_programming">pair programming</a>. I usually jump at any opportunity to pair-program with anyone knowing that there is always something to learn for at least one of the participants, but ideally for all. Recently, I am aiming to bring my pair programming game to the next level, by vowing to pair program with each and everyone at my current work place. And not just with engineers, but with HR, marketing and heck, even the CEO.</p>]]></content><author><name>Adam Kornafeld</name></author><category term="career," /><category term="fun," /><category term="earn," /><category term="learn" /><summary type="html"><![CDATA[There is a mantra - earn, learn and have fun - that I heard a while ago that I have been using ever since as a leveling gauge. The idea being that as an employee one should consider whether working for a given company makes sense or it is time to move on. It provides this simple barometer that as long as you earn a living, learn something every day at work while you also enjoy it as in you are having fun then you are at the right place. However, if any of those three conditions makes you feel lacking then probably it is time to look around what the job market might have in store for you. 💵 Earn Stating the obvious - the world we live in is material. Unless you hit the jackpot, you have to make a living. When you decide to work for an employer, you are technically selling the time you have on your hand (no pun intended). Regardless of the job requirements or the qualifications one has, at the core every employee is conducting a business transaction of selling time. 🧑‍🎓 Learn While most occupations I can think of entail some level of learning, I feel lucky having chosen software engineering as my profession. It is widely regarded as a quickly evolving industry where you have to keep up with it, lest it will leave your outdated knowledge in the dust. A more interesting aspect of it is the choice one eventually has to make. Become an expert in a specific sub-domain or try to maintain an all rounded but not necessarily the deepest insight into a broader spectrum of the industry. As with most difficult questions in life, this is not a left or right one. There is no right answer. I for one, like to maintain an open attitude towards software engineering in general. At the end of the day, this profession is about solving problems. There are lots of tools out there to help you out. However, no one tool is a silver bullet that will help you yield an ideal solution to any problem. For that reason, I keep learning every day and try hard not to use a hammer when it comes to driving screws. 🕹 Have Fun I like to put some skin in the game every once in a while. I usually challenge my colleagues for a pizza. Large, name your toppings. Some were skeptical in the beginning, but after I had that pie delivered to a colleague on a different continent it became a hallmark in no time. My utmost favorite pastime at work is pair programming. I usually jump at any opportunity to pair-program with anyone knowing that there is always something to learn for at least one of the participants, but ideally for all. Recently, I am aiming to bring my pair programming game to the next level, by vowing to pair program with each and everyone at my current work place. And not just with engineers, but with HR, marketing and heck, even the CEO.]]></summary></entry><entry><title type="html">The one that suppresses log messages</title><link href="https://kornafeld.com/2021/08/21/on-logging.html" rel="alternate" type="text/html" title="The one that suppresses log messages" /><published>2021-08-21T00:00:00-04:00</published><updated>2021-08-21T00:00:00-04:00</updated><id>https://kornafeld.com/2021/08/21/on-logging</id><content type="html" xml:base="https://kornafeld.com/2021/08/21/on-logging.html"><![CDATA[<p>In the <a href="https://kornafeld.com/2021/04/10/on-naming.html">previous post</a> we have implemented a <code class="language-plaintext highlighter-rouge">Clock</code> that is useful for deterministic testing of logic that depends on the passing of time. In this post, we will take a look at logic that is driven by time.</p>

<h2 id="-connecting-the-dots">✨ Connecting the dots</h2>

<p>In his 2005 Stanford Commencement Address, Steve Jobs famously told the story of <a href="https://youtu.be/UF8uR6Z6KLc?t=55">connecting the dots</a>. I feel lucky to have been able to connect some dots myself. When my focus shifted from mobile app development to backend coding, the lack of user interfaces were somewhat frustrating in the beginning. But soon, it dawned on me that there is indeed a user interface that most backend engineers will be staring at once their code is deployed in production. Whether this user interface is helpful and a joy to use, or a useless nightmare depends on us, engineers. This user interface is the log stream. Besides the API that your backend code connects to the outside world with, the stream of log messages will be your only window into the inner workings of your code once it has been deployed. And oh, you will be staring at it wondering what on earth is your code doing on a late-late Sunday night. Enough intro, let’s do some coding, shall we?</p>

<h2 id="-the-fallacy-of-log-messages">🍃 The fallacy of log messages</h2>

<p>Take a look at this piece of code:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (customer.exists(customerId)) {
    customerRepository.delete(customerId);
    log.info("{} deleted", customerId);
}
</code></pre></div></div>

<p>Perfect logic, right? If the customer exists, we delete it. What’s wrong here, you might ask? Let’s say that the type of <code class="language-plaintext highlighter-rouge">customerId</code> is number and its value for the sake of this example is 5. The log message will rightfully read: ‘5 deleted’. Is that useful? Well, it might be if customer is the only type of entity in your system that can be deleted. If, however, there is also a product entity or god forbid a few hundred other entities in your system this message will not prove too useful. You might call me out here saying, but look at the code, it’s super obvious that we are deleting a customer. And you are right as long as you have the source code in front of you. And that is a fallacy I see quite a few backend engineers fall into. At the time of writing the business logic, everything is super logical in their head, so the amount of information they will put into the log message will be minimal. Detrimental even, if you have to figure out a bug on a Sunday night and all you have is this log message. But no source code.</p>

<p>You might ask, how can we <em>fix</em> the code above? Well, for starters we can include the type of entity in the log message:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (customer.exists(customerId)) {
    customerRepository.delete(customerId);
    log.info("Customer({}) deleted", customerId);
}
</code></pre></div></div>

<p>This would yield a message like ‘Customer(5) deleted’. Much better, but thinking about this with a user experience perspective we can take this one step further:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (customer.exists(customerId)) {
    customerRepository.delete(customerId);
    log.info("Customer(customerId={}) deleted", customerId);
}
</code></pre></div></div>

<p>Your ‘sleepy, on-call self’ will thank me at 3 am on a Sunday-night after reading the log message <code class="language-plaintext highlighter-rouge">Customer(customerId=5) deleted</code> and knowing exactly what’s going on here. Also, please invest in writing self-documenting code. One way of doing that is strategically placing debug messages, like so:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>if (customer.exists(customerId)) {
    customerRepository.delete(customerId);
    log.info("Customer(customerId={}) deleted", customerId);
} else {
    log.debug("Customer(customerId={}) does not exist, nothing to delete", customerId);
}
</code></pre></div></div>

<h2 id="-the-waterfall-of-log-messages">💦 The waterfall of log messages</h2>

<p>Another issue with log messages that I ran into before is that an ill-placed log message in a loop or a logic that gets called often floods the log stream. This renders it nigh impossible to extract anything useful from it. You suddenly feel like having to find the needle in the haystack. The simple solution to this is of course <em>‘let’s just not put log inside loops’</em>. However, that’s not always possible. Often times, at development time it might not even be obvious that the log message you implement will end up being executed frequently. So what can we do, if we find ourselves in a situation that we would like to see the log message, but also not flood the log stream with it?</p>

<h2 id="-log-suppressor">🪵 Log suppressor</h2>

<p>You might be lucky, that the log library you use provides support for that. I wasn’t, so let’s implement a log suppressor. A small utility class that you can wrap your log messages with and have it keep track of time to determine if it should log the message or swallow it. Naming our class is fairly straightforward this time. Call it <code class="language-plaintext highlighter-rouge">LogSuppressor</code> and feel lucky that we found a suitable name for our class on first try.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class LogSuppressor {
   
    private final Duration interval;

    public LogSuppressor(Duration interval) {
        this.interval = interval;
    }
}
</code></pre></div></div>

<p>By passing an <code class="language-plaintext highlighter-rouge">interval</code> at construction time, our class will emit only one log message per interval. The interval is fully configurable, from minutes, to days, even weeks. You name it. We also need to keep track of how much time has passed since a log message was last emitted. For that we will maintain an internal instance of <code class="language-plaintext highlighter-rouge">Instant</code>. We can initialize it with <code class="language-plaintext highlighter-rouge">Instant.MIN</code> to signal that our log suppressor has not suppressed anything yet.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class LogSuppressor {
   
    private final Duration interval;
    private Instant suppressedAt;

    public LogSuppressor(Duration interval) {
        this.interval = interval;
        this.suppressedAt = Instant.MIN;
    }
}
</code></pre></div></div>

<p>With those two properties in place we can implement the main function of our utility class: <code class="language-plaintext highlighter-rouge">LogSuppressor::suppress</code></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public boolean suppress(Runnable function) {
    Instant now = Instant.now();
    if (now.isAfter(suppressedAt)) {
        function.run();
        suppressedAt = now;
        return false;
    }
    return true;
}
</code></pre></div></div>

<p>That should be straightforward, we look at the current <em>instant</em> and see if it is after the instant when our log suppressor last suppressed a message. On first run, the condition will be true as the current instant of our clock will be after <code class="language-plaintext highlighter-rouge">Instant.MIN</code> and we run the function that we received as an input. This function wraps a log message. We also update the instant value of <code class="language-plaintext highlighter-rouge">suppressedAt</code> and return false signaling that this time the suppressor did not suppress. In all other cases, we return true. Let’s look at a usage example:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>LogSuppressor dailySuppressor = new LogSuppressor(Duration.ofDays(1));

dailySuppressor.suppress(() -&gt; log.info("This message will be logged once a day"));
</code></pre></div></div>

<p>Two nuances to recognize. One, restarting the app during the day will cause the log message to be emitted again, so it is possible for our log suppressor to log more than once a day. If need be, some persistence can be added to deal with that. Second, the input argument of the <code class="language-plaintext highlighter-rouge">suppress</code> function is a generic function. It is not limited to implement only log messages. You can technically use <code class="language-plaintext highlighter-rouge">LogSuppressor</code> to suppress any kind of business logic. My gut feeling, though, is that there might be side effects there, so I opt not to generify <code class="language-plaintext highlighter-rouge">LogSuppressor</code> as something like <code class="language-plaintext highlighter-rouge">FunctionSuppressor</code>.</p>

<h2 id="-testing-log-suppressor-">🪵 Testing log suppressor ⏱</h2>

<p>To frame this post, we can make one quick improvement by using the <code class="language-plaintext highlighter-rouge">ToyClock</code> from the <a href="https://kornafeld.com/2021/04/10/on-naming.html">previous post</a>. See, the passing of time is at the heart of our <code class="language-plaintext highlighter-rouge">LogSuppressor</code> and writing a deterministic unit test for it could be a challenge. Unless of course we have <code class="language-plaintext highlighter-rouge">ToyClock</code> with which we can tightly control the ticking of time. So let’s inject a clock into <code class="language-plaintext highlighter-rouge">LogSuppressor</code></p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>class LogSuppressor {
   
    private final Duration interval;
    private Instant suppressedAt;
    private final Clock clock;

    public LogSuppressor(Duration interval, Clock clock) {
        this.interval = interval;
        this.suppressedAt = Instant.MIN;
        this.clock = clock != null ? clock : Clock.systemUTC();
    }

    public LogSuppressor(Duration interval) {
        this(interval, Clock.systemUTC());
    }

}
</code></pre></div></div>

<p>There was a <em>buy one get one free</em> deal, so we threw in a convenience constructor as usually we will want to rely on the system clock. There is only one change we need to make to the implementation of the <code class="language-plaintext highlighter-rouge">suppress</code> function. That is inject the clock into <code class="language-plaintext highlighter-rouge">Instant::now</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Instant now = Instant.now(clock);
</code></pre></div></div>

<p>With the clock in place, testing the logic becomes a breeze:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@Test
void suppress() {
    Clock twentyOneSecondTickingClock = new ToyClock(Duration.ofMinutes(21));
    LogSuppressor minuteSuppressor = new LogSuppressor(Duration.ofMinutes(1), twentyOneSecondTickingClock);
    // every call to suppress ticks the toy clock injected into LogSuppressor once
    
    minuteSuppressor.suppress(() -&gt; log.info("This message will be logged")); // clock advances 21 seconds
    minuteSuppressor.suppress(() -&gt; log.info("This message will be suppressed")); // clock advances to 42 seconds, one minute has not elapsed yet, log is suppressed
    minuteSuppressor.suppress(() -&gt; log.info("This message will be logged")); // clock advances 63 seconds
}
</code></pre></div></div>

<p>And that wraps our coding session for today. Happy coding!</p>]]></content><author><name>Adam Kornafeld</name></author><category term="java" /><category term="log" /><category term="suppress" /><category term="user-interface" /><summary type="html"><![CDATA[In the previous post we have implemented a Clock that is useful for deterministic testing of logic that depends on the passing of time. In this post, we will take a look at logic that is driven by time. ✨ Connecting the dots In his 2005 Stanford Commencement Address, Steve Jobs famously told the story of connecting the dots. I feel lucky to have been able to connect some dots myself. When my focus shifted from mobile app development to backend coding, the lack of user interfaces were somewhat frustrating in the beginning. But soon, it dawned on me that there is indeed a user interface that most backend engineers will be staring at once their code is deployed in production. Whether this user interface is helpful and a joy to use, or a useless nightmare depends on us, engineers. This user interface is the log stream. Besides the API that your backend code connects to the outside world with, the stream of log messages will be your only window into the inner workings of your code once it has been deployed. And oh, you will be staring at it wondering what on earth is your code doing on a late-late Sunday night. Enough intro, let’s do some coding, shall we? 🍃 The fallacy of log messages Take a look at this piece of code: if (customer.exists(customerId)) { customerRepository.delete(customerId); log.info("{} deleted", customerId); } Perfect logic, right? If the customer exists, we delete it. What’s wrong here, you might ask? Let’s say that the type of customerId is number and its value for the sake of this example is 5. The log message will rightfully read: ‘5 deleted’. Is that useful? Well, it might be if customer is the only type of entity in your system that can be deleted. If, however, there is also a product entity or god forbid a few hundred other entities in your system this message will not prove too useful. You might call me out here saying, but look at the code, it’s super obvious that we are deleting a customer. And you are right as long as you have the source code in front of you. And that is a fallacy I see quite a few backend engineers fall into. At the time of writing the business logic, everything is super logical in their head, so the amount of information they will put into the log message will be minimal. Detrimental even, if you have to figure out a bug on a Sunday night and all you have is this log message. But no source code. You might ask, how can we fix the code above? Well, for starters we can include the type of entity in the log message: if (customer.exists(customerId)) { customerRepository.delete(customerId); log.info("Customer({}) deleted", customerId); } This would yield a message like ‘Customer(5) deleted’. Much better, but thinking about this with a user experience perspective we can take this one step further: if (customer.exists(customerId)) { customerRepository.delete(customerId); log.info("Customer(customerId={}) deleted", customerId); } Your ‘sleepy, on-call self’ will thank me at 3 am on a Sunday-night after reading the log message Customer(customerId=5) deleted and knowing exactly what’s going on here. Also, please invest in writing self-documenting code. One way of doing that is strategically placing debug messages, like so: if (customer.exists(customerId)) { customerRepository.delete(customerId); log.info("Customer(customerId={}) deleted", customerId); } else { log.debug("Customer(customerId={}) does not exist, nothing to delete", customerId); } 💦 The waterfall of log messages Another issue with log messages that I ran into before is that an ill-placed log message in a loop or a logic that gets called often floods the log stream. This renders it nigh impossible to extract anything useful from it. You suddenly feel like having to find the needle in the haystack. The simple solution to this is of course ‘let’s just not put log inside loops’. However, that’s not always possible. Often times, at development time it might not even be obvious that the log message you implement will end up being executed frequently. So what can we do, if we find ourselves in a situation that we would like to see the log message, but also not flood the log stream with it? 🪵 Log suppressor You might be lucky, that the log library you use provides support for that. I wasn’t, so let’s implement a log suppressor. A small utility class that you can wrap your log messages with and have it keep track of time to determine if it should log the message or swallow it. Naming our class is fairly straightforward this time. Call it LogSuppressor and feel lucky that we found a suitable name for our class on first try. class LogSuppressor { private final Duration interval; public LogSuppressor(Duration interval) { this.interval = interval; } } By passing an interval at construction time, our class will emit only one log message per interval. The interval is fully configurable, from minutes, to days, even weeks. You name it. We also need to keep track of how much time has passed since a log message was last emitted. For that we will maintain an internal instance of Instant. We can initialize it with Instant.MIN to signal that our log suppressor has not suppressed anything yet. class LogSuppressor { private final Duration interval; private Instant suppressedAt; public LogSuppressor(Duration interval) { this.interval = interval; this.suppressedAt = Instant.MIN; } } With those two properties in place we can implement the main function of our utility class: LogSuppressor::suppress public boolean suppress(Runnable function) { Instant now = Instant.now(); if (now.isAfter(suppressedAt)) { function.run(); suppressedAt = now; return false; } return true; } That should be straightforward, we look at the current instant and see if it is after the instant when our log suppressor last suppressed a message. On first run, the condition will be true as the current instant of our clock will be after Instant.MIN and we run the function that we received as an input. This function wraps a log message. We also update the instant value of suppressedAt and return false signaling that this time the suppressor did not suppress. In all other cases, we return true. Let’s look at a usage example: LogSuppressor dailySuppressor = new LogSuppressor(Duration.ofDays(1)); dailySuppressor.suppress(() -&gt; log.info("This message will be logged once a day")); Two nuances to recognize. One, restarting the app during the day will cause the log message to be emitted again, so it is possible for our log suppressor to log more than once a day. If need be, some persistence can be added to deal with that. Second, the input argument of the suppress function is a generic function. It is not limited to implement only log messages. You can technically use LogSuppressor to suppress any kind of business logic. My gut feeling, though, is that there might be side effects there, so I opt not to generify LogSuppressor as something like FunctionSuppressor. 🪵 Testing log suppressor ⏱ To frame this post, we can make one quick improvement by using the ToyClock from the previous post. See, the passing of time is at the heart of our LogSuppressor and writing a deterministic unit test for it could be a challenge. Unless of course we have ToyClock with which we can tightly control the ticking of time. So let’s inject a clock into LogSuppressor class LogSuppressor { private final Duration interval; private Instant suppressedAt; private final Clock clock; public LogSuppressor(Duration interval, Clock clock) { this.interval = interval; this.suppressedAt = Instant.MIN; this.clock = clock != null ? clock : Clock.systemUTC(); } public LogSuppressor(Duration interval) { this(interval, Clock.systemUTC()); } } There was a buy one get one free deal, so we threw in a convenience constructor as usually we will want to rely on the system clock. There is only one change we need to make to the implementation of the suppress function. That is inject the clock into Instant::now. Instant now = Instant.now(clock); With the clock in place, testing the logic becomes a breeze: @Test void suppress() { Clock twentyOneSecondTickingClock = new ToyClock(Duration.ofMinutes(21)); LogSuppressor minuteSuppressor = new LogSuppressor(Duration.ofMinutes(1), twentyOneSecondTickingClock); // every call to suppress ticks the toy clock injected into LogSuppressor once minuteSuppressor.suppress(() -&gt; log.info("This message will be logged")); // clock advances 21 seconds minuteSuppressor.suppress(() -&gt; log.info("This message will be suppressed")); // clock advances to 42 seconds, one minute has not elapsed yet, log is suppressed minuteSuppressor.suppress(() -&gt; log.info("This message will be logged")); // clock advances 63 seconds } And that wraps our coding session for today. Happy coding!]]></summary></entry><entry><title type="html">The one that implements a Clock for deterministic tests</title><link href="https://kornafeld.com/2021/07/10/on-naming.html" rel="alternate" type="text/html" title="The one that implements a Clock for deterministic tests" /><published>2021-07-10T00:00:00-04:00</published><updated>2021-07-10T00:00:00-04:00</updated><id>https://kornafeld.com/2021/07/10/on-naming</id><content type="html" xml:base="https://kornafeld.com/2021/07/10/on-naming.html"><![CDATA[<p>Is this thing on?! Hello World. It sounds about right to discuss naming in the first post. In software engineering naming is considered to be <em>hard</em>. So read on to learn how the name <em>Bit More</em> came to be. But first, let’s do some coding, shall we?!</p>

<p>One of these days I was facing some code - more about that in another post - that was reliant on the passing of time and in dire need of some tests thrown at. Java has a fairly straightforward way of representing time, namely <code class="language-plaintext highlighter-rouge">ZonedTime</code> and <code class="language-plaintext highlighter-rouge">LocalDateTime</code>. For simplicity’s sake, let’s focus on <code class="language-plaintext highlighter-rouge">LocalDateTime</code>. As long as your code deals with logic in the present all you need is <code class="language-plaintext highlighter-rouge">LocalDateTime::now</code> to get you the current date and time represented in the default time zone of the system.</p>

<p>Upon closer look, the <code class="language-plaintext highlighter-rouge">now</code> method comes in two flavors. One takes no arguments, the other takes a <code class="language-plaintext highlighter-rouge">Clock</code>. The former will use the default clock of the system in the background. A clock can give you the current instant. What current means depends on the clock you instantiate.</p>

<p>There are three options to choose from. We will see in a bit that neither perfectly suits our needs for testing. See, testing anything that depends on the passing of time becomes tricky quicker than the blink of an eye. A bulletproof solution is a challenge to say the least.</p>

<h2 id="vanilla-flavor---system-clock">Vanilla Flavor - System Clock🔋</h2>

<p>Flavor zero: <code class="language-plaintext highlighter-rouge">Clock::systemUTC</code> or <code class="language-plaintext highlighter-rouge">Clock::systemDefaultZone</code> represents a regular clock with its hands set at the current time, happily ticking as one expects clocks to be ticking.</p>

<h2 id="flavor-one---fixed-clock-">Flavor One - Fixed Clock 🔋🚫</h2>

<p>Flavor one: <code class="language-plaintext highlighter-rouge">Clock::fixed</code> represents a clock with its batteries out. You can set the hands to any hour, minute and second but the clock will be frozen in time. Any call to the <code class="language-plaintext highlighter-rouge">Clock::instant</code> method will yield the same point in time that you set during instantiation.</p>

<h2 id="flavor-two---offset-clock-">Flavor Two - Offset Clock 🕕</h2>

<p>Flavor two: <code class="language-plaintext highlighter-rouge">Clock::offset</code> takes inspiration from the previous two. You can set the hands and the clock will be ticking starting from that epoch you set. Let’s see this in action:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>LocalDateTime epoch = LocalDateTime.of(1983, 4, 10, 12, 30);
Clock baseClock = Clock.systemUTC();
Duration offset = Duration.between(LocalDateTime.now(clock), epoch);
Clock clock = Clock.offset(baseClock, epoch);
</code></pre></div></div>

<p>In the snippet above, <code class="language-plaintext highlighter-rouge">clock</code> represents a clock that started ticking on the 10th of April in 1983 at 12:30pm. Injecting this clock into <code class="language-plaintext highlighter-rouge">LocalDateTime</code> would yield the <code class="language-plaintext highlighter-rouge">epoch</code> value:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>LocalDateTime timeIn1983 = LocalDateTime.now(clock);
</code></pre></div></div>

<p>Call it 10 seconds later, it would yield the instant that is 10 seconds later but still in 1983.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> // Loiter for 10 seconds
 LocalDateTime tenSecondsLaterIn1983 = LocalDateTime.now(clock);
</code></pre></div></div>

<p>Apart from being able to wind this clock back into the past or forward into the future - or <a href="https://youtu.be/VLKDKWCWVXc?t=25">back to the future</a> 😉 -, it will tick as you expect a regular clock to tick - once every second.</p>

<p>Building upon the offset clock, we can create a clock that runs in the past or in the future, but with an interval that is different from the regular one second.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Clock fastClockIn1983 = Clock.tick(offsetClock, Duration.ofSeconds(5));
</code></pre></div></div>

<p>This clock will be advancing 5 seconds with every tick while a regular clock would advance only 1 second. We can go crazy with the duration value and create clocks that leap hours, days or even months at a time.</p>

<h2 id="using-clock-in-tests">Using Clock in tests</h2>

<p>With these flavors, we have a handful of options to choose from for our testing needs. However, the problem is that the flavors that represent running clocks - with the batteries in -, we have no control over when those ticks happen. So the test code that we implement will end up using <code class="language-plaintext highlighter-rouge">Thread.sleep</code> to pass time or will be running a loop for a set period. Neither of these options are ideal. The former will obviously make our test run longer than needed:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>for (int i = 0; i &lt; 5; i++) { // Loop for 5 seconds
    // do some business logic
    Thread.sleep(1000); // Let one second pass
}
</code></pre></div></div>

<p>The latter will yield nondeterministic results. The test will work most of the time, but every once in a while results might be flaky due to tiny nanosecond differences:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>LocalDateTime until = LocalDateTime.now().plusSeconds(5);
while (LocalDateTime.now().isBefore(until)) { // Loop for at least 5 seconds
    // do some business logic
}
</code></pre></div></div>

<h2 id="-toy-clock-">⏰ Toy Clock 🧸</h2>

<p>For the ultimate test, we would like to have <em>total control</em> over our clock including when the ticks happen. We have seen that the options that ship with Java do not provide such a clock out of the box, so we will have to get our hands dirty: let’s try to create such a clock.</p>

<p>Did I mention naming is <em>hard</em>? I usually don’t land on the perfect name immediately, so I will start out with a placeholder name and see if during implementation something better pops up. I am gonna go with <code class="language-plaintext highlighter-rouge">ToyClock</code>.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>public class ToyClock extends Clock {
     
    private final Clock baseClock;
    private final Duration tickInterval;
    private final AtomicLong tickCount = new AtomicLong(0);

    private TickingClock(LocalDateTime epoch, Duration tickInterval) {
        Clock fixedClock = Clock.fixed(epoch.toInstant(ZoneOffset.UTC), ZoneOffset.UTC);
        this.baseClock = Clock.offset(fixedClock, calculateOffset(fixedClock, epoch));
        this.tickInterval = tickInterval;
    }
     
}
</code></pre></div></div>

<p>In the snippet above, we are extending the abstract <code class="language-plaintext highlighter-rouge">Clock</code> to create our <code class="language-plaintext highlighter-rouge">ToyClock</code> class. For starters, let’s give it three properties. A <code class="language-plaintext highlighter-rouge">baseClock</code> serving as the foundation of our implementation. It is a fixed clock with the batteries 🔋 out. A <code class="language-plaintext highlighter-rouge">tickInterval</code> to control how long or short the ticks are. And finally, a counter to keep track of how many times did our clock tick as it will be ticking only <em>upon request</em>.</p>

<p>To extend <code class="language-plaintext highlighter-rouge">Clock</code> we have to override the abstract methods:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@Override
public ZoneId getZone() {
    return baseClock.getZone();
}

@Override
public Clock withZone(ZoneId zone) {
    return baseClock.withZone(zone);
}

@Override
public Instant instant() {
    return baseClock.instant()
            .plus(tickInterval.multipliedBy(tickCount.incrementAndGet()));
}
</code></pre></div></div>

<p>Functions, <code class="language-plaintext highlighter-rouge">getZone</code> and <code class="language-plaintext highlighter-rouge">withZone</code> are just <em>nuances</em>, we have to override these. Lucky for us, the <code class="language-plaintext highlighter-rouge">baseClock</code> makes their implementation trivial. The real action happens in <code class="language-plaintext highlighter-rouge">instant</code>. Here we once again rely on <code class="language-plaintext highlighter-rouge">baseClock</code> to get the current instant - which is always the same, frozen in time - and add <code class="language-plaintext highlighter-rouge">tickInterval</code> x <code class="language-plaintext highlighter-rouge">tickCount</code>. Leveraging the <code class="language-plaintext highlighter-rouge">AtomicLong</code> class, we maintain thread safety of our implementation by incrementing and getting the counter value as an atomic operation. A side effect of a call to the <code class="language-plaintext highlighter-rouge">instant</code> method will be one tick of our <code class="language-plaintext highlighter-rouge">ToyClock</code>. Notice that <code class="language-plaintext highlighter-rouge">tickInterval</code> can be positive or negative, meaning that we can make our clock run forward or backward. But the real beauty is that peeking into the implementation of <code class="language-plaintext highlighter-rouge">LocalDateTime::now</code> we see this:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>final Instant now = clock.instant();  // called once
</code></pre></div></div>

<p>So by injecting a <code class="language-plaintext highlighter-rouge">ToyClock</code> instance into <code class="language-plaintext highlighter-rouge">LocalDateTime::now</code> we can actually get a time value while also being able to control the ticking of our clock. A side effect of calling <code class="language-plaintext highlighter-rouge">LocalDateTime::now</code> is going to be exactly one tick of our clock. With this we can create test environments in which the passing of time is under <em>tight control</em> and yield deterministic outcomes, no matter how complex the time logic is. To demonstrate, let’s quickly implement a unit test of <code class="language-plaintext highlighter-rouge">ToyClock</code>. To make it fun, let’s have it tick <em>backwards</em> by 5 minutes.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>@Test
void tickBackward() {
    LocalDateTime epoch = LocalDateTime.now();
    int tickIntervalMinutes = -5;
    Clock clock = new ToyClock(epoch, Duration.ofMinutes(tickIntervalMinutes));
    for (int i = 0; i &lt; 3; i++) {
        LocalDateTime now = LocalDateTime.now(clock); // calls Clock#instant that causes the tick
        assertEquals((i+1) * tickIntervalMinutes, now.getMinute());
    }
}
</code></pre></div></div>

<p>Inside the for loop, the clock ticks exactly once for a total of 3 ticks, so at the end our time will be 15 seconds in the past. It turns out that no better name popped up, so I am gonna stick with <code class="language-plaintext highlighter-rouge">ToyClock</code> for now. And that’s enough coding for one day.</p>

<p>If you read this far, now you must be really curious how the name of the site, <em>Bit More</em>, came about. The idea is actually my <a href="https://www.instagram.com/cilamila/?hl=en">wife</a>’s. She is not a software engineer, so I am not surprised that she is better at this than I ever hope to be.</p>

<p>The startup that I work for was looking for a name and I was discussing this with her during dinner one day. We started throwing out ideas while having toast that evening. The toaster had a button labelled ‘Bit more’ with a function of giving the bread an extra 30 seconds of heat. The domain of the startup is finance, so when my wife grabbed the ready toasts from the toaster she lit up with joy shouting ‘this is it’. I did enter the name into the company’s name selection pool, but ultimately ‘Bit More’ did not make the final round. That left my wife a tad bit sad as she was convinced this is a great name. So about a year went by and I told her that I feel the need to start writing some sort of a blog and that I want to write about code. Without hesitation she jumped up and said: you have to name it <em>Bit More</em>.</p>

<p>Hungry for more time? It so happens that a good friend of mine, Attila, is also into time. He is an avid collector of Seiko quartz watches. Take a look at his impressive collection <a href="https://www.instagram.com/letscollectseikoquartz/">here</a>.</p>]]></content><author><name>Adam Kornafeld</name></author><category term="java" /><category term="clock" /><category term="time" /><category term="mock" /><summary type="html"><![CDATA[Is this thing on?! Hello World. It sounds about right to discuss naming in the first post. In software engineering naming is considered to be hard. So read on to learn how the name Bit More came to be. But first, let’s do some coding, shall we?! One of these days I was facing some code - more about that in another post - that was reliant on the passing of time and in dire need of some tests thrown at. Java has a fairly straightforward way of representing time, namely ZonedTime and LocalDateTime. For simplicity’s sake, let’s focus on LocalDateTime. As long as your code deals with logic in the present all you need is LocalDateTime::now to get you the current date and time represented in the default time zone of the system. Upon closer look, the now method comes in two flavors. One takes no arguments, the other takes a Clock. The former will use the default clock of the system in the background. A clock can give you the current instant. What current means depends on the clock you instantiate. There are three options to choose from. We will see in a bit that neither perfectly suits our needs for testing. See, testing anything that depends on the passing of time becomes tricky quicker than the blink of an eye. A bulletproof solution is a challenge to say the least. Vanilla Flavor - System Clock🔋 Flavor zero: Clock::systemUTC or Clock::systemDefaultZone represents a regular clock with its hands set at the current time, happily ticking as one expects clocks to be ticking. Flavor One - Fixed Clock 🔋🚫 Flavor one: Clock::fixed represents a clock with its batteries out. You can set the hands to any hour, minute and second but the clock will be frozen in time. Any call to the Clock::instant method will yield the same point in time that you set during instantiation. Flavor Two - Offset Clock 🕕 Flavor two: Clock::offset takes inspiration from the previous two. You can set the hands and the clock will be ticking starting from that epoch you set. Let’s see this in action: LocalDateTime epoch = LocalDateTime.of(1983, 4, 10, 12, 30); Clock baseClock = Clock.systemUTC(); Duration offset = Duration.between(LocalDateTime.now(clock), epoch); Clock clock = Clock.offset(baseClock, epoch); In the snippet above, clock represents a clock that started ticking on the 10th of April in 1983 at 12:30pm. Injecting this clock into LocalDateTime would yield the epoch value: LocalDateTime timeIn1983 = LocalDateTime.now(clock); Call it 10 seconds later, it would yield the instant that is 10 seconds later but still in 1983. // Loiter for 10 seconds LocalDateTime tenSecondsLaterIn1983 = LocalDateTime.now(clock); Apart from being able to wind this clock back into the past or forward into the future - or back to the future 😉 -, it will tick as you expect a regular clock to tick - once every second. Building upon the offset clock, we can create a clock that runs in the past or in the future, but with an interval that is different from the regular one second. Clock fastClockIn1983 = Clock.tick(offsetClock, Duration.ofSeconds(5)); This clock will be advancing 5 seconds with every tick while a regular clock would advance only 1 second. We can go crazy with the duration value and create clocks that leap hours, days or even months at a time. Using Clock in tests With these flavors, we have a handful of options to choose from for our testing needs. However, the problem is that the flavors that represent running clocks - with the batteries in -, we have no control over when those ticks happen. So the test code that we implement will end up using Thread.sleep to pass time or will be running a loop for a set period. Neither of these options are ideal. The former will obviously make our test run longer than needed: for (int i = 0; i &lt; 5; i++) { // Loop for 5 seconds // do some business logic Thread.sleep(1000); // Let one second pass } The latter will yield nondeterministic results. The test will work most of the time, but every once in a while results might be flaky due to tiny nanosecond differences: LocalDateTime until = LocalDateTime.now().plusSeconds(5); while (LocalDateTime.now().isBefore(until)) { // Loop for at least 5 seconds // do some business logic } ⏰ Toy Clock 🧸 For the ultimate test, we would like to have total control over our clock including when the ticks happen. We have seen that the options that ship with Java do not provide such a clock out of the box, so we will have to get our hands dirty: let’s try to create such a clock. Did I mention naming is hard? I usually don’t land on the perfect name immediately, so I will start out with a placeholder name and see if during implementation something better pops up. I am gonna go with ToyClock. public class ToyClock extends Clock { private final Clock baseClock; private final Duration tickInterval; private final AtomicLong tickCount = new AtomicLong(0); private TickingClock(LocalDateTime epoch, Duration tickInterval) { Clock fixedClock = Clock.fixed(epoch.toInstant(ZoneOffset.UTC), ZoneOffset.UTC); this.baseClock = Clock.offset(fixedClock, calculateOffset(fixedClock, epoch)); this.tickInterval = tickInterval; } } In the snippet above, we are extending the abstract Clock to create our ToyClock class. For starters, let’s give it three properties. A baseClock serving as the foundation of our implementation. It is a fixed clock with the batteries 🔋 out. A tickInterval to control how long or short the ticks are. And finally, a counter to keep track of how many times did our clock tick as it will be ticking only upon request. To extend Clock we have to override the abstract methods: @Override public ZoneId getZone() { return baseClock.getZone(); } @Override public Clock withZone(ZoneId zone) { return baseClock.withZone(zone); } @Override public Instant instant() { return baseClock.instant() .plus(tickInterval.multipliedBy(tickCount.incrementAndGet())); } Functions, getZone and withZone are just nuances, we have to override these. Lucky for us, the baseClock makes their implementation trivial. The real action happens in instant. Here we once again rely on baseClock to get the current instant - which is always the same, frozen in time - and add tickInterval x tickCount. Leveraging the AtomicLong class, we maintain thread safety of our implementation by incrementing and getting the counter value as an atomic operation. A side effect of a call to the instant method will be one tick of our ToyClock. Notice that tickInterval can be positive or negative, meaning that we can make our clock run forward or backward. But the real beauty is that peeking into the implementation of LocalDateTime::now we see this: final Instant now = clock.instant(); // called once So by injecting a ToyClock instance into LocalDateTime::now we can actually get a time value while also being able to control the ticking of our clock. A side effect of calling LocalDateTime::now is going to be exactly one tick of our clock. With this we can create test environments in which the passing of time is under tight control and yield deterministic outcomes, no matter how complex the time logic is. To demonstrate, let’s quickly implement a unit test of ToyClock. To make it fun, let’s have it tick backwards by 5 minutes. @Test void tickBackward() { LocalDateTime epoch = LocalDateTime.now(); int tickIntervalMinutes = -5; Clock clock = new ToyClock(epoch, Duration.ofMinutes(tickIntervalMinutes)); for (int i = 0; i &lt; 3; i++) { LocalDateTime now = LocalDateTime.now(clock); // calls Clock#instant that causes the tick assertEquals((i+1) * tickIntervalMinutes, now.getMinute()); } } Inside the for loop, the clock ticks exactly once for a total of 3 ticks, so at the end our time will be 15 seconds in the past. It turns out that no better name popped up, so I am gonna stick with ToyClock for now. And that’s enough coding for one day. If you read this far, now you must be really curious how the name of the site, Bit More, came about. The idea is actually my wife’s. She is not a software engineer, so I am not surprised that she is better at this than I ever hope to be. The startup that I work for was looking for a name and I was discussing this with her during dinner one day. We started throwing out ideas while having toast that evening. The toaster had a button labelled ‘Bit more’ with a function of giving the bread an extra 30 seconds of heat. The domain of the startup is finance, so when my wife grabbed the ready toasts from the toaster she lit up with joy shouting ‘this is it’. I did enter the name into the company’s name selection pool, but ultimately ‘Bit More’ did not make the final round. That left my wife a tad bit sad as she was convinced this is a great name. So about a year went by and I told her that I feel the need to start writing some sort of a blog and that I want to write about code. Without hesitation she jumped up and said: you have to name it Bit More. Hungry for more time? It so happens that a good friend of mine, Attila, is also into time. He is an avid collector of Seiko quartz watches. Take a look at his impressive collection here.]]></summary></entry></feed>