<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title>Chemaclass - software-design</title><subtitle>Tech Lead sharing practical insights on software craftsmanship, TDD, leadership, Bitcoin, and AI. Blog posts, book summaries, and conference talks.</subtitle><link rel="self" type="application/atom+xml" href="https://chemaclass.com/tags/software-design/atom.xml"/><link rel="alternate" type="text/html" href="https://chemaclass.com"/><generator uri="https://www.getzola.org/">Zola</generator><updated>2025-01-18T00:00:00+00:00</updated><id>https://chemaclass.com/tags/software-design/atom.xml</id><entry xml:lang="en"><title>Bitcoin with Rigor</title><subtitle>A technical study about how Bitcoin works</subtitle><category term="bitcoin" scheme="https://chemaclass.com/tags/bitcoin/" label="Bitcoin"/><category term="privacy" scheme="https://chemaclass.com/tags/privacy/" label="Privacy"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2025-01-18T00:00:00+00:00</published><updated>2025-01-18T00:00:00+00:00</updated><author><name>
Jose Sanchis</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/bitcoin-with-rigor/"/><id>https://chemaclass.com/readings/bitcoin-with-rigor/</id><summary type="html">Bitcoin with Rigor goes beyond a basic explanation of Bitcoin, offering a detailed analysis of how Bitcoin works. It breaks down its components clearly, helping readers understand both the parts and the system as a whole. This deep dive explores Bitcoin’s potential and risks, recognizing it as a decade-old monetary experiment that hasn’t yet stood the test of time like gold or silver. Despite this, its innovative design makes it a promising system for the present and future.</summary><content type="html">&lt;p>Bitcoin with Rigor goes beyond a basic explanation of Bitcoin, offering a detailed analysis of how Bitcoin works. It breaks down its components clearly, helping readers understand both the parts and the system as a whole. This deep dive explores Bitcoin’s potential and risks, recognizing it as a decade-old monetary experiment that hasn’t yet stood the test of time like gold or silver. Despite this, its innovative design makes it a promising system for the present and future.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>Bitcoin’s novelty makes it challenging to grasp fully. It requires knowledge of computing, economics, and finance while also being a digital asset with no historical precedent to guide understanding. Yet, much like the Internet (also relatively young but undeniably transformative), Bitcoin has the potential to redefine systems and leave a lasting impact.&lt;/p>
&lt;p>This book aims to equip readers with the tools to understand how Bitcoin works, empowering them to form their own well-informed opinions about its role in society or, if they choose, to begin a professional journey in this evolving sector. Written with rigor but without excessive technicality, the content is designed to be concise, accessible, and engaging, providing the foundation needed to truly comprehend Bitcoin.&lt;/p>
&lt;hr />
&lt;h2 id="content">Content
&lt;a class="heading-anchor" href="#content" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;h3 id="1-basic-computer-concepts">1. Basic Computer Concepts
&lt;a class="heading-anchor" href="#1-basic-computer-concepts" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Information and Communication&lt;/li>
&lt;li>How Computers Work&lt;/li>
&lt;li>Languages and Protocols&lt;/li>
&lt;li>The OSI Model: The Onion of Computing&lt;/li>
&lt;li>Logic Gates: Fundamental Computing Functions&lt;/li>
&lt;li>Client-Server and P2P Networks&lt;/li>
&lt;/ul>
&lt;h3 id="2-basic-cryptographic-concepts">2. Basic Cryptographic Concepts
&lt;a class="heading-anchor" href="#2-basic-cryptographic-concepts" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Asymmetric Complexity&lt;/li>
&lt;li>Ciphers, Keys, and Symmetric Cryptography&lt;/li>
&lt;li>Asymmetric Cryptography&lt;/li>
&lt;li>The Cypherpunk Movement&lt;/li>
&lt;li>Cryptographic Hash Functions&lt;/li>
&lt;/ul>
&lt;h3 id="3-the-basics-of-bitcoin-and-cryptocurrencies">3. The Basics of Bitcoin and Cryptocurrencies
&lt;a class="heading-anchor" href="#3-the-basics-of-bitcoin-and-cryptocurrencies" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Key Generators and Randomness&lt;/li>
&lt;li>Cryptographic Signatures&lt;/li>
&lt;li>The Discrete Logarithm Problem&lt;/li>
&lt;li>Elliptic Curve Cryptography&lt;/li>
&lt;li>Merkle Tree&lt;/li>
&lt;li>Traditional Digital Money&lt;/li>
&lt;li>Distributed Consensus Algorithms&lt;/li>
&lt;li>Proof of Work (PoW)&lt;/li>
&lt;/ul>
&lt;h3 id="4-bitcoin-piece-by-piece">4. Bitcoin Piece by Piece
&lt;a class="heading-anchor" href="#4-bitcoin-piece-by-piece" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Blockchain or Hashchain?&lt;/li>
&lt;li>Block Synchronization&lt;/li>
&lt;li>The Headerchain&lt;/li>
&lt;li>Keys and Signatures in Bitcoin&lt;/li>
&lt;li>Transactions and the UTXO Model&lt;/li>
&lt;li>Bitcoin Script and Opcodes&lt;/li>
&lt;li>P2PKH (Pay to Public Key Hash) and P2PK&lt;/li>
&lt;li>Multi signatures and P2SH (Pay to Script Hash)&lt;/li>
&lt;/ul>
&lt;h3 id="5-nodes-and-blockchain-in-detail">5. Nodes and Blockchain in Detail
&lt;a class="heading-anchor" href="#5-nodes-and-blockchain-in-detail" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Transaction Size and Frequency&lt;/li>
&lt;li>Initializing a Full Network Node&lt;/li>
&lt;li>Databases and Pruning&lt;/li>
&lt;li>How a Wallet Works&lt;/li>
&lt;li>Wallets, SPV Nodes, and Others&lt;/li>
&lt;li>From Mempool to Blockchain&lt;/li>
&lt;li>Forks and Consensus Criticality&lt;/li>
&lt;li>Hard Fork vs. Soft Fork&lt;/li>
&lt;/ul>
&lt;h3 id="6-bitcoin-updates-and-applications">6. Bitcoin Updates and Applications
&lt;a class="heading-anchor" href="#6-bitcoin-updates-and-applications" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Much Work Ahead&lt;/li>
&lt;li>Transaction Malleability&lt;/li>
&lt;li>Segregated Witness (SegWit)&lt;/li>
&lt;li>P2WPKH and P2WSH&lt;/li>
&lt;li>Unilateral Payment Channels&lt;/li>
&lt;li>Lightning Channels&lt;/li>
&lt;li>Multi-channel Payments and HTLCs&lt;/li>
&lt;li>Lightning Network&lt;/li>
&lt;li>Discreet Log Contracts (DLCs)&lt;/li>
&lt;li>MAST, P2SMR, and P2CH&lt;/li>
&lt;li>Taproot and Grafroot&lt;/li>
&lt;/ul>
&lt;hr />
&lt;h2 id="book-presentation">Book presentation
&lt;a class="heading-anchor" href="#book-presentation" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/55ZRM7j1Jzc"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div>
&lt;hr />
&lt;p>&lt;strong>Related links&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Book website: &lt;a rel="external" href="https://www.bitcoinconrigor.com">www.bitcoinconrigor.com&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>Bitcoin: A Work in Progress</title><subtitle>Technical innovations from the trenches</subtitle><category term="bitcoin" scheme="https://chemaclass.com/tags/bitcoin/" label="Bitcoin"/><category term="privacy" scheme="https://chemaclass.com/tags/privacy/" label="Privacy"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2024-08-02T00:00:00+00:00</published><updated>2024-08-02T00:00:00+00:00</updated><author><name>
Sjors Provoost</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/bitcoin-wip/"/><id>https://chemaclass.com/readings/bitcoin-wip/</id><summary type="html">With thousands of 'crypto' projects out there, they say Bitcoin is old and boring, but nothing could be further from the truth. This book will guide you through the latest developments in Bitcoin, as seen through the eyes of one of its many developers.</summary><content type="html">&lt;p>With thousands of “crypto” projects out there, they say Bitcoin is old and boring, but nothing could be further from the truth. This book will guide you through the latest developments in Bitcoin, as seen through the eyes of one of its many developers.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>You’ll learn about the latest soft fork known as Taproot, the challenges of keeping open source software free of money-stealing bugs and malware, new ways to protect nodes against evildoers on the internet, how to deal with the ever-growing blockchain, and more!&lt;/p>
&lt;p>The book links to more than two hundred articles, videos, podcasts, and even the source code. And thanks to a tiny QR code next to every link, you’ll never have to type long URLs.&lt;/p>
&lt;hr />
&lt;h2 id="contents">Contents
&lt;a class="heading-anchor" href="#contents" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;h3 id="the-basics">The basics
&lt;a class="heading-anchor" href="#the-basics" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>1. Bitcoin Addresses&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>2. DNS Bootstrap and Tor V3&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>3. SegWit&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>4. libsecp256k1&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="resource-usage">Resource usage
&lt;a class="heading-anchor" href="#resource-usage" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>5. Sync Time and AssumeUTXO&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>6. Utreexo&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="attacking-bitcoin">Attacking Bitcoin
&lt;a class="heading-anchor" href="#attacking-bitcoin" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>7. Eclipse Attacks&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>8. Fake Nodes&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>9. Guix&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="better-wallets">Better Wallets
&lt;a class="heading-anchor" href="#better-wallets" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>10. Script, P2SH, and Miniscript&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="taproot">Taproot
&lt;a class="heading-anchor" href="#taproot" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>11. Taproot and Schnorr&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>12. Activation Options&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="appendix">Appendix
&lt;a class="heading-anchor" href="#appendix" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span>A. More Episodes&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>B. A crime on Testnet&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>C. Bitcoin White Paper&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>&lt;img src="/images/readings/2024-08-02/sjors-and-chema.webp" alt="cover" />&lt;/p>
&lt;hr />
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/pQb24UWWSl8"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div>
&lt;hr />
&lt;p>&lt;strong>Related links&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Book source code: &lt;a rel="external" href="https://github.com/Sjors/nado-book/">GitHub/Sjors&lt;/a>&lt;/li>
&lt;li>Book website: &lt;a rel="external" href="https://www.btcwip.com/">www.btcwip.com&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>What Is Waterfall?</title><subtitle>What makes Waterfall a poor fit for modern software development?</subtitle><category term="agile" scheme="https://chemaclass.com/tags/agile/" label="Agile"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="team-management" scheme="https://chemaclass.com/tags/team-management/" label="Team Management"/><category term="productivity" scheme="https://chemaclass.com/tags/productivity/" label="Productivity"/><published>2024-08-01T00:00:00+00:00</published><updated>2024-08-01T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/what-is-waterfall/"/><id>https://chemaclass.com/blog/what-is-waterfall/</id><summary type="html">Waterfall is like following a straight path where you move from one step to the next in a defined order, like water flowing down a waterfall through different stages. The problem is that each step can take a lot of time and resources to complete. Plus, you don't get feedback until the entire stage is finished, which can lead to a lot of wasted time. This is especially tricky in software development, where things are always changing and evolving.</summary><content type="html">&lt;p>Waterfall is like following a straight path where you move from one step to the next in a defined order, like water flowing down a waterfall through different stages.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>The problem is that each step can take a lot of time and resources to complete. Plus, you don’t get feedback until the entire stage is finished, which can lead to a lot of wasted time. This is especially tricky in software development, where things are always changing and evolving.&lt;/p>
&lt;p>It usually follows a straightforward sequence like this:&lt;/p>
&lt;p>&lt;img src="/images/blog/2024-08-01/waterfall.jpg" alt="Waterfall img from Comic Agile" />&lt;/p>
&lt;h2 id="the-reality-of-waterfall">The reality of Waterfall
&lt;a class="heading-anchor" href="#the-reality-of-waterfall" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Waterfall can be like communism in theory, it seems perfect on paper but doesn’t work in the real world.&lt;/p>
&lt;ul>
&lt;li>Clients often don’t know exactly what they want.&lt;/li>
&lt;li>Requirements are constantly changing.&lt;/li>
&lt;li>Businesses need to quickly adapt to market shifts and client needs.&lt;/li>
&lt;li>Software needs to be flexible to keep up with these changes.&lt;/li>
&lt;/ul>
&lt;p>So, in a constantly changing world, Waterfall can really hurt a business. It tends to frustrate developers and teams, and it can also annoy clients and the businesses paying for the software. This usually leads to delays and extra costs.&lt;/p>
&lt;h2 id="why-companies-still-use-waterfall">Why companies still use Waterfall
&lt;a class="heading-anchor" href="#why-companies-still-use-waterfall" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Even with its problems, a lot of companies still use Waterfall because it seems straightforward and logical. This makes them reluctant to take the time to learn Agile. Plus, getting management to agree to switch to Agile can be a tough sell, especially since it requires an investment in time and learning.&lt;/p>
&lt;p>The big issue is when higher-ups dictate exactly how teams should work, leading to micromanagement. This messes up the flexibility that Agile brings. From what I’ve seen, this is a common problem.&lt;/p>
&lt;blockquote>
&lt;p>Looking back, switching to Agile could have fixed a lot of issues.&lt;/p>
&lt;/blockquote>
&lt;hr />
&lt;p>&lt;img src="/images/blog/2024-08-01/footer.webp" alt="agile vs waterfall" />&lt;/p>
&lt;h2 id="why-agile-was-created">Why Agile was created
&lt;a class="heading-anchor" href="#why-agile-was-created" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Agile was created to overcome the limits of the Waterfall method. It focuses on constant interaction with clients and teams.&lt;/p>
&lt;p>Agile builds autonomous and responsible teams that handle tasks from start to finish, reducing wasted time and resources. It emphasizes flexibility, collaboration, and customer feedback.&lt;/p>
&lt;p>Unlike Waterfall, Agile uses iterative development, breaking projects into small, manageable sprints or iterations that last 1-4 weeks. Each cycle includes planning, development, testing, and review, aiming to deliver value quickly and gather feedback to improve.&lt;/p>
&lt;h3 id="key-aspects-of-agile">Key aspects of Agile
&lt;a class="heading-anchor" href="#key-aspects-of-agile" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Iterative Development&lt;/strong>: Work in small chunks and adjust as you go.&lt;/li>
&lt;li>&lt;strong>Customer Collaboration&lt;/strong>: Keep talking with clients to make sure they’re happy.&lt;/li>
&lt;li>&lt;strong>Cross-Functional Teams&lt;/strong>: Teams with different skills working together.&lt;/li>
&lt;li>&lt;strong>Adaptive Planning&lt;/strong>: Stay flexible and tweak plans based on feedback.&lt;/li>
&lt;li>&lt;strong>Continuous Improvement&lt;/strong>: Always look for ways to get better.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Read the original &lt;a rel="external" href="https://agilemanifesto.org/">Agile Manifesto&lt;/a>.&lt;/p>
&lt;/blockquote>
&lt;h3 id="where-can-you-start">Where can you start?
&lt;a class="heading-anchor" href="#where-can-you-start" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>As a developer, you can boost your agility by diving into &lt;a href="/blog/effective-pair-programming/">pair programming&lt;/a> and &lt;a href="/blog/test-driven-development/">TDD&lt;/a>.&lt;/p>
&lt;ul>
&lt;li>With pair programming, two developers work side by side, which means you get instant feedback and shared problem-solving, leading to better code.&lt;/li>
&lt;li>TDD, on the other hand, involves writing tests before the code, which help specifying what you want to do, focusing on baby steps.&lt;/li>
&lt;/ul>
&lt;p>Together, these practices make your development process more flexible, collaborative, and high-quality, fitting right in with Agile’s focus on quick adjustments and continuous improvement. See more practices &lt;a href="/readings/extreme-programming-explained/#practices">here&lt;/a>.&lt;/p>
&lt;blockquote>
&lt;p>The key is collaboration, baby steps, and quick feedback on everything you work on.&lt;/p>
&lt;/blockquote>
&lt;h2 id="my-experience-with-agile">My experience with Agile
&lt;a class="heading-anchor" href="#my-experience-with-agile" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>I’ve &lt;a href="/talks/">talked&lt;/a> about Agile at various tech events and explored it in depth because I’m passionate about how it can supercharge software teams. When done right, Agile can completely change how teams work, making them faster, more efficient, and better at delivering what clients and businesses really need.&lt;/p>
&lt;ul>
&lt;li>2022-06-26 | &lt;a rel="external" href="https://phpconference.com/mixed/update-your-team-to-be-more-extreme/">International PHP Conference&lt;/a> [Berlin, Germany] (EN)&lt;/li>
&lt;li>2022-09-16 | &lt;a rel="external" href="https://codetalks.de/">Code Talks&lt;/a> [Hamburg, Germany] (EN)&lt;/li>
&lt;li>2022-10-26 | &lt;a rel="external" href="https://phpconference.com/mixed/update-your-team-to-be-more-extreme/">International PHP Conference&lt;/a> [Munich, Germany] (EN)&lt;/li>
&lt;li>2022-12-21 | IES Ginés Pérez Chirinos [Murcia, Spain] (ES)&lt;/li>
&lt;li>2023-01-19 | &lt;a rel="external" href="https://devm.io/update-your-team-to-be-more-extreme/">devm.io&lt;/a> [Remote] (EN)&lt;/li>
&lt;li>2023-07-28 | &lt;a rel="external" href="https://www.wearedevelopers.com/world-congress">WeAreDeveloper World Congress&lt;/a> [Berlin, Germany] (EN)&lt;/li>
&lt;/ul>
&lt;h3 id="wearedevelopers-world-congress-in-berlin">WeAreDevelopers World Congress in Berlin
&lt;a class="heading-anchor" href="#wearedevelopers-world-congress-in-berlin" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/dqtAyl-SvaY"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Mastering Bitcoin</title><subtitle>Programming the Open Blockchain</subtitle><category term="bitcoin" scheme="https://chemaclass.com/tags/bitcoin/" label="Bitcoin"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="privacy" scheme="https://chemaclass.com/tags/privacy/" label="Privacy"/><published>2024-07-05T00:00:00+00:00</published><updated>2024-07-05T00:00:00+00:00</updated><author><name>
Andreas M. Antonopoulos</name></author><author><name>
David A. Harding</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/mastering-bitcoin/"/><id>https://chemaclass.com/readings/mastering-bitcoin/</id><summary type="html">This book covers everything from the basics to the most profound technical aspects of how Bitcoin works. It is an excellent guide through the complex world of Bitcoin, providing the knowledge you need to participate in the Internet of Money.</summary><content type="html">&lt;p>This book covers everything from the basics to the most profound technical aspects of Bitcoin. It is an excellent guide through this complex world, providing the knowledge you need to participate in the Internet of money.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>The first edition of this book was published in December 2014. Ten years later, a third edition with up-to-date knowledge just came out. It is highly recommended to all people interested in this outstanding topic and the tech behind it.&lt;/p>
&lt;ul>
&lt;li>A broad introduction to Bitcoin and its underlying blockchain, ideal for nontechnical users, investors, and business executives.&lt;/li>
&lt;li>An explanation of Bitcoin’s technical foundation and cryptographic currency for developers, engineers, and software and systems architects.&lt;/li>
&lt;li>Details of the Bitcoin decentralized network, peer-to-peer architecture, transaction lifecycle, and security principles.&lt;/li>
&lt;li>New developments such as Taproot, Tapscript, Schnorr signatures, and the Lightning Network.&lt;/li>
&lt;li>A deep dive into Bitcoin applications, including how to combine the building blocks this platform offers into powerful new tools.&lt;/li>
&lt;li>User stories, analogies, examples, and code snippets illustrating key technical concepts.&lt;/li>
&lt;/ul>
&lt;hr />
&lt;h2 id="contents">Contents
&lt;a class="heading-anchor" href="#contents" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ol>
&lt;li>Introduction&lt;/li>
&lt;li>How Bitcoin Works&lt;/li>
&lt;li>Bitcoin Core: The Reference Implementation&lt;/li>
&lt;li>Keys and Addresses&lt;/li>
&lt;li>Wallet recovery&lt;/li>
&lt;li>Transactions&lt;/li>
&lt;li>Authorization and Authentication&lt;/li>
&lt;li>Digital Signatures&lt;/li>
&lt;li>Transaction Fees&lt;/li>
&lt;li>The Bitcoin Network&lt;/li>
&lt;li>The Blockchain&lt;/li>
&lt;li>Mining and Consensus&lt;/li>
&lt;li>Bitcoin Security&lt;/li>
&lt;li>Second-Layer Applications&lt;/li>
&lt;/ol>
&lt;p>&lt;img src="/images/readings/2024-07-05/footer.webp" alt="blog-footer" />&lt;/p>
&lt;blockquote>
&lt;p>Full book also on &lt;a rel="external" href="https://github.com/bitcoinbook/bitcoinbook">GitHub&lt;/a>.&lt;/p>
&lt;/blockquote>
&lt;h3 id="key-takeaways">Key takeaways
&lt;a class="heading-anchor" href="#key-takeaways" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ol>
&lt;li>Your wallet doesn’t store any bitcoins, only the keys to access them. The coins are stored in the blockchain.&lt;/li>
&lt;li>Understanding &lt;strong>UTXOs&lt;/strong> is crucial to understanding how transactions work.
&lt;ul>
&lt;li>These are outputs from previous transactions that have not yet been spent&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>A transaction needs:
&lt;ul>
&lt;li>&lt;strong>inputs&lt;/strong>: UTXOs and an unlocking script (scriptSig) which proves ownership.&lt;/li>
&lt;li>&lt;strong>outputs&lt;/strong>: the recipient’s address and the amount to send.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The unlocking script is written in a custom language called &lt;a rel="external" href="https://en.bitcoin.it/wiki/Script">&lt;strong>Script&lt;/strong>&lt;/a>.
&lt;ul>
&lt;li>Transaction validation is achieved through the execution of a script.&lt;/li>
&lt;li>This allows for a nearly infinite variety of conditions to be expressed.&lt;/li>
&lt;li>Script is a stack-based lang, processed from left to right.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Bitcoin is often referred to as “&lt;strong>programmable money&lt;/strong>” because it allows: Multisignature, timelocking and conditional spending.
&lt;ul>
&lt;li>Extends Bitcoin’s functionality beyond simple value transfers.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Common Bitcoin &lt;a href="/blog/programmable-money#common-bitcoin-address-types">&lt;strong>address types&lt;/strong>&lt;/a>:
&lt;ul>
&lt;li>P2PKH: Begins with &lt;code>1&lt;/code>. Standard transactions using public key hashes.&lt;/li>
&lt;li>P2SH: Begins with &lt;code>3&lt;/code>. Encapsulates complex scripts like multisig.&lt;/li>
&lt;li>P2WPKH: Begins with &lt;code>bc1&lt;/code>. Native SegWit, more efficient transactions.&lt;/li>
&lt;li>P2WSH: Begins with &lt;code>bc1&lt;/code>. SegWit for complex scripts.&lt;/li>
&lt;li>P2TR: Begins with &lt;code>bc1p&lt;/code>. Taproot, enhanced privacy and efficiency.&lt;/li>
&lt;li>Bech32: Starts with &lt;code>bc1&lt;/code>. Human-readable, designed for SegWit and Taproot.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Digital signatures serve three purposes in Bitcoin transactions:
&lt;ul>
&lt;li>First, it proves that the controller of a private key has &lt;strong>authorized&lt;/strong> the spending of those funds.&lt;/li>
&lt;li>Secondly, the proof of authorization is &lt;strong>undeniable&lt;/strong>.&lt;/li>
&lt;li>Thirdly, the authorized transaction &lt;strong>cannot be changed&lt;/strong> by unauthenticated third parties.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;a rel="external" href="https://en.wikipedia.org/wiki/Schnorr_signature">Schnorr’s signatures&lt;/a> aren’t specific to the &lt;a rel="external" href="https://en.wikipedia.org/wiki/Elliptic-curve_cryptography">elliptic curve cryptography&lt;/a> (ECC) that Bitcoin uses, although it is most associated with ECC today. They have a number of nice properties:
&lt;ul>
&lt;li>&lt;strong>Provable security&lt;/strong>: strong security guarantees based on the hardness of the discrete logarithm problem, with formal proofs ensuring their robustness against attacks.&lt;/li>
&lt;li>&lt;strong>Linearity&lt;/strong>: allows for efficient aggregation of multiple signatures into a single compact signature, simplifying verification processes.&lt;/li>
&lt;li>&lt;strong>Batch verification&lt;/strong>: enabling multiple signatures to be verified simultaneously in a more computationally efficient manner.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The best known secure secret sharing algorithm is &lt;a rel="external" href="https://en.wikipedia.org/wiki/Shamir&amp;#x27;s_secret_sharing">Shamir’s Secret Sharing&lt;/a>.&lt;/li>
&lt;li>Running a &lt;strong>full node&lt;/strong> gives you the pure Bitcoin experience: independent verification of all transactions without trusting any other system.&lt;/li>
&lt;li>&lt;strong>Merkle tres&lt;/strong> are used to summarize all transactions in a block, producing an overall commitment to the entire set of transactions and permitting a very efficient process to verify whether a transaction is included in a block.&lt;/li>
&lt;li>Mining is one of the inventions that make Bitcoin special, a &lt;strong>decentralized consensus&lt;/strong> mechanism that is the basis for P2P digital cash.
&lt;ul>
&lt;li>It secures the Bitcoin system and enables the emergence of network-wide consensus without a central authority.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>A &lt;strong>soft-fork&lt;/strong> is a forward-compatible change to the consensus rules that allows unupgraded clients to continue to operate in consensus with the new rules.&lt;/li>
&lt;/ol></content></entry><entry xml:lang="en"><title>Learning Concurrency in Golang</title><subtitle>A horse racing emulator explained step by step</subtitle><category term="golang" scheme="https://chemaclass.com/tags/golang/" label="Golang"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2024-04-02T00:00:00+00:00</published><updated>2024-04-02T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/learning-concurrency-in-golang/"/><id>https://chemaclass.com/blog/learning-concurrency-in-golang/</id><summary type="html">I wanted to learn a new programming language, so after trying some, I ended up with Golang as one of my favorites for its simplicity and capabilities. It has features I haven’t used in years, like multithreading and concurrency.</summary><content type="html">&lt;p>I wanted to learn a new language, so after trying some, I ended up with Golang as one of my favorites for its simplicity and capabilities. It has features I haven’t used in years, like multithreading and concurrency.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>&lt;a rel="external" href="https://go.dev/">Golang&lt;/a> (or &lt;code>Go&lt;/code>) supports concurrency through lightweight threads called goroutines. These are different from traditional multithreading, like in Java, where you have to handle sync and coordination to manage shared resources safely. In contrast, Go’s goroutines are lightweight, managed by the Go runtime, and cheaper to create and manage.&lt;/p>
&lt;p>While parallelism is &lt;strong>doing&lt;/strong> several things simultaneously, concurrency is about &lt;strong>dealing&lt;/strong> with several things at the same time. When we talk about concurrency and parallelism, we don’t know the order of things. We don’t know what’s going to happen first or what’s going to end first. There is an undefined order of execution.&lt;/p>
&lt;blockquote>
&lt;p>Imagine you cooking: preparing a soup, a salad and an omelet. You would be one single unit, but you are preparing different dishes. You might finish the salad first or the soup or the omelet… We cannot guarantee that! This would be concurrency, as you are alone dealing with several things. As soon as your partner comes and helps you cook, then we will be talking about parallelism.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;img src="/images/blog/2024-04-02/concurrency-vs-multithreading.jpg" alt="concurrency vs multithreading" />&lt;/p>
&lt;p>I remember building a similar game in &lt;code>Java&lt;/code> when I was learning multithreading ten years ago… let’s use this opportunity to do it again with modern &lt;code>Go&lt;/code>.&lt;/p>
&lt;p>I built a terminal game emulator that mimics one horse racing. Each horse is a goroutine that runs in a shared bidimensional matrix. Once a horse reaches the end, it notifies the shared channel between all other horses (running in different processes) and they all stop, showing in the terminal the winner of the race.&lt;/p>
&lt;p>I separated the code into four areas to help visualize it:&lt;/p>
&lt;ul>
&lt;li>Entry point&lt;/li>
&lt;li>Generating the board&lt;/li>
&lt;li>Rendering the game&lt;/li>
&lt;li>Moving the horses&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="/images/blog/2024-04-02/race-horses-demo.gif" alt="horse racing terminal demo" />&lt;/p>
&lt;h3 id="entry-point">Entry point
&lt;a class="heading-anchor" href="#entry-point" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>The struct &lt;code>Horse&lt;/code> represents each Horse in the race.
The game consists of a list of lines, in which each Horse is running.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">type&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> struct&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> Name&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> string&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> The name of the horse&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> Line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> The competition line&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span> (&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">h &lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Letter&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> string&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sprintf&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">%c&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span> h&lt;/span>&lt;span>.&lt;/span>&lt;span>Name&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">0&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span> (&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">h &lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Equals&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">other&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> bool&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> other&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> !=&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;amp;&amp;amp;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> h&lt;/span>&lt;span>.&lt;/span>&lt;span>Line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ==&lt;/span>&lt;span> other&lt;/span>&lt;span>.&lt;/span>&lt;span>Line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;amp;&amp;amp;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> h&lt;/span>&lt;span>.&lt;/span>&lt;span>Name&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ==&lt;/span>&lt;span> other&lt;/span>&lt;span>.&lt;/span>&lt;span>Name&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span> (&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">h &lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> String&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> string&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sprintf&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">%s&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> (line:&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">%d&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">)&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span> h&lt;/span>&lt;span>.&lt;/span>&lt;span>Name&lt;/span>&lt;span>,&lt;/span>&lt;span> h&lt;/span>&lt;span>.&lt;/span>&lt;span>Line&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>You can spawn a new process using the keyword &lt;code>go&lt;/code> when invoking any function.
In this game, this is used 1) to render the game &lt;code>RenderGame()&lt;/code> and 2) for each horse’s movement &lt;code>startRuningHorseInLine()&lt;/code>. The goal is to keep the “rendering” and “movement logic” working in parallel.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> main&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> const&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> lines&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> lineLength&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> 12&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> 30&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> NewRaceBoard&lt;/span>&lt;span>(&lt;/span>&lt;span>lines&lt;/span>&lt;span>,&lt;/span>&lt;span> lineLength&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> go&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> RenderGame&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span> winnerChan&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> make&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">chan&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> each horse will be moved in different processes&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> go&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> startRunningHorseInLine&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;span> winnerChan&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> wait until one horse reaches the end&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> winner&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;lt;-&lt;/span>&lt;span>winnerChan&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> render one last time to ensure the latest board state&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> RenderRaceBoard&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;amp;&lt;/span>&lt;span>winner&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span> fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Println&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Race finished!&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Printf&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"># Winner: &lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">%s&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">\n&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span> winner&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="generating-the-board">Generating the board
&lt;a class="heading-anchor" href="#generating-the-board" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>The race board is a bidimensional matrix of pointers to Horses. Each line “contains” only one Horse; aka only one pointer will point to an actual Horse, the rest will be pointers to &lt;code>nil&lt;/code>. During the generation of the Board, we will create one Horse at the first position of each line.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> NewRaceBoard&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">lines&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> lineLength&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span>)&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> make&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;span> lines&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> make&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;span> lineLength&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">0&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;amp;&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> Name&lt;/span>&lt;span>:&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> generateHorseName&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> Line&lt;/span>&lt;span>:&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> board&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>The names are randomly generated using &lt;code>HorseNames&lt;/code>.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">var&lt;/span>&lt;span> HorseNames&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">2&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">string&lt;/span>&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> {&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Alloping&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Giggles&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>}&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> {&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">A-lot&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Gallop&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>}&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> {&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">BoJack&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Jack&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>}&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> {&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Baroness&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">Belle&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>}&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> ...&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> generateHorseName&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> string&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> name&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span> HorseNames&lt;/span>&lt;span>[&lt;/span>&lt;span>rand&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Intn&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">len&lt;/span>&lt;span>(&lt;/span>&lt;span>HorseNames&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">0&lt;/span>&lt;span>]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> surname&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span> HorseNames&lt;/span>&lt;span>[&lt;/span>&lt;span>rand&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Intn&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">len&lt;/span>&lt;span>(&lt;/span>&lt;span>HorseNames&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> name&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> +&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> +&lt;/span>&lt;span> surname&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="rendering-the-game">Rendering the game
&lt;a class="heading-anchor" href="#rendering-the-game" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Then &lt;code>RenderGame()&lt;/code>, &lt;code>renderRaceBoard()&lt;/code>, &lt;code>renderRaceLine()&lt;/code> and &lt;code>renderRacePosition()&lt;/code> are separated to help focus on each method’s responsibility, identifying what is the subject to be rendered.&lt;/p>
&lt;blockquote>
&lt;p>&lt;code>RenderGame()&lt;/code> is being executed in another process using &lt;code>go&lt;/code>.&lt;/p>
&lt;/blockquote>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> RenderGame&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> time&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sleep&lt;/span>&lt;span>(&lt;/span>&lt;span>renderDelay&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span> time&lt;/span>&lt;span>.&lt;/span>&lt;span>Millisecond&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> RenderRaceBoard&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> RenderRaceBoard&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> winner&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> use a &amp;quot;string buffer&amp;quot; to save the whole board state&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> so we can later use one IO call to render it&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> var&lt;/span>&lt;span> buffer&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> bytes&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Buffer&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">\n&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> renderRaceLine&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;amp;&lt;/span>&lt;span>buffer&lt;/span>&lt;span>,&lt;/span>&lt;span> winner&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> clearScreen&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Println&lt;/span>&lt;span>(&lt;/span>&lt;span>buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">String&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> clearScreen&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> cmd&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span> exec&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Command&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">clear&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> cmd&lt;/span>&lt;span>.&lt;/span>&lt;span>Stdout&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span> os&lt;/span>&lt;span>.&lt;/span>&lt;span>Stdout&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> cmd&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Run&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> renderRaceLine&lt;/span>&lt;span>(&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> buffer&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">bytes&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Buffer&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> winner&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span>fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sprintf&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> %.2d&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> | &lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> var&lt;/span>&lt;span> current&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> h&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> renderRacePosition&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;span> col&lt;/span>&lt;span>,&lt;/span>&lt;span> buffer&lt;/span>&lt;span>,&lt;/span>&lt;span> winner&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> h&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> !=&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> current&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span>h&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span>fmt&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sprintf&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">| &lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">%s&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>,&lt;/span>&lt;span> current&lt;/span>&lt;span>.&lt;/span>&lt;span>Name&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> current&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Equals&lt;/span>&lt;span>(&lt;/span>&lt;span>winner&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> [Won!]&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">\n&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> renderRacePosition&lt;/span>&lt;span>(&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> line&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> buffer&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">bytes&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Buffer&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#E36209, #FFAB70);"> winner&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ==&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);"> &amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span> current&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span>]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> current&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Equals&lt;/span>&lt;span>(&lt;/span>&lt;span>winner&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> removeChars&lt;/span>&lt;span>(&lt;/span>&lt;span>buffer&lt;/span>&lt;span>,&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">+&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">-&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span> buffer&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">WriteString&lt;/span>&lt;span>(&lt;/span>&lt;span>current&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Letter&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span> current&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="moving-the-horses">Moving the horses
&lt;a class="heading-anchor" href="#moving-the-horses" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>In &lt;code>main(...)&lt;/code>, the &lt;code>winnerChan&lt;/code> is a shared channel that will be used by the first Horse reaching the last position in their line.&lt;/p>
&lt;p>Each Horse will run a loop until reaching the end of the line OR receiving (via the shared channel &lt;code>winnerChan&lt;/code>) the message that another Horse already won the race. Until then, each horse will move independently by randomly sleeping milliseconds before moving to the next position in their line.&lt;/p>
&lt;blockquote>
&lt;p>&lt;code>startRuningHorseInLine()&lt;/code> runs in another process using &lt;code>go&lt;/code>.&lt;/p>
&lt;/blockquote>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="go">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> main&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);">...&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> winnerChan&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> make&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">chan&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> range&lt;/span>&lt;span> board&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> each horse will be moved in different processes&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> go&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> startHorseRunning&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;span> winnerChan&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> wait until one horse reaches the end&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> winner&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;lt;-&lt;/span>&lt;span>winnerChan&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);">...&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> startRunningHorseInLine&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> winnerChan&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> chan&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> select&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> case&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;lt;-&lt;/span>&lt;span>winnerChan&lt;/span>&lt;span>:&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> check if another horse finished&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> return&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> in such a case, then stop the for loop&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> default&lt;/span>&lt;span>:&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> time&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Sleep&lt;/span>&lt;span>(&lt;/span>&lt;span>time&lt;/span>&lt;span>.&lt;/span>&lt;span>Millisecond&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span> time&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Duration&lt;/span>&lt;span>(&lt;/span>&lt;span>rand&lt;/span>&lt;span>.&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Intn&lt;/span>&lt;span>(&lt;/span>&lt;span>maxSleepDelay&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> moveHorseOnePos&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>,&lt;/span>&lt;span> line&lt;/span>&lt;span>,&lt;/span>&lt;span> winnerChan&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">func&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> moveHorseOnePos&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);">board&lt;/span>&lt;span> [&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">*&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);">Horse&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> line&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> int&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#E36209, #FFAB70);"> winnerChan&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> chan&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Horse&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> cols&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> len&lt;/span>&lt;span>(&lt;/span>&lt;span>board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> for&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> :=&lt;/span>&lt;span> cols&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> -&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> 1&lt;/span>&lt;span>;&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;gt;&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> 0&lt;/span>&lt;span>;&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">--&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">-&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ==&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> continue&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> here we identify that there is a horse in&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> the following column, so we move it to the&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> current column, and we set `nil` the other one&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">-&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">-&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>]&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> nil&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> if&lt;/span>&lt;span> col&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">+&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ==&lt;/span>&lt;span> cols&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> winnerChan&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> &amp;lt;-&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> *&lt;/span>&lt;span>board&lt;/span>&lt;span>[&lt;/span>&lt;span>line&lt;/span>&lt;span>]&lt;/span>&lt;span>[&lt;/span>&lt;span>col&lt;/span>&lt;span>]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> break&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="source-code">Source code
&lt;a class="heading-anchor" href="#source-code" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>The code displayed in this post is a simplified version, so if you would like to check the working source, you can do it here: &lt;a rel="external" href="https://github.com/Chemaclass/go-horse-racing">Chemaclass/go-horse-racing&lt;/a>.&lt;/p>
&lt;aside class="kudos">
&lt;span class="kudos__icon" aria-hidden="true">🧠&lt;/span>
&lt;div class="kudos__content">
&lt;p>Thanks to my former Team Lead, Andrei Boar, who helped me review my first original solution and provided an &lt;a rel="external" href="https://gist.github.com/zuzuleinen/79413aa7933d7d6c6d84ec6ba8c3910a">alternative solution&lt;/a> (simpler and better!), which I applied to my original code. The main learning was using a &lt;code>chan Horse&lt;/code> to pass the winner Horse from &lt;code>main()&lt;/code>, instead of using a &lt;code>chan bool&lt;/code> and a &lt;code>sync.WaitGroup&lt;/code> between all threads.&lt;/p>
&lt;/div>
&lt;/aside></content></entry><entry xml:lang="en"><title>Great Engineering</title><subtitle>A great engineer is not just a great coder</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="career" scheme="https://chemaclass.com/tags/career/" label="Career"/><category term="communication" scheme="https://chemaclass.com/tags/communication/" label="Communication"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2023-12-30T00:00:00+00:00</published><updated>2023-12-30T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/great-engineering/"/><id>https://chemaclass.com/blog/great-engineering/</id><summary type="html">Coding is not just another job. In the right environment, writing software can be really fun and, even more, it can be your personal hobby as well! So... you might be focused on coding, coding and more coding to level up your own career skills.</summary><content type="html">&lt;p>Coding is not just another job. Writing software can be really fun and, even more, it can be your personal hobby as well! You might be focused on coding, coding and more coding to level up your own career skills.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>That itself is not bad at all! Practice makes the master, so a lot of coding will inevitably help you improve your coding skills… but there are &lt;a href="/blog/the-path-to-seniority-in-software/">other aspects&lt;/a> that you need to keep in mind in order to help you grow into a great engineer.&lt;/p>
&lt;p>As a software engineer, your job is not “just to write software” for its own sake, but to &lt;strong>use software to solve actual business problems&lt;/strong>. To make that happen there are plenty of different methodologies out there, for sure. However, we must agree that you need to identify and understand your customer’s needs in order to know what to actually build.&lt;/p>
&lt;p>You need to know your product, at least until certain level, so you can design your software using a similar language closer to the business, which will help its evolution and overall quality. This will be beneficial for the future (and present) maintenance of the product.&lt;/p>
&lt;p>IT, Software, Product and People topics are highly interconnected, and having at least a common ground of understanding the relation between these can help the overall goal of every single individual.&lt;/p>
&lt;p>The result of a great team work is not equal as the sum of the individual parts, as it’s multiplying the created value among their peers. To that, great communication skills are a key part of it in order to create as much clarity as possible from any different abstraction level.&lt;/p>
&lt;p>That’s why a great engineer is a knowledgeable person about business, customer, product, and coding. Understanding these points have a strong impact on your daily work as it creates a difference between the average and the great engineer.&lt;/p>
&lt;hr />
&lt;p>Image original by &lt;a rel="external" href="https://hybridhacker.email">Nicola Ballotta&lt;/a>.&lt;/p></content></entry><entry xml:lang="en"><title>How to Test Private Methods?</title><subtitle>Testing private methods. When and how?</subtitle><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2023-10-20T00:00:00+00:00</published><updated>2023-10-20T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/how-to-test-private-methods/"/><id>https://chemaclass.com/blog/how-to-test-private-methods/</id><summary type="html">From time to time I have had to face this question: how to test private methods? I have put together in an article the techniques that I usually use.</summary><content type="html">&lt;p>This is a question that I have encountered with some frequency for a long time. So I thought I would put together my thoughts on the subject here.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="short-answer">Short answer
&lt;a class="heading-anchor" href="#short-answer" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Never.&lt;/p>
&lt;h2 id="long-answer">Long answer
&lt;a class="heading-anchor" href="#long-answer" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Never ever.&lt;/p>
&lt;hr />
&lt;h2 id="what-if">What if…?
&lt;a class="heading-anchor" href="#what-if" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>If you really want to test a private method, consider extracting that private method logic into a separate class, and write a unit test for that class’ behavior.&lt;/p>
&lt;blockquote>
&lt;p>For this one, I was inspired by Fran Iglesias’ &lt;a rel="external" href="https://franiglesias.github.io/test-private-methods/">original post&lt;/a>.&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>Introducing a New Tech Stack</title><subtitle>How to introduce new technologies in your team</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="team-management" scheme="https://chemaclass.com/tags/team-management/" label="Team Management"/><category term="communication" scheme="https://chemaclass.com/tags/communication/" label="Communication"/><published>2023-04-14T00:00:00+00:00</published><updated>2023-04-14T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/introducing-a-new-tech-stack/"/><id>https://chemaclass.com/blog/introducing-a-new-tech-stack/</id><summary type="html">To introduce a new tech stack for the entire team, it's important to bring "why?" and a strategy to make it happen among the team because it will affect everyone.</summary><content type="html">&lt;p>To introduce a new tech stack for the entire team, it’s important to bring “why?” and a strategy to make it happen among the team because it will affect everyone.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="why-that-new-tech-stack">Why that new tech stack?
&lt;a class="heading-anchor" href="#why-that-new-tech-stack" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>To decide whether to use a new tech stack or not, remember that it is a team decision, according to the standardization and maintainability of the project when introducing such a new tech stack. But, most importantly, what problem do you want to solve by introducing it? Is it because it is “cool”? Or is there an actual “necessity” that this tech helps you with?&lt;/p>
&lt;h3 id="the-direction-of-the-technology">The direction of the technology
&lt;a class="heading-anchor" href="#the-direction-of-the-technology" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>When proposing adopting a new library, framework, or entire new technology, an important aspect to consider is to be aware of its background and the direction where they are moving forward.&lt;/p>
&lt;p>What is the motivation for that tech, and why are you considering introducing it in your current stack?&lt;/p>
&lt;h3 id="coupling-and-dependencies">Coupling and dependencies
&lt;a class="heading-anchor" href="#coupling-and-dependencies" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>When adopting new technologies in our daily business, it’s not rare that we tend to couple ourselves with them. This makes it harder later to roll back if, after a while, we regret that decision.&lt;/p>
&lt;p>Don’t get me wrong, learning and experimenting with new technologies is great, but introducing it in your daily work is another story because it will affect the whole team. So we need to be more careful with this.&lt;/p>
&lt;p>&lt;img src="/images/blog/2023-04-14/middle.webp" alt="blog-middle" />&lt;/p>
&lt;h2 id="the-focus-of-the-conversation">The focus of the conversation
&lt;a class="heading-anchor" href="#the-focus-of-the-conversation" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>What does this new tech contribute to the project?&lt;/li>
&lt;li>What problem do we want to solve by introducing it?&lt;/li>
&lt;li>Can we solve that problem with our current tech?&lt;/li>
&lt;li>If we already have a similar tech, do we want to have a mix of both?&lt;/li>
&lt;li>What are the tradeoffs when using it vs. not using it?&lt;/li>
&lt;li>Is it worth the incremental complexity over time?&lt;/li>
&lt;li>What is the team strategy to onboard everyone if we aim to use it?&lt;/li>
&lt;/ul>
&lt;h3 id="architectural-decision-records-adrs">Architectural Decision Records (ADRs)
&lt;a class="heading-anchor" href="#architectural-decision-records-adrs" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Whatever the outcome, you should write it down as an &lt;a rel="external" href="https://adr.github.io/">ADR&lt;/a> to revisit it as time passes. An ADR will record your team’s decisions regarding the pros and cons, among any other arguments you have found together, to decide what to do and why.&lt;/p>
&lt;p>ADRs are useful to understand old decisions and keep them in the version control, in the same project if possible. They are helpful for the current team, but also for new joiners.&lt;/p>
&lt;p>&lt;img src="/images/blog/2023-04-14/footer.webp" alt="blog-footer" />&lt;/p>
&lt;aside class="kudos">
&lt;span class="kudos__icon" aria-hidden="true">🧠&lt;/span>
&lt;div class="kudos__content">
&lt;p>Thanks to my friends &lt;a rel="external" href="https://x.com/evrtrabajo">Manu&lt;/a>, &lt;a rel="external" href="https://x.com/Tito_Kati">Antonio&lt;/a>, and &lt;a rel="external" href="https://x.com/JesusValera96">Jesus&lt;/a>, who helped me come up with this summary of ideas after brainstorming together.&lt;/p>
&lt;/div>
&lt;/aside></content></entry><entry xml:lang="en"><title>Accelerate</title><subtitle>Building and Scaling High Performing Technology Organizations</subtitle><category term="devops" scheme="https://chemaclass.com/tags/devops/" label="Devops"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="agile" scheme="https://chemaclass.com/tags/agile/" label="Agile"/><category term="productivity" scheme="https://chemaclass.com/tags/productivity/" label="Productivity"/><published>2023-03-19T00:00:00+00:00</published><updated>2023-03-19T00:00:00+00:00</updated><author><name>
Nicole Forsgren</name></author><author><name>
Jez Humble</name></author><author><name>
Gene Kim</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/accelerate/"/><id>https://chemaclass.com/readings/accelerate/</id><summary type="html">The Science of Lean Software and DevOps: Building and Scaling High Performing Technology Organizations</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>Accelerate explores how software development teams using &lt;strong>Lean Software&lt;/strong> and &lt;strong>DevOps&lt;/strong> can measure their performance and the performance of software engineering teams impacts the overall performance of an organization.&lt;/p>
&lt;blockquote>
&lt;p>Note: DevOps is about integrating and automating software development (Dev) and IT operations (Ops), focusing on improving (and shortening) the systems development life cycle.&lt;/p>
&lt;/blockquote>
&lt;h2 id="key-capabilities">Key Capabilities
&lt;a class="heading-anchor" href="#key-capabilities" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;h3 id="continuous-delivery">Continuous Delivery
&lt;a class="heading-anchor" href="#continuous-delivery" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Use Version Control for all Production Artifacts&lt;/li>
&lt;li>Automate Your Deployment Process&lt;/li>
&lt;li>Implement Continuous Integration&lt;/li>
&lt;li>Use Trunk-Based Development Methods&lt;/li>
&lt;li>Implement Test Automation&lt;/li>
&lt;li>Continuous Delivery (CD)&lt;/li>
&lt;/ul>
&lt;h3 id="architecture">Architecture
&lt;a class="heading-anchor" href="#architecture" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Use a Loosely Coupled Architecture&lt;/li>
&lt;/ul>
&lt;h3 id="product-and-process">Product and Process
&lt;a class="heading-anchor" href="#product-and-process" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Gather and Implement Customer Feedback&lt;/li>
&lt;li>Make the Flow of Work Visible through the Value Stream&lt;/li>
&lt;li>Work in Small Batches&lt;/li>
&lt;li>Foster and Enable Team Experimentation&lt;/li>
&lt;/ul>
&lt;h3 id="lean-management-and-monitoring">Lean Management and Monitoring
&lt;a class="heading-anchor" href="#lean-management-and-monitoring" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Have a Lightweight Change Approval Processes&lt;/li>
&lt;li>Monitor across Application and Infrastructure to Inform Business Decisions&lt;/li>
&lt;li>Check System Health Proactively&lt;/li>
&lt;li>Improve Processes and Manage Work with WIP (Work-In-Process) Limits&lt;/li>
&lt;li>Visualize Work to Monitor Quality and Communicate throughout the Team&lt;/li>
&lt;/ul>
&lt;h3 id="cultural">Cultural
&lt;a class="heading-anchor" href="#cultural" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Support a Generative Culture&lt;/li>
&lt;li>Encourage and Support Learning&lt;/li>
&lt;li>Support and Facilitate Collaboration among Teams&lt;/li>
&lt;li>Provide Resources and Tools that Make Work Meaningful&lt;/li>
&lt;li>Support or Embody Transformational Leadership&lt;/li>
&lt;/ul>
&lt;h2 id="four-key-metrics">Four Key Metrics
&lt;a class="heading-anchor" href="#four-key-metrics" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>Change Lead Time&lt;/strong>
&lt;ul>
&lt;li>Time to implement, test, and deliver code for a feature&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Deployment Frequency&lt;/strong>
&lt;ul>
&lt;li>Number of deployments in a given duration of time&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Change Failure Rate&lt;/strong>
&lt;ul>
&lt;li>Percentage of failed changes over all changes (regardless of success)&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Mean Time to Recovery&lt;/strong>
&lt;ul>
&lt;li>Time it takes to restore service after production failure&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/_d9cws_T9qk"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Recipes for Decoupling</title><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2022-11-28T00:00:00+00:00</published><updated>2022-11-28T00:00:00+00:00</updated><author><name>
Matthias Noback</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/recipes-for-decoupling/"/><id>https://chemaclass.com/readings/recipes-for-decoupling/</id><summary type="html">What is coupling, and why is it bad? What is decoupling, and how to do it efficiently? This book is a compilation of strategies to decouple your domain code from those infrastructure details, so you can enjoy a healthier system in the long run.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>What is coupling, and why is it bad? What is decoupling, and how to do it efficiently? This book is a compilation of
strategies to decouple your domain code from those infrastructure details, so you can enjoy a healthier system in the
long run.&lt;/p>
&lt;p>This book will teach you how to create &lt;a rel="external" href="https://phpstan.org/">&lt;strong>PHPStan&lt;/strong>&lt;/a> rules from no knowledge, and then it will
guide you across many decoupling opportunities like, for example:&lt;/p>
&lt;ul>
&lt;li>web framework&lt;/li>
&lt;li>cli frameworks&lt;/li>
&lt;li>form validation&lt;/li>
&lt;li>orm and database&lt;/li>
&lt;li>test framework&lt;/li>
&lt;/ul>
&lt;p>Read more about the book: &lt;a rel="external" href="https://matthiasnoback.nl/book/recipes-for-decoupling/">matthiasnoback.nl/book/recipes-for-decoupling/&lt;/a>&lt;/p>
&lt;blockquote>
&lt;p>Buy it here: &lt;a rel="external" href="https://leanpub.com/recipes-for-decoupling">leanpub.com/recipes-for-decoupling&lt;/a>&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>Modern Software Engineering</title><subtitle>Doing What Works to Build Better Software Faster</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="agile" scheme="https://chemaclass.com/tags/agile/" label="Agile"/><published>2022-06-29T00:00:00+00:00</published><updated>2022-06-29T00:00:00+00:00</updated><author><name>
David Farley</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/modern-software-engineering/"/><id>https://chemaclass.com/readings/modern-software-engineering/</id><summary type="html">This book presents software development as an engineering practice at every level. To master software engineering, we must become experts at learning and managing complexity.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>This book presents software development as an engineering practice at every level.
To master software engineering, we must become experts at learning and managing complexity.&lt;/p>
&lt;h3 id="optimize-for-learning">Optimize for learning
&lt;a class="heading-anchor" href="#optimize-for-learning" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>To optimize for learning, the book presents five behaviors to consider:&lt;/p>
&lt;ul>
&lt;li>Working iteratively&lt;/li>
&lt;li>Feedback&lt;/li>
&lt;li>Incrementalism&lt;/li>
&lt;li>Empiricism&lt;/li>
&lt;li>Being Experimental&lt;/li>
&lt;/ul>
&lt;p>The main idea is to work on small steps, collect feedback, and adjust.&lt;/p>
&lt;h3 id="optimize-for-managing-complexity">Optimize for managing complexity
&lt;a class="heading-anchor" href="#optimize-for-managing-complexity" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>The book presents other five ideas to manage complexity:&lt;/p>
&lt;ul>
&lt;li>Modularity&lt;/li>
&lt;li>Cohesion&lt;/li>
&lt;li>Separation of Concerns&lt;/li>
&lt;li>Information Hiding and Abstraction&lt;/li>
&lt;li>Managing Coupling&lt;/li>
&lt;/ul>
&lt;p>We need to manage the complexity of our systems.&lt;/p>
&lt;h3 id="tools-to-support-engineering-in-software">Tools to support engineering in software
&lt;a class="heading-anchor" href="#tools-to-support-engineering-in-software" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Going deeper into some ideas that were already discussed in the book, like:&lt;/p>
&lt;ul>
&lt;li>Testability&lt;/li>
&lt;li>Deployability&lt;/li>
&lt;li>Controlling the variables&lt;/li>
&lt;li>Continuous Delivery&lt;/li>
&lt;/ul>
&lt;hr />
&lt;p>A great video explaining the core ideas behind this book by his author:&lt;/p>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/TRqYQnCfgH8"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Modern CTO</title><category term="leadership" scheme="https://chemaclass.com/tags/leadership/" label="Leadership"/><category term="team-management" scheme="https://chemaclass.com/tags/team-management/" label="Team Management"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="communication" scheme="https://chemaclass.com/tags/communication/" label="Communication"/><published>2022-01-23T00:00:00+00:00</published><updated>2022-01-23T00:00:00+00:00</updated><author><name>
Joel Beasley</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/modern-cto/"/><id>https://chemaclass.com/readings/modern-cto/</id><summary type="html">In Modern CTO, Joel Beasley provides readers with an in-depth road map on how to successfully navigate the unexplored and jagged transition between these two roles. Drawing from personal experience, Joel gives a refreshing take on the challenges, lessons, and things to avoid on this journey.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>Developers are not CTOs, but developers can learn how to be CTOs.&lt;/p>
&lt;p>In Modern CTO, Joel Beasley provides readers with an in-depth road map on how to successfully navigate the unexplored
and jagged transition between these two roles. Drawing from personal experience, Joel gives a refreshing take on the
challenges, lessons, and things to avoid on this journey.&lt;/p>
&lt;p>These are the topics you’ll find in the book:&lt;/p>
&lt;h4 id="a-modern-cto-knows">A Modern CTO knows…
&lt;a class="heading-anchor" href="#a-modern-cto-knows" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;ul>
&lt;li>Developers are not CTOs&lt;/li>
&lt;li>The spaghetti code MVP epidemic&lt;/li>
&lt;li>Over-engineering is a problem&lt;/li>
&lt;li>Whether to hire, buy, or out-build their competitor&lt;/li>
&lt;li>How not to scale prematurely&lt;/li>
&lt;li>How to solve any problem&lt;/li>
&lt;li>How to work with programers when you aren’t one&lt;/li>
&lt;li>UX mistakes to watch out for&lt;/li>
&lt;li>When to speak up&lt;/li>
&lt;li>When to hire &amp;amp; fire consultants&lt;/li>
&lt;li>How to analyze failure&lt;/li>
&lt;li>How to bounce back from unforeseen constraints&lt;/li>
&lt;li>Answer the question: “How difficult is it to code…?”&lt;/li>
&lt;li>How to avoid the “bottom of the ninth” guy&lt;/li>
&lt;li>When to respond to feedback&lt;/li>
&lt;li>How to validate an expert in any field&lt;/li>
&lt;li>How to effectively communicate complex ideas&lt;/li>
&lt;/ul>
&lt;h3 id="favorite-quotes">Favorite quotes
&lt;a class="heading-anchor" href="#favorite-quotes" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;blockquote>
&lt;p>If I rest on past achievements, I’ll never grow.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>LEVERAGE OTHERS’ EXPERIENCE Books condense a lifetime of experience into a few hours’ read. Powerful right?&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>There are only two reasons you write bad code:&lt;/p>
&lt;p>1)You know how to write good code, but you choose to write bad code.
2)You don’t know how to write good code. And both suck.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>As the CTO, you must have a business focus.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>Always return to your core goals. […] I make sure each core goal has a clearly defined “why?” behind it, this way
if I ever get lost, I go back to my “why.”&lt;/p>
&lt;/blockquote>
&lt;ul>
&lt;li>Reference to “&lt;a href="/readings/start-with-why">Start with Why&lt;/a>” by Simon Sinek.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>When you’re the boss, remember this golden rule: Ask people what they think instead of telling them what to do.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>If I can’t asses the human component, I can’t lead a team. […] Team composition carries as much or perhaps even
greater weight than programming expertise.&lt;/p>
&lt;/blockquote>
&lt;blockquote>
&lt;p>As CTO, if you can’t explain value simply, it means you don’t understand the business value behind your technology.&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>London vs Chicago</title><subtitle>It's an integration, not a choice</subtitle><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><published>2021-11-20T00:00:00+00:00</published><updated>2021-11-20T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/london-vs-chicago/"/><id>https://chemaclass.com/blog/london-vs-chicago/</id><summary type="html">There are two known schools in TDD: the mockist school (aka Outside-in) and the classicist school (aka Inside-out).</summary><content type="html">&lt;p>There are two known schools in TDD: the mockist school (aka Outside-in) and the classicist school (aka Inside-out).&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h3 id="why-london-and-chicago">Why London and Chicago?
&lt;a class="heading-anchor" href="#why-london-and-chicago" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Two companies, one from London and another from Chicago, claimed to do TDD, but they were focused on different aspects.
The London company was building software from outside-in, while the Chicago company from inside-out. Let’s see them in more detail.&lt;/p>
&lt;h2 id="outside-in-london-school">Outside-in: London School
&lt;a class="heading-anchor" href="#outside-in-london-school" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>It provides a behavior-driven approach to TDD. Starting from the outside of the application and working inwards to
lower layers. For example, starting from the API/Controllers down to the application or domain layers.&lt;/p>
&lt;h3 id="pros">PROS
&lt;a class="heading-anchor" href="#pros" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Behavioral Focused&lt;/strong>: this requires a lot of test doubles because you will test a lot of abstractions that don’t exist
yet as you are creating a high level of logic first. You aren’t going to write dead-code but it will be easy to create
tests highly coupled to the logic and therefore making refactoring a difficult task.&lt;/li>
&lt;li>&lt;strong>Command-Query separation&lt;/strong>: is a discipline for managing side effects. Either you perform an action (command) or you ask
for a value (query).&lt;/li>
&lt;/ul>
&lt;h3 id="cons">CONS
&lt;a class="heading-anchor" href="#cons" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Fragile Tests&lt;/strong>: it tends to create tests that break easily because they are usually too coupled to the production code.&lt;/li>
&lt;li>&lt;strong>Difficult Refactoring&lt;/strong>: for the same reason as “Fragile Tests”, having tests coupled to production code make continuous
refactoring very difficult and time-consuming.&lt;/li>
&lt;/ul>
&lt;h2 id="inside-out-chicago-school">Inside-out: Chicago School
&lt;a class="heading-anchor" href="#inside-out-chicago-school" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>It’s an informal, exploratory, state-based approach of TDD. Starting from the inside of the application (usually the
domain) and working out towards the APIs.&lt;/p>
&lt;h3 id="pros-1">PROS
&lt;a class="heading-anchor" href="#pros-1" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Strong Safety Net&lt;/strong>: it tends to produce tests that are decoupled from the implementation. Enabling you to adopt a more
experimental style of changing software without fear of breaking it. This is desired for continuous refactoring.&lt;/li>
&lt;li>&lt;strong>High Cohesion&lt;/strong>: as tests become more general, the production code becomes more specific. This promotes high cohesion,
and with high cohesion comes loose coupling. Something that promotes high code quality: extensibility,
maintainability, and testability.&lt;/li>
&lt;li>&lt;strong>Minimizes Test Doubles&lt;/strong>: building from the inside out requires fewer test doubles since you are building on top of the
previously written tests. This helps to develop less fragile tests.&lt;/li>
&lt;/ul>
&lt;h3 id="cons-1">CONS
&lt;a class="heading-anchor" href="#cons-1" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>YAGNI&lt;/strong>: it’s often over-engineering solutions, with code that is not really needed (or even used!) in the end.&lt;/li>
&lt;/ul>
&lt;h2 id="london-and-chicago-work-better-together">London and Chicago work better together
&lt;a class="heading-anchor" href="#london-and-chicago-work-better-together" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>It’s not about choosing one over the other. It’s about understanding your context and driving optimization towards those
qualities that need to be optimized. London and Chicago each have their pros and cons. The best approach to TDD is an
integrated adoption of these two schools.&lt;/p>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/rbSDGr-_UwY"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div>
&lt;hr />
&lt;h3 id="references">References
&lt;a class="heading-anchor" href="#references" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;a href="/blog/test-driven-development/">Test-Driven (Development)&lt;/a>&lt;/li>
&lt;li>&lt;a href="/blog/tdd-vs-bdd/">TDD vs BDD&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://gist.github.com/xpepper/2e3519d2cb8568a0b13739d9ae497f21">Notes about “London vs Chicago TDD styles”&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>TDD vs BDD</title><subtitle>Design or Workflow?</subtitle><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2021-09-25T00:00:00+00:00</published><updated>2021-09-25T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/tdd-vs-bdd/"/><id>https://chemaclass.com/blog/tdd-vs-bdd/</id><summary type="html">These are two different techniques. The key to each of them is about the mindset and context of what you want to achieve.</summary><content type="html">&lt;p>These are two different techniques. The key to each of them is about the mindset and context of what you want to achieve.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="bdd-is-a-test-driven-feature">BDD is a “test-driven feature”
&lt;a class="heading-anchor" href="#bdd-is-a-test-driven-feature" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Basically, it’s a test-first driven development, where the main focus is ensuring the expected final behavior, and
therefore the result of the software logic that you want to have at the end.&lt;/p>
&lt;p>In BDD the main focus is the behavior of your domain logic that doesn’t exist yet. It is, from an abstract point of
view, about the whole functionality and domain requirements.&lt;/p>
&lt;h2 id="tdd-is-about-the-rhythm">TDD is about the rhythm
&lt;a class="heading-anchor" href="#tdd-is-about-the-rhythm" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ol>
&lt;li>Specify what you want.&lt;/li>
&lt;li>Make it work.&lt;/li>
&lt;li>Make it better.&lt;/li>
&lt;/ol>
&lt;p>TDD is not just about the already known “red-green-refactor” mentality, but mostly about the workflow that helps you
understand the constant design decisions you are doing every time for every logic you are designing.&lt;/p>
&lt;blockquote>
&lt;p>TDD is about constant feedback about your decisions.&lt;/p>
&lt;/blockquote>
&lt;p>In the context of OOP (to make the examples clearer), there are always tons of different ways to design your class:&lt;/p>
&lt;ul>
&lt;li>What’s the name of this method’s class?&lt;/li>
&lt;li>What are the dependencies or collaborators for this class?&lt;/li>
&lt;li>How will this class behave when I use this other class inside it?&lt;/li>
&lt;li>What’s the expected outcome of this method when I give these arguments?&lt;/li>
&lt;li>etc, etc…&lt;/li>
&lt;/ul>
&lt;p>We ask these questions (and many more) every time, and we do give them an answer as well, but usually without any
rational thinking or feedback about it. We just do what we think is “the best” at that particular time focusing on
making something work, but is it enough to make it work?&lt;/p>
&lt;h2 id="the-constant-feedback-loop">The constant feedback loop
&lt;a class="heading-anchor" href="#the-constant-feedback-loop" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Testing is not just a great tool because it gives you some safety-net so you can refactor with confidence, but also
because it helps design a better system. &lt;strong>How is that?&lt;/strong> Because before going for any solution, it makes you think about
the decisions that you need to do. You are challenging yourself to understand the arguments of your decisions, and why A
and not B is a better solution in a particular context.&lt;/p>
&lt;p>BDD and TDD aren’t mutually exclusive, in fact, they can and should coexist. It depends mostly on the context of what
you want to build and test.&lt;/p>
&lt;p>&lt;img src="/images/blog/2021-09-25/bdd-and-tdd.webp" alt="blog-bdd-and-tdd" />&lt;/p>
&lt;p>BDD is about Test-First feature development. The goal is not how but what. The feedback loop is long because you will
get “green” feedback once the feature is implemented and working as expected.&lt;/p>
&lt;p>TDD is also another Test-First driven but, unlike BDD, it is about a shorter and quicker feedback loop.&lt;/p>
&lt;ol>
&lt;li>First, you &lt;strong>specify what you want&lt;/strong>. You think about the design of your class or method. Its name or signature. Its
dependencies. But all of this by baby steps, one at a time.&lt;/li>
&lt;li>Second, you &lt;strong>make that little thing work&lt;/strong> in the simplest possible way.&lt;/li>
&lt;li>Finally, you &lt;strong>make it better&lt;/strong>. Because software is hard and complicated enough to make it right on the first try, so
refactoring is a must to keep a system healthy. At this point, with a “green running test”, you can refactor and
improve your logic safely.&lt;/li>
&lt;/ol>
&lt;p>The above is basically TDD, right, but… what’s so special about it? The constant &lt;strong>feedback loop&lt;/strong> and &lt;strong>design
decisions&lt;/strong> that you need to make before you are actually writing the solution. This is the power of TDD.&lt;/p>
&lt;h3 id="why-so-small-steps-in-tdd">Why so small steps in TDD?
&lt;a class="heading-anchor" href="#why-so-small-steps-in-tdd" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Theoretically you “must” write little steps for every iteration, but why? &lt;strong>It’s all about the feedback loop&lt;/strong>. This is
up to you, your expectations, and your experience with testing.&lt;/p>
&lt;p>&lt;img src="/images/blog/2021-09-25/footer.jpg" alt="small steps in the tdd feedback loop" />&lt;/p>
&lt;hr />
&lt;h3 id="resources">Resources
&lt;a class="heading-anchor" href="#resources" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;a rel="external" href="https://chemaclass.com/blog/test-driven-development/">https://chemaclass.com/blog/test-driven-development/&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://blog.testlodge.com/tdd-vs-bdd/">https://blog.testlodge.com/tdd-vs-bdd/&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>Test-Driven (Development)</title><subtitle>What is challenging about it?</subtitle><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><published>2021-08-01T00:00:00+00:00</published><updated>2021-08-01T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/test-driven-development/"/><id>https://chemaclass.com/blog/test-driven-development/</id><summary type="html">TDD is a design practice, not just a testing technique. Writing tests first changes how you think about code and shape its structure.</summary><content type="html">&lt;p>The complexity here is not about writing tests itself, but the habits that we have to change to create software that is
easy to be tested.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="the-root-of-the-problem">The root of the problem
&lt;a class="heading-anchor" href="#the-root-of-the-problem" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Without (a solid) experience in testing, developers can have a hard time while trying to apply testing in general as
part of their daily job. It’s not simply because of the topic’s complexity, but &lt;strong>because they are used to writing
code that is hard to test.&lt;/strong>&lt;/p>
&lt;p>Writing tests for already working software (mainly when it was done without considering testing at all) translates as
something boring, next to useless, usually accompanied by a lack of motivation, blaming the wrong subject in this
context: “the tests itself are reducing my speed”.&lt;/p>
&lt;blockquote>
&lt;p>In a domain context, if a piece of software logic is hard to be tested, the problem is not the test, but the code which wasn’t well written.&lt;/p>
&lt;/blockquote>
&lt;p>There are already hundreds of tutorials, books, and documentation about testing, but I can share my experience and how I
do apply this great (and mandatory) philosophy in my daily work.&lt;/p>
&lt;h3 id="test-driven-is-based-on-this-simple-rule">Test-Driven is based on this simple rule
&lt;a class="heading-anchor" href="#test-driven-is-based-on-this-simple-rule" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Instead of: design code -&amp;gt; develop code -&amp;gt; write tests.&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="/images/blog/2021-08-01/non-tdd-style.png" alt="non-tdd-style" />&lt;/p>
&lt;ul>
&lt;li>It’s about: write failing automated test -&amp;gt; run failing test -&amp;gt; develop code to make test pass -&amp;gt; run test -&amp;gt; repeat.&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="/images/blog/2021-08-01/tdd-style.png" alt="tdd-style" />&lt;/p>
&lt;p>The idea of driving your code by testing can be understood depending on the abstraction level of what you’re writing at
the moment. You don’t want to create a wrong coupling between the tests and the code being tested. You want to test the
behavior of your logic.&lt;/p>
&lt;p>TDD is based on a loop of baby steps that helps you find &lt;strong>patterns&lt;/strong> and guide your software design every little iteration
with &lt;strong>constant refactorings&lt;/strong>. It’s the best choice if you want to ensure the expected behavior of all possible paths of
your logic.&lt;/p>
&lt;p>The beauty of this is that you don’t need to know the full algorithm from the very beginning. Instead, you are
&lt;strong>discovering&lt;/strong> how your logic should be by expressing the desired implementation, step by step, on automated tests.&lt;/p>
&lt;p>Considering writing tests for your software at the same time you’re writing it, will &lt;strong>irremediably force you to write
better software&lt;/strong>. Because you want to write software which has to be easy to be tested, and therefore it will end up with
higher quality.&lt;/p>
&lt;blockquote>
&lt;p>I already wrote another post about the relation between software &lt;strong>quality and testing&lt;/strong>: &lt;a href="/blog/the-art-of-testing/">The Art of Testing: where design meets quality&lt;/a>.&lt;/p>
&lt;/blockquote>
&lt;h2 id="improve-your-test-driven-skills">Improve your Test-Driven skills
&lt;a class="heading-anchor" href="#improve-your-test-driven-skills" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>&lt;img src="/images/blog/2021-08-01/tdd-style-with-git.png" alt="tdd-style" />&lt;/p>
&lt;p>The best way to learn Test-Driven is doing software katas. Try them alone and with others. Both are equally important.&lt;/p>
&lt;ul>
&lt;li>Alone: to challenge your inner self without any distraction but yourself.&lt;/li>
&lt;li>With others: pair-programming is essential in our job. Katas are the best tools to train our communication skills and
learn together from each other.&lt;/li>
&lt;/ul>
&lt;h3 id="what-s-a-code-kata">What’s a Code Kata?
&lt;a class="heading-anchor" href="#what-s-a-code-kata" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Software developers don’t practice enough. Most of our learning takes place on the job, which means that most of our
mistakes get made there as well.&lt;/p>
&lt;p>Other creative professions practice: musicians play technical pieces, poets constantly rewrite works. In karate, most of
a student’s time is spent learning and refining basic moves. These are katas.&lt;/p>
&lt;h3 id="what-is-the-goal-of-a-kata-what-should-we-have-at-the-end">What is the goal of a kata? What should we have at the end?
&lt;a class="heading-anchor" href="#what-is-the-goal-of-a-kata-what-should-we-have-at-the-end" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Katas exist to help developers get the same benefits as practicing in any other profession. There are simple, artificial
exercises that let you experiment and learn without the pressure of a production environment.&lt;/p>
&lt;blockquote>
&lt;p>There are no right or wrong answers in any software kata: the benefit comes from the process, not from the result.&lt;/p>
&lt;/blockquote>
&lt;h3 id="tips">Tips
&lt;a class="heading-anchor" href="#tips" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Once you have resolved a kata, try it again in a few weeks or months.&lt;/li>
&lt;li>Try to explore new solutions. Be creative in the process and do not rush.&lt;/li>
&lt;li>When doing them in groups, they are not a competition to see who will accomplish more of the exercise.&lt;/li>
&lt;li>The focus should be on the process, never on the result.&lt;/li>
&lt;li>The true valuable outcome of any kata is the learnings that you (and your team) will get after talking about it and
sharing your experiences.&lt;/li>
&lt;/ul>
&lt;p>You can find a lot of katas on the Internet. For example:&lt;/p>
&lt;ul>
&lt;li>&lt;a rel="external" href="http://codekata.com">http://codekata.com&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://codingdojo.org/kata">https://codingdojo.org/kata&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://github.com/gamontal/awesome-katas">https://github.com/gamontal/awesome-katas&lt;/a>&lt;/li>
&lt;/ul>
&lt;hr />
&lt;h3 id="tdd-is-more-as-a-workflow-than-a-design">TDD is more as a workflow than a design
&lt;a class="heading-anchor" href="#tdd-is-more-as-a-workflow-than-a-design" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;blockquote>
&lt;p>“TDD is a design tool.” That’s what Sandro has said for years. But not anymore. After working with different teams and in different organisations, and also carefully inspecting how he works, Sandro changed his mind about the role of TDD in software design.&lt;/p>
&lt;/blockquote>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/KyFVA4Spcgg"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div>
&lt;p>TDD in a nutshell; it’s about the rhythm.&lt;/p>
&lt;ol>
&lt;li>Specify what you want.&lt;/li>
&lt;li>Make it work.&lt;/li>
&lt;li>Make it better.&lt;/li>
&lt;/ol>
&lt;hr />
&lt;h2 id="kent-beck">Kent Beck
&lt;a class="heading-anchor" href="#kent-beck" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;blockquote class="twitter-tweet">&lt;p lang="en" dir="ltr">1. Change the code as usual&lt;br>2. Write a test that only passes after the change&lt;br>3. Revert to before 1&lt;br>4. Type the test again (copy/paste is cheating &amp;amp; invalidates the warranty of the exercise)&lt;br>5. Make it compile by changing the code&lt;br>6. See it fail&lt;br>7. Change the code to make it pass&lt;/p>&amp;mdash; Kent Beck 🌻 (@KentBeck) &lt;a href="https://twitter.com/KentBeck/status/1421257650113634304?ref_src=twsrc%5Etfw">July 30, 2021&lt;/a>&lt;/blockquote> &lt;script async src="https://platform.twitter.com/widgets.js" charset="utf-8">&lt;/script>
&lt;hr />
&lt;p>Images original by &lt;a rel="external" href="https://x.com/evrtrabajo">Emmanuel Valverde Ramos&lt;/a>.&lt;/p></content></entry><entry xml:lang="en"><title>Phel: The Lisp That Compiles to PHP</title><subtitle>A new functional language with full interoperability with PHP</subtitle><category term="phel" scheme="https://chemaclass.com/tags/phel/" label="Phel"/><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="open-source" scheme="https://chemaclass.com/tags/open-source/" label="Open Source"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2021-02-01T00:00:00+00:00</published><updated>2021-02-01T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/phel-first-release/"/><id>https://chemaclass.com/blog/phel-first-release/</id><summary type="html">The new Functional Programming language build-in for PHP.</summary><content type="html">&lt;p>A new Functional Programming language built-in on the PHP ecosystem.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="the-phel-language">The Phel Language
&lt;a class="heading-anchor" href="#the-phel-language" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Phel is a Functional Programming (&lt;a rel="external" href="https://en.wikipedia.org/wiki/Functional_programming">FP&lt;/a>) language that compiles to PHP. It is a dialect of &lt;a rel="external" href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp&lt;/a> inspired by Clojure and Janet.&lt;/p>
&lt;h2 id="features">Features
&lt;a class="heading-anchor" href="#features" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>Built on PHP’s ecosystem&lt;/li>
&lt;li>Good error reporting&lt;/li>
&lt;li>Different Datastructures (Arrays, Tables, and Tuples)&lt;/li>
&lt;li>Macros&lt;/li>
&lt;li>Recursive functions&lt;/li>
&lt;li>Powerful but simple Syntax&lt;/li>
&lt;li>REPL&lt;/li>
&lt;/ul>
&lt;h2 id="why-phel">Why Phel?
&lt;a class="heading-anchor" href="#why-phel" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Phel is a creation of Jens Haase, and as he mentions on the website, “it is the result of many &lt;a rel="external" href="https://phel-lang.org/blog/functional-programming-in-php">failed attempts to do functional programming in PHP&lt;/a>”. He was looking for something with these characteristics:&lt;/p>
&lt;ul>
&lt;li>A LISP-inspired&lt;/li>
&lt;li>Functional Programming language&lt;/li>
&lt;li>That runs on cheap hosting providers&lt;/li>
&lt;li>It is easy to write and debug&lt;/li>
&lt;/ul>
&lt;p>From my point, it was ending May 2020 when I finished the
book &lt;a href="/readings/7-languages-in-7-weeks/">Seven Languages in Seven Weeks&lt;/a>, in which I learned the basics of Prolog, Erlang,
Clojure, Haskell… I even created a &lt;a rel="external" href="https://github.com/Chemaclass/7LangIn7Weeks">repository in GitHub&lt;/a> to store my
progress over the weeks with these learnings.&lt;/p>
&lt;p>Suddenly, one day beginning of June 2020, I saw a post in Reddit’s PHP official channel, with a message of someone
mentioning this “&lt;strong>Phel&lt;/strong>” project, claiming to be a &lt;strong>“functional programming language” written in PHP&lt;/strong>!&lt;/p>
&lt;p>I’ve worked with PHP for around 8 years so far, and I always try to combine OOP with FP as much as possible (because I
believe both paradigms are better combined; they are not necessarily exclusive), and a new dialect of Lisp fully written
in native &lt;strong>PHP 7.4&lt;/strong> sounded really exciting. I decided to take a look at it. I wanted to see the code.&lt;/p>
&lt;p>The fresh readings from &lt;a rel="external" href="https://x.com/matthiasnoback">Mathias NoBack&lt;/a> books such
as &lt;a href="/readings/object-design-style-guide">Object Design Style Guide&lt;/a>,
and &lt;a href="/readings/advance-web-application-architecture/">Advanced Web Application Architecture&lt;/a>, gave me the courage to try
out what I learned and put it for real in a project. This was (and still is) a beautiful challenge, which helps me to
test myself and grow professionally at the same time.&lt;/p>
&lt;p>My first commit was done on 6 June 2020, and since then I’ve been dedicating a little time each day during my free
hours to its development.
It helps me to learn more about FP, languages’ internal design, and best practices on testing and software design
architecture in general.&lt;/p>
&lt;p>I started applying minor changes until I ended refactoring the whole compiler structure (among other things) in order to
make the modules easier to read and understand. Psalm, strict types, good test coverage, unifying code style… These were
some of my favorite topics.&lt;/p>
&lt;h2 id="example-of-phel-code">Example of Phel code
&lt;a class="heading-anchor" href="#example-of-phel-code" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>The following example gives a short impression of how Phel looks like:&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="plain">&lt;span class="giallo-l">&lt;span># Define a namespace&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>(ns my\example)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span># Define a variable with name &amp;quot;my-name&amp;quot; and value &amp;quot;world&amp;quot;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>(def my-name &amp;quot;world&amp;quot;)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span># Define a func with name &amp;quot;print-name&amp;quot; and one argument&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>(defn print-name [your-name]&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> (print &amp;quot;hello&amp;quot; your-name))&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span># Call the function&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>(print-name my-name)&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h2 id="current-status-of-phel">Current status of Phel
&lt;a class="heading-anchor" href="#current-status-of-phel" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>We prepared an easy &lt;a rel="external" href="https://phel-lang.org/documentation/getting-started/">Getting Started Guide&lt;/a>&lt;/li>
&lt;li>We prepared an easy GitHub Repository with a &lt;a rel="external" href="https://github.com/phel-lang/phel-scaffolding">Phel Scaffolding Template&lt;/a>&lt;/li>
&lt;li>We just released the first official version: &lt;a rel="external" href="https://github.com/phel-lang/phel-lang/tags">0.1.0&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Check out the official website with all documentation: &lt;a rel="external" href="https://phel-lang.org/">https://phel-lang.org/&lt;/a>&lt;/p>
&lt;hr />
&lt;h3 id="also-recommended">Also recommended
&lt;a class="heading-anchor" href="#also-recommended" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;a rel="external" href="https://jesusvalera.dev/functional-programming-with-phel/">Functional Programming with Phel - JesusValera&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>To Mock or Not to Mock</title><subtitle>How to escape the mocking hell</subtitle><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><published>2021-01-11T00:00:00+00:00</published><updated>2021-01-11T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/to-mock-or-not-to-mock/"/><id>https://chemaclass.com/blog/to-mock-or-not-to-mock/</id><summary type="html">Mocking is useful, but 'what to mock' usually turns out to be a more complicated than expected if you don't treat this carefully.</summary><content type="html">&lt;p>Mocking is useful, but “what to mock” usually turns out to be more complicated than expected if you don’t treat
this carefully.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h4 id="how-to-escape-the-mocking-hell">How to escape the mocking hell
&lt;a class="heading-anchor" href="#how-to-escape-the-mocking-hell" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;p>What is actually happening when we create a mock? Which types of mocks are there? Is mocking good or bad? Well, as
always, everything depends on the context. And here we will consider some of the main situations about when to mock and
when not to mock, but especially why.&lt;/p>
&lt;h2 id="what-happens-when-you-mock-something">What happens when you mock something?
&lt;a class="heading-anchor" href="#what-happens-when-you-mock-something" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>First, we should define what is a mock:&lt;/p>
&lt;blockquote>
&lt;p>In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when it is impractical or impossible to incorporate a real object into a unit test.&lt;/p>
&lt;/blockquote>
&lt;p>Mocking makes sense in a &lt;em>unit testing&lt;/em> context. An integration test should go through the real implementation checking
the integration between multiple units, which are even allowed to talk to the DB or File IO: infrastructure code.
Therefore we should agree that &lt;em>a unit test is a fast and deterministic test that doesn’t rely on external dependencies
and doesn’t require any special context to run&lt;/em>.&lt;/p>
&lt;p>Mock objects meet the &lt;em>interface&lt;/em> requirements. In consequence, they allow us to write and unit-test functionality
without calling complex underlying or collaborating classes.&lt;/p>
&lt;p>A mock is a test double that stands in for real implementation code during the unit testing process. It is also capable
of producing assertions about how it was manipulated by the test subject during the test run.&lt;/p>
&lt;blockquote>
&lt;p>I strongly recommend you to read this post if you want to get into the details of why &lt;a rel="external" href="https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a">Mocking is a code smell&lt;/a> (Topics like these: What is a mock? What is a unit test? What is test coverage? What is tight coupling? What causes tight coupling? What does composition have to do with mocking? How do we remove coupling? and more!)&lt;/p>
&lt;/blockquote>
&lt;h2 id="the-problem-with-mocking">The problem with mocking
&lt;a class="heading-anchor" href="#the-problem-with-mocking" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>When you mock you are overriding the logic of the mocked class. The real logic is getting hidden behind the scenes and
there is actually where bugs love to live. Consider that:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>The mock may have attributes, methods, or arguments that the real object doesn’t.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>The mock’s &lt;em>return values may differ from the real objects’ return values&lt;/em>. For example, it may return a different
type of object that has different attributes.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>The mock’s &lt;em>side effects and behavior may differ from the real objects’ ones&lt;/em>. For example, maybe the mock fails to
raise an exception when the real object would raise it.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="alternatives-to-mocking">Alternatives to mocking
&lt;a class="heading-anchor" href="#alternatives-to-mocking" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>“Are you saying that mocking is bad and we shouldn’t mock?!” No.&lt;/p>
&lt;p>It depends on what you are “overriding”.&lt;/p>
&lt;ul>
&lt;li>Is your business domain logic what you are mocking? Then it’s wrong.&lt;/li>
&lt;li>Is the connection to the DB what you are mocking? Then it’s right.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>It depends on the context of the logic and where that logic belongs.&lt;/p>
&lt;/blockquote>
&lt;p>Is it part of your business domain logic? Then you shouldn’t mock it but instantiate it.&lt;/p>
&lt;p>Is it part of any infrastructure dependency like DB connection, IO file system, Network, or any external service that
has nothing to do directly with your business domain? Then &lt;em>mock it using abstractions/interfaces&lt;/em>.&lt;/p>
&lt;p>The interface should be the &lt;em>contract between your business domain logic and its external infrastructure dependencies&lt;/em>.
Imagine how easy it would be to unit test your domain logic by instantiating it and calling their methods with different
arguments expecting different inputs under your entire control.&lt;/p>
&lt;h2 id="some-tricks">Some tricks
&lt;a class="heading-anchor" href="#some-tricks" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>When you are writing a unit test:&lt;/p>
&lt;ul>
&lt;li>Try to instantiate your classes first.&lt;/li>
&lt;li>Avoid mocking concrete classes. I wrote an article exclusively about this:
encouraging &lt;a rel="external" href="https://medium.com/swlh/final-classes-in-php-9174e3e2747e">final classes&lt;/a> and interfaces.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Mock interfaces. Instantiate concrete classes.&lt;/p>
&lt;/blockquote>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/RbSqXFUfRMU"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div>
&lt;p>“Excessive use of mocks leads to legacy code.” - Philippe Boargau&lt;/p>
&lt;h3 id="how-can-we-avoid-excessive-mocking">How can we avoid excessive mocking?
&lt;a class="heading-anchor" href="#how-can-we-avoid-excessive-mocking" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Favor immutable state over a mutable state.&lt;/li>
&lt;li>Make dependencies explicit.&lt;/li>
&lt;li>Program to an interface, not to an implementation.&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="/images/blog/2021-01-11/footer.webp" alt="mock interfaces, instantiate concrete classes" />&lt;/p>
&lt;hr />
&lt;h4 id="references">References
&lt;a class="heading-anchor" href="#references" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;ul>
&lt;li>&lt;a rel="external" href="https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a">Mocking is a code smell&lt;/a> - Eric Elliott&lt;/li>
&lt;li>&lt;a rel="external" href="https://blog.cleancoder.com/uncle-bob/2014/05/10/WhenToMock.html">When to mock&lt;/a> &amp;amp; &lt;a rel="external" href="https://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.html">Test Definitions&lt;/a> - Uncle Bob&lt;/li>
&lt;li>&lt;a rel="external" href="https://matthiasnoback.nl/2018/09/final-classes-by-default-why/">Final classes by default&lt;/a> - Matthias Noback&lt;/li>
&lt;li>&lt;a rel="external" href="https://www.seanh.cc/2017/03/17/the-problem-with-mocks/">The problem with mocks&lt;/a> - Sean Hammond&lt;/li>
&lt;li>&lt;a rel="external" href="https://www.artima.com/weblogs/viewpost.jsp?thread=126923">A Set of Unit Testing Rules&lt;/a> - Michael Feathers&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>Principles of package design</title><subtitle>Creating Reusable Software Components</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2020-11-12T00:00:00+00:00</published><updated>2020-11-12T00:00:00+00:00</updated><author><name>
Matthias Noback</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/packaging-design/"/><id>https://chemaclass.com/readings/packaging-design/</id><summary type="html">Apply design principles to your classes, preparing them for reuse. You will use package design principles to create packages that are just right in terms of cohesion and coupling, and are user- and maintainer-friendly at the same time.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>Apply design principles to your classes, preparing them for reuse. You will use package design principles to create
packages that are just right in terms of cohesion and coupling, and are user- and maintainer-friendly at the same time.&lt;/p>
&lt;p>The first part of this book walks you through the five SOLID principles that will help you improve the design of your
classes. The second part introduces you to the best practices of package design, and covers both package cohesion
principles and package coupling principles. Cohesion principles show you which classes should be put together in a
package, when to split packages, and if a combination of classes may be considered a “package” in the first place.
Package coupling principles help you choose the right dependencies and prevent wrong directions in the dependency graph
of your packages.&lt;/p>
&lt;h3 id="what-you-ll-learn">What You’ll Learn
&lt;a class="heading-anchor" href="#what-you-ll-learn" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Apply the SOLID principles of class design&lt;/li>
&lt;li>Determine if classes belong in the same package&lt;/li>
&lt;li>Know whether it is safe for packages to depend on each other&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>Typed Arrays in PHP</title><subtitle>An alternative to the missing feature in PHP: Generics</subtitle><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><published>2020-10-13T00:00:00+00:00</published><updated>2020-10-13T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/typed-arrays-php/"/><id>https://chemaclass.com/blog/typed-arrays-php/</id><summary type="html">Argument unpacking, function variable argument list, and variadics function.</summary><content type="html">&lt;p>Argument unpacking, function variable argument list, and variadics function.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h3 id="the-perfect-combination">The perfect combination
&lt;a class="heading-anchor" href="#the-perfect-combination" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Argument unpacking: Instead of passing the argument itself to the function, the elements it contains will be passed (as individual arguments).&lt;/li>
&lt;li>Function variable argument list: The arguments will be passed into the given variable as an array.&lt;/li>
&lt;li>Variadics function: Types can be checked with a type-hint.&lt;/li>
&lt;/ul>
&lt;p>We will use this snippet for our examples.
Having a class, &lt;code>Customer&lt;/code>:&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">/**&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> * @psalm-immutable &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> */&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">final&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> class&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> Customer&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> Using PHP 8 constructor property promotion&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> https://wiki.php.net/rfc/constructor_promotion&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> public&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> function&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> __construct&lt;/span>&lt;span>(&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> public&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> string&lt;/span>&lt;span> $&lt;/span>&lt;span>name&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> )&lt;/span>&lt;span> {&lt;/span>&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">//&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> We create a list of 6 customers&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> =&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> array_map&lt;/span>&lt;span>(&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> fn&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">int&lt;/span>&lt;span> $&lt;/span>&lt;span>i&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">:&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span> =&amp;gt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">name-&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">{&lt;/span>&lt;span>$&lt;/span>&lt;span>i&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">}&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;quot;&lt;/span>&lt;span>)&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#005CC5, #79B8FF);"> range&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">1&lt;/span>&lt;span>,&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> 6&lt;/span>&lt;span>)&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>Whenever we want to manipulate a list of Customers, we can pass as an argument: &lt;code>…$customers&lt;/code>.&lt;/p>
&lt;h2 id="how-we-used-to-do-it">How we used to do it
&lt;a class="heading-anchor" href="#how-we-used-to-do-it" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>We define the array type using the PHPDoc param comment block above. But we cannot define the real type of the item. The code will still run without any problem passing any type on that argument &lt;code>array $customers&lt;/code>:&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">/**&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> * &lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">@param&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">[]&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> */&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">function&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">array&lt;/span>&lt;span> $&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">:&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> void&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> foreach&lt;/span>&lt;span> (&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> as&lt;/span>&lt;span> $&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> ... some irrelevant logic for this example&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>The code below would work at “compile-time”. But it might fail at “runtime”.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">any name&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> AnyOtherType&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>An alternative (recommended!) might be to extract that logic and ask for the particular type in order to “check it” at runtime in that particular moment, failing if one of the items wasn’t really a Customer:&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">/**&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> * &lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">@param&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">[]&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> */&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">function&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">array&lt;/span>&lt;span> $&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">:&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> void&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> foreach&lt;/span>&lt;span> (&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> as&lt;/span>&lt;span> $&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoice&lt;/span>&lt;span>(&lt;/span>&lt;span>$&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">function&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoice&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">Customer&lt;/span>&lt;span> $&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">:&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> void&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> ... some irrelevant logic for this example&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>Everything here below would work at “compile-time”. It will for sure break during “runtime” if the &lt;code>createInvoice(Customer $customer)&lt;/code> receives something different than a Customer.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">any name&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span>[&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> AnyOtherType&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>]&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> won&amp;#39;t work&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>By doing that &lt;code>createInvoice(Customer $customer)&lt;/code> we are ensuring the type of the argument, which is good! But, what about going one step further. Could we check the types of the elements when calling the function &lt;code>createInvoiceForCustomers(array $customers)&lt;/code>, even making the IDE complain when the types are not right?&lt;/p>
&lt;p>Well, that’s actually what Generics are for, but sadly, they are not yet in PHP. Not even in the upcoming PHP 8. Hopefully in a near future, but we cannot predict that for now.
Luckily, we have currently an alternative nowadays, but it’s not that popular. It has its own “pros” and “cons”, so let’s take a look at an example first:&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">function&lt;/span>&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">Customer&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> ...&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">:&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> void&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>{&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> foreach&lt;/span>&lt;span> (&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);"> as&lt;/span>&lt;span> $&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span> {&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);"> createInvoice&lt;/span>&lt;span>(&lt;/span>&lt;span>$&lt;/span>&lt;span>customer&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span> }&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>}&lt;/span>&lt;/span>&lt;/code>&lt;/pre>
&lt;p>Everything here below would work at “compile-time”. It will for sure break during “runtime” if the &lt;code>createInvoice()&lt;/code> receives something different than a Customer.&lt;/p>
&lt;pre class="giallo" style="color-scheme: light dark; color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);">&lt;code data-lang="php">&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);">&amp;lt;&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">?&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);">php&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">...&lt;/span>&lt;span>$&lt;/span>&lt;span>customers&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> OK&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">any name&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span>)&lt;/span>&lt;span>,&lt;/span>&lt;span> &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#D73A49, #F97583);"> new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> Customer&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">any name&lt;/span>&lt;span style="color: light-dark(#032F62, #9ECBFF);">&amp;#39;&lt;/span>&lt;span>)&lt;/span>&lt;span>,&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> //&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> OK&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">//&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> This is not even possible to write. The IDE will yell at you. &lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6A737D, #6A737D);">//&lt;/span>&lt;span style="color: light-dark(#6A737D, #6A737D);"> It&amp;#39;s expecting a `Customer`, but `AnyOtherType` is given:&lt;/span>&lt;/span>
&lt;span class="giallo-l">&lt;span style="color: light-dark(#6F42C1, #B392F0);">createInvoiceForCustomers&lt;/span>&lt;span>(&lt;/span>&lt;span style="color: light-dark(#D73A49, #F97583);">new&lt;/span>&lt;span style="color: light-dark(#005CC5, #79B8FF);"> AnyOtherType&lt;/span>&lt;span>(&lt;/span>&lt;span>)&lt;/span>&lt;span>)&lt;/span>&lt;span>;&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;h3 id="pros">PROS
&lt;a class="heading-anchor" href="#pros" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>We can easily type a list of any concrete type.&lt;/li>
&lt;/ul>
&lt;h3 id="cons">CONS
&lt;a class="heading-anchor" href="#cons" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>We better define our functions with one or two arguments max. Otherwise, it would be too complicated to read.&lt;/li>
&lt;/ul>
&lt;h3 id="important-remarks">Important remarks
&lt;a class="heading-anchor" href="#important-remarks" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>It needs to be the last taken argument of a function.&lt;/li>
&lt;li>It helps to minimize the number of arguments that we use in a function.&lt;/li>
&lt;/ul>
&lt;h2 id="variadics-simulate-typed-arrays-use-them-wisely">Variadics simulate typed arrays, use them wisely
&lt;a class="heading-anchor" href="#variadics-simulate-typed-arrays-use-them-wisely" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Argument unpacking is a great feature that, in combination with variadic functions, can help us to simulate typed arrays. With great power comes great responsibility, and this is no exception.
We need to learn about our toolbox in order to use it wisely.&lt;/p>
&lt;p>&lt;img src="/images/blog/2020-10-13/footer.webp" alt="typed arrays with variadics in php" />&lt;/p>
&lt;hr />
&lt;h3 id="references">References
&lt;a class="heading-anchor" href="#references" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;a rel="external" href="https://wiki.php.net/rfc/argument_unpacking">Argument unpacking&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list">Function variable argument list&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://wiki.php.net/rfc/variadics">Variadics function&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>Object design style guide</title><subtitle>Powerful Techniques for Creating Flexible, Readable, and Maintainable Object-Oriented Code in Any OO Language</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><published>2020-10-10T00:00:00+00:00</published><updated>2020-10-10T00:00:00+00:00</updated><author><name>
Matthias Noback</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/object-design-style-guide/"/><id>https://chemaclass.com/readings/object-design-style-guide/</id><summary type="html">Matthias Noback's practical guide to writing flexible, readable, and maintainable object-oriented code, with techniques applicable to any OO language from Python to PHP.</summary><content type="html">&lt;p>Objects are the central concept of languages like Java, Python, C#. Applying best practices for object design means that
your code will be easy to read, write, and maintain.&lt;/p>
&lt;p>This book captures dozens of techniques for creating pro-quality OO code that can stand the
test of time.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>Examples are in an instantly-familiar pseudocode, teaching techniques you can apply to any OO language, from C++ to PHP.&lt;/p></content></entry><entry xml:lang="en"><title>Domain-Driven Design Distilled</title><category term="ddd" scheme="https://chemaclass.com/tags/ddd/" label="Ddd"/><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2020-09-10T00:00:00+00:00</published><updated>2020-09-10T00:00:00+00:00</updated><author><name>
Vaughn Vernon</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/domain-driven-design-distilled/"/><id>https://chemaclass.com/readings/domain-driven-design-distilled/</id><summary type="html">Domain-Driven Design Distilled brings DDD to life. Whether you're a developer, consultant, or customer, it will help you understand it, so you can benefit from its power.</summary><content type="html">&lt;p>Domain-Driven Design Distilled brings DDD to life. Whether you’re a developer, consultant, or customer, it will help
you understand it, so you can benefit from its power.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;hr />
&lt;p>This book delivers powerful results in practice, which is why developers worldwide are rapidly moving to adopt it.
This is an accessible guide to the basics:&lt;/p>
&lt;ul>
&lt;li>What is DDD?&lt;/li>
&lt;li>What problems does it solve?&lt;/li>
&lt;li>How does it work?&lt;/li>
&lt;li>How to quickly gain value from it?&lt;/li>
&lt;/ul>
&lt;p>You’ll learn how to segregate domain models using the powerful &lt;strong>Bounded Contexts&lt;/strong> pattern, to develop a &lt;strong>Ubiquitous
Language&lt;/strong> within an explicitly bounded context and help &lt;strong>domain experts&lt;/strong> and &lt;strong>developers work together&lt;/strong> to create
that language.&lt;/p>
&lt;p>How to use Subdomains to handle legacy systems and integrate multiple Bounded Contexts to define team relationships and
technical mechanisms.&lt;/p>
&lt;blockquote>
&lt;p>Domain-Driven Design Distilled brings &lt;strong>DDD&lt;/strong> to life.&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>Advanced Web Application Architecture</title><subtitle>The best guide that brings your coding skills a level up.</subtitle><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><published>2020-08-16T00:00:00+00:00</published><updated>2020-08-16T00:00:00+00:00</updated><author><name>
Matthias Noback</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/advance-web-application-architecture/"/><id>https://chemaclass.com/readings/advance-web-application-architecture/</id><summary type="html">The best guide that brings your coding and architecture skills a level up. All the modern PHP features combined with the elegance of a well designed modular design.</summary><content type="html">&lt;p>This book helps you get your web applications back in shape. It contains many techniques for decoupling from
infrastructure (like the framework or the database).&lt;/p>
&lt;p>“The best guide that brings your coding and architecture skills a level up. All the modern PHP features combined with the elegance of a well-designed modular design.”&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>In Part 1 we unlock a collection of design patterns which help you establish a clean separation between core and
infrastructure code. Part 2 shows how these design patterns resonate at a higher level with architectural concepts like
layers, ports and adapters (a.k.a. Hexagonal architecture). The book finishes with a discussion of testing strategies and design trade-offs.&lt;/p>
&lt;h3 id="what-you-ll-learn">What you’ll learn
&lt;a class="heading-anchor" href="#what-you-ll-learn" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Separating mixed code into core and infrastructure code by refactoring into patterns.&lt;/li>
&lt;li>Dividing your code into layers, and making a clear distinction between an application’s ports and adapters.&lt;/li>
&lt;li>Testing decoupled applications.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Buy the book: &lt;a rel="external" href="https://leanpub.com/web-application-architecture">https://leanpub.com/web-application-architecture&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;hr />
&lt;h3 id="design-patterns-for-modernizing-legacy-code-bases">Design patterns for modernizing legacy code bases
&lt;a class="heading-anchor" href="#design-patterns-for-modernizing-legacy-code-bases" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/WI1QY6OMglE"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Strict Types in PHP</title><subtitle>declare(strict_types=1);</subtitle><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2020-08-09T00:00:00+00:00</published><updated>2020-08-09T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/strict-types/"/><id>https://chemaclass.com/blog/strict-types/</id><summary type="html">Why declaring strict_types in PHP improves code readability and prevents silent type coercion bugs.</summary><content type="html">&lt;p>In December 2015, PHP 7 introduced scalar type declarations and with it the strict types flag. What is this new feature?&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>The good thing about declaring a PHP file as strict is that it actually applies to &lt;strong>ONLY the current file&lt;/strong>. It ensures that this file has strict types, but it doesn’t apply to any other file in the whole project. It allows you to do, step by step, this migration from non-strict code to strict code, especially for new files or projects.&lt;/p>
&lt;blockquote>
&lt;p>To enable the strict mode, a single declare directive must be placed at the top of the file. This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function’s return type.&lt;/p>
&lt;/blockquote>
&lt;h2 id="strict-types-affect-coercion-types">Strict types affect coercion types
&lt;a class="heading-anchor" href="#strict-types-affect-coercion-types" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Using a type hint without &lt;code>strict_types&lt;/code> may lead to subtle bugs.&lt;/p>
&lt;p>Without this, &lt;code>int $x&lt;/code> meant &lt;code>$x must have a value coercible to an int&lt;/code>. Any value that could be coerced to an int would pass the hint type, including:&lt;/p>
&lt;ul>
&lt;li>a proper &lt;code>int&lt;/code> (example: 42 -&amp;gt; 42)&lt;/li>
&lt;li>a &lt;code>float&lt;/code> (example: 13.1459 -&amp;gt; 13)&lt;/li>
&lt;li>a &lt;code>bool&lt;/code> (example: true -&amp;gt; 1)&lt;/li>
&lt;li>a &lt;code>null&lt;/code> (example: null -&amp;gt; 0)&lt;/li>
&lt;li>a &lt;code>string&lt;/code> with leading digits (example: “15 Trees” -&amp;gt; 15)&lt;/li>
&lt;/ul>
&lt;p>By setting &lt;code>strict_types=1&lt;/code>, you tell the engine that int $x means $x must only be an int proper, no type coercion allowed. You have the great assurance you’re getting exactly and only what was given, without any conversion or potential loss.&lt;/p>
&lt;h2 id="who-should-care-about-this-strict-type-line">Who should care about this “strict type” line?
&lt;a class="heading-anchor" href="#who-should-care-about-this-strict-type-line" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Actually, declare(strict_types=1); is more for the reader than for the writer. Why? Because it will explicitly tell the reader:&lt;/p>
&lt;ul>
&lt;li>The types in this current scope (file/class) are treated strictly.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>‘strict_types=1’ is more for the reader than for the writer&lt;/p>
&lt;/blockquote>
&lt;p>The writer just needs to maintain such strictness while writing the expected behavior. That said, as a writer, you should care about your readers, which also includes your future self. Because you are going to be one of them.&lt;/p>
&lt;p>&lt;img src="/images/blog/2020-08-09/footer.webp" alt="strict_types declaration in php" />&lt;/p>
&lt;hr />
&lt;h2 id="references">References
&lt;a class="heading-anchor" href="#references" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>&lt;a rel="external" href="https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.scalar-type-declarations">Scalar type declarations&lt;/a>&lt;/li>
&lt;li>&lt;a rel="external" href="https://stackoverflow.com/questions/48723637/what-do-strict-types-do-in-php/48723830#48723830">What do strict types do in PHP&lt;/a>&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>The Art of Refactoring</title><subtitle>When, how, and why</subtitle><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2020-06-28T00:00:00+00:00</published><updated>2020-06-28T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/the-art-of-refactoring/"/><id>https://chemaclass.com/blog/the-art-of-refactoring/</id><summary type="html">If you see something, in the scope of your current task, that can be easily improved, improve it. And if you have any questions about it, ask.</summary><content type="html">&lt;p>If you see something, in the scope of your current task, that can be easily improved, improve it. And if you have any questions about it, ask.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="what-is-refactoring">What is refactoring?
&lt;a class="heading-anchor" href="#what-is-refactoring" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Refactoring means improving your code. It can go from making a variable name more readable, extract some lines of code into a private method, or separate the responsibilities of a class into subclasses, for example.&lt;/p>
&lt;p>Refactoring is the action of showing that you care about what you do as a professional. It can be a controversial topic; it is indeed one of the major controversial topics since a long time ago. But we shouldn’t stop trying our best in order to improve the quality of the system just because of that controversiality.&lt;/p>
&lt;h2 id="when-and-how-should-we-refactor">When and how should we refactor?
&lt;a class="heading-anchor" href="#when-and-how-should-we-refactor" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Always. In the scope of your current task unless it is an already planned task, something like “architecture refactoring” or similar, where the scope of the task is actually to do refactoring.&lt;/p>
&lt;blockquote>
&lt;p>Refactoring should be part of our daily job, not a separate task by default.&lt;/p>
&lt;/blockquote>
&lt;p>We do not need to ask permission to refactor. Or do we ask our managers for permission to do our best job?&lt;/p>
&lt;p>In order to do proper refactoring, the intention of such refactoring needs to be clear. What is intended to achieve and how? Pair programming (or even pair thinking!) certainly helps in this topic because it syncs two brains on the same topic and that encourages team building and a better understanding of them.&lt;/p>
&lt;p>Applying refactoring in a collaborative way, in a “bidirectional channel”, is fundamental when working within a team. Refactoring shouldn’t be a taboo topic, on the contrary: it will be helpful in order to unify the goals and the direction of the team code quality.&lt;/p>
&lt;h3 id="some-personal-advice-about-the-how">Some personal advice about the “how”
&lt;a class="heading-anchor" href="#some-personal-advice-about-the-how" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Continuous improvement is what we’re looking for within this topic, but…&lt;/p>
&lt;ul>
&lt;li>If you realize your changes are generating more noise than help, stop immediately and think again if your changes are worth it in the current system status.&lt;/li>
&lt;/ul>
&lt;p>Maybe it’s not the right moment for that refactoring.&lt;/p>
&lt;p>Maybe you are polluting your current diff with out-scoped changes.&lt;/p>
&lt;p>Or, maybe, your refactoring idea is too big to be applied in your current task. In such a case, a follow-up task (in order to apply the refactoring) would be a better idea.&lt;/p>
&lt;ul>
&lt;li>If you see that refactoring is perhaps needed even before starting your current task, do the refactoring first.&lt;/li>
&lt;/ul>
&lt;p>We usually refactor in order to increase our productivity, making the code more readable and therefore easier to understand.&lt;/p>
&lt;h3 id="testing">Testing
&lt;a class="heading-anchor" href="#testing" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Be aware that you should have a pretty good suite of tests covering the logic that you might have changed. Without tests, refactoring can be really risky. Usually, the easier something is to be tested, the easier it is to be replaced or removed.&lt;/p>
&lt;p>You can read more about how testing is related to quality here.&lt;/p>
&lt;h2 id="why-should-we-do-it">Why should we do it?
&lt;a class="heading-anchor" href="#why-should-we-do-it" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>Wouldn’t you want to have a better system as time goes by?&lt;/p>
&lt;p>Software isn’t like wine: it doesn’t get better as time passes by. Therefore, if you want to have a better system you must work for it.&lt;/p>
&lt;p>&lt;img src="/images/blog/2020-06-28/footer.webp" alt="refactoring as continuous improvement" />&lt;/p></content></entry><entry xml:lang="en"><title>Final Classes in PHP | Java | Any</title><subtitle>Final, or not final, that's the question</subtitle><category term="php" scheme="https://chemaclass.com/tags/php/" label="Php"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><published>2020-06-06T00:00:00+00:00</published><updated>2020-06-06T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/final-classes/"/><id>https://chemaclass.com/blog/final-classes/</id><summary type="html">Clear contracts, isolated side effects, testability, low complexity and cognitive load, code fluidity, and confidence in yourself.</summary><content type="html">&lt;p>Clear contracts, isolated side effects, testability, low complexity and cognitive load, code fluidity, and confidence in yourself.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h2 id="motivation">Motivation
&lt;a class="heading-anchor" href="#motivation" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;h3 id="reduce-the-scope-visibility-to-the-minimum">Reduce the scope visibility to the minimum
&lt;a class="heading-anchor" href="#reduce-the-scope-visibility-to-the-minimum" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>When you see a class prefixed with final you will prevent a particular class to be extended by any other, which not only makes it more readable but also makes you be sure that the scope of the logic where you are is limited to that particular class.&lt;/p>
&lt;h3 id="encourage-composition-over-inheritance-mentality">Encourage “composition over inheritance” mentality
&lt;a class="heading-anchor" href="#encourage-composition-over-inheritance-mentality" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>The Open-Close Principle states: open for extension but closed for modification.&lt;/p>
&lt;p>If for any reason, a good one you should be completely aware of, you decide to create an inheritance there, well, then just drop the final keyword and you are good to go.&lt;/p>
&lt;p>When you “by default” can’t extend from a class (because it’s final), you will help yourself by thinking about using composition instead of inheritance.&lt;/p>
&lt;h2 id="why-isn-t-this-class-final">Why isn’t this class final?
&lt;a class="heading-anchor" href="#why-isn-t-this-class-final" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>If we aim for composition over inheritance, then we should try to avoid inheritance as much as possible, and use it only when it’s really necessary. Inheritance is often misused in OOP.&lt;/p>
&lt;h3 id="misconception">Misconception
&lt;a class="heading-anchor" href="#misconception" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>When we first taught OOP, we usually introduced the classic inheritance example.&lt;/p>
&lt;p>Nonetheless, when Alan Kay created Smalltalk, the inheritance was never the main concept of it. The main concept was messaging, which is that you can send messages to objects and they encapsulate the data and logic in it, and you can change their behavior by using different objects, which is actually composition. But the concept of inheritance is so popular that it eventually overshadows composition.&lt;/p>
&lt;h3 id="benefits">Benefits
&lt;a class="heading-anchor" href="#benefits" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Clear contracts. Using interfaces will force you to think in terms of communication between objects.&lt;/li>
&lt;li>Isolated, side effect free code units. Injecting interfaces only as dependencies will remove every nasty side effect around the code you are working on.&lt;/li>
&lt;li>Testability. Mocking dependencies is extremely easy when they are interfaces.&lt;/li>
&lt;li>Low, manageable complexity. Since everything is isolated, you won’t need to worry about rippling changes. This dramatically decreases the complexity of your code.&lt;/li>
&lt;li>Low cognitive load. With decreased complexity, your brain will be free to focus on what matters.&lt;/li>
&lt;li>Code fluidity. By removing any unnecessary coupling, you will be able to move things around way more easily than before.&lt;/li>
&lt;li>Confidence in yourself. Being able to test your code in isolation so well will give you a wonderful sense of confidence in changing it.&lt;/li>
&lt;/ul>
&lt;h2 id="composition-over-inheritance">Composition over inheritance
&lt;a class="heading-anchor" href="#composition-over-inheritance" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;p>If you feel the need to reconfigure an object, to change parts of an algorithm, or to rewrite part of the implementation, consider creating a new class instead of overriding an existing class.
If you need to represent a hierarchy of classes, where subclasses are proper substitutes for their parent classes. This would be the classic situation where you may still consider inheritance. However, the result may even be better if you don’t inherit from concrete parent classes but from abstract interfaces.&lt;/p>
&lt;h3 id="what-you-should-start-doing-instead">What you should start doing instead
&lt;a class="heading-anchor" href="#what-you-should-start-doing-instead" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Use interfaces to define the contracts between your classes.&lt;/li>
&lt;li>Use final classes to implement behavior for those interfaces.&lt;/li>
&lt;li>Use composition (using dependency injection through constructor) to put things together and prevent complexity.&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Interfaces -&amp;gt; Final classes -&amp;gt; Composition&lt;/p>
&lt;/blockquote>
&lt;p>&lt;img src="/images/blog/2020-06-06/footer.webp" alt="interfaces, final classes and composition" />&lt;/p></content></entry><entry xml:lang="en"><title>7 languages in 7 weeks</title><subtitle>Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="software" scheme="https://chemaclass.com/tags/software/" label="Software"/><category term="career" scheme="https://chemaclass.com/tags/career/" label="Career"/><published>2020-05-21T00:00:00+00:00</published><updated>2020-05-21T00:00:00+00:00</updated><author><name>
Bruce Tate</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/7-languages-in-7-weeks/"/><id>https://chemaclass.com/readings/7-languages-in-7-weeks/</id><summary type="html">A hands-on tour of Clojure, Haskell, Io, Prolog, Scala, Erlang, and Ruby. Learn how each language thinks differently and broaden your perspective on programming.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>You should learn a programming language every year, as recommended by The Pragmatic Programmer. But if one per year is
good, how about Seven Languages in Seven Weeks? In this book you’ll get a hands-on tour of Clojure, Haskell, Io, Prolog,
Scala, Erlang, and Ruby. Whether your favorite language is on that list, you’ll broaden your perspective of
programming by examining these languages side-by-side. You’ll learn something new from each, and best of all, you’ll
learn how to learn a language quickly.&lt;/p>
&lt;hr />
&lt;p>I created this repository with all my learnings while I was reading and playing aroung with the languages from this book
&lt;a rel="external" href="https://github.com/Chemaclass/7LangIn7Weeks">https://github.com/Chemaclass/7LangIn7Weeks&lt;/a>&lt;/p></content></entry><entry xml:lang="en"><title>The Art of Testing: Where Design Meets Quality</title><subtitle>From a software developer's point of view</subtitle><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><published>2020-04-07T00:00:00+00:00</published><updated>2020-04-07T00:00:00+00:00</updated><author><name>
Chemaclass</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/blog/the-art-of-testing/"/><id>https://chemaclass.com/blog/the-art-of-testing/</id><summary type="html">Why you should consider testing as part of your daily development habit and how it's directly linked to the software quality.</summary><content type="html">&lt;p>Why you should consider testing as part of your daily development habit and how it’s directly linked to the software
quality.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;p>This post intends not to explain the different testing techniques that we can use. I’m not going to tell you the differences between unit, integration, feature, or end-to-end testing.&lt;/p>
&lt;p>I’m still amazed by the lack of experience with testing in software in general. Common ignorance in this world about
best testing practices for us as developers. Inexperience that you can easily see if you have already worked on
different projects and teams.&lt;/p>
&lt;h3 id="software-testing">Software testing
&lt;a class="heading-anchor" href="#software-testing" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Some horrible patterns I’ve seen (and done):&lt;/p>
&lt;ul>
&lt;li>Testing for the sake of testing: testing every single file, sometimes wrongly considered a unit.&lt;/li>
&lt;li>Mocking every class we intend to test, overriding the actual implementation, and creating a fake behavior,
providing a false coverage perception.&lt;/li>
&lt;li>Coupling production code with tests everywhere, so it’s impossible to change anything without breaking some tests,
even if the feature itself it’s working as intended.&lt;/li>
&lt;li>Not testing at all because “why should we even test anything if the feature is done, and it works? Why should we
spend more time on this if it’s done?.”&lt;/li>
&lt;/ul>
&lt;p>One of the main reasons for software testing is actually verifying a suite of proofs for the expected behavior of the
final software piece. However, testing can (and should) be more than that.&lt;/p>
&lt;h3 id="software-design">Software design
&lt;a class="heading-anchor" href="#software-design" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Software design goes from algorithm to architecture design. Even when I believe these two levels of components have
different needs and requirements, they still share some common patterns. For example, testing, and this is what we are
going to talk about right now:&lt;/p>
&lt;blockquote>
&lt;p>If it’s easy to test, it will likely be because of good design.&lt;/p>
&lt;/blockquote>
&lt;h3 id="software-quality">Software quality
&lt;a class="heading-anchor" href="#software-quality" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Is quality hard to measure out? Indeed. There are different measurement keys that we should take while considering
quality for any piece of software. Still, I’m sure we could agree on this:&lt;/p>
&lt;blockquote>
&lt;p>If you aim for quality in your software, you better seek a good design.&lt;/p>
&lt;/blockquote>
&lt;p>Testing by itself means “proving,” as we all know. That said… how difficult it sometimes turns to prove some logic
that we finally give up because of its complexity itself?&lt;/p>
&lt;p>The art of testing is about using testing itself to help and contribute to the final result. To encourage good design,
suppose we’re able to use testing (of any kind) in our favor, depending on the context of what we want to prove. In that
case, it will undoubtedly help us increase the product’s end quality.&lt;/p>
&lt;p>Therefore, testing should be used not only to prove the behavior of our software but also to guide our software to a better design.&lt;/p>
&lt;p>Should we test everything? Well, that’s the million-dollar question. In my opinion, everything depends on the context. We
might encounter situations where tests might not be beneficial. Even in those situations, we should write our code as if
it could be tested anyway.&lt;/p>
&lt;p>&lt;img src="/images/blog/2020-04-07/footer.webp" alt="testable code and good design" />&lt;/p>
&lt;blockquote>
&lt;p>Testable code tends to better design and, therefore, better quality.&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>Clean Agile</title><subtitle>Back to Basics</subtitle><category term="agile" scheme="https://chemaclass.com/tags/agile/" label="Agile"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2020-03-12T00:00:00+00:00</published><updated>2020-03-12T00:00:00+00:00</updated><author><name>
Robert C. Martin</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/clean-agile/"/><id>https://chemaclass.com/readings/clean-agile/</id><summary type="html">This book is about Agile. What it was, what it is, and what it will be. This is a back-to-basics talk that covers the history of Agile, what motivated it, and what has happened to it since.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>
&lt;p>Clean Agile comes from Uncle Bob, one of the founding fathers of Agile, one of the seventeen people who authored the
&lt;a rel="external" href="https://agilemanifesto.org/">Agile Manifesto&lt;/a> back in 2001.&lt;/p>
&lt;hr />
&lt;p>This talk is about Agile. What it was, what it is, and what it will be. This is a back-to-basics talk that covers the
history of Agile, what motivated it, and what has happened to it since. The talk covers the basic practices of Agile,
and compares and contrasts those practices with the current menagerie of Agile processes.&lt;/p>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/FedQ2NlgxMI"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Working Effectively with Legacy Code</title><subtitle>Start-to-finish strategies for working with large, untested legacy code bases</subtitle><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><published>2019-07-01T00:00:00+00:00</published><updated>2019-07-01T00:00:00+00:00</updated><author><name>
Michael Feathers</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/working-effectively-with-legacy-code/"/><id>https://chemaclass.com/readings/working-effectively-with-legacy-code/</id><summary type="html">Michael Feathers presents practical techniques for adding tests to untested code, breaking dependencies, and safely refactoring large legacy systems without introducing bugs.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>&lt;h2 id="what-is-legacy-code">What is legacy code?
&lt;a class="heading-anchor" href="#what-is-legacy-code" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;blockquote>
&lt;p>Legacy code is simply code without tests.&lt;/p>
&lt;/blockquote>
&lt;h3 id="benefits-of-tests">Benefits of tests
&lt;a class="heading-anchor" href="#benefits-of-tests" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>Behavior is central to understanding the benefits of testing:&lt;/p>
&lt;blockquote>
&lt;p>Behavior is the most important thing about software. It is what users depend on. Users like it when we add behavior (provided it is what they really wanted), but if we change or remove behavior they depend on (introduce bugs), they stop trusting us.&lt;/p>
&lt;/blockquote>
&lt;h3 id="how-to-get-tests-in-place-in-legacy-codebases">How to get tests in place in legacy codebases
&lt;a class="heading-anchor" href="#how-to-get-tests-in-place-in-legacy-codebases" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>When we change code, we should have tests in place. To put the tests in place, we often have to change code.&lt;/p>
&lt;p>The suggested approach:&lt;/p>
&lt;ol>
&lt;li>Identify change points.&lt;/li>
&lt;li>Find test points.&lt;/li>
&lt;li>Break dependencies.&lt;/li>
&lt;li>Write tests.&lt;/li>
&lt;li>Make changes and refactor.&lt;/li>
&lt;/ol>
&lt;p>Another useful term is a “&lt;strong>seam&lt;/strong>.” A seam, in this context, is “&lt;strong>a place where you can alter behavior in your program
without editing in that place&lt;/strong>”. The analogy is to a seam in clothing, the place where two parts are stitched together.
In software, &lt;strong>these places are generally places where there are well-defined interfaces&lt;/strong>. This can be leveraged to change
the implementation using techniques such as dependency injection or mocking interfaces in the case of writing tests.&lt;/p>
&lt;hr />
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/wRtJRkRIa2s"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry><entry xml:lang="en"><title>Clean Architecture</title><subtitle>A Craftsman's Guide to Software Structure and Design</subtitle><category term="architecture" scheme="https://chemaclass.com/tags/architecture/" label="Architecture"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="ddd" scheme="https://chemaclass.com/tags/ddd/" label="Ddd"/><published>2018-06-04T00:00:00+00:00</published><updated>2018-06-04T00:00:00+00:00</updated><author><name>
Robert C. Martin</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/clean-architecture/"/><id>https://chemaclass.com/readings/clean-architecture/</id><summary type="html">Robert C. Martin's guide to SOLID principles, component design, and architectural boundaries that keep software systems flexible, testable, and maintainable over time.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>&lt;h3 id="code-design-principles-solid">Code design principles (SOLID)
&lt;a class="heading-anchor" href="#code-design-principles-solid" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Single Responsibility&lt;/strong>: a class should have one, and only one, reason to change. Or the new version: a module
should be responsible to one, and only one, actor.&lt;/li>
&lt;li>&lt;strong>Open-closed&lt;/strong>: a class should be open for extension but closed for modification.&lt;/li>
&lt;li>&lt;strong>Liskov’s Substitution&lt;/strong>: objects in a program should be replaceable with instances of their subtypes without
altering the correctness of that program.&lt;/li>
&lt;li>&lt;strong>Interface Segregation&lt;/strong>: many client-specific interfaces are better than one general-purpose interface.&lt;/li>
&lt;li>&lt;strong>Dependency Inversion&lt;/strong> : one should depend upon abstractions, not concretions.&lt;/li>
&lt;/ul>
&lt;h3 id="component-principles">Component principles
&lt;a class="heading-anchor" href="#component-principles" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;h4 id="component-cohesion">Component cohesion
&lt;a class="heading-anchor" href="#component-cohesion" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;ul>
&lt;li>&lt;strong>Reuse/Release Equivalence&lt;/strong> Principle: classes and modules (i.e. a component) reused together should be released
together. They should have the same version number and there should be proper documentation such as changelogs.&lt;/li>
&lt;li>&lt;strong>Common Closure&lt;/strong> Principle: classes that change together should be grouped together, and vice versa. The single
responsibility principle at component-level.&lt;/li>
&lt;li>&lt;strong>Common Reuse&lt;/strong> Principle: don’t force users of a component to depend on things they don’t need. The Interface
Segregation Principle at component-level.&lt;/li>
&lt;/ul>
&lt;h4 id="component-coupling">Component coupling
&lt;a class="heading-anchor" href="#component-coupling" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;ul>
&lt;li>&lt;strong>Acyclic Dependencies&lt;/strong> Principle: no cycle in the dependency graph. Cycles couple components and, among other
things, force them to be to released together. Use the dependency inversion principle to break cycles.&lt;/li>
&lt;li>&lt;strong>The Stable Dependency&lt;/strong> Principle: less stable components should depend on more stable components. Depend on the
direction of stability.&lt;/li>
&lt;li>&lt;strong>Stable Abstractions&lt;/strong> Principle: stable components should be abstract, and vice versa. An example of an abstract
stable component is a high-level policy which is changed by extension following the open-closed principle.&lt;/li>
&lt;/ul>
&lt;h3 id="architecture-principles">Architecture principles
&lt;a class="heading-anchor" href="#architecture-principles" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;h4 id="setting-boundaries">Setting boundaries
&lt;a class="heading-anchor" href="#setting-boundaries" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;p>Boundaries are lines that separate software elements. They separate things that matter from things that don’t, i.e.
high-level components from low-level components. If a high-level component depends on a low-level component at the
source level, changes in the low-level components will spread to the high-level component. Therefore, we place a
boundary between the two, using polymorphism to invert the logic flow. This is the Dependency Inversion Principle in the
SOLID principles.&lt;/p>
&lt;h4 id="separating-layers">Separating layers
&lt;a class="heading-anchor" href="#separating-layers" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;p>We can identify four main layers, although the number may vary:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Entities&lt;/strong>: objects containing critical business logic. For example, a bank could establish that no loans are
granted to customers not satisfying some credit score requirements. Entities may be shared across apps in the same
enterprise.&lt;/li>
&lt;li>&lt;strong>Use-cases&lt;/strong>: app-specific business rules. For example, the sequence of screens to execute a bank transfer.&lt;/li>
&lt;li>&lt;strong>Interface adapters&lt;/strong>: Gateways, presenters and controllers. For example, this layer will contain the MVC
architecture of the GUI and also objects that transform data between the format of the database and the use-cases.&lt;/li>
&lt;li>&lt;strong>Frameworks and drivers&lt;/strong>: web frameworks, database, the view of MVC.&lt;/li>
&lt;/ul></content></entry><entry xml:lang="en"><title>The Pragmatic Programmer</title><subtitle>The journey to mastery</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><category term="career" scheme="https://chemaclass.com/tags/career/" label="Career"/><published>2016-10-01T00:00:00+00:00</published><updated>2016-10-01T00:00:00+00:00</updated><author><name>
Andrew Hunt</name></author><author><name>
David Thomas</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/the-pragmatic-programmer/"/><id>https://chemaclass.com/readings/the-pragmatic-programmer/</id><summary type="html">Key takeaways from Hunt and Thomas's classic on pragmatic software development, including DRY principles, the right mindset, and how to choose and master your tools.</summary><content type="html">&lt;span id="continue-reading">&lt;/span>&lt;h3 id="takeaways">Takeaways
&lt;a class="heading-anchor" href="#takeaways" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Don’t Repeat Yourself.&lt;/li>
&lt;li>Mindset is as important as knowledge.&lt;/li>
&lt;li>Good code is easier to change than bad design.&lt;/li>
&lt;li>Choose great tools and become fluent with them.&lt;/li>
&lt;/ul>
&lt;hr />
&lt;h4 id="don-t-repeat-yourself-dry">Don’t Repeat Yourself (DRY)
&lt;a class="heading-anchor" href="#don-t-repeat-yourself-dry" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;ul>
&lt;li>Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.&lt;/li>
&lt;li>DRY is about the duplication of knowledge, of intent. It’s about expressing the same thing in two different places,
possibly in two totally different ways.&lt;/li>
&lt;/ul>
&lt;h4 id="mindset-is-as-important-as-knowledge">Mindset is as important as knowledge
&lt;a class="heading-anchor" href="#mindset-is-as-important-as-knowledge" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h4>
&lt;p>Some important aspects of this mindset include:&lt;/p>
&lt;ul>
&lt;li>Taking responsibility for your work by not making excuses or passing blame when things go wrong.&lt;/li>
&lt;li>Writing software that’s good enough. This means not wasting time on things that are better than they need to be to
make the product successful.&lt;/li>
&lt;li>Not ignoring technical debt. The authors use the analogy of broken windows:&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Don’t leave “broken windows” (bad designs, wrong decisions, or poor code) unrepaired.
Fix each one as soon as it is discovered. If there is insufficient time to fix it properly, then board it up.
Perhaps you can comment out the offending code, or display a “Not Implemented” message, or substitute dummy data instead.&lt;/p>
&lt;/blockquote></content></entry><entry xml:lang="en"><title>97 Things Every Programmer Should Know</title><subtitle>Collective whisdom from the experts</subtitle><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="career" scheme="https://chemaclass.com/tags/career/" label="Career"/><category term="productivity" scheme="https://chemaclass.com/tags/productivity/" label="Productivity"/><published>2016-07-15T00:00:00+00:00</published><updated>2016-07-15T00:00:00+00:00</updated><author><name>
Kevlin Henney</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/97-things-every-programmer-should-know/"/><id>https://chemaclass.com/readings/97-things-every-programmer-should-know/</id><summary type="html">Tap into the wisdom of experts to learn what every programmer should know, no matter what language you use. With the 97 short and extremely useful tips for programmers in this book, you'll expand your skills by adopting new approaches to old problems, learning appropriate best practices, and honing your craft through sound advice.</summary><content type="html">&lt;p>Tap into the wisdom of experts to learn what every programmer should know, no matter what language you use. With the 97
short and extremely useful tips for programmers in this book, you’ll expand your skills by adopting new approaches to
old problems, learning appropriate best practices, and honing your craft through sound advice.&lt;/p>
&lt;span id="continue-reading">&lt;/span>&lt;h3 id="my-main-key-takeaways">My main key takeaways
&lt;a class="heading-anchor" href="#my-main-key-takeaways" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;p>01.- Pay off technical debt as soon as possible.&lt;/p>
&lt;p>02.- Learn and master &lt;strong>functional programming&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>It makes your code less prone to error and easier to debug.&lt;/li>
&lt;/ul>
&lt;p>03.- Don’t guess what a user would do; get users to do things and watch them.&lt;/p>
&lt;p>04.- &lt;strong>Automate&lt;/strong> coding standards.&lt;/p>
&lt;p>05.- Write &lt;strong>simple&lt;/strong> code, simple descriptive names, simple relationships.&lt;/p>
&lt;p>06.- Before you refactor: consider the existing tests and code.&lt;/p>
&lt;ul>
&lt;li>Work in increments, make sure the tests still pass after every change.&lt;/li>
&lt;/ul>
&lt;p>08.- Always leave code &lt;strong>cleaner&lt;/strong> than you found it, even if you didn’t write it.&lt;/p>
&lt;p>10.- Choose your libraries/frameworks carefully to avoid unnecessary complexity.&lt;/p>
&lt;p>11.- Make your code easy to understand using &lt;strong>domain&lt;/strong> terms.&lt;/p>
&lt;p>13.- The &lt;strong>layout&lt;/strong> of the code is also important.&lt;/p>
&lt;p>14.- Use code &lt;strong>reviews&lt;/strong> focusing on knowledge-sharing among team members.&lt;/p>
&lt;p>15.- Objects &lt;strong>immutable&lt;/strong> whenever relevant. Each variable should have the smallest possible scope. Never include more than four function arguments.&lt;/p>
&lt;p>18.- Take &lt;strong>responsibility&lt;/strong> for your own education and never stop learning.&lt;/p>
&lt;ul>
&lt;li>That can be done with just a bit of time each week and there are many ways to go about it (e.g. podcasts, lessons, books, etc.).&lt;/li>
&lt;/ul>
&lt;p>19.- When designing an API, aim to make it &lt;strong>easy to use&lt;/strong>, not convenient to code.&lt;/p>
&lt;p>20.- Deploy early and &lt;strong>often&lt;/strong>. Do not leave it until the end of the project.&lt;/p>
&lt;p>22.- Improving your skills should be on your daily basis.&lt;/p>
&lt;p>23.- Adapt the technical level of your domain-specific language to your audience.&lt;/p>
&lt;p>24.- &lt;strong>Don’t be afraid&lt;/strong> to break things if that’s what’s necessary to fix things.&lt;/p>
&lt;p>25.- Careful with your test data because that might go public accidentally.&lt;/p>
&lt;p>26.- Handle your errors as they appear, don’t leave it for later.&lt;/p>
&lt;p>27.- Learn &lt;strong>different&lt;/strong> programming languages.&lt;/p>
&lt;ul>
&lt;li>Learn their own “culture” or way of doing things.&lt;/li>
&lt;li>It’ll make you definitely a better programmer.&lt;/li>
&lt;/ul>
&lt;p>28.- Don’t just catch your errors, really handle them.&lt;/p>
&lt;p>29.- Understand at least some complexities of your business, not just programming.&lt;/p>
&lt;p>30.- &lt;strong>DRY&lt;/strong>: Don’t Repeat Yourself.&lt;/p>
&lt;p>32.- Encapsulate behavior, not just state.&lt;/p>
&lt;p>33.- Floating point numbers inevitably can create errors in calculations.&lt;/p>
&lt;p>34.- &lt;strong>Open source&lt;/strong> is a great opportunity to do interesting work and develop programming skills.&lt;/p>
&lt;p>36.- Give proper &lt;strong>context&lt;/strong> when asking for help, because people can’t just guess what’s going on.&lt;/p>
&lt;p>37.- It’s not about the long hours. Learn to &lt;strong>work effectively&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>Dedicate time to continuous learning and to think about what you’re doing.&lt;/li>
&lt;/ul>
&lt;p>38.- Write proper &lt;strong>bug reports&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>Precisely how to reproduce the bug,&lt;/li>
&lt;li>how often it appears,&lt;/li>
&lt;li>what should have happened,&lt;/li>
&lt;li>what actually happened.&lt;/li>
&lt;/ul>
&lt;p>39.- Don’t write unnecessary code.&lt;/p>
&lt;ul>
&lt;li>Only write code that adds value and is needed right now.&lt;/li>
&lt;li>&lt;strong>Remove dead code&lt;/strong>.&lt;/li>
&lt;/ul>
&lt;p>41.- The main cause of delays in application response time is a high number of remote &lt;strong>interprocess&lt;/strong> communications, not the algorithm.&lt;/p>
&lt;ul>
&lt;li>DB connections, for example.&lt;/li>
&lt;/ul>
&lt;p>42.- If a compiler warning shows up in your build, fix it.&lt;/p>
&lt;ul>
&lt;li>Don’t leave it for later. Even if it’s not going to be a problem in production.&lt;/li>
&lt;li>Compiler == any static code analysis for non compiled languages.&lt;/li>
&lt;/ul>
&lt;p>43.- Learning to use &lt;strong>command line&lt;/strong> tools is a valuable educational experience, and you might end up preferring them.&lt;/p>
&lt;p>44.- Learn (at least) two different languages and paradigms well.&lt;/p>
&lt;p>45.- Invest some time to &lt;strong>master&lt;/strong> the IDE you’re using.&lt;/p>
&lt;ul>
&lt;li>It’ll make your life easier and save you time in the long run.&lt;/li>
&lt;/ul>
&lt;p>46.- Know and work with your limitations: budget, resources, time, etc.&lt;/p>
&lt;p>47.- Work in &lt;strong>small tasks&lt;/strong>, don’t be afraid to throw away changes.&lt;/p>
&lt;ul>
&lt;li>You’ll still have the insight you gained from the experience.&lt;/li>
&lt;li>Know what you need to accomplish beforehand.&lt;/li>
&lt;/ul>
&lt;p>48.- Use a relational DB if your application is going to handle a large, persistent, interconnected set of data.&lt;/p>
&lt;p>49.- Learn to &lt;strong>communicate&lt;/strong> well in many “languages”: not just to your machine, but also to business partners, and maybe even pick up a foreign language too.&lt;/p>
&lt;ul>
&lt;li>It’s good for connections and for life.&lt;/li>
&lt;/ul>
&lt;p>54.- Think twice before implementing “temporary solutions”.&lt;/p>
&lt;p>55.- Make &lt;strong>GUI&lt;/strong> easy to use correctly and hard to use incorrectly.&lt;/p>
&lt;ul>
&lt;li>Anticipate errors and find ways to prevent them.&lt;/li>
&lt;li>It’s about the user’s experience, not your own.&lt;/li>
&lt;/ul>
&lt;p>56.- In projects, find ways to make &lt;strong>the invisible visible&lt;/strong>.&lt;/p>
&lt;p>57.- Message passing leads to better &lt;strong>scalability&lt;/strong> in parallel systems.&lt;/p>
&lt;p>58.- Write code that other people can easily &lt;strong>understand&lt;/strong>.&lt;/p>
&lt;p>59.- Using &lt;strong>polymorphism&lt;/strong> in your classes and objects reduces the need for if/else statements, which results in smaller, safer code.&lt;/p>
&lt;p>60.- QA is your friend, not your enemy.&lt;/p>
&lt;p>61.- Version your releases.&lt;/p>
&lt;p>62.- Make sure your source code clearly states what the program is doing.&lt;/p>
&lt;p>63.- Learn about the build process. It’s an important part of development.&lt;/p>
&lt;p>64.- Practice &lt;strong>pair programming&lt;/strong>.&lt;/p>
&lt;p>65.- Prefer &lt;strong>domain-specific types&lt;/strong> over primitive types.&lt;/p>
&lt;ul>
&lt;li>They make the code more readable and less prone to errors in development.&lt;/li>
&lt;/ul>
&lt;p>67.- A professional takes &lt;strong>personal responsibility&lt;/strong> for their career and their code.&lt;/p>
&lt;p>68.- Use version control.&lt;/p>
&lt;p>69.- Sometimes the best way to solve a problem is to step away from the computer and let the solution magically appear in your mind.&lt;/p>
&lt;p>70.- Reading code is a good way to &lt;strong>learn&lt;/strong>. Other people’s or your old code.&lt;/p>
&lt;p>72.- Reinventing the wheel is a great way to develop your skills.&lt;/p>
&lt;p>75.- If the code you wrote is truly horrifying, don’t try to fix. &lt;strong>Delete it&lt;/strong> and start again.&lt;/p>
&lt;p>76.- Apply the Single Responsibility Principle (&lt;strong>SRP&lt;/strong>).&lt;/p>
&lt;p>77.- If a client or team member requests a product change, don’t dismiss it outright even if you don’t agree. Ask why instead.&lt;/p>
&lt;ul>
&lt;li>That will lead to a more productive conversation and better results.&lt;/li>
&lt;/ul>
&lt;p>78.- If you’re doing the same thing over and over, try to find a way to &lt;strong>automate it&lt;/strong>.&lt;/p>
&lt;p>79.- Take advantage of code analysis tools.&lt;/p>
&lt;p>80.- Write tests based on the &lt;strong>desired functionality&lt;/strong> of your program, not incidental behavior.&lt;/p>
&lt;p>83.- Testing takes time, but it ensures the &lt;strong>quality&lt;/strong> of the end product. Do it.&lt;/p>
&lt;p>85.- There are many benefits to collaborative work and pair programming.&lt;/p>
&lt;p>86.- Sometimes fixing a mistake in the code leads to uncovering a hidden error.&lt;/p>
&lt;p>87.- Write code &lt;strong>with other programmers in mind&lt;/strong>.&lt;/p>
&lt;p>88.- Learn to use Unix tools. Learn how to use the &lt;strong>terminal&lt;/strong>.&lt;/p>
&lt;p>89.- Use the right algorithm and data structure for the job.&lt;/p>
&lt;ul>
&lt;li>To do that, you need to understand them well.&lt;/li>
&lt;/ul>
&lt;p>90.- Have a good logging policy.&lt;/p>
&lt;p>91.- Using the DRY principle helps you identify and repair performance bottlenecks.&lt;/p>
&lt;p>92.- Testers and programmers should &lt;strong>collaborate&lt;/strong>.&lt;/p>
&lt;p>93.- Write code as if you had to support it for the &lt;strong>rest of your life&lt;/strong>.&lt;/p>
&lt;p>94.- Try to write &lt;strong>“small” functions&lt;/strong>.&lt;/p>
&lt;ol start="95">
&lt;li>Good tests act as &lt;strong>documentation&lt;/strong> for the code they’re testing.&lt;/li>
&lt;/ol>
&lt;ul>
&lt;li>They describe how the code works.&lt;/li>
&lt;/ul>
&lt;ol start="96">
&lt;li>
&lt;p>To be a good programmer, you have to care about the &lt;strong>quality&lt;/strong> of the code.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Really, really &lt;strong>discuss things&lt;/strong> with your customers before assuming you understand what they want.&lt;/p>
&lt;/li>
&lt;/ol></content></entry><entry xml:lang="en"><title>Clean Code</title><subtitle>A Handbook of Agile Software Craftsmanship</subtitle><category term="clean-code" scheme="https://chemaclass.com/tags/clean-code/" label="Clean Code"/><category term="software-design" scheme="https://chemaclass.com/tags/software-design/" label="Software Design"/><category term="testing" scheme="https://chemaclass.com/tags/testing/" label="Testing"/><category term="tdd" scheme="https://chemaclass.com/tags/tdd/" label="Tdd"/><category term="refactoring" scheme="https://chemaclass.com/tags/refactoring/" label="Refactoring"/><published>2016-05-01T00:00:00+00:00</published><updated>2016-05-01T00:00:00+00:00</updated><author><name>
Robert C. Martin</name></author><link rel="alternate" type="text/html" href="https://chemaclass.com/readings/clean-code/"/><id>https://chemaclass.com/readings/clean-code/</id><summary type="html">Even bad code can function. But if code isn't clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn't have to be that way.</summary><content type="html">&lt;p>Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year,
countless hours and significant resources are lost because of poorly written code. But it doesn’t have to be that way.&lt;/p>
&lt;span id="continue-reading">&lt;/span>
&lt;hr />
&lt;h2 id="summary">Summary
&lt;a class="heading-anchor" href="#summary" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h2>
&lt;h3 id="chapter-1-what-is-clean-code">Chapter 1: What Is Clean Code?
&lt;a class="heading-anchor" href="#chapter-1-what-is-clean-code" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>The code can be measured with either “good” or “bad” in the code review or by how many minutes it takes you to talk
about it.&lt;/li>
&lt;li>A clean code should be elegant, efficient, readable, simple, without duplications, and well-written.&lt;/li>
&lt;li>You should add value to the business with your code.&lt;/li>
&lt;li>Clean code offers quality and understanding when we open the source file.&lt;/li>
&lt;li>It is necessary that your code is clean and readable for anyone to find and easily understand. Avoid wasting others’
time.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-2-meaningful-names">Chapter 2: Meaningful Names
&lt;a class="heading-anchor" href="#chapter-2-meaningful-names" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Names of the classes, variables, and methods must be meaningful and clearly indicate what a method does or what an
attribute is.&lt;/li>
&lt;li>Create pronounceable names to facilitate communication.&lt;/li>
&lt;li>Avoid acronyms and avoid confusing names, which may bring anyone who reads the code to the wrong conclusions.&lt;/li>
&lt;li>Use names that reflect the system domain, the context, and the problems that must be solved.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-3-functions">Chapter 3: Functions
&lt;a class="heading-anchor" href="#chapter-3-functions" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Methods should be easy to read and understand.&lt;/li>
&lt;li>Methods should convey its intention.&lt;/li>
&lt;li>Methods should be small.&lt;/li>
&lt;li>They must have up to 20 lines.&lt;/li>
&lt;li>Methods should only do one thing.&lt;/li>
&lt;li>You should use names with words that say what it really does.&lt;/li>
&lt;li>The optimal number of parameters of a method is zero, after one and two.&lt;/li>
&lt;li>Three should be avoided, but if you think it should be used, have a justification.&lt;/li>
&lt;li>&lt;code>Boolean&lt;/code> type as a parameter already states that it does more than one thing.&lt;/li>
&lt;li>Avoid duplication.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-4-comments">Chapter 4: Comments
&lt;a class="heading-anchor" href="#chapter-4-comments" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>One of the common reasons for comments is because the code is bad.&lt;/li>
&lt;li>If you’re thinking about writing a comment, then the code should be refactored.&lt;/li>
&lt;li>Comments do not save a bad code.&lt;/li>
&lt;li>Try to explain what the code causes happening.&lt;/li>
&lt;li>Comments can be useful when placed in certain places.&lt;/li>
&lt;li>Don’t explain your code with comments. Use informative vars/method names.&lt;/li>
&lt;li>Comments can be used to express the importance of certain points.&lt;/li>
&lt;li>Do not write comments with redundant, useless, or false information.&lt;/li>
&lt;li>They shouldn’t be used to indicate who changed or why, use versioning.&lt;/li>
&lt;li>Don’t comment code that will not be used. Remove it instead.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-5-formatting">Chapter 5: Formatting
&lt;a class="heading-anchor" href="#chapter-5-formatting" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Formatting should indicate things of importance since it is a developer of communication form.&lt;/li>
&lt;li>A messy code is hard to read.&lt;/li>
&lt;li>The readability of the code will take effect on all the changes that will be made.&lt;/li>
&lt;li>Smaller classes are easier to understand.&lt;/li>
&lt;li>Set a limit of characters per line of code. For example 120.&lt;/li>
&lt;li>Try to keep more next related concepts vertically to create a code stream.&lt;/li>
&lt;li>Use spaces between operators, parameters, and commas.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-6-objects-and-data-structure">Chapter 6: Objects and Data Structure
&lt;a class="heading-anchor" href="#chapter-6-objects-and-data-structure" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Follow the &lt;a rel="external" href="https://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter&lt;/a>:
&lt;ul>
&lt;li>Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.&lt;/li>
&lt;li>Each unit should only talk to its friends; don’t talk to strangers.&lt;/li>
&lt;li>Only talk to your immediate friends.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Do not make dumb objects.&lt;/li>
&lt;li>Objects hide the data abstraction and expose methods that operate the data.&lt;/li>
&lt;li>Data structures expose your data and do not have significant methods.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-7-error-handling">Chapter 7: Error Handling
&lt;a class="heading-anchor" href="#chapter-7-error-handling" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Error handling should be planned carefully by all programmers.&lt;/li>
&lt;li>When wrong things occur, we have to get it to do the right things.&lt;/li>
&lt;li>Give preference to launching an exception than treating it just to hide.&lt;/li>
&lt;li>Create messages with information about the error.&lt;/li>
&lt;li>Mention that it failed. Where was this failure? If possible, mention why it failed.&lt;/li>
&lt;li>Look at separate business rules for errors and error handling.&lt;/li>
&lt;li>Avoid returning a &lt;code>NULL&lt;/code> in methods, preferably to return an empty object.&lt;/li>
&lt;li>Avoid passing &lt;code>NULL&lt;/code> to the methods; this can generate &lt;code>NullPointerExceptions&lt;/code>.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-8-boundary">Chapter 8: Boundary
&lt;a class="heading-anchor" href="#chapter-8-boundary" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>In third-party code, to avoid passing objects, APIs look forward in order to keep things in the same class.&lt;/li>
&lt;li>Perform tests on the API’s third party.&lt;/li>
&lt;li>Study the documentation and test the third API before you start using it.&lt;/li>
&lt;li>Check well the features you will use.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-9-unit-tests">Chapter 9: Unit Tests
&lt;a class="heading-anchor" href="#chapter-9-unit-tests" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>Make sure each piece of code is doing what you expect it to do.&lt;/li>
&lt;li>Follow the &lt;a rel="external" href="https://en.wikipedia.org/wiki/Test-driven_development">TDDs law&lt;/a>:
&lt;ul>
&lt;li>Don’t create code before you have a failing test.&lt;/li>
&lt;li>Don’t create more tests than necessary to fail.&lt;/li>
&lt;li>You cannot write more code than enough to pass the test that is failing.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Keep your test clean.&lt;/li>
&lt;li>The tests must undergo changes in the same way that the code.&lt;/li>
&lt;li>The dirtier the code, the more difficult test will be to maintain.&lt;/li>
&lt;li>Use the F.I.R.S.T rule for testing:
&lt;ul>
&lt;li>The test is fast-running.&lt;/li>
&lt;li>The tests are independent of others.&lt;/li>
&lt;li>The test is repeatable in various environments.&lt;/li>
&lt;li>The test is self-validating.&lt;/li>
&lt;li>The test is timely.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The test is as important as the production code.&lt;/li>
&lt;/ul>
&lt;h3 id="chapter-10-classes">Chapter 10: Classes
&lt;a class="heading-anchor" href="#chapter-10-classes" title="Copy link" aria-label="Link to this section">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>By default, classes should start with the variables:
&lt;ul>
&lt;li>Static and constants public.&lt;/li>
&lt;li>Static and variable private.&lt;/li>
&lt;li>Instances and variables privates.&lt;/li>
&lt;li>Soon after comes the functions.&lt;/li>
&lt;li>The class name should represent its responsibility.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The class must have only &lt;a rel="external" href="https://en.wikipedia.org/wiki/Single-responsibility_principle">one responsibility&lt;/a>: one reason to change.&lt;/li>
&lt;li>You should try to make a brief description of the class.&lt;/li>
&lt;li>The methods should be small and one responsibility.&lt;/li>
&lt;/ul>
&lt;hr />
&lt;p>This interview is based on Uncle Bob’s book “Clean Code”. They cover some existing guides that can help you become a
better programmer and explore how books and current trends are shaping the software landscape.&lt;/p>
&lt;div style="position:relative;aspect-ratio:16/9;width:100%;">
&lt;iframe
src="https://www.youtube-nocookie.com/embed/QnmRpHFoYLk"
title="YouTube video"
width="560"
height="315"
loading="lazy"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
referrerpolicy="strict-origin-when-cross-origin"
style="position:absolute;inset:0;width:100%;height:100%;border:0;"
allowfullscreen>
&lt;/iframe>
&lt;/div></content></entry></feed>