<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marian Jureczko on Medium]]></title>
        <description><![CDATA[Stories by Marian Jureczko on Medium]]></description>
        <link>https://medium.com/@marian.jureczko?source=rss-db4d040c4a05------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*dKUJAcZmMwQ6i1w7</url>
            <title>Stories by Marian Jureczko on Medium</title>
            <link>https://medium.com/@marian.jureczko?source=rss-db4d040c4a05------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:12:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marian.jureczko/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rearranger: Make Your Tests Expressive, Not Exhaustive]]></title>
            <link>https://towardsdev.com/rearranger-make-your-tests-expressive-not-exhaustive-7c60545ba11a?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c60545ba11a</guid>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[domain-driven-design]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Sun, 02 Nov 2025 19:37:56 GMT</pubDate>
            <atom:updated>2025-11-02T19:37:56.707Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*maiJ4a6eV5fc8W7OVbGKFQ.png" /></figure><p>In complex domains creating test data by hand can be tedious and error-prone, leading to high maintenance costs. As the domain model evolves every change in constructors, invariants, or dependencies can force widespread test rewrites just to keep them compiling and valid. Domain objects often have invariants and numerous required fields, so manually instantiating them in tests can lead to tight coupling between the test and the production. You can saw how <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> helps reduce this coupling by auto-generating valid objects with random data, so that tests only specify the fields relevant to their flow in <a href="https://medium.com/towardsdev/on-testing-in-ddd-f250482b5717">On testing in DDD</a>. Test-arranger is an open-source tool that instantiates classes with pseudo-random values. This makes tests more focused on intent and less brittle to changes in constructors or domain.</p><p>However, pure randomness sometimes isn’t enough. Often we need a specific value for a particular field to drive a certain test scenario, while still letting test-arranger handle the rest. One solution is to add a copy or setter method, possibly to use Lombok’s @Builder(toBuilder=true) or @With annotations in the domain class to clone and modify the random instance. For example, we can generate a random Product and then use product.toBuilder().name(&quot;Pineapple&quot;).build() to set a custom value. This works, but it required modifying the production code (e.g. adding a builder) just for testing purposes, which is debatable. Another approach was to use a <a href="https://github.com/ocadotechnology/test-arranger?tab=readme-ov-file#custom-arrangers">CustomArranger</a>, which defines a tailored recipe for generating domain-valid instances with specific rules or fixed fields. It is also possible to use <a href="https://github.com/ocadotechnology/test-arranger?tab=readme-ov-file#adjusting-the-arranged-data">test-arranger’s overrides</a> feature to supply specific field values at object creation. A combination of these two approaches is presented in the section containing the example.</p><p>The approaches mentioned so far are really just different incarnations of the classic <a href="https://martinfowler.com/bliki/ObjectMother.html">Object Mother</a> pattern — a central way of producing test data objects in a controlled, domain-valid manner. It’s a proven strategy, but sometimes it’s much more convenient to take an existing instance and make a few ad-hoc tweaks directly in the test where it’s needed — keeping the setup lightweight and the intent clear. Wouldn’t it be nice if we could clone an existing object and only change the parts we care about in the test?</p><h3>Rearranger: Copy with Selective Overrides</h3><p>The <a href="https://github.com/ocadotechnology/test-arranger/releases/tag/v1.7.0">latest test-arranger release, i.e. 1.7.0</a> introduces Rearranger, a feature that lets us do exactly that — clone an existing object while overriding selected fields or properties. Instead of building a fresh object from scratch, we start from an already valid instance and change only what’s relevant to the particular test. This keeps the rest of the data consistent and realistic, and highlights the important differences for the scenario. In short, Rearranger aims to make tests more expressive by focusing only on what matters while not enforcing the heavy weight Objects Mothers.</p><p><strong>How it works: </strong>Rearranger provides a simple API in both Java and Kotlin for cloning an object with overrides. Under the hood, it performs a shallow copy of the source object. That is, it creates a new object of the same type, copies over all field values from the source, then applies the overrides you specify for certain fields. “Shallow” means if some fields are themselves objects (especially mutable ones), the <em>references</em> are reused rather than deeply cloning those sub-objects — this is by design, to keep the test intent explicit (if you need independent copies of nested objects, you should arrange those deliberately). Rearranger handles both Java records and regular classes (POJOs), whether they are mutable or immutable, and even if they lack no-args constructors or have private setters.</p><p>Two flavors of the API are available:</p><ul><li><strong>Java static method:</strong> Rearranger.copy(source, Map&lt;String, Supplier&lt;?&gt;&gt; overrides) – You pass in the source instance and a map of field names to supplier functions that produce the override values. Field names should match the record component name or property name. A supplier (or lambda) is used so that the override value is generated lazily (allowing references to the source or other computed values if needed).</li><li><strong>Kotlin DSL:</strong> Rearranger.copy(source) { PropertyRef set value } – In Kotlin, you can call copy on a source object with a lambda where you use <em>property references</em> to set new values. This is a type-safe, refactor-safe way to specify overrides without using string names.</li></ul><h3>Why Rearranger matters — Example</h3><p>Let’s use a hypothetical domain to illustrate the challenge that Rearranger solves. Consider a domain class User. It’s a core concept in the tested system and is created numerous times across different test cases. To avoid all the boilerplate code involved in creating User instances for various scenarios, the following UserArranger was introduced:</p><pre>class UserArranger extends CustomArranger&lt;User&gt; {<br>    @Override<br>    protected User instance() {<br>        ...<br>    }<br><br>    public static User vipUser() {<br>        return some(User.class, Map.of(<br>                &quot;isVip&quot;, () -&gt; true,<br>                &quot;points&quot;, () -&gt; 100<br>        ));<br>    }<br>}</pre><p>We now have a default procedure for creating Users of any kind (represented by the instance() method) and a method corresponding to a specific domain concept — a VIP user. This concept maps to the isVip flag and a decent amount of points. We can assume that those methods are used frequently to create User instances by calling either some(User.class) or UserArranger.vipUser() whenever a VIP user is needed for a particular test case.</p><p>But here comes the challenge. Our User class is an immutable record, and we’ve just added a new use case: VIP users will receive a personal assistant who supports them over the phone. Therefore, VIP users should be prompted to provide a phone number if they haven’t done so already.</p><p>To test this scenario, we need an instance of a VIP user without a phone number. However, the instance created by UserArranger.vipUser() always includes one. We could add a vipUserWithoutPhone() method to UserArranger, but missing phone number is a corner case — such a method would have very low reusability and therefore is weakly justified.</p><p>Alternatively, we could create the User instance from scratch, but that would require a lot of code (definitely more than one line 😉) and would not reuse the VIP concept already defined in UserArranger, leading to conceptual duplication. The challenge is not a blocker, but clearly, there’s no perfect solution.</p><h4>Rearranger to the rescue</h4><p>Thanks to Rearranger, we can take an existing immutable instance and use it as a template for creating copies — but we don’t have to follow the template strictly. We can modify specific fields in the process.</p><p>In our challenging test case, we can copy the VIP user and simply erase their phone number:</p><pre>User vip = Arranger.vipUser();<br>User vipWithoutPhoneNumber = Rearranger.copy(original, Map.of(<br>    &quot;phone&quot;, () -&gt; null<br>));</pre><p>In the code above, vip might represent a random VIP user. We then call Rearranger.copy(vip, ...), passing a map of field overrides for the properties we want to change. Here, we override &quot;phone&quot; to be null, effectively removing the phone number.</p><p>The result, vipWithoutPhoneNumber, is a new User instance identical to the original in every field except phone. All other attributes (such as name, email, and so on) retain their original random yet realistic values.</p><p>This approach allows our test to focus purely on the behavior differences for a VIP user without a phone number — without manually constructing the entire User object or duplicating irrelevant setup logic.</p><h4>In Kotlin it is even better</h4><p>In Kotlin, using Rearranger is even more succinct thanks to its DSL. We do not have to use strings to refer to fields, instead everything can be done in a type safe manner:</p><pre>val vip = Arranger.vipUser()<br>val vipWithoutPhoneNumber = Rearranger.copy(vip) {<br>    User::phone set null<br>}</pre><p>Note that when performing an IDE-driven refactoring such as renaming phone to phoneNumber, User::phone will be automatically updated to use the new field name.</p><h3>When to Use Arranger vs. Custom Arranger vs. Rearranger</h3><p>By now you might wonder: given all these tools, which one should be used in which situation? Test-arranger actually provides a few ways to get test data, and they complement each other. Here’s a quick comparison:</p><p><strong>Situation Preferred Approach. </strong>Need a completely fresh random object? Use Arranger.some(X.class) (Java) or some&lt;X&gt;() (Kotlin) – this gives a brand-new object with all fields random. Start from an existing instance &amp; tweak a few fieldsUse Rearranger.copy(existing, overrides) – clone and override only what you need for that scenario. Need domain-valid variants systematically? Create a CustomArranger (subclass of CustomArranger&lt;T&gt;) to define a recipe for that type, and use it via Arranger.some or by explicitly calling the CustomArranger methods. You can still follow it with Rearranger.copy for additional per-test tweaks if needed.</p><p>In practice, these approaches often work together. You might define some domain-specific factory methods or custom arrangers for complex setup (ensuring invariants are met in the random data). Those can give you a base object that represents a meaningful scenario (for example, Arranger.vipOrderFor(product) might return an Order that is randomized but always marked as a VIP order with that product). Then, for a particular test case, you can call Rearranger to adjust one or two fields on that base object to fit the test. A typical composition might look like:</p><pre>Order vipOrder = Arranger.vipOrderFor(expensiveSku);<br>Order expeditedVipOrder = Rearranger.copy(vipOrder, Map.of(<br>    &quot;priority&quot;, () -&gt; Priority.EXPEDITED<br>));</pre><p>In this example, the first line uses a custom arranger or factory method to get a semi-random vipOrder (perhaps with a high value of points and flagged as VIP). The second line clones that order but overrides the priority field to EXPEDITED, giving us an order that is both VIP and expedited. All other details (customer info, items list, etc.) remain consistent with vipOrder. The result is we quickly obtained a realistic test object for an “expedited VIP order” scenario with minimal code and without manually specifying irrelevant data.</p><p>Summary of usage: Use Arranger when you need a brand new object with random data. Use Rearranger when you already have an object (perhaps from a previous Arranger call or from your test setup) and you just want to change a couple of things on it to create a new scenario. Use CustomArranger when randomness alone isn’t enough to produce valid data (e.g., certain combinations or formats are required — as we did for barcodes in the previous article’s example). You can mix these: e.g., get a base object via a CustomArranger or Arranger, then apply Rearranger to fine-tune it for the test. Still not enough? Check the <a href="https://medium.com/towardsdev/on-testing-in-ddd-f250482b5717">On testing in DDD</a> to read about fixtures.</p><h3>Conclusion</h3><p>The Rearranger feature adds a powerful new tool to the test-arranger library, making it easier to express complex test scenarios with minimal setup. By allowing us to start with a known-good instance and adjust just the needed details, it keeps test code <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a> (Don’t Repeat Yourself) and focused on behavior rather than boilerplate. In a domain with rich data and invariants, this can save a lot of time and make tests more resilient to changes in the domain model. We no longer have to choose between full randomness and full manual construction — we can have a mix of both, using randomness to handle the incidental details and selective overrides to control the critical ones.</p><p>You can find test-arranger (including Rearranger) on <a href="https://mvnrepository.com/artifact/com.ocadotechnology.gembus/test-arranger/1.7.0">Maven Central Repository</a> and <a href="https://github.com/ocadotechnology/test-arranger">GitHub</a> — it’s open source, so feel free to check out the code, try it in your projects, and even contribute if you find it useful. With Arranger and Rearranger in your testing toolbox, setting up realistic test data for even the most complex domain objects becomes a much more effortless task. Here’s to less flaky tests and more productive development!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c60545ba11a" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/rearranger-make-your-tests-expressive-not-exhaustive-7c60545ba11a">Rearranger: Make Your Tests Expressive, Not Exhaustive</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplify Android Tests with test-arranger: Write Less, Maintain Less]]></title>
            <link>https://towardsdev.com/simplify-android-tests-with-test-arranger-write-less-maintain-less-ceb4e00a2c3c?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/ceb4e00a2c3c</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[testing-tools]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-testing]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Mon, 12 May 2025 10:29:12 GMT</pubDate>
            <atom:updated>2025-05-12T10:29:12.002Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*olejYDo3gm5AyeTJdqHyWw.png" /></figure><p>Test data setup can easily become the most repetitive and error-prone part of writing tests in Android. Whether you’re writing unit tests for a ViewModel or integration tests for a use case, you&#39;ll often find yourself creating the same combinations of test objects over and over — cluttering test bodies and making them hard to read. In the production code, there are invariants, that, when not fulfilled, may result in tests failures. Arranging test data can easily result in tight coupling between tests and production code. That means each update in the production class leads to a change in the tests that use the class.</p><p>This post introduces an approach to organizing test data using <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> — now available in version <a href="https://github.com/ocadotechnology/test-arranger/releases/tag/v1.6.4">1.6.4</a> with Android compatibility. We&#39;ll explore how this library helps keep your tests expressive, maintainable, and free from redundant boilerplate — all within the limitations of the Android runtime.</p><h3>Why test-arranger for Android?</h3><p>We often define our test data inline like in this test:</p><pre>class ProductTest {<br><br>    @Test<br>    fun shortName() {<br>        // arrange<br>        val name = &quot;name&quot;<br>        val product = Product(<br>            id = &quot;id&quot;,<br>            name = name,<br>            brand = &quot;brand&quot;,<br>            price = BigDecimal.TEN,<br>            temperatureRegime = TemperatureRegime.Chill,<br>            backOfPack = BackOfPack(),<br>            images = null<br>        )<br><br>        // act<br>        val actual = product.shortName()<br><br>        // assert<br>        assertThat(name).contains(actual)<br>    }<br>}</pre><p>Given the production code is as follows:</p><pre>data class Product(<br>    val id: String,<br>    val name: String,<br>    val brand: String,<br>    val price: BigDecimal,<br>    val temperatureRegime: TemperatureRegime,<br>    val backOfPack: BackOfPack,<br>    val images: List&lt;Image&gt;?,<br>...<br>) {<br>    fun shortName(): String {<br>        ...<br>    }<br>}</pre><p>This works — but it’s noisy, error-prone, and hard to maintain. Over time, test data creeps into test bodies and starts repeating across files. Minor changes to the Product models ripple through many tests.</p><h4>What we want instead:</h4><ul><li><strong>Readable tests</strong>: tests that say <em>what</em> they test, not <em>how</em> to build the data</li><li><strong>Reusable setups</strong>: common arrangements in one place</li><li><strong>Minimal boilerplate</strong>: focus on intent, not object creation</li></ul><p>This is where test-arranger shines.</p><h3>The Android Runtime Problem</h3><p>Until recently, the use test-arranger in Android projects was limited because it depended on parts of the JDK that Android doesn’t support. Android doesn’t use the standard Java Virtual Machine (JVM) — it runs on <strong>ART/Dalvik</strong>, a runtime that only supports a subset of Java classes. That means:</p><ul><li>No java.lang.invoke</li><li>No java.beans</li><li>Limited reflection APIs</li><li>Not all java.util.* types are available or work the same way</li></ul><p>So unless a library is <strong>Android-aware</strong>, it might not work in your tests — especially when running instrumented tests.</p><h4>What’s New in Version 1.6.4?</h4><p>Version 1.6.4 of test-arranger solves this by being <strong>Android-safe</strong>:</p><ul><li>✅ Avoids unsupported JVM classes</li><li>✅ Safe to use in instrumented Android tests</li><li>✅ Same clean API</li></ul><p>You can now bring powerful, expressive test data setups into your Android app test — without hacks, wrappers, or custom builders.</p><h3>How It Works</h3><p>At its core, test-arranger is a tool for defining reusable and composable test arrangements. Below is an improved version of the test shown at the beginning of this post, now enhanced with test-arranger features:</p><pre>class ProductTest {<br><br>    @Test<br>    fun shortNameArranger() {<br>        // arrange<br>        val product = some&lt;Product&gt;()<br><br>        // act<br>        val actual = product.shortName()<br><br>        // assert<br>        assertThat(product.name).contains(actual)<br>    }<br>}</pre><p>Much cleaner — and if the Product constructor changes in the future, you won’t need to update anything. This gives us two important benefits. First, it loosens the coupling with production code: the constructor&#39;s signature is no longer relevant, and the test doesn&#39;t need to know about any Product parameters other than the name. Second, even the name parameter itself isn’t particularly important — we don’t set it explicitly. The purpose of the test is simply to ensure that the shortName() method transforms the name in a specific way. The exact value of the name doesn’t matter. This reflects the TDD principle of avoiding duplication in tests, as recommended by Kent Beck in <em>Test-Driven Development: By Example</em>.</p><p><strong>Using random data in tests can reduce coupling with production code, making the test’s purpose more apparent</strong></p><p>The suggested solution works well when the test doesn’t depend on any specific field values — it keeps the setup lightweight and flexible.</p><p>But what if we <em>do</em> need control over a specific field — say the product’s name — because our test makes assertions based on it? For instance, when testing a method like shortName() that parses a string, we can’t avoid setting that string explicitly.</p><p>Here’s how we do that cleanly without giving up the benefits of randomness and decoupling:</p><pre>val product = some&lt;Product&gt;()<br>    .copy(name = &quot;Great Value Frozen Pineapple Chunks 500g&quot;)</pre><p>This combines the best of both worlds:</p><ul><li>some&lt;Product&gt;() generates a complete, valid instance</li><li>.copy(...) tweaks only the part we care about</li></ul><p>This is especially elegant with Kotlin’s data class feature — copy() is built-in and avoids the need for custom builders or mutability.</p><p>Now the test stays focused:</p><pre>@Test<br>fun shortName_shouldReturnCorrectValue() {<br>    val product = some&lt;Product&gt;()<br>        .copy(name = &quot;Great Value Frozen Pineapple Chunks 500g&quot;)<br><br>    val actual = product.shortName()<br><br>    assertEquals(&quot;Frozen Pineapple&quot;, actual)<br>}</pre><p>The test depends only on the name — exactly what’s needed and nothing more. No unnecessary boilerplate, no tight coupling, and the intent is crystal clear just by reading the code.</p><p>If the class under test isn’t a data class, and you’re okay with some mutability, you can also use the Kotlin DSL-like syntax supported by test-arranger:</p><pre>val product = some&lt;Product&gt; {<br>    name = &quot;Great Value Frozen Pineapple Chunks 500g&quot;<br>}</pre><p>This works great for mutable classes in Android models (e.g., JSON DTOs or parcelables).</p><h4>Respecting Invariants in Android Tests</h4><p>So far, we’ve used test-arranger to easily generate random yet valid test data, but real-world models often have <strong>invariants</strong>: rules that must hold for an object to be valid. In such cases, random data may violate constraints, leading to either exceptions or meaningless test behavior (garbage in — garbage out).</p><p>Take this simplified example of a Product class used in Android:</p><pre>data class Product(<br>    val name: String,<br>    val barcodes: List&lt;Barcode&gt;<br>) {<br>    fun hasBarcode(barcode: Barcode): Boolean {<br>        ...<br>    }<br>}</pre><p>And a Barcode model with different types:</p><pre>data class Barcode(<br>    val code: String,<br>    val type: BarcodeType<br>)<br><br>enum class BarcodeType {<br>    Code39, Code93, Code128, Ean8, Ean13, Jan, UpcA, UpcE<br>}</pre><p>Suppose that depending on the type, the code must follow certain rules (e.g., length or checksum). If test-arranger creates random values without following these rules, hasBarcode() and related tests will behave unpredictably. Some barcode types — like EAN or UPC — might contain dynamic content such as expiry dates. But your Product model might only store <em>canonical</em> versions (standardized barcodes without expiry), while still needing to match incoming dynamic barcodes at runtime. So your hasBarcode() logic might accept both exact matches and compatible variants. This subtle business logic depends heavily on having only correct barcodes.</p><p>How to solve that with test-arranger? To generate <strong>valid and meaningful</strong> test instances, test-arranger allows defining a <strong>custom arranger</strong>. Here’s a custom Kotlin arranger for Barcode:</p><pre>class BarcodeArranger : CustomArranger&lt;Barcode&gt;() {<br>    override fun instance(): Barcode {<br>        val barcode = super.instance()<br>        return barcode.copy(code = generateCanonicalCode(barcode.getType()))<br>    }<br><br>    private fun generateCanonicalCode(type: BarcodeType): String {<br>        ...<br>    }<br>}</pre><p>To avoid scanning the entire classpath, declare the root package where your arrangers live (save it in arranger.propertiefile):</p><pre>arranger.root=com.yourcompany.arrangers</pre><p>With this in place, some&lt;Barcode&gt;() will now use BarcodeArranger, ensuring that all barcodes are valid and meaningful. Unfortunately, we can be certain about this behaviour only in unit tests. In Android tests, which run on ART or Dalvik, some reflection-related Java features may not work as expected. To address this, there’s an additional property: arranger.android.customArrangers. It doesn&#39;t rely on reflection-based classpath scanning and therefore works on any runtime. However, it&#39;s less convenient to use, as its value must be a comma-separated list of the canonical class names of all custom arrangers:</p><pre>arranger.android.customArrangers=com.BarcodeArranger,com.ProductArranger</pre><p>Here’s a test that checks that a product without the barcode does not match it:</p><pre>@Test<br>fun hasNotBarcode() {<br>    val barcode = some&lt;Barcode&gt;()<br>    val product = some&lt;Product&gt;()<br>    assertFalse(product.hasBarcode(barcode))<br>}</pre><p>And one that ensures a match when the barcode is present:</p><pre>@Test<br>fun hasBarcode() {<br>    val barcode = some&lt;Barcode&gt;()<br>    val product = some&lt;Product&gt;().copy(<br>        barcodes = listOf(some&lt;Barcode&gt;(), barcode)<br>    )<br>    assertTrue(product.hasBarcode(barcode))<br>}</pre><p><strong>When a class cannot be fully randomized, provide a custom arranger (which should extend </strong><strong>CustomArranger) that creates the class in a controlled way, without violating its invariants.</strong></p><p>We will likely need additional tests for various barcode-related cases, such as a product associated with a canonical barcode and a real barcode that aligns with the canonical one. To facilitate this, it would be useful to have one method that generates a canonical barcode and another that generates a real barcode with a random code, but consistent with the given canonical barcode. Since both methods deal with arranging barcodes, why not place them in a BarcodeArranger?</p><pre>class BarcodeArranger : CustomArranger&lt;Barcode&gt;() {<br>    override fun instance(): Barcode {<br>        val barcode = super.instance()<br>        return barcode.copy(code = generateCanonicalCode(barcode.getType()))<br>    }<br><br>    private fun generateCanonicalCode(type: BarcodeType): String {<br>        ...<br>    }<br><br>    companion object {<br>        fun canonical(): Barcode {<br>            ...<br>        }<br><br>        fun inLineWith(canonical: Barcode): Barcode {<br>            ...<br>        }<br>    }<br>}</pre><p>Please note that the new methods are effectively static. They do not share any state with the BarcodeArranger class. However, I chose to place them inside the arranger because it makes it clear where to look for factory methods that generate objects for tests. By centralizing these methods in the BarcodeArranger, you avoid duplication and maintain consistency across your tests.</p><p><strong>All methods responsible for instantiating classes for testing should ideally be placed in the corresponding class arranger. Additionally, reusing these methods wherever appropriate helps prevent duplication in your test data setup, making your tests cleaner and easier to maintain.</strong></p><h4>A group of loosely coupled classes</h4><p>So far, the examples have focused on arranging a limited set of classes, typically within a single aggregate. However, real-world systems often involve multiple aggregates, with test cases depending on interactions between them. This complexity is not easily addressed using arrangers, as they are typically associated with a single class. So, when two aggregates are involved, which arranger should contain the logic?</p><p>Fortunately, there’s a well-established design pattern for this scenario: <strong>the fixture</strong>. Gerard Meszaros discusses various forms of the fixture pattern in <em>xUnit Test Patterns: Refactoring Test Code</em>. It’s worth reading for further insight into how to apply this pattern.</p><p>Let’s imagine a scenario in a shop where products are assigned to one or more aisles, and aisles are organized in a tree-like structure. In this case, Product and Aisle would each be separate aggregates. Here&#39;s an extension of the Product class with an aisles field:</p><pre>data class Product(<br>    val id: String,<br>    val name: String,<br>    val aisles: List&lt;AisleId&gt;<br>)</pre><p>And the Aisle class:</p><pre>data class Aisle(<br>    val id: AisleId,<br>    val name: String,<br>    val description: String,<br>    val subAisles: List&lt;Aisle&gt;<br>)</pre><p>We also have a method filterProductsByAisle that filters products based on the aisle they belong to:</p><pre>fun filterProductsByAisle(products: List&lt;Product&gt;, aisle: Aisle): List&lt;Product&gt; {<br>    // filter logic<br>}</pre><p>To test that the method filters out products not assigned to the given aisle, we can use random data:</p><pre>@Test<br>fun filterProductsNotAssignedToAisle() {<br>    // Arrange<br>    val aisle = some&lt;Aisle&gt;()<br>    val products = someObjects&lt;Product&gt;(10).toList()<br><br>    // Act<br>    val actual = sut.filterProductsByAisle(products, aisle)<br><br>    // Assert<br>    assertThat(actual).isEmpty()<br>}</pre><p>This test works well as we need only random data. However, to test the positive case where products belong to specific aisles, we need to create a fixture that ties products to aisles:</p><pre>class ProductsAndAislesFixture(noOfProducts: Int) {<br>    val aisle: Aisle = some&lt;Aisle&gt;()<br>    val aislesIds: Set&lt;AisleId&gt; = aisle.allIds()<br>    val products: List&lt;Product&gt; = someObjects&lt;Product&gt;(noOfProducts)<br>        .map { it.copy(aisles = listOf(someFrom(aislesIds))) }<br>        .toList()<br>}</pre><p>The corresponding test could look like this:</p><pre>@Test<br>fun filterProductsAssignedToAisle() {<br>    // Arrange<br>    val fixture = ProductsAndAislesFixture(20)<br><br>    // Act<br>    val actual = sut.filterProductsByAisle(fixture.products, fixture.aisle)<br><br>    // Assert<br>    assertThat(actual).hasSize(20)<br>}</pre><p>By using a fixture, the test becomes more readable, and its intention is clear: it checks if products are filtered correctly based on their aisle association.</p><p><strong>When tests require complex data (involving multiple aggregates), it’s best to store the data creation logic in a fixture. This improves clarity and reduces duplication.</strong></p><p>The fixture example above is a bit simplified, as it’s designed for a single use case. In a more complex setup, we would consider immutability of the data or move data creation logic out of the constructor, especially if the test cases require slight variations in the data setup. In such cases, dedicated methods can be used to handle the different arrangements, with method names that clearly describe the specific data they create.</p><p>Additionally, for time-consuming fixture creation, it might be useful to share the fixture across test cases. In enterprise systems, you might even consider creating data in an embedded database using Spring repositories. The full range of fixture variants is thoroughly covered in Meszaros’s book.</p><h3>Conclusions and summary</h3><p>This post not only describes a handy library for test data generation but also suggests an approach for organizing test data generation code. This approach can be summarized in the following list:</p><ul><li>Use someXxx methods to generate simple values and objects.</li><li>Use XxxArranger (extending CustomArranger) to handle all methods for instantiating type Xxx.</li><li>Use XxxFixture to generate groups of objects of different types.</li></ul><p>This is an approach I’ve used for several years, and it has served me well. However, it’s not without its drawbacks. Let’s examine the pros and cons.</p><h4>Test-arranger Pros</h4><p><strong>Looser coupling with production code</strong>: Tests do not depend on complex constructors or implementation details (e.g., required fields) of the production classes. Under the hood, test-arranger uses reflection to instantiate classes. This means it can automatically adapt to changes, as long as they don’t affect the test’s goal.</p><p><strong>Less code in tests</strong>: test-arranger handles the job of providing values for all required fields. The test code only includes what’s relevant to the test’s goal, reducing the amount of code.</p><p><strong>Clearer test intention</strong>: As mentioned earlier, with test-arranger, tests only include the relevant parts. This makes the original test intention much easier to identify, without all the boilerplate code cluttering the view.</p><p><strong>Additional possibilities for detecting issues</strong>: Random data is used to cover fields irrelevant to the test case. However, the code under test may use the data incorrectly, and a variable that should be irrelevant might cause the test to fail. This is a good thing because it can help identify defects in unexpected places. While this may not happen immediately, it’s better to detect issues this way than not at all. Consider the case with enumerations in contract tests: <a href="https://stackoverflow.com/questions/43089653/should-i-test-all-enum-values-in-a-contract/66040704#66040704">StackOverflow reference</a>.</p><p><strong>Fewer duplications in the test code</strong>: Organizing test data arrangements into arrangers and fixtures makes it easier to find the right factory method. While this doesn’t completely eliminate code duplication, it at least provides opportunities for reuse.</p><p><strong>Lower test maintenance cost</strong>: As previously mentioned, looser coupling with production code, less test code, reduced duplication, and clearer intentions all contribute to fewer adjustments needed when production code changes. When changes do occur, the tests should be easier for maintainers to understand.</p><h4>Test-arranger Cons</h4><p><strong>More challenging debugging</strong>: Fields initialized with random data often have complex values, which are harder to memorize. As a result, tracking the data flow can be more difficult.</p><p><strong>Flaky tests</strong>: If the expectations about what’s relevant are incorrect (e.g., a misunderstanding of the code under test), and random data is used where a specific value is needed, the test may fail. This happens when unacceptable data is drawn. This downside is the flip side of the “additional possibilities for detecting issues” advantage. When there’s a mistake in the test, random data may sometimes “mask” it, leading to a passing test. Conversely, when the mistake is in the production code, random data may help uncover the issue, leading to a bug fix. From my experience, both situations are rare, and the occasional flakiness only increases my confidence in non-flaky test suites. You may read more about that in <a href="https://medium.com/@marian.jureczko/flaky-tests-the-handy-simplification-6e4296a0d71b">Flaky tests — the handy simplification</a>.</p><p><strong>Slower tests</strong>: Generating random data isn’t as fast as manually assigning values to variables. While the time difference is typically small (just a fraction of a second), it’s still noticeable in some cases.</p><h4>Key Takeaways: Further Reading and Next Steps</h4><p>I hope my approach to organizing test data helps in addressing challenges related to complex test data arrangements and reduces the need to frequently adjust tests in response to changes in production code. If the concept of fixtures and arrangers feels overwhelming, you can start simple. It’s not an all-or-nothing solution. Even using random data in just one test case will provide some benefit. So, you can take small, manageable steps at a pace that suits you and your project.</p><p>This post is a condensed version, focused on Android development, of <a href="https://medium.com/towardsdev/on-testing-in-ddd-f250482b5717">On testing in DDD</a>. You may want to read the full post for more details about the approach.</p><p>You can find the test-arranger library on GitHub: <a href="https://github.com/ocadotechnology/test-arranger">https://github.com/ocadotechnology/test-arranger</a>. If you find it useful, give it a ⭐ — it’s a great way to show there’s interest from the Android community and helps justify continued maintenance and improvements. And most importantly, try it out in your next Android project — you won’t regret it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ceb4e00a2c3c" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/simplify-android-tests-with-test-arranger-write-less-maintain-less-ceb4e00a2c3c">Simplify Android Tests with test-arranger: Write Less, Maintain Less</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Jetpack Compose Demystified: Key Concepts Unveiled Through Sharing ViewModels Across Screens]]></title>
            <link>https://towardsdev.com/jetpack-compose-demystified-key-concepts-unveiled-through-sharing-viewmodels-across-screens-af38c16ad415?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/af38c16ad415</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[viewmodel]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Fri, 10 Jan 2025 18:47:51 GMT</pubDate>
            <atom:updated>2025-01-13T19:50:01.768Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QyYpUO7ocD2x-mmlT-DQUg.png" /></figure><p>Compose functions are a modern way to describe UI in Android applications. A Compose function is simply a regular function adorned with the @Composable annotation. These functions usually return Unit, and the UI they describe is metaphorically &quot;emitted.&quot; The UI output is determined by the data passed through its input parameters.</p><p>When parameters alone are insufficient for maintaining or sharing data, a ViewModel can step in. ViewModel provides persistence capabilities, allowing data to outlive the lifecycle of an Android activity. Compose functions can include other Compose functions, forming a hierarchy of composables that ultimately correspond to UI widgets. Near the root of this hierarchy is the screen itself.</p><p>In a typical design, there is often a one-to-one relationship between screens and their ViewModel. However, in real-world applications, multiple screens might need to work on the same data. Synchronizing data between the ViewModel instances of different screens would require significant effort—something we’d all prefer to avoid.</p><p>Fortunately, the same ViewModel can be shared across multiple screens. This concept serves as a case study to uncover the intricate internal workings of Jetpack Compose, which are often glossed over in tutorials. Let’s dive deep into these details, unveiling knowledge vital for building non-trivial Android applications.</p><h3>How recomposition works</h3><p>Let’s begin by understanding recomposition. But first, what is recomposition?</p><p>The process of executing Compose functions to “emit” UI elements, ultimately rendering a screen, is called composition. However, user interactions — like tapping the screen — trigger events that update the Compose function’s parameters which in turn results in rendering updated version of the screen. And the process is called recomposition.</p><p>Let’s illustrate that with an example. That’s a very simple composable function that shows a text on the screen:</p><pre>@Composable<br>fun Greeting(name: String) {<br>    Text(text = &quot;Hello $name!&quot;)<br>}</pre><p>When the name parameter changes, a recomposition occurs, updating the displayed text. However, a more interactive UI might involve allowing the user to input their name, using something like this:</p><pre>    TextField(<br>        value = text,<br>        onValueChange = { text = it },<br>        label = { Text(&quot;Name&quot;) }<br>    ) </pre><p>If we naively update onValueChange with something like this:</p><pre>Greeting(it)</pre><p>We’d encounter an error: @Composable invocations can only happen from the context of a @Composable function. Besides, we don’t want multiple Greeting components on the screen.</p><h4>Mutable state</h4><p>The correct approach involves mutableState. Here&#39;s an example:</p><pre>val name = mutableStateOf(&quot;&quot;)<br>Column {<br>  Greeting(name.value)<br>  NameInput(name = name, onValueChange = {name.value = it})<br>}</pre><p>While this is a step forward, Android Studio might still raise warnings about code correctness. For now, observe how changes to name.value automatically trigger recomposition.</p><p>But what exactly is mutableState (yes there is a immutable one as well)? If you&#39;re familiar with the state machine design pattern, brace yourself: this is something else entirely.</p><p>The assignment name.value = it involves more than it seems. It invokes a setter, encapsulating complex logic (was there something about such design in Martin’s “Clean Code”?):</p><pre>set(value) = next.withCurrent {<br>     if (!policy.equivalent(it.value, value)) {<br>          next.overwritable(this, it) { this.value = value }<br>     }<br>}</pre><p>So it first check if the value indeed has changed and before accepting the new value it calls some additional method. The setter flow ultimately reaches this:</p><pre>internal fun notifyWrite(snapshot: Snapshot, state: StateObject) {<br>    snapshot.writeCount += 1<br>    snapshot.writeObserver?.invoke(state)<br>}</pre><p>In this piece of code, we finally encounter the keyword that reveals what the state actually represents: the “observer” word. The mutable state is, in essence, an observable object with a registered observer. Whenever a new value is submitted to the mutable state, it identifies who is interested in the value (i.e., who is observing it) and informs the observer about the change. In our case, this process is equivalent to triggering a recomposition.</p><p>But who exactly is the observer? Could it be our Greeting composable function? That seems quite challenging to grasp. The Greeting function is not directly connected to our mutable state; it merely receives a string stored in the state. Tracing which function or component is consuming a particular string seems implausible.</p><p>But who is the observer? Can it be our Greeting composable function? That looks quite challenging. Greeting is not directly connected with our mutable state, it merely gets a string stored in the state. Tracing who is consuming a string doesn’t sound plausible.</p><p>The observable is defined as ((Any) -&gt; Unit)?. While this formally answers the &quot;who&quot; question, it doesn&#39;t necessarily enhance our understanding of how it relates to the Greeting composable function.</p><h4>Snapshots</h4><p>That leads us towards another concept, the snapshots. You can find a comprehensive description of them at <a href="https://blog.zachklipp.com/introduction-to-the-compose-snapshot-system/">https://blog.zachklipp.com/introduction-to-the-compose-snapshot-system/</a>, here I will provide only a brief overview. A Snapshot represents the value of all states (mutableStateOf()) at a given moment. In our example, there is a Snapshot in the background, but using snapshots explicitly might look like this:</p><pre>val snapshot = Snapshot.takeMutableSnapshot()<br>snapshot.enter {<br>    state.value = newValue<br>} <br>snapshot.apply()</pre><p>The above example uses a mutable snapshot (do you notice the “creativity” in the naming? To me, it feels poetic — full of oxymorons. And there are more of them; recall the state).</p><p>First, we create a snapshot. What does that mean? A snapshot captures the values of all states (including those created with mutableStateOf()) that existed in the program at the moment the snapshot was taken. Think of it as a photo, where the &quot;friends&quot; being captured are the states.</p><p>Next, we can enter the snapshot to modify the values of certain states — hence the name “mutable snapshot.” However, this process isn’t as simple as variable assignment. Snapshots provide isolation, which means we don’t need to worry about multithreading conflicts. This is achieved through explicit change commitment using the apply() method. Only after executing apply() do the changes become visible.</p><p>How does this relate to recomposition? Compose provides a global snapshot, meaning all our code effectively operates within an implicit enter block. Background commits occur in this global snapshot, where the equivalent of apply() is a mechanism called &quot;advances.&quot;</p><p>If you’re curious about how this works, explore the Snapshot.sendApplyNotifications() method. To understand how this leads to recomposition, study the SnapshotStateObserver class. The observeReads method, in particular, might be of special interest:</p><pre>fun &lt;T : Any&gt; observeReads(<br>  scope: T, <br>  onValueChangedForScope: (T) -&gt; Unit, <br>  block: () -&gt; Unit<br>)<br></pre><p>The block parameter represents a piece of code where state reads will be tracked. The SnapshotStateObserver plays a critical role in associating state with composable functions. Each composable function can be viewed as a &quot;node&quot; in the composition tree. When a composable reads a state object, SnapshotStateObserver records this relationship by mapping the state to the composable function that accessed it.</p><p>As a result of this mapping, whenever a mapped state changes, SnapshotStateObserver triggers the execution of the code block submitted as onValueChangedForScope. This block receives a parameter of type T called scope, which corresponds to nodes in the composition tree. On each execution, the scope is used to mark the associated node as requiring recomposition.</p><p>The observeReads method is invoked multiple times with various scopes (often instances of the LayoutNode type). The key point is that recomposition isn’t immediate—it’s not just a callback. Instead, the callback marks the nodes needing refreshment. Once all nodes are marked, the recomposition process launches and updates all marked nodes. This approach optimizes performance: a data change triggers updates only for the affected UI widgets, minimizing unnecessary work. This targeted refresh mechanism ensures efficient UI updates. he solution is available at <a href="https://github.com/mjureczko/shared-viewmodel/tree/mutable_state">https://github.com/mjureczko/shared-viewmodel/tree/mutable_state</a>.</p><p>Let’s summarize the entire flow step by step:</p><ol><li>The UI code is scanned by SnapshotStateObserver.observeReads() to associate states with the corresponding nodes in the composition tree that read those states.</li><li>A state is modified.</li><li>The change is persisted in the snapshot system, ensuring the new value is consistently available across threads and system components.</li><li>The snapshot is observed, and SnapshotStateObserver is notified of the change.</li><li>Using the mapping established in step 1, SnapshotStateObserver marks nodes in the composition tree that need to be refreshed with the new value.</li><li>Compose re-executes the composable functions associated with the marked nodes.</li><li>These composable functions read the new state values, leading to updated views being rendered.</li></ol><p>The relationships between the involved classes are depicted in the conceptual diagram below. Note that this diagram simplifies the reality of the SDK for smoother understanding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hLmUX51nIjyGGLviG2PNTw.png" /></figure><p>The description above (not just the diagram) is a simplified representation of what actually happens in Compose. Its purpose is to provide a general understanding of the concept, as the full implementation — along with all its intricate details — is far too complex for a single blog post.</p><p>This explanation is based, to some extent, on reading and debugging the SDK code. As a result, there’s a possibility that I may have overlooked an important concept or misunderstood certain aspects. Additionally, some of the functionality described is not part of the public API, meaning it could change in future versions, potentially rendering parts of this explanation outdated. That said, the focus here is on the general concept rather than specific implementation details. As such, I believe the content will remain a useful reference for understanding Compose’s workings for a long time.</p><h4>Have we remembered about everything?</h4><p>Do you recall Android Studio’s warning about state? If you launch the application, you’ll notice that it doesn’t work as expected: the TextField doesn&#39;t accept the characters the user types. This happens because the state is <strong>transient</strong> — or, in more Android-like terms, it’s a <strong>forgetful state</strong>.</p><p>When recomposition is triggered in response to entering a character, it eventually reaches this line:</p><pre>val name = mutableStateOf(&quot;&quot;)</pre><p>This line creates a brand-new mutable state, which replaces the old one. However, unlike the previous state, this new state contains an empty string. As a result, the old state is effectively forgotten.</p><p>The solution to this problem is to <strong>remember the state</strong>, which can be achieved as follows:</p><pre>var name by remember { mutableStateOf(&quot;&quot;) }</pre><p>That solves the issue, but since I plan to analyze what happens in remember, let&#39;s rewrite it in a simpler, less &quot;elegant&quot; way:</p><pre>var name = remember { mutableStateOf(&quot;&quot;) }</pre><p>This project version is available at <a href="https://github.com/mjureczko/shared-viewmodel/tree/remember">https://github.com/mjureczko/shared-viewmodel/tree/remember</a>.</p><p>The remember function comes in several variations. Notably, there is a version that accepts an additional parameter called key. This provides insight into what remember actually does. If you inspect its implementation, you&#39;ll find the following code:</p><pre>@Composable<br>inline fun &lt;T&gt; remember(<br>    key1: Any?,<br>    crossinline calculation: @DisallowComposableCalls () -&gt; T<br>): T {<br>    return currentComposer.cache(currentComposer.changed(key1), calculation)<br>}</pre><p>The lambda where we previously inserted the mutable state is referred to as a <strong>calculation</strong>, and it is stored in a cache. These calculations are conditional — they are executed (in this case, the creation of a new mutable state) only when changed(key1) indicates that they should be. The key1 parameter serves as the trigger, allowing us to control whether the calculation result will be recreated.</p><p>To better understand how remember works, try the following code:</p><pre>val key = Random.nextInt(2)<br>Log.i(&quot;TAG&quot;, &quot;key = $key&quot;)<br>var name = remember(key) { mutableStateOf(&quot;&quot;) }<br>Column {<br>  val name1 = name.value<br>  Greeting(name1)<br>  NameInput(name = name.value, onValueChange = {<br>    name.value = it<br>  })<br>}</pre><p>The code above updates the state correctly as long as nextInt() generates the same value. However, when the value changes, it starts over with a brand-new mutable state.</p><p>One might wonder what kind of cache is used by remember. That’s a great question. Try typing something into the app and then rotating the smartphone—the text disappears. This happens because when the composable function that called remember is removed from the composition tree (e.g., due to a state change that causes the composable to no longer be displayed), the remembered value is discarded. The next time the composable re-enters the composition, the remember block is re-evaluated, creating a new value.</p><p>Specifically, if the activity is recreated (such as during a configuration change like screen rotation), the remembered value is lost because the composable itself is recreated. To prevent forgetting user-entered data, we need more than just remember. This brings us to another important component—not specific to Compose—the <strong>ViewModel</strong>.</p><p>There’s also rememberSaveable, which has &quot;better memory.&quot; However, it doesn’t align with the main topic of this post, so let’s set it aside and move on to the <strong>ViewModel</strong>.</p><h4>ViewModel</h4><p>ViewModel is part of the Android Jetpack library androidx.lifecycle. It acts as a storage mechanism that is more persistent than remember and even outlives an activity. For example, it can survive such dramatic events like screen rotations.</p><p>The lifecycle of a ViewModel aligns more closely with the user&#39;s expectations. It persists through configuration changes but is destroyed when its associated activity is finished. Importantly, a ViewModel doesn’t have to be scoped to an activity. It can also be created within the context of a composable screen, in which case it will live as long as the composable does.</p><p>Here’s how we can create a ViewModel scoped to a composable screen:</p><pre>val viewModel: NameViewModel = viewModel()                            <br>Column {                                                               <br>    Greeting(viewModel.name.value, color = color)                     <br>    NameInput(name = viewModel.name.value, onValueChange = {           <br>        viewModel.name.value = it                                     <br>    })                                                                <br>}</pre><p>To fully understand the snippet above, it’s helpful to also know the definition of the NameViewModel:</p><pre>class NameViewModel : ViewModel() {<br>    val name: MutableState&lt;String&gt; = mutableStateOf(&quot;&quot;)<br>}</pre><p>The code is quite straightforward. We extend the ViewModel class and use the viewModel() method to instantiate it. This method is smart enough not to overwrite the existing instance during recomposition. Instead, it returns the already existing instance.</p><p>As a result, any UI widget connected to the mutable state stored in the ViewModel will preserve its content. So, is the problem solved?</p><p>Not necessarily. This brings us to the need to clarify what “scoped to a composable screen” actually means. <a href="https://developer.android.com/topic/libraries/architecture/viewmodel">The official documentation</a> explains that the ViewModel&#39;s lifecycle is controlled by a ViewModelStoreOwner, which is implemented by Activity and certain other classes. In the context of a composable screen, the ViewModelStoreOwner is an instance of NavBackStackEntry. This moves us toward the topic of Compose Navigation, which is discussed in the next section.</p><p>For now, it’s essential to understand that whenever a new screen is created, a new instance of the ViewModel is also created. This behavior is likely not what we want to achieve. However, you can check the solution at: <a href="https://github.com/mjureczko/shared-viewmodel/tree/navigation_scoped_viewmodel">https://github.com/mjureczko/shared-viewmodel/tree/navigation_scoped_viewmodel</a>.</p><p>Additionally, the ViewModel has its own lifecycle. It includes an onCleared() method, which is called by Android just before the ViewModel is destroyed. It might be interesting to explore how this works in the context of an Activity’s lifecycle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LyMYN9oakxXEml-T2D-Q8A.png" /><figcaption>Activity and ViewModel lifecycle</figcaption></figure><p>Activities provide a solution for preserving UI states through the onSaveInstanceState() method. This allows us to serialize the state when the application is sent to the background or when a configuration change occurs. Later, we can deserialize it in the onCreate() method when the application is restored.</p><p>With a ViewModel, all of this functionality is effectively handled for us. The ViewModel automatically preserves its state across all Activity states—except for the &quot;nonexistent&quot; state, when the Activity is completely destroyed.</p><p>An Activity reaches the “nonexistent” state when the user explicitly exits the application (e.g., by pressing the back button) or when Android decides to kill the application for some reason. The Activity’s destruction is typically covered by the onDestroy() method. However, not all calls to onDestroy() are guaranteed. Those marked with an asterisk indicate that the method is optional—Android may destroy the Activity without invoking onDestroy().</p><p>A similar challenge exists with the ViewModel&#39;s onCleared() method. While it is guaranteed to be called when the user finishes the application, there are no guarantees when Android itself decides to terminate the application.</p><p>As the diagram shows, the last method that is guaranteed to be called during an Activity’s lifecycle is onStop(). Therefore, if you have data that must be available for the next application execution, the onStop() method is the last reliable place to persist it—assuming you want to handle persistence within the lifecycle context (though this is not obligatory).</p><p>In our application, the ViewModel only maintains the state of UI widgets, and there isn’t significant pressure to ensure a flawless user experience. As such, we can manage without persistence. However, as the post title suggests, our focus is on <strong>sharing </strong><strong>ViewModels across screens</strong>.</p><p>The current solution scopes the ViewModel to the screen (or, more precisely, to the navigation entry). This means that creating a new screen results in a new instance of the ViewModel, and leaving the screen ends the lifecycle of that ViewModel.</p><p>Let’s explore alternative methods for creating ViewModels to determine if we can achieve a better approach.</p><p><strong>Activity scoped ViewModel. </strong>We can create the ViewModel in the Activity and pass the instance to composables:</p><pre>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        val viewModel: NameViewModel by viewModels()<br>        ...</pre><p>This approach is straightforward and works well in many cases, but it has some drawbacks. The solution becomes more complex when your application includes multiple activities. While you can structure your application so that all screens share the same activity, there may be valid reasons for using multiple activities, making this approach less optimal.</p><p>Another limitation is that you cannot create the ViewModel exactly where it’s needed. Instead, you must initialize it in the Activity’s onCreate() method and pass it down to the composable screens. This involves adding the ViewModel to the method signatures along the way, which can clutter your code.</p><p>Despite these drawbacks, they are relatively moderate, making this a viable first candidate for a solution. You can explore the implementation here: <a href="https://github.com/mjureczko/shared-viewmodel/tree/activity_scoped_viewmodel">https://github.com/mjureczko/shared-viewmodel/tree/activity_scoped_viewmodel</a></p><p><strong>ViewModelProvider.</strong> The traditional way of instantiating a ViewModel is by using ViewModelProviders, which has since been updated to ViewModelProvider (without the “s”). The ViewModelProvider functions as a registry for ViewModels. When you request a ViewModel for the first time, the ViewModelProvider creates and returns a new instance. If you request the same ViewModel again, it will return the previously created instance.</p><pre>val viewModel = ViewModelProvider(owner).get(NameViewModel::class.java)</pre><p>This approach sounds like a solution for having an Activity-scoped ViewModel without needing to modify numerous composable function signatures. The ViewModelProvider constructor accepts an instance of ViewModelStoreOwner, an interface implemented by both ComponentActivity and NavBackStackEntry.</p><ol><li><strong>ComponentActivity</strong>: When using ComponentActivity, the behavior mirrors the scenario described earlier. The ViewModel is associated with the Activity, but instead of passing the ViewModel itself, you propagate the Activity through all the composable functions.</li><li><strong>NavBackStackEntry</strong>: This introduces a navigation component. Since this involves navigation, it’s time to delve into the navigation topic in detail.</li></ol><h3>How navigation works</h3><p>Navigation in an application refers to transitioning between screens. But what exactly is a screen? Intuitively, it’s everything the device displays to the user — and this intuitive answer is quite accurate. However, in Compose, a screen has additional significance.</p><p>A screen is also a part of a <strong>navigation graph</strong>, managed by the <strong>navigation framework</strong>, which ensures seamless transitions between different screens. Navigation allows users to move effortlessly from one screen to another, providing a cohesive user experience.</p><p>Let’s dive into the details of navigation to understand how it works.</p><h4>Navigation</h4><p>The navigation component follows a set of core principles:</p><ol><li><strong>Fixed Start Screen</strong>: There is always a designated start screen.</li><li><strong>Back Stack</strong>: The navigation state is defined as a stack of screens, often referred to as the “back stack.”</li><li><strong>Initial State</strong>: The start screen is initially placed on the back stack.</li><li><strong>Navigating to a New Screen</strong>: When the user navigates to a new screen, it is added to the top of the back stack.</li><li><strong>Navigating Back</strong>: When the user navigates back, the top screen is removed from the back stack, revealing the previous screen at the top.</li></ol><p>Navigation consists of three main elements:</p><ol><li><strong>Navigation Graph</strong>: Defines all the screens and the possible paths (or routes) between them.</li><li><strong>NavHost</strong>: A container that displays the current screen based on the back stack.</li><li><strong>NavController</strong>: A component responsible for navigating between screens and managing updates to the back stack.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UhWAbeiPjUa7wQ7TWhyICA.png" /></figure><p>To navigate between screens, let’s define a new composable function that will serve as the root of the hierarchy and act as a container for navigation:</p><pre>private const val BLUE_SCREEN = &quot;blue&quot;<br>private const val RED_SCREEN = &quot;red&quot;<br>private const val GREEN_SCREEN = &quot;green&quot;<br><br>@Composable<br>private fun ComposeRoot() {<br>    val navController = rememberNavController()<br><br>    NavHost(navController, startDestination = BLUE_SCREEN) {<br>        val goToBlue = { navController.navigate(BLUE_SCREEN) }<br>        val goToRed = { navController.navigate(RED_SCREEN) }<br>        val goToGreen = { navController.navigate(GREEN_SCREEN) }<br>        composable(route = BLUE_SCREEN) {<br>            BlueScreen(navController, goToRed, goToGreen)<br>        }<br>        composable(route = RED_SCREEN) {<br>            RedScreen(navController, goToBlue, goToGreen)<br>        }<br>        composable(route = GREEN_SCREEN) {<br>            GreenScreen(navController, goToBlue, goToRed)<br>        }<br>    }<br>}</pre><p>As shown, all the screens are defined within the NavHost. The NavHost includes a reference to the NavController and specifies the start screen, providing everything needed to render the initial screen.</p><p>Navigation between screens is handled using lambdas stored in the goToXxx variables. Each lambda uses the NavController&#39;s navigate method, passing the destination screen&#39;s name as a parameter. These lambdas are passed down into the screens and are typically used as onClick callbacks for buttons. As a result, clicking a button triggers the navigation and renders the destination screen on the device.</p><p>The navigation graph ties everything together. Have you noticed the composable() functions? That’s another very informative name picked by the framework creators, so you probably know what is the function responsibility at the first glance and I do not even need to explain that it is defined in the NavGraphBuilder and that by calling it we are defining the navigation graph.</p><p>The example provided is straightforward and doesn’t cover all the features of navigation. However, it should be sufficient to grasp the general concept. For more details and a complete implementation, check out the solution here: <a href="https://github.com/mjureczko/shared-viewmodel/tree/activity_scoped_viewmodel">https://github.com/mjureczko/shared-viewmodel/tree/activity_scoped_viewmodel</a></p><h4>Parameters in the Navigation</h4><p>The solution created in the previous section works as expected. However, we might simplify it further. Navigation provides a way to parameterize the process of navigating to a screen. Instead of sharing ViewModels, we can consider propagating the state using parameters. This approach can also help build a deeper understanding of how navigation works.</p><p>The core change involves modifying the navigation component. After introducing the ability to pass the entered name as a parameter, it looks like this:</p><pre>@Composable<br>private fun ComposeRoot() {<br>    val navController = rememberNavController()<br><br>    NavHost(navController, startDestination = &quot;$BLUE_SCREEN/{$NAME_ARGUMENT}&quot;) {<br>        val goToBlue: (String) -&gt; Unit = { navController.navigate(&quot;$BLUE_SCREEN/$it&quot;) }<br>        val goToRed: (String) -&gt; Unit = { navController.navigate(&quot;$RED_SCREEN/$it&quot;) }<br>        val goToGreen: (String) -&gt; Unit = { navController.navigate(&quot;$GREEN_SCREEN/$it&quot;) }<br>        composable(<br>            route = &quot;$BLUE_SCREEN/{$NAME_ARGUMENT}&quot;,<br>            arguments = listOf(navArgument(NAME_ARGUMENT) { type = NavType.StringType })<br>        ) {backStackEntry -&gt;<br>            val argumentValue = backStackEntry.arguments?.getString(NAME_ARGUMENT) ?: &quot;&quot;<br>            BlueScreen(argumentValue, goToRed, goToGreen)<br>        }<br>        composable(<br>            route = &quot;$RED_SCREEN/{$NAME_ARGUMENT}&quot;,<br>            arguments = listOf(navArgument(NAME_ARGUMENT) { type = NavType.StringType })<br>        ) {backStackEntry -&gt;<br>            val argumentValue = backStackEntry.arguments?.getString(NAME_ARGUMENT) ?: &quot;&quot;<br>            RedScreen(argumentValue, goToBlue, goToGreen)<br>        }<br>        composable(<br>            route = &quot;$GREEN_SCREEN/{$NAME_ARGUMENT}&quot;,<br>            arguments = listOf(navArgument(NAME_ARGUMENT) { type = NavType.StringType })<br>        ) {backStackEntry -&gt;<br>            val argumentValue = backStackEntry.arguments?.getString(NAME_ARGUMENT) ?: &quot;&quot;<br>            GreenScreen(argumentValue, goToBlue, goToRed)<br>        }<br>    }<br>}</pre><p>In the snippet above, parameters are declared within the route name. Wrapping a string with { } marks it as a parameter name. However, this alone is not enough—we must also declare the parameter type. This is done by providing a list of NamedNavArgument. As the name suggests, we specify the parameter name again, followed by its type, which in this case is String. While other types are supported, they are generally limited to primitives. This suggests the design is intended for values easily serializable into strings. Although theoretically, any data can be serialized into a string, this isn’t always the most practical or elegant solution.</p><p>Each composable() call includes a lambda that launches the destination screen. A new element to note here is the backStackEntry. Previously ignored, it now plays a crucial role, as we use its arguments field to retrieve the passed parameter. Remember, navigation operates as a stack, and this backStackEntry represents the new entry in the stack, created for the screen about to be displayed. By inspecting this entry, we can access the parameter value in the context of the new screen. We simply read the parameter, pass it to the screen, and the task is complete. The complete solution is available here:<br><a href="https://github.com/mjureczko/shared-viewmodel/tree/navigation_parameters">https://github.com/mjureczko/shared-viewmodel/tree/navigation_parameters</a></p><p>Whether this is a simplification depends on perspective — it’s a matter of taste. The answer isn’t straightforward. Before making any judgment, careful testing is crucial. Try entering a character as the name, navigating to another screen, entering another character, and repeating the process as many times as desired. Everything works as expected. Well done! But what happens if you press the back button? Navigation takes you back to the previous screen as expected. However, notice the name — it’s reverted! Each press of the back button removes one character from the name until it is completely empty.</p><p>This behavior is due to how the navigation stack works. Pressing back removes entries from the stack and restores the state to what it was when the screen was first entered. This behavior clearly does not provide a shared state.</p><h4>Explore the navigation stack</h4><p>We’ve already learned that navigation creates a stack, where all previous screens are stored as instances of NavBackStackEntry. We’ve also seen that a ViewModel has a ViewModelStoreOwner, which determines its lifecycle. This makes it possible to locate the NavBackStackEntry where the ViewModel was originally created and reuse it as the state owner for a new screen.</p><p>The key part is finding the relevant NavBackStackEntry in the stack. This can be addressed with the following code:</p><pre>@Composable<br>fun sharedViewModelStoreOwner(<br>  navBackStackEntry: NavBackStackEntry, <br>  navController: NavController<br>): NavBackStackEntry {<br>  return remember(navBackStackEntry) { <br>    navController.getBackStackEntry(BLUE_SCREEN) <br>  }<br>}</pre><p>In the snippet above, we hardcoded BLUE_SCREEN because it represents our starting screen, where the ViewModel is initially created.</p><p>With this function in place, we need to update our screens. For example, let’s consider the RED_SCREEN. In the navigation graph definition (within the ComposeRoot function), we need the following:</p><pre>composable(route = RED_SCREEN) {navBackStackEntry: NavBackStackEntry -&gt;<br>    RedScreen(navBackStackEntry, navController, goToBlue, goToGreen)<br>}</pre><p>In the screen itself, we can use the navBackStackEntry and navController to retrieve the ViewModel from another screen. Here&#39;s how:</p><pre>@Composable<br>fun RedScreen(navBackStackEntry: NavBackStackEntry, navController: NavHostController, goToBlue: () -&gt; Unit, goToGreen: () -&gt; Unit) {<br>    val viewModelStoreOwner = sharedViewModelStoreOwner(navBackStackEntry, navController)<br>    val viewModel: NameViewModel = viewModel(viewModelStoreOwner)<br>    GenericScreen(Color.Red, viewModel, goToBlue, {}, goToGreen)<br>}</pre><p>As a result, RED_SCREEN now uses the ViewModel created for BLUE_SCREEN. This means that any changes the user makes while interacting with RED_SCREEN will persist and be available when they navigate back to BLUE_SCREEN. You can explore the complete solution here:<br><a href="https://github.com/mjureczko/shared-viewmodel/tree/shared_navigation_scoped_viewmodel">https://github.com/mjureczko/shared-viewmodel/tree/shared_navigation_scoped_viewmodel</a></p><p>Is this approach better than using an Activity-scoped ViewModel? It does offer an opportunity to reduce boilerplate code, as the ViewModel is instantiated in the Compose code which reduces the need to pass it around between functions. However, whether this is preferable ultimately depends on personal preference, it’s a matter of taste.</p><h3>Summary</h3><p>We explored two solutions for sharing a ViewModel between different screens, delving into various details of Compose building blocks along the way.</p><ol><li><strong>Activity-Scoped </strong><strong>ViewModel</strong>: This approach passes the ViewModel as a function parameter through consecutive screens.</li><li><strong>Navigation Stack </strong><strong>ViewModel</strong>: This leverages the navigation stack to identify the ViewModel&#39;s creator, enabling reclamation and reuse of the same ViewModel.</li></ol><p>These two methods don’t exhaust all possibilities. Depending on your specific requirements, you might need to devise a third approach. Nonetheless, this journey should have clarified the underlying concepts and made exploring alternatives significantly easier.</p><p><strong>A Note on ViewModel Best Practices.</strong> Both solutions rely on ViewModel, so it’s worth emphasizing a critical best practice: avoid references from the ViewModel to UI widgets or the Activity. This becomes particularly important with shared ViewModels. When a ViewModel is shared across multiple screens, it may outlive the UI widgets or the Activity. Holding such references in these cases could lead to unintended behavior or memory issues.</p><p><strong>Share Your Thoughts.</strong> I believe learning is a collaborative journey, and your insights can enrich this exploration. If you notice mistakes, have questions about specific details, or think some sections could benefit from additional clarification, I wholeheartedly encourage you to leave a comment. Your feedback not only helps me refine my understanding but also makes this post more valuable to others. So, let’s keep the conversation going!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af38c16ad415" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/jetpack-compose-demystified-key-concepts-unveiled-through-sharing-viewmodels-across-screens-af38c16ad415">Jetpack Compose Demystified: Key Concepts Unveiled Through Sharing ViewModels Across Screens</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recruiter Fallacies: the common interview mistakes and how to avoid them]]></title>
            <link>https://medium.com/@marian.jureczko/recruiter-fallacies-the-common-interview-mistakes-and-how-to-avoid-them-f60527dd54c1?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/f60527dd54c1</guid>
            <category><![CDATA[recruitment-process]]></category>
            <category><![CDATA[recruitment]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Wed, 06 Mar 2024 20:26:25 GMT</pubDate>
            <atom:updated>2024-03-06T20:26:25.968Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*7NB5CYYZh4I6JJ5TTsJq2Q.jpeg" /></figure><p>Welcome to one of two versions of an exploration into the intricate web of cognitive biases commonly encountered throughout the recruitment journey. In this post, we delve into the recruiter’s viewpoint, while the counterpart dissects these challenges candidate’s perspective flavour.</p><p>During job interviews, I’ve encountered numerous surprising situations. These situations didn’t follow a common pattern and could involve different aspects of the recruitment process. Sometimes, it was unexpected candidate assessment, but it could also pertain to the behaviour of either the candidate or the recruiter. After spending several years as a recruiter, I began to notice recurring themes. However, I couldn’t fully understand these patterns until I read Daniel Kahneman’s “Thinking, Fast and Slow” [Kahneman2011]. In this book, the author explores various intriguing topics, including biases and fallacies that people often fall victim to. I was astonished to find how many of these biases and fallacies manifest during the recruitment process.</p><p>I invite you to explore a synthesis of Kahneman’s theories (his book served as an excellent starting point, but I also drew from additional sources) and my own 15 years of experience as a recruiter in the IT industry. My hope is that this synthesis will aid recruiters in more accurately assessing candidates by helping them in avoiding common fallacies and biases. Eager to enhance your recruitments? Let’s explore the realm of fallacies and biases because the best way to resist them is to be aware of their existence.</p><h3>What we believe we observe during the interview</h3><p>To kick off our exploration, let’s begin by examining the biases that most directly influence the gap between what we think we see and what truly unfolds before us.</p><h4>How much time does a recruiter need to assess a candidate during interview?</h4><p>Do we need to spend so much time on recruitment? Can’t recruiters make faster decisions? These questions have intrigued some scientists too, and they conducted experiments to uncover the truth [Prickett2000]. Their focus was on determining how long it takes to form an assessment of an interviewee that remains consistent throughout the interview. But before revealing the answer, I’ll let you take a moment to make your own guess. In the meantime, let’s dive into the experiment’s crucial details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*OQRetTutP2niohjxem4k9A.jpeg" /></figure><p>The scientists organized 59 employment interviews, each conducted by two trained interviewers. These sessions were planned for a mere twenty minutes, following predefined scenarios and consisting of questions such as:</p><ul><li>“Tell us something about your general career interests.”</li><li>“What would you call your potentials or assets?”</li><li>“What kind of people do you like being around?”</li></ul><p>It’s important to note that these interviews didn’t resemble the comprehensive technical interviews often seen in the IT field. They were more like screenings. However, they were conducted with scientific precision. Naïve observers were introduced to the recruitment processes and asked about the expected outcomes early on. And here’s the promised answer — it took these observers a mere 20 seconds to make assessments that would stand unwavering through the rest of the interview (technically speaking, was in line with recruiters assessment delivered after the whole interview). That’s right, just 20 seconds. To put it into perspective, 20 seconds is barely enough time for a polite handshake and a brief greeting. It leaves little room for gaining substantial insights through interview questions alone.</p><p>Of course, in the world of IT, interviews typically involve far more than just first impressions. Detailed programming languages and frameworks questions are the norm. Nonetheless, this study highlights the undeniable impact of first impressions. While it may not be the sole determining factor in interviews involving a comprehensive set of technical questions, it still play a significant role, especially when combined with the confirmation bias (discussed later).</p><p>Interviews can be stressful situations, and stress can affect candidate professionalism. While there’s no one-size-fits-all solution for handling stress, preparation can make a significant difference. Knowing the interview procedure in advance or having some insight into the interviewers can alleviate stress. Hence I have a tip for recruiters: consider sharing technical details about the interview environment before the session (not the interview questions, of course). Creating a comfortable environment for candidates can help reduce stress, ultimately minimizing the risk of rejecting a good candidate due to a bad first impression.</p><h4>Recency bias</h4><p>We delved into how interviews kick off, but now it’s time to shift our focus to the other end of the spectrum — those critical final moments. This is where the recency bias can cast its deceptive spell. This cognitive fallacy is most commonly associated with duping investors into thinking a few recent data points signify a new trend, when, in reality, it’s just a fluke. The recency bias gives undue weight to the most recent events, often overlooking the historical context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*frTzT6A-Qq6tVeaEQ1N89w.jpeg" /></figure><p>Regrettably, this bias can rear its head during recruitment as well. I often have heard recruiters arguing that the candidate’s most brilliant ideas popped up right at the end of an interview and that an extra five minutes would be enough to unravel the task at hand. Alas, there were no extra minutes and the candidate didn’t finish the task (it could be argued that this is a recruiter’s mistake, but let’s set that aside as it’s unrelated to the cognitive biases we’re discussing here). The assumption that the task would have been cracked given more time is pure speculation — it didn’t actually happen during the interview. You might wonder whether any harm is done in such situations. Perhaps the candidate is a sharp individual who simply needed a little extra time to shine. However, remember that this bias cuts both ways. A glaring mistake made in the final moments of the session can outweigh several previously stellar answers. It’s a bias, plain and simple, leading to inaccurate assessments.</p><p>For recruiters, awareness is key. They must consciously consider the weight of events that unfold towards the end of an interview. Being aware of the recency bias can help prevent misjudgments.</p><h4>Halo effect</h4><p>In the realm of tech interviews the first impression and the recency bias, often come together to form a more comprehensive concept: the halo effect. But before we dive into its definition, let’s embark on a thought experiment together.</p><p><a href="https://docs.google.com/forms/d/e/1FAIpQLSdpcvsaQINzj3GR1JQ9Syvi6eLFHs2Haz6weCv3FuN92KWVBQ/viewform">Here is a questionnaire</a>, and your task is to guess who John Ausonius is. The catch? No Googling allowed — it’s all about making an educated guess based on what you see. Curious how other answered? <a href="https://docs.google.com/forms/d/e/1FAIpQLSdpcvsaQINzj3GR1JQ9Syvi6eLFHs2Haz6weCv3FuN92KWVBQ/viewanalytics">You can find the answers here</a>, but I must admit, I’m in the dark about the results myself. This questionnaire could have been tampered with, or the experiment might not effectively illustrate the halo effect I’m about to discuss.</p><p>So, what’s this bias all about? At its core, it’s about extrapolating what we know about certain traits or attributes to other aspects we’re less familiar with. Imagine it as a “halo” of knowledge casting light onto the unknown.</p><p>In the context of the John Ausonius questionnaire, respondents were likely unaware of his identity, with only his appearance as a known factor. Unfortunately, relying on appearance is a shaky premise at best. Nevertheless, it can lead one to believe that since he is good-looking, he is supposed to hold a respected position — perhaps a movie star or a promising scientist. If you found yourself thinking along these lines, you fell victim to the halo effect.</p><p>Do you wondered if the halo effect, that tendency to make judgments based on appearance, was just a mere coincidence? Fortunately, an empirical study conducted by Landy and Signal in 1974 [Landy1974] delved into this very question. In this study, researchers crafted a collection of essays, purposefully mixing poorly written and well-crafted pieces. But that’s not all; they added a twist by attaching photographs of supposed authors to two-thirds of the essays. These photos featured individuals categorized as either good-looking and attractive or, conversely, as less attractive. Next, a group of sixty students was brought in to rate these essays on a scale from 1 to 9. What did the results reveal? Well, when it came to well-written essays attributed to attractive authors, they received an average rating of 6.7. On the flip side, essays attributed to less attractive authors garnered an average rating of 5.9. As for the poorly written essays, the contrast was even starker. Those linked to attractive authors received an average rating of 5.2, whereas essays by less attractive authors plummeted to a mere 2.7. This study serves as a powerful illustration of how the halo effect can profoundly influence our judgments, even in seemingly objective scenarios like evaluating written work.</p><p>Now, consider how this plays out in interviews. It’s all too common for interviewers to overvalue a candidate’s standout responses, extrapolating their strengths into areas where they may lack core competencies. This is particularly true when the interview superficially covers the core skills. The interviewer might assume that because the candidate excelled in one domain, they possess a breadth of knowledge and problem-solving abilities on others. Statements like “It was a great conversation with the candidate” can inadvertently cloud the judgment of technical competencies. It’s vital for recruiters to stay grounded in their criteria and not fall into the trap of accepting an eloquent beginner while dismissing a seasoned engineer who may have had a few stumbling moments in less relevant discussions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*E7DCXpieCziWAov09VWYMg.jpeg" /></figure><p>Moreover, it’s worth noting that the halo effect isn’t the only player in town. There’s also a darker counterpart known as the “horn effect,” “devil effect,” or the “reverse halo effect.” This is where the interviewer allows one negative aspect or a less-than-stellar performance to cast a long shadow over the candidate’s overall assessment.</p><p>In the world of tech interviews, recognizing and mitigating these biases is crucial to making fair and accurate evaluations. So, let’s strive for objectivity and ensure that our decisions are grounded in a thorough understanding of the candidates’ true capabilities.</p><h4>Priming</h4><p>In our quest to understand the intricacies of cognitive biases, here’s another fascinating phenomenon to ponder. I’ve set up a little experiment for you to try, which delves into the world of priming. I’ve crafted two questionnaires: <a href="https://docs.google.com/forms/d/e/1FAIpQLSdtOz58yhj3hZFXX20xpPerJEuidiLw65l6G5vQyMhR__ASbg/viewform">the first</a> and <a href="https://docs.google.com/forms/d/e/1FAIpQLSfrUYXwNvOePWTVGH6uudaP4sOeehMyH0dsVOP2fcNIM0D4iA/viewform">the second</a>. Your task is to fill out just one of them, selecting at random. After completing the questionnaire, take a moment to check out the results for both versions. You can find <a href="https://docs.google.com/forms/d/e/1FAIpQLSdtOz58yhj3hZFXX20xpPerJEuidiLw65l6G5vQyMhR__ASbg/viewanalytics">the results for the first one here</a> and <a href="https://docs.google.com/forms/d/e/1FAIpQLSfrUYXwNvOePWTVGH6uudaP4sOeehMyH0dsVOP2fcNIM0D4iA/viewanalytics">the second one here</a>.</p><p>You might have noticed that both questionnaires present the same riddle, with the only distinction lying in the introduction. While both intros may appear somewhat quirky, the essential difference is that one is centered around food, while the other revolves around showers. The intriguing part is that the introduction primes your mind, potentially leading you to favor “soup” as the answer in the food-related version and “soap” in the shower-related one. These questionnaires were released into the wild, so I’m curious to see if they truly capture this psychological phenomenon.</p><p>But let’s dive deeper into the concept of priming. In essence, it revolves around the power of subtle suggestions. According to the definition, these suggestions can act as stimuli that trigger your thinking towards ideas and concepts associated with the given stimulus. An illustrative example of this effect comes from Bargh et al.’s empirical study [Bargh1996]. In this experiment, participants engaged in a lexical exercise involving words related to stereotypes of elderly people. Meanwhile, another group worked with random words. When the real experiment began, both groups were asked to change rooms. The time it took for participants to traverse to the new location was meticulously measured. The results were astonishing: those primed with “elderly” words walked notably slower.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*wkXrIoiZkm1hnzuMnKsqiA.jpeg" /></figure><p>Now, how does all this relate to interviews in the tech world? Well, here’s where it gets unique, as the candidate becomes the unwitting victim of this phenomenon. During an interview, candidates naturally hang on to every word uttered by the interviewer. In the world of IT, there often aren’t universally “right” or “wrong” answers; context plays a significant role. While interviewers possess an understanding of their company’s context, candidates may not. So, when an interviewer subtly suggests, for instance, that “shifting right” in testing is superior to “shifting left,” it can have a profound impact on how a candidate approaches a task related to test strategy. There’s nothing inherently wrong with this. Why should the candidate not try to figure out what answers the recruiter expects? The issue may lie with the interviewer.</p><p>The question arises: What’s the interviewer’s ultimate goal? Is it to assess the candidate’s ability to build upon suggestions, consciously or unconsciously made, or to gain insights into their competencies and preferences? The answer is clear — it’s the latter. Hence, it’s crucial for interviewers to be mindful of the suggestions they convey during interviews. Refrain from hinting at answers, whether they’re “good” or “wrong.” It’s all about ensuring a fair evaluation that truly reflects the candidate’s capabilities and telling stories about topics related to interview question doesn’t help here. In the world of tech interviews, understanding the power of priming can help to navigate the interview process with greater clarity and fairness.</p><h3>How we interpret what is observed during the interview</h3><p>In the intricate world of tech interviews, even if the recruiter skillfully identifies and separates the facts gleaned during the interview from any embellishments, there’s still a formidable challenge ahead. There are lurking pitfalls of cognitive biases that lead people astray when attempting to craft a comprehensive conclusion.</p><h4>Conjunction fallacy</h4><p>Let’s delve into the world of cognitive biases once again, using an intriguing exercise suggested in a study by Tversky and Kahneman [Tversky1983]. I have<a href="https://docs.google.com/forms/d/e/1FAIpQLSfm3f6bbN0QAsRPQFsMPQl729rZAlMgCmphfBRJL96IA0mGUg/viewform?fbzx=31179410294709331"> a questionnaire </a>where your task is to rank seven different statements about Linda from most to least probable.</p><p>At first glance, some of these options might seem plausible, while others appear unexpected. However, the key to this exercise lies in identifying the two relevant choices: “Linda is a bank teller” and “Linda is a bank teller and is actively involved in the feminist movement.” When placed side by side, it becomes apparent that the latter is an elaboration on the former. Essentially, it suggests that among all the bank tellers globally, some are actively engaged in the feminist movement. Therefore, “Linda is a bank teller and is actively involved in the feminist movement” is a subset of “Linda is a bank teller.” From a mathematical perspective, the only correct answer to this exercise is to rank “Linda is a bank teller” higher in the list. This is a fundamental principle of probability — conjunctions of two events may not be more probable than those events in isolation. <a href="https://docs.google.com/spreadsheets/d/156aPg-Rt_lcgVEPyTHWr3CNbH34j3EGOvrKK1u-1WTw/edit?resourcekey#gid=338905984">On this chart, you can discover the number of individuals who resisted this fallacy and how many succumbed to it</a>.</p><p>However, our intuition often leads us astray. Tversky and Kahneman conducted the Linda experiment with statistics students (who should have been well-versed in probability), and a staggering 85% of participants made the mistake. There are various possible explanations for this phenomenon, one of which is highly relevant to our discussion on recruitment — scenario thinking. This concept was vividly illustrated by Dawes [Dawes1988]. In an experiment, people were asked if a tennis player who is an alcoholic could win a tournament in the next eight months. Most respondents were skeptical. However, when this question was accompanied by a hypothetical scenario where the player joined an Alcoholics Anonymous club, stopped drinking, trained rigorously while finding motivation in overcoming addiction (note that this scenario didn’t claim he had actually done these things), the player’s chances were rated much higher.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*k7ZJ4M-w7BYCbtDixenqRA.jpeg" /></figure><p>Now, let’s circle back to the world of tech recruitment. While the interview outcome may not be used to predict tournament victories, it’s not too dissimilar from predicting a candidate’s performance within a company. So, recruiters, take heed of the conjunction fallacy and scenario thinking. Be cautious about hastily building conclusions and remember the gaps in your knowledge about the candidate. Intuition has a knack for filling in those gaps, often leading us astray.</p><h4>What you see is all there is</h4><p>To illustrate the next cognitive bias, let’s engage in a thought experiment proposed by none other than Daniel Kahneman in his insightful work [Kahneman2011]. We’ll delve into the world of recruitment this time, so the implications are immediate and profound.</p><p>Imagine we have a candidate named Mindik. We’re tasked with making a judgment about her potential as a leader. The initial information we receive is that Mindik is intelligent and strong. What’s your immediate reaction? Are you already forming a conclusion? Hold that thought.</p><p>Now, consider the complete description of Mindik: she is not only intelligent and strong but also corrupt and cruel. How does your perspective change now? If you find yourself oscillating between acceptance and rejection, you’re not alone. It’s a common response, and it underscores how our intuitive judgments can be wrong.</p><p>However, the proper approach to recruitment isn’t to jump to conclusions based on limited information. Instead, we should start by clarifying what do we need to learn about a candidate to make an accurate assessment of their suitability for a leadership position? Statistically, it’s likely that you instantly categorized Mindik as either a “good” or “bad” candidate. This is not the correct approach, but it is precisely how our intuition operates.</p><p>The cognitive bias at play here is known as “What You See Is All There Is” (WYSIATI). It suggests that we tend to form judgments based on the information readily available to us, without delving into what we don’t know or considering the quality and quantity of the data. We often accept the available information, without scrutinizing potential gaps or assessing the credibility of the source. The key factor is the consistency of the narrative — if the story aligns, we’re inclined to draw a conclusion swiftly. “Mindik is intelligent and strong” makes a consistent story (although we know hardly a thing about her), so our mind is eager to jump to the conclusion right away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*z1BZY00CCu4JfPLKc_qDOQ.jpeg" /></figure><p>Kahneman shares a captivating example of this bias from his experience with the army, where he was responsible for evaluating officer candidates. During the evaluation, soldiers faced the challenging task of crossing over a wall without touching it, armed only with a bar as a tool. They had to organize themselves, with some emerging as leaders and others as team players. Kahneman and his colleagues observed the proceedings closely, but they fell into the WYSIATI trap.</p><p>They were so confident in the evaluations they provided, despite having ample time to witness how the soldiers truly performed. Astonishingly, there was virtually no correlation between the evaluations and actual performance. The bias was so entrenched that Kahneman initially believed their assessments were accurate and resisted changing the process. This brings us close to the next bias, but before we dive into that, let’s return briefly to the realm of technology.</p><p>In the field of IT recruitment, we shouldn’t expect a different outcome. However, awareness of the existence of such biases can spark reflection. Recruiters can ponder what remains unknown about a candidate, what aspects are uncertain, and how these should factor into the final assessment.</p><h4>Confirmation bias</h4><p>Let’s talk about confirmation bias, a cognitive quirk that can significantly impact decision-making and assessment processes. To illustrate this phenomenon, we’ll explore a guessing game that not only challenges your intellect but also reveals the power of this cognitive bias.</p><p>In this game, one player devises a rule for generating sequences of three numbers and shares an example sequence that adheres to the rule. The challenge for the other player is to deduce the rule. The twist? The second player can propose new sequences of three numbers, and the first player responds with a simple “yes” or “no” to indicate whether the proposed sequence adheres to the rule. The goal is to pinpoint the rule with the fewest sequences, making it a captivating brain teaser. Intrigued? You can try it for yourself. Here is <a href="https://docs.google.com/forms/d/e/1FAIpQLSdZfvo_vwq1FP3oTUpTqUVFZcRdpmwMdGZmI5DuDBKq8FOB7g/viewform?fbzx=-4934869796456939894">the task</a>.</p><p>This exercise plays on the confirmation bias, one of the intriguing fallacies that affect our thinking. The optimal strategy here is to divide the set of potential answers in a way that maximizes the rejection of possibilities, leading to the quickest path to the solution. However, a more common approach is to craft a theory and then seek evidence that supports it. For instance, you might theorize that the rule pertains to providing three even numbers (the numbers from the task were 2, 4 and 6). We already know the conjunction fallacy, so for sure you do not think that the rule is “three <strong>subsequent</strong> even numbers”. To confirm this theory, you might test sequences like 10, 20, 30 and then 100, 1000, 10000, hoping for two “yes” responses to build your confidence. But here’s the twist: the correct rule is “any increasing sequence.” In this case, our sequences were quite similar, and we failed to explore the broader domain of possibilities.</p><p>I’m not privy to the theories participants formulated and, consequently, whether the suggested sequence aligned with the theory. Instead, I can provide you with <a href="https://docs.google.com/spreadsheets/d/1WixHTyTwo_l3-LfKD41szFTnKQ1hH1e9hwxyrpRlreE/edit?resourcekey#gid=1563869756">statistics regarding the proportion of sequences conforming to the actual rule</a>, namely, “any increasing sequence.”</p><p>The confirmation bias manifests in various forms, and in the context of our numerical exercise, it takes the shape of <strong>selective hypothesis testing</strong>. This bias leads us to test hypotheses by actively searching for confirming evidence, ultimately seeking out answers that align with our theories.</p><p>Now, let’s pivot to the world of tech recruitment, where the confirmation bias can lurk and influence decisions. An intriguing experiment conducted by Snyder and Swann [Snyder1978] sheds light on this phenomenon. Participants were asked to rate individuals on the introversion-extroversion scale after an interview. The catch? Some interviewees were introduced as introverts, while others were presented as extroverts. What happened next was eye-opening. Participants selected interview questions from a predefined list that strongly aligned with the presumed traits. If an interviewee was labeled an introvert, the majority of questions assumed introversion, such as, “What do you find unpleasant about noisy parties?” In contrast, for those labeled as extroverts, the questions mostly presumed extroversion, like, “What would you do to liven up a dull party?” This experiment was not about disproving hypotheses; it was all about seeking confirmation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*jCz2BPZsKqQEGEWJVK9OWQ.jpeg" /></figure><p>What participants engaged in during this experiment mirrors an imperfect recruiting strategy. Recruiters should avoid a barrage of similar questions aimed at confirming a preconceived hypothesis about a candidate. If you search long enough, you’ll eventually find confirmation, even for a false hypothesis.</p><p>Confirmation bias comes in various shades, another of its intriguing forms is known as <strong>selective recall</strong>. Let’s delve into a scientific experiment from the late ’70s that vividly illustrates this cognitive quirk and explore how it might influence tech recruitment.</p><p>In this experiment conducted by Snyder and Cantor in 1979 [Snyder1979], participants were presented with a profile of a fictitious woman. This profile was meticulously designed to feature a balanced mix of both introverted and extroverted behaviors. However, the real twist came when participants were later asked to recall specific examples of her introversion and extroversion. What made this study particularly fascinating was the context provided before the recall task. Some participants were informed that the assessment was for a librarian job, while others were told it was related to real estate sales. The result? The type of assessment significantly influenced what participants remembered. Those who believed the assessment was for a librarian position found it easier to recall introverted behaviors, while participants in the context of real estate sales more frequently recalled extroverted behaviors.</p><p>Now, in the realm of tech recruitment, we typically know the nature of the job upfront. However, this doesn’t exempt us from the influence of selective recall. The impact may manifest in a different context, one where a relevant hypothesis about the candidate is identified at a later stage in the evaluation process. Recruiters might find themselves delving into their memory to either confirm or refute this hypothesis. In such situations, the value of meticulous note-taking cannot be overstated. While notes may not be comprehensive, they are certainly less biased than our memory.</p><p>In the realm of confirmation bias, the story doesn’t end with merely seeking out information that confirms our preexisting beliefs. It extends to how we interpret the information we gather. A fascinating experiment at Stanford University in 1979, conducted by Lord and his colleagues [Lord1979], sheds light on this cognitive phenomenon.</p><p>The study involved participants who had clear and distinct views on the topic of capital punishment — with half of them in favor of it and the other half against it. These individuals were presented with deliberately fabricated studies that compared the effects of the death penalty on various aspects of society.</p><p>One of these studies supported the notion of severe punishment, while the other argued against it. After reading these studies, the participants were asked to share their opinions. The outcome of this experiment wasn’t surprising: participants perceived the studies that aligned with their preexisting beliefs as more convincing and better conducted. This peculiar effect, documented by the researchers, is known as <strong>selective interpretation</strong>. This intriguing study highlights how confirmation bias can not only guide our information-seeking behavior but also influence the way we interpret and evaluate the information we encounter. It’s a reminder of the complex interplay between our preconceived notions and the objective evaluation of evidence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*AZlCA5A3D6eHIILfeDBdnA.jpeg" /></figure><p>The last type of confirmation bias I’d like to discuss is the <strong>illusion of correlation. </strong>It is a particularly intriguing cognitive phenomenon worth exploring. This bias was vividly demonstrated in a fascinating experiment involving the Rorschach test, as outlined by Plous [Plous1993].</p><p>You might recognize the Rorschach test from its depiction in various movies. In this test, patients are presented with cards featuring black inkblots of various shapes and are tasked with describing the objects or shapes they perceive within these abstract images.</p><p>Now, in the experiment described by Plous, participants were provided with contrived Rorschach test results. They were then instructed to search for potential correlations between the patients’ sexual orientation and their responses to the test inkblots. The catch? The data was artfully crafted in a way that avoided any genuine positive correlations between these variables.</p><p>Despite this careful manipulation, the participants managed to uncover imagined relationships, such as the belief that “homosexual individuals often interpret the inkblots as buttocks,” even though these specific instances were statistically underrepresented. Astonishingly, the mere discovery of one or two instances of these perceived correlations led the participants to disregard the abundance of cases that failed to support such relationships. It’s quite evident that these imaginary correlations, fueled by the illusion of correlation, can have significant implications, particularly when it comes to fields like recruitment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*stKXhjSCFVDLSafSuiKHmw.jpeg" /></figure><p>Within the world of recruitment, the confirmation bias takes on various forms, and together, these biases pose a significant challenge for recruiters. This cognitive machinery has a unique ability to unconsciously weave an imaginary narrative around a candidate. To shed light on this intricate phenomenon, let’s visualize it on a chart.</p><p>Imagine we’re beginning with a set of recruitment observations: the candidate’s responses, behaviors exhibited during the interview, and so on. We can represent these data points as dots on a graph, where the X-axis denotes the different areas of focus during the recruitment process, and the Y-axis quantifies the candidate’s performance in those areas:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*PupdlF5b_lxKEkbi8OKwqw.png" /></figure><p>Now, it’s essential to recognize that there’s a considerable amount of empty space between these data points. In reality, it’s nearly impossible to gather exhaustive information about every facet of a candidate’s competencies within the confines of an interview. As a result, many facts about the individual remain shrouded in mystery. However, understanding more about the candidate offers the potential for a more accurate assessment.</p><p>This brings us to a natural tendency that many recruiters face: the inclination to construct hypotheses about the candidate, which essentially create a more holistic and comprehensive portrayal than the raw data points can provide. In simpler terms, it involves crafting a narrative about the candidate.</p><p>This pivotal moment can become a turning point, as the recruiter may inadvertently steer the interview in a direction that aligns with their preconceived story. This may involve asking questions that confirm this imagined narrative, making it easier to remember facts that fit the story while conveniently forgetting those that do not. In essence, the recruiters may begin to seek confirmation of their hypothesis. This is where the danger lies. Instead of an accurate assessment, the recruiter ends up with a fabricated story, one potentially influenced by the initial impression and further fueled by the confirmation bias. On our chart, we can depict this as a red line that effectively connects the dots, a line drawn through simple interpolation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*rFWX7NSgorLDSj62xAlr2A.png" /></figure><p>Conversely, it’s crucial to realize that this constructed narrative may not necessarily reflect the true essence of the candidate. The line, symbolizing the recruiter’s mental storyline, indeed passes through all the data points but is primarily composed of the spaces between those points. These gaps are filled with intuition or recruiter fallacies, and there’s no concrete evidence to substantiate the accuracy of this narrative. The actual candidate could be entirely different, for better or worse. It’s possible that the true narrative of the candidate is better represented by the blue line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*BzvgKEh0M2w70-6I6x6cKg.png" /></figure><p>The real challenge here is that there are no tangible facts or observations that decisively determine which of these two lines is correct — both appear to align with everything observed during the interview. Nonetheless, there’s a significant risk that the recruiters might end up assessing the story about the candidate that they’ve constructed (the red line) rather than evaluating the actual candidate.</p><p>For recruiters, it’s imperative to remain rooted in the realm of concrete facts, as otherwise they enter the territory of invented narratives, fallacies, and cognitive biases which can lead to misguided decisions and missed opportunities for exceptional talent. The recruitment processes can also help mitigate the risks associated with invented narratives. One effective approach is to create a system where recruiters collaborate, compare notes, and challenge each other’s conclusions when doubt arises. A notable example of how this challenge is addressed can be found in Laszlo Bock’s book [Bock2018], where he sheds light on Google’s strategies for promoting objectivity and reducing bias in their recruitment processes. By encouraging teamwork and cross-examination, companies can foster a fairer and more reliable hiring environment that ultimately benefits both candidates and employers.</p><h3>Consideration about recruitment result correctness</h3><p>With the recruitment process now concluded, and hopefully, with minimal influence from the fallacies discussed in previous sections, it’s time to turn our attention to the quality of this evaluation.</p><h4>Regression toward the mean</h4><p>Let me start the discussion about recruitment results with a phenomenon called “regression toward the mean”. Imagine you’re conducting a test for a group of students. They are entirely unprepared and are randomly guessing the answers. Each question provides three possible choices, so, on average, you’d expect the results to hover around 33%. However, some students, driven by luck, end up with higher scores, while others, less fortunate, score below this average.</p><p>Now, let’s focus our attention on the top-performing student and the one who scored the lowest. Both were unprepared and based their answers on random guesses. Now, what if we were to administer a different test with entirely new questions, again outside the realm of their knowledge? How would these two students perform this time?</p><p>Should we expect that the top scorer will remain the best, and the lowest scorer will remain the worst? Rather, the most likely outcome is that their scores will regress toward the average, around 33%. Why? Because in our thought experiment, the deviations from the average are entirely random. It’s statistically more probable that, in the second test, the top performer’s score will decrease, while the lowest scorer’s score will improve. It’s the concept of regression toward the mean in action.</p><p>Before delving into how this connects to the world of recruitment, let’s explore an example from Kahneman’s book [Kahneman2011], featuring insights from an army aircraft instructor. The instructor emphasizes the importance of not praising pilots after they flawlessly execute a challenging manoeuvre because, in the next attempt, they are likely to make mistakes. Conversely, the instructor advocates providing harsh feedback to pilots who make errors, as this tends to enhance their performance in subsequent attempts. If by any chance, you believe that the instructor could be right, I recommend reading the great book of Miłosz Brzeziński [Brzeziński2013]. What is important for us, is that the instructor described the regression toward the mean in action while believing in a fictional theory fuelled by the illusion of correlation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*-otW5Zz57SeZq0RYSfmQ2w.jpeg" /></figure><p>Now, let’s transition back to the realm of recruitment. Have you ever encountered a situation where a candidate aced the interviews but struggled in their actual job? Conversely, have you witnessed someone who barely scraped through the interviews go on to become a superstar in the role? These scenarios, while perhaps not in such extreme forms, are common, and they are manifestations of the regression toward the mean. This time it’s not a cognitive bias; it’s simple mathematics at play. You can’t expect to replicate the same outcome when drawing from the same random distribution on two separate occasions. The key takeaway here is that we should avoid overestimating the significance of recruitment outcomes. Still sceptical? Take a look at the statistics I’ll be presenting in the following section.</p><h4>Can we predict an employee’s performance on the basis of a job interview?</h4><p>In the realm of tech recruitment, there’s a fascinating study by two researchers who condensed 85 years of research into a comprehensive analysis [Schmidt1998]. They performed a meta-analysis encompassing 32,000 employees across 515 vastly diverse job roles. The core objective was to construct a prediction model based on scores derived from interviews conducted during the recruitment process. This model aimed to forecast the actual job performance of the candidates post-employment. In simpler terms, they delved into how well recruitment results correlate with the subsequent performance of employed candidates. Now, let’s delve into the results they uncovered:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df7e1abc235eca74938d0737091b8225/href">https://medium.com/media/df7e1abc235eca74938d0737091b8225/href</a></iframe><p>The figures you see in the table quantify the extent to which these recruitment methods predict job performance. Let’s take the first row as an example. The researchers devised a prediction model that relied on the outcomes of General Mental Ability tests as its input. The model was then trained to forecast the performance of employees in their roles after the interview. An accuracy score of 100% would indicate a perfect match between the model’s predictions and actual job performance. However, when it came to General Mental Ability tests, the model achieved a mere 26%. In other words, a significant portion of real job performance couldn’t be explained by the test results.</p><p>In the source [Schmidt1998], you’ll encounter different figures, as the authors opted to present validity coefficients instead of explained variances. They argue that the former holds superior practical economic value. Nevertheless, it’s relatively simple to derive one from the other since the explained variance is the square of the validity coefficient.</p><p>What’s crucial for us to glean from this data is that none of the interview types excel at effectively predicting a candidate’s future performance. The most promising approach, work samples, only accounts for 29% of job performance. It’s worth noting that this data isn’t the most recent, and we can assume that improvements have been made in interview design since then. In practice, modern IT recruitment frequently involves a variety of interview types. It’s plausible that reanalyzing the same criteria for contemporary IT recruitment might yield more encouraging results. However, the fundamental lesson remains the same: recruitment outcomes should not be overestimated. An interview is not synonymous with the actual job. Factors such as motivations, pressure, and attitudes differ between the two contexts, potentially leading candidates to behave differently on the job compared to during the interview process.</p><h4>Intuition versus statistics</h4><p>If you’ve been following this post from the beginning, you might be arriving at the conclusion that intuition isn’t the ideal tool for assessing candidates. However, if you still have some doubts, this section aims to dispel them.</p><p>Let’s kick things off with an example from Kahneman [Kahneman2011], which happens to revolve around recruitment, making it a perfect fit for our narrative. Kahneman credits Meehl and his enlightening book [Meehl1954] as the true inspiration behind this example.</p><p>The story unfolds with Kahneman being called upon by the army to assist in revamping their underperforming recruitment process. The original approach involved trained personnel conducting 20-minute interviews to evaluate candidates for high-ranking positions. The assessments were intuitive but frustratingly weakly correlated with the candidates’ later job performance.</p><p>Meehl, as stated in his book [Meehl1954], regarded intuition as a poor tool, often outperformed by a simple linear regression. Taking a page from Meehl’s playbook, Kahneman proposed a formalized assessment that focused on six different candidate characteristics, each scored on a 5-point grading scale. When the data was quantified in this manner, it made little difference whether the final evaluation was carried out by trained personnel or a linear regression model. The results were significantly improved compared to the previous approach.</p><p>Now, let’s move from the military to an even more vivid example, this time in the world of wine. Have you ever come across the Ashenfelter’s model? You might intuitively think that assessing wine quality requires an expert meticulously examining taste, colour, and a host of other enigmatic factors. Well, that’s one way to go about it. However, there’s another approach pioneered by Professor Orley Ashenfelter from Princeton University. He meticulously analyzed data regarding wine prices in the Bordeaux region from 1952 to 1980, creating a model that correlated wine prices with factors like date and weather conditions. To the shock of wine experts, Ashenfelter’s model, despite being so simple, consistently outperformed all other wine price estimates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*54wAqKybH2gp8_75EJ8T0w.jpeg" /></figure><p>The lesson here is clear: despite what our intuition might suggest, mathematical models, even relatively simple ones, often outshine intuition. However, there are exceptions to this rule. When dealing with rare events, models face significant challenges, precisely where our intuition tends to excel. Consider a task where we need to predict whether Mr X will head to the cinema in the evening. The model would likely excel at quantifying the cinema’s offerings and correlating them with Mr X’s preferences. However, what if we discover that Mr X has broken his leg? Such a rare event is unlikely to be accounted for in the model. Yet, it’s an intuitive leap for humans to conclude that Mr X will visit the hospital, not the cinema. Nonetheless, as a recruiter, keep in mind that your candidates rarely have broken legs, and the model often provides a more reliable assessment.</p><h3>My insights and pieces of advice</h3><p>In our journey through the intricacies of cognitive biases in recruitment, we’ve explored various pitfalls that can influence the hiring process. I’ve strived to present these biases from the perspective of recruiters, in terms of how to safeguard themselves from becoming unwitting victims of these biases. Let’s take a moment to revisit the biases we’ve covered:</p><ul><li><strong>In the process of observation:</strong><br>- <strong>First Impression</strong>: Recruiters often form an unchanging opinion of a candidate within the first 20 seconds of an interview.<br>- <strong>Recency Bias</strong>: Recent events tend to carry more weight in our minds.<br>- <strong>Halo Effect</strong>: People tend to extrapolate positive ratings of one characteristic to other unknown characteristics of the same person.<br>- <strong>Priming</strong>: Exposure to one stimulus can influence a response to another unrelated stimulus.</li><li><strong>In the interpretation of observations:</strong><br>- <strong>Conjunction fallacy</strong> — people intuitively overvalue the probability of conjunction of events.<br>- <strong>What You See Is All There Is</strong>: The quality and quantity of data seem less important than the consistency of the story created from the data.<br>- <strong>Confirmation Bias</strong>: Tendency to prefer analyse that confirms initial thought. This includes selective hypothesis testing, selective recall, selective interpretation, and the illusion of correlation, resulting in the invention of imagined narratives that feel unquestionably true.</li><li><strong>Recruitment result correctness:</strong><br>- <strong>Regression Toward the Mean</strong>: Subjects who score extremely high or low in one attempt tend to move closer to the mean in subsequent attempts.<br>- <strong>Statistical Models vs. Intuition</strong>: Statistical models typically outperform human intuition.</li></ul><p>These biases are just the tip of the iceberg. Each of them is a deep and complex topic on its own. For those looking to delve further into the subject, I highly recommend Daniel Kahneman’s book [Kahneman2011]. Additionally, most of these concepts have comprehensive Wikipedia pages that offer rich and detailed content.</p><p>But I’d like to go beyond the scientific aspect and share some personal insights and interpretations.</p><p><strong>The first piece of advice is this: don’t try to be Sherlock Holmes.</strong> In Arthur Conan Doyle’s novels, Sherlock Holmes had an extraordinary ability to observe a person and deduce intricate details about their life, all from a simple glance. He could discern a scar and weave a compelling life story from it. The more details Sherlock provided, the more convincing the story seemed to be, but that is only the conjunction fallacy at play. In reality, the story is a cumulation of cognitive biases with the special role of the “what you see is all there is” and confirmation bias. Do you recall the story about the alcoholic tennis player, doesn’t it relate to Sherlock’s stories?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*7QSjVsVTmQ9QCnLe3hwUbg.png" /></figure><p>Consider explaining a scar in the Sherlock Holmes way. It’s easy to imagine that the scar is a battle wound from a neighboring country, and statistically speaking, that might seem like the most plausible explanation. However, reality is far more intricate. By merely gazing at a scar, you can’t definitively discern whether it’s a war injury, the result of an accident, or even a scar from a childhood mishap. The multitude of possibilities, regardless of their likelihood, accumulates to create a probability far higher than that of the seemingly most reasonable explanation. This insight serves as a poignant reminder that people’s lives are remarkably diverse, filled with unexpected twists and turns. So, if you ever find yourself in the role of a recruiter, caught in the trap of crafting an elaborate story for a candidate based on limited information, take a deep breath. Count to five, and let the impulse to draw hasty conclusions dissipate. Recognize that this tendency is a cognitive fallacy, and in the world of recruitment, objectivity and evidence should always take precedence.</p><p><strong>During the recruitment process, it’s vital to stick to the facts.</strong> This practice significantly reduces susceptibility to biases. What’s more, simple rating systems can often outperform human intuition, as demonstrated by Ashenfelter’s model versus wine experts. So, there’s no need to invent elaborate stories about candidates, no matter how tempting it may be.</p><p><strong>Taking notes is incredibly useful.</strong> Written observations are less subjective than relying on memory, and they naturally steer you towards sticking to the facts. When notes align with <strong>predefined scenarios and evaluation criteria</strong>, it further minimizes the risk of falling victim to confirmation bias. Each candidate is described in a standardized manner, leaving little room for the recruiter to introduce additional questions or, worse, “creative” theories about the candidate. Consider the story from the Israel army, where astonishing results were achieved by introducing six characteristics, each graded on a 5-point scale.</p><p><strong>Lastly, be aware of cognitive fallacies.</strong> Familiarizing yourself with these biases allows you to recognize them when they occur. Being aware is the first and most important step in resisting them.</p><p>In summary, I recommend the following:</p><ul><li>Avoid attempting to be Sherlock Holmes.</li><li>Stick to the facts.</li><li>Make thorough notes.</li><li>Follow a predefined interview scenario.</li><li>Utilize pre-defined evaluation criteria.</li><li>Be well-versed in cognitive fallacies.</li></ul><p>In closing, navigating the complexities of the recruitment process with a keen awareness of cognitive biases is not just a matter of professional acumen but a testament to a commitment to fair and effective hiring. As we strive for objective, data-driven assessments, we can foster an environment where biases hold no sway, and every candidate is evaluated on their merits. Embracing the principles of transparency, consistency, and open-mindedness will not only benefit our organizations but also contribute to a more equitable and inclusive future for the tech industry. So, as you embark on your recruitment journey, remember: you are armed with knowledge and insight, and together, we can work to transcend the limitations of cognitive biases and build stronger teams.</p><h3>References</h3><p>[Bargh1996] Bargh, J., Chen, M., Burrows. L. (1996). Automaticity of social behavior: direct effects of trait construct and stereotype-activation on action. In <em>Journal of Personality and Social Psychology.</em></p><p>[Bock2018] Bock, J. (2018) Work Rules!</p><p>[Brzeziński2013] Brzeziński, M. (2013). Głaskologia. Faktyczne reguły motywowania i rozumienia motywacji.</p><p>[Dawes1988] Dawes, R., &amp; Kagen, J. (1988). Rational choice in an uncertain world.</p><p>[Kahneman2011] Kahneman, D. (2011). Thinking, Fast and Slow.</p><p>[Landy1974] Landy, D., Signal, H. (1974). Beauty is talent: Task evaluation as a function of the performer’s physical attractiveness. In<em> Journal of Personality and Social Psychology</em>.</p><p>[Lord1979] Lord, C. G., Ross, L., &amp; Lepper, M. R. (1979). Biased assimilation and attitude polarization: The effects of prior theories on subsequently considered evidence. <em>In Journal of personality and social psychology.</em></p><p>[Meehl1954] Meehl, P. E. (1954). Clinical versus statistical prediction: A theoretical analysis and a review of the evidence.</p><p>[Plous1993] Plous, S. (1993). The psychology of judgment and decision making.</p><p>[Prickett2000] Prickett, T., Gada-Jain, N., &amp; Bernieri, F. J. (2000). The importance of first impressions in a job interview. In <em>annual meeting of the Midwestern Psychological Association</em>.</p><p>[Schmidt1998] Schmidt, F. L., &amp; Hunter, J. E. (1998). The validity and utility of selection methods in personnel psychology: Practical and theoretical implications of 85 years of research findings. In <em>Psychological bulletin</em>.</p><p>[Snyder1979] Snyder, M. &amp; Cantor, N. (1979), Testing hypotheses about other people: the use of historical knowledge. In <em>Journal of Experimental Social Psychology.</em></p><p>[Snyder1978] Snyder, M. &amp; Swann, W. (1978). Hypothesis-Testing Processes in Social Interaction. In <em>Journal of Personality and Social Psychology</em>.</p><p>[Tversky1983] Tversky, A., Kahneman, D. (1983). Extensional versus intuitive reasoning: The conjunction fallacy in probability judgment. In <em>Psychological Review.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f60527dd54c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Recruiter Fallacies: A Guide for Candidates to Avoid Misjudgment]]></title>
            <link>https://medium.com/@marian.jureczko/recruiter-fallacies-a-guide-for-candidates-to-avoid-misjudgment-e2bab21d72c3?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2bab21d72c3</guid>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Wed, 06 Mar 2024 20:26:06 GMT</pubDate>
            <atom:updated>2024-03-06T20:26:06.600Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*7NB5CYYZh4I6JJ5TTsJq2Q.jpeg" /></figure><p>Welcome to one of two versions of an exploration into the intricate web of cognitive biases commonly encountered throughout the recruitment journey. In this installment, we delve into the candidate’s perspective, while the counterpart dissects the same challenges from the recruiter’s vantage point.</p><p>During job interviews, I’ve encountered numerous surprising situations. These situations didn’t follow a common pattern and could involve different aspects of the recruitment process. Sometimes, it was unexpected candidate assessment, but it could also pertain to the behaviour of either the candidate or the recruiter. After spending several years as a recruiter, I began to notice recurring themes. However, I couldn’t fully understand these patterns until I read Daniel Kahneman’s “Thinking, Fast and Slow” [Kahneman2011]. In this book, the author explores various intriguing topics, including biases and fallacies that people often fall victim to. I was astonished to find how many of these biases and fallacies manifest during the recruitment process.</p><p>I invite you to explore a synthesis of Kahneman’s theories (his book served as an excellent starting point, but I also drew from additional sources) and my own 15 years of experience as a recruiter in the IT industry. My hope is that this synthesis will assist candidates in avoiding being misjudged due to unintentionally creating a wrong impression. Eager to enhance your recruitments? Let’s explore the realm of fallacies and biases because the best way to resist them is to be aware of their existence.</p><h3>What the recruiters believe they are observing during the interview</h3><p>To kick off our exploration, let’s begin by examining the biases that most directly influence the gap between what the recruiters think they perceive and what truly unfolds before them.</p><h4>How much time does a recruiter need to assess a candidate during interview?</h4><p>Does the interview need to take so much time? Can’t the recruiters make faster decisions? These questions have intrigued some scientists too, and they conducted experiments to uncover the truth [Prickett2000]. Their focus was on determining how long it takes to form an assessment of an interviewee that remains consistent throughout the interview. But before revealing the answer, I’ll let you take a moment to make your own guess. In the meantime, let’s dive into the experiment’s crucial details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*OQRetTutP2niohjxem4k9A.jpeg" /></figure><p>The scientists organized 59 employment interviews, each conducted by two trained interviewers. These sessions were planned for a mere twenty minutes, following predefined scenarios and consisting of questions such as:</p><ul><li>“Tell us something about your general career interests.”</li><li>“What would you call your potentials or assets?”</li><li>“What kind of people do you like being around?”</li></ul><p>It’s important to note that these interviews didn’t resemble the comprehensive technical interviews often seen in the IT field. They were more like screenings. However, they were conducted with scientific precision. Naïve observers were introduced to the recruitment processes and asked about the expected outcomes early on. And here’s the promised answer — it took these observers a mere 20 seconds to make assessments that would stand unwavering through the rest of the interview (technically speaking, was in line with recruiters assessment delivered after the whole interview). That’s right, just 20 seconds. To put it into perspective, 20 seconds is barely enough time for a polite handshake and a brief greeting. It leaves little room for gaining substantial insights through interview questions alone.</p><p>Of course, in the world of IT, interviews typically involve far more than just first impressions. Detailed programming languages and frameworks questions are the norm. Nonetheless, this study highlights the undeniable impact of first impressions. While it may not be the sole determining factor in interviews involving a comprehensive set of technical questions, it still play a significant role, especially when combined with the confirmation bias (discussed later).</p><p>I won’t be offering expert advice on how to make a great first impression, as there are countless tutorials available for that. However, it’s essential to remember that interviews can be stressful situations, and stress can affect our professionalism. While there’s no one-size-fits-all solution for handling stress, preparation can make a significant difference. I’m not just talking about knowing the answers to interview questions — although that certainly helps — but also being well-versed in technicalities. Knowing the interview procedure in advance or having some insight into the interviewers can alleviate stress. Imagine you’re in the middle of discussing database indexing, and suddenly, your throat goes dry. You begin to wonder if it’s okay to reach for the water bottle right in front of you. Knowing in advance that drinks are available for candidates would have made things much simpler.</p><h4>Recency bias</h4><p>We delved into how interviews kick off, but now it’s time to shift our focus to the other end of the spectrum — those critical final moments. This is where the recency bias can cast its deceptive spell. This cognitive fallacy is most commonly associated with duping investors into thinking a few recent data points signify a new trend, when, in reality, it’s just a fluke. The recency bias gives undue weight to the most recent events, often overlooking the historical context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*frTzT6A-Qq6tVeaEQ1N89w.jpeg" /></figure><p>Regrettably, this bias can rear its head during recruitment as well. I often have heard recruiters arguing that the candidate’s most brilliant ideas popped up right at the end of an interview and that an extra five minutes would be enough to unravel the task at hand. Alas, there were no extra minutes and the candidate didn’t finish the task (it could be argued that this is a recruiter’s mistake, but let’s set that aside as it’s unrelated to the cognitive biases we’re discussing here). The assumption that the task would have been cracked given more time is pure speculation — it didn’t actually happen during the interview. You might wonder whether any harm is done in such situations. Perhaps the candidate is a sharp individual who simply needed a little extra time to shine. However, remember that this bias cuts both ways. A glaring mistake made in the final moments of the session can outweigh several previously stellar answers. It’s a bias, plain and simple, leading to inaccurate assessments.</p><p>How to avoid it? There are no magic solutions. The best suggestion is to strive not to make mistakes as the interview winds down. However, such advice is of little value as in reality, no one consciously makes mistakes during an interview. While, it’s a predicament that remains largely beyond our control, you can take proactive steps to minimize its impact, such as avoiding controversial statements at the end of the session.</p><h4>Halo effect</h4><p>In the realm of tech interviews the first impression and the recency bias, often come together to form a more comprehensive concept: the halo effect. But before we dive into its definition, let’s embark on a thought experiment together.</p><p><a href="https://docs.google.com/forms/d/e/1FAIpQLSdpcvsaQINzj3GR1JQ9Syvi6eLFHs2Haz6weCv3FuN92KWVBQ/viewform">Here is a questionnaire</a>, and your task is to guess who John Ausonius is. The catch? No Googling allowed — it’s all about making an educated guess based on what you see. Curious how other answered? <a href="https://docs.google.com/forms/d/e/1FAIpQLSdpcvsaQINzj3GR1JQ9Syvi6eLFHs2Haz6weCv3FuN92KWVBQ/viewanalytics">You can find the answers here</a>, but I must admit, I’m in the dark about the results myself. This questionnaire could have been tampered with, or the experiment might not effectively illustrate the halo effect I’m about to discuss.</p><p>So, what’s this bias all about? At its core, it’s about extrapolating what we know about certain traits or attributes to other aspects we’re less familiar with. Imagine it as a “halo” of knowledge casting light onto the unknown.</p><p>In the context of the John Ausonius questionnaire, respondents were likely unaware of his identity, with only his appearance as a known factor. Unfortunately, relying on appearance is a shaky premise at best. Nevertheless, it can lead one to believe that since he is good-looking, he is supposed to hold a respected position — perhaps a movie star or a promising scientist. If you found yourself thinking along these lines, you fell victim to the halo effect.</p><p>Do you wondered if the halo effect, that tendency to make judgments based on appearance, was just a mere coincidence? Fortunately, an empirical study conducted by Landy and Signal in 1974 [Landy1974] delved into this very question. In this study, researchers crafted a collection of essays, purposefully mixing poorly written and well-crafted pieces. But that’s not all; they added a twist by attaching photographs of supposed authors to two-thirds of the essays. These photos featured individuals categorized as either good-looking and attractive or, conversely, as less attractive. Next, a group of sixty students was brought in to rate these essays on a scale from 1 to 9. What did the results reveal? Well, when it came to well-written essays attributed to attractive authors, they received an average rating of 6.7. On the flip side, essays attributed to less attractive authors garnered an average rating of 5.9. As for the poorly written essays, the contrast was even starker. Those linked to attractive authors received an average rating of 5.2, whereas essays by less attractive authors plummeted to a mere 2.7. This study serves as a powerful illustration of how the halo effect can profoundly influence our judgments, even in seemingly objective scenarios like evaluating written work.</p><p>Now, consider how this plays out in interviews. It’s all too common for interviewers to overvalue a candidate’s standout responses, extrapolating their strengths into areas where they may lack core competencies. This is particularly true when the interview superficially covers the core skills. The interviewer might assume that because the candidate excelled in one domain, they possess a breadth of knowledge and problem-solving abilities on others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*E7DCXpieCziWAov09VWYMg.jpeg" /></figure><p>Moreover, it’s worth noting that the halo effect isn’t the only player in town. There’s also a darker counterpart known as the “horn effect,” “devil effect,” or the “reverse halo effect.” This is where the interviewer allows one negative aspect or a less-than-stellar performance to cast a long shadow over the candidate’s overall assessment. Favourable conditions for the ‘horn effect’ occur when we resort to guessing answers we are unfamiliar with. In such cases, there is a risk of uttering nonsense, which may prompt the recruiter to use this answer as a basis for extrapolation during the assessment.</p><h4>Priming</h4><p>In our quest to understand the intricacies of cognitive biases, here’s another fascinating phenomenon to ponder. I’ve set up a little experiment for you to try, which delves into the world of priming. I’ve crafted two questionnaires: <a href="https://docs.google.com/forms/d/e/1FAIpQLSdtOz58yhj3hZFXX20xpPerJEuidiLw65l6G5vQyMhR__ASbg/viewform">the first</a> and <a href="https://docs.google.com/forms/d/e/1FAIpQLSfrUYXwNvOePWTVGH6uudaP4sOeehMyH0dsVOP2fcNIM0D4iA/viewform">the second</a>. Your task is to fill out just one of them, selecting at random. After completing the questionnaire, take a moment to check out the results for both versions. You can find <a href="https://docs.google.com/forms/d/e/1FAIpQLSdtOz58yhj3hZFXX20xpPerJEuidiLw65l6G5vQyMhR__ASbg/viewanalytics">the results for the first one here</a> and <a href="https://docs.google.com/forms/d/e/1FAIpQLSfrUYXwNvOePWTVGH6uudaP4sOeehMyH0dsVOP2fcNIM0D4iA/viewanalytics">the second one here</a>.</p><p>You might have noticed that both questionnaires present the same riddle, with the only distinction lying in the introduction. While both intros may appear somewhat quirky, the essential difference is that one is centered around food, while the other revolves around showers. The intriguing part is that the introduction primes your mind, potentially leading you to favor “soup” as the answer in the food-related version and “soap” in the shower-related one. These questionnaires were released into the wild, so I’m curious to see if they truly capture this psychological phenomenon.</p><p>But let’s dive deeper into the concept of priming. In essence, it revolves around the power of subtle suggestions. According to the definition, these suggestions can act as stimuli that trigger your thinking towards ideas and concepts associated with the given stimulus. An illustrative example of this effect comes from Bargh et al.’s empirical study [Bargh1996]. In this experiment, participants engaged in a lexical exercise involving words related to stereotypes of elderly people. Meanwhile, another group worked with random words. When the real experiment began, both groups were asked to change rooms. The time it took for participants to traverse to the new location was meticulously measured. The results were astonishing: those primed with “elderly” words walked notably slower.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*wkXrIoiZkm1hnzuMnKsqiA.jpeg" /></figure><p>Now, how does all this relate to interviews in the tech world? Well, here’s where it gets unique, as the candidate becomes the unwitting victim of this phenomenon. During an interview, candidates naturally hang on to every word uttered by the interviewer. In the world of IT, there often aren’t universally “right” or “wrong” answers; context plays a significant role. While interviewers possess an understanding of their company’s context, candidates may not. So, when an interviewer subtly suggests, for instance, that “shifting right” in testing is superior to “shifting left,” it can have a profound impact on how a candidate approaches a task related to test strategy. There’s nothing inherently wrong with this. Why should we not try to figure out what answers the recruiter expects? The issue may lie with the interviewer.</p><h3>How the recruiters interpret what is observed during the interview</h3><p>In the intricate world of tech interviews, even if the recruiter skillfully identifies and separates the facts gleaned during the interview from any embellishments, there’s still a formidable challenge ahead. There are lurking pitfalls of cognitive biases that lead people astray when attempting to craft a comprehensive conclusion.</p><h4>Conjunction fallacy</h4><p>Let’s delve into the world of cognitive biases once again, using an intriguing exercise suggested in a study by Tversky and Kahneman [Tversky1983]. I have<a href="https://docs.google.com/forms/d/e/1FAIpQLSfm3f6bbN0QAsRPQFsMPQl729rZAlMgCmphfBRJL96IA0mGUg/viewform?fbzx=31179410294709331"> a questionnaire </a>where your task is to rank seven different statements about Linda from most to least probable.</p><p>At first glance, some of these options might seem plausible, while others appear unexpected. However, the key to this exercise lies in identifying the two relevant choices: “Linda is a bank teller” and “Linda is a bank teller and is actively involved in the feminist movement.” When placed side by side, it becomes apparent that the latter is an elaboration on the former. Essentially, it suggests that among all the bank tellers globally, some are actively engaged in the feminist movement. Therefore, “Linda is a bank teller and is actively involved in the feminist movement” is a subset of “Linda is a bank teller.” From a mathematical perspective, the only correct answer to this exercise is to rank “Linda is a bank teller” higher in the list. This is a fundamental principle of probability — conjunctions of two events may not be more probable than those events in isolation. <a href="https://docs.google.com/spreadsheets/d/156aPg-Rt_lcgVEPyTHWr3CNbH34j3EGOvrKK1u-1WTw/edit?resourcekey#gid=338905984">On this chart, you can discover the number of individuals who resisted this fallacy and how many succumbed to it</a>.</p><p>However, our intuition often leads us astray. Tversky and Kahneman conducted the Linda experiment with statistics students (who should have been well-versed in probability), and a staggering 85% of participants made the mistake. There are various possible explanations for this phenomenon, one of which is highly relevant to our discussion on recruitment — scenario thinking. This concept was vividly illustrated by Dawes [Dawes1988]. In an experiment, people were asked if a tennis player who is an alcoholic could win a tournament in the next eight months. Most respondents were skeptical. However, when this question was accompanied by a hypothetical scenario where the player joined an Alcoholics Anonymous club, stopped drinking, trained rigorously while finding motivation in overcoming addiction (note that this scenario didn’t claim he had actually done these things), the player’s chances were rated much higher.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*k7ZJ4M-w7BYCbtDixenqRA.jpeg" /></figure><h4>What you see is all there is</h4><p>To illustrate the next cognitive bias, let’s engage in a thought experiment proposed by none other than Daniel Kahneman in his insightful work [Kahneman2011]. We’ll delve into the world of recruitment this time, so the implications are immediate and profound.</p><p>Imagine we have a candidate named Mindik. We’re tasked with making a judgment about her potential as a leader. The initial information we receive is that Mindik is intelligent and strong. What’s your immediate reaction? Are you already forming a conclusion? Hold that thought.</p><p>Now, consider the complete description of Mindik: she is not only intelligent and strong but also corrupt and cruel. How does your perspective change now? If you find yourself oscillating between acceptance and rejection, you’re not alone. It’s a common response, and it underscores how our intuitive judgments can be wrong.</p><p>However, the proper approach to recruitment isn’t to jump to conclusions based on limited information. Instead, we should start by clarifying what do we need to learn about a candidate to make an accurate assessment of their suitability for a leadership position? Statistically, it’s likely that you instantly categorized Mindik as either a “good” or “bad” candidate. This is not the correct approach, but it is precisely how our intuition operates.</p><p>The cognitive bias at play here is known as “What You See Is All There Is” (WYSIATI). It suggests that we tend to form judgments based on the information readily available to us, without delving into what we don’t know or considering the quality and quantity of the data. We often accept the available information, without scrutinizing potential gaps or assessing the credibility of the source. The key factor is the consistency of the narrative — if the story aligns, we’re inclined to draw a conclusion swiftly. “Mindik is intelligent and strong” makes a consistent story (although we know hardly a thing about her), so our mind is eager to jump to the conclusion right away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*z1BZY00CCu4JfPLKc_qDOQ.jpeg" /></figure><p>Kahneman shares a captivating example of this bias from his experience with the army, where he was responsible for evaluating officer candidates. During the evaluation, soldiers faced the challenging task of crossing over a wall without touching it, armed only with a bar as a tool. They had to organize themselves, with some emerging as leaders and others as team players. Kahneman and his colleagues observed the proceedings closely, but they fell into the WYSIATI trap.</p><p>They were so confident in the evaluations they provided, despite having ample time to witness how the soldiers truly performed. Astonishingly, there was virtually no correlation between the evaluations and actual performance. The bias was so entrenched that Kahneman initially believed their assessments were accurate and resisted changing the process. This brings us close to the next bias, but before we dive into that, let’s return briefly to the realm of technology.</p><p>In the field of IT recruitment, we shouldn’t expect a different outcome. From the candidate’s perspective, the situation may be very challenging. How to act when the recruiter creates an unfavourable narrative filling the gaps about a candidate using bad intuition? It should be a trigger to showcase experience in other areas, suggesting a broader skill set, so that recruiters leave the interview with the impression that the candidate brings more to the table than the few points used as evaluation criteria. That should help the recruiter to remember that they don’t know everything about the candidate, and that the simple picture suggested by intuition (or the WYSIATI trap) is incomplete.</p><h4>Confirmation bias</h4><p>Let’s talk about confirmation bias, a cognitive quirk that can significantly impact decision-making and assessment processes. To illustrate this phenomenon, we’ll explore a guessing game that not only challenges your intellect but also reveals the power of this cognitive bias.</p><p>In this game, one player devises a rule for generating sequences of three numbers and shares an example sequence that adheres to the rule. The challenge for the other player is to deduce the rule. The twist? The second player can propose new sequences of three numbers, and the first player responds with a simple “yes” or “no” to indicate whether the proposed sequence adheres to the rule. The goal is to pinpoint the rule with the fewest sequences, making it a captivating brain teaser. Intrigued? You can try it for yourself. Here is <a href="https://docs.google.com/forms/d/e/1FAIpQLSdZfvo_vwq1FP3oTUpTqUVFZcRdpmwMdGZmI5DuDBKq8FOB7g/viewform?fbzx=-4934869796456939894">the task</a>.</p><p>This exercise plays on the confirmation bias, one of the intriguing fallacies that affect our thinking. The optimal strategy here is to divide the set of potential answers in a way that maximizes the rejection of possibilities, leading to the quickest path to the solution. However, a more common approach is to craft a theory and then seek evidence that supports it. For instance, you might theorize that the rule pertains to providing three even numbers (the numbers from the task were 2, 4 and 6). We already know the conjunction fallacy, so for sure you do not think that the rule is “three <strong>subsequent</strong> even numbers”. To confirm this theory, you might test sequences like 10, 20, 30 and then 100, 1000, 10000, hoping for two “yes” responses to build your confidence. But here’s the twist: the correct rule is “any increasing sequence.” In this case, our sequences were quite similar, and we failed to explore the broader domain of possibilities.</p><p>I’m not privy to the theories participants formulated and, consequently, whether the suggested sequence aligned with the theory. Instead, I can provide you with <a href="https://docs.google.com/spreadsheets/d/1WixHTyTwo_l3-LfKD41szFTnKQ1hH1e9hwxyrpRlreE/edit?resourcekey#gid=1563869756">statistics regarding the proportion of sequences conforming to the actual rule</a>, namely, “any increasing sequence.”</p><p>The confirmation bias manifests in various forms, and in the context of our numerical exercise, it takes the shape of <strong>selective hypothesis testing</strong>. This bias leads us to test hypotheses by actively searching for confirming evidence, ultimately seeking out answers that align with our theories.</p><p>Now, let’s pivot to the world of tech recruitment, where the confirmation bias can lurk and influence decisions. An intriguing experiment conducted by Snyder and Swann [Snyder1978] sheds light on this phenomenon. Participants were asked to rate individuals on the introversion-extroversion scale after an interview. The catch? Some interviewees were introduced as introverts, while others were presented as extroverts. What happened next was eye-opening. Participants selected interview questions from a predefined list that strongly aligned with the presumed traits. If an interviewee was labeled an introvert, the majority of questions assumed introversion, such as, “What do you find unpleasant about noisy parties?” In contrast, for those labeled as extroverts, the questions mostly presumed extroversion, like, “What would you do to liven up a dull party?” This experiment was not about disproving hypotheses; it was all about seeking confirmation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*jCz2BPZsKqQEGEWJVK9OWQ.jpeg" /></figure><p>What participants engaged in during this experiment mirrors an imperfect recruiting strategy - if you search long enough, you’ll eventually find confirmation, even for a false hypothesis. As a candidate, there’s not much you can do to change this dynamic without potentially impacting your evaluation negatively. However, there might be an opportunity for dialogue where you can ask to explore why the certain aspect is important for the company’s interests, and together, with the recruiter, you may discover that it’s not as critical as the amount of questions suggested, allowing to move on to more valuable interview topics. It’s a potential win-win scenario.</p><p>Confirmation bias comes in various shades, another of its intriguing forms is known as <strong>selective recall</strong>. Let’s delve into a scientific experiment from the late ’70s that vividly illustrates this cognitive quirk and explore how it might influence tech recruitment.</p><p>In this experiment conducted by Snyder and Cantor in 1979 [Snyder1979], participants were presented with a profile of a fictitious woman. This profile was meticulously designed to feature a balanced mix of both introverted and extroverted behaviors. However, the real twist came when participants were later asked to recall specific examples of her introversion and extroversion. What made this study particularly fascinating was the context provided before the recall task. Some participants were informed that the assessment was for a librarian job, while others were told it was related to real estate sales. The result? The type of assessment significantly influenced what participants remembered. Those who believed the assessment was for a librarian position found it easier to recall introverted behaviors, while participants in the context of real estate sales more frequently recalled extroverted behaviors.</p><p>In the realm of confirmation bias, the story doesn’t end with merely seeking out information that confirms our preexisting beliefs. It extends to how we interpret the information we gather. A fascinating experiment at Stanford University in 1979, conducted by Lord and his colleagues [Lord1979], sheds light on this cognitive phenomenon.</p><p>The study involved participants who had clear and distinct views on the topic of capital punishment — with half of them in favor of it and the other half against it. These individuals were presented with deliberately fabricated studies that compared the effects of the death penalty on various aspects of society.</p><p>One of these studies supported the notion of severe punishment, while the other argued against it. After reading these studies, the participants were asked to share their opinions. The outcome of this experiment wasn’t surprising: participants perceived the studies that aligned with their preexisting beliefs as more convincing and better conducted. This peculiar effect, documented by the researchers, is known as <strong>selective interpretation</strong>. This intriguing study highlights how confirmation bias can not only guide our information-seeking behavior but also influence the way we interpret and evaluate the information we encounter. It’s a reminder of the complex interplay between our preconceived notions and the objective evaluation of evidence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*AZlCA5A3D6eHIILfeDBdnA.jpeg" /></figure><p>The last type of confirmation bias I’d like to discuss is the <strong>illusion of correlation. </strong>It is a particularly intriguing cognitive phenomenon worth exploring. This bias was vividly demonstrated in a fascinating experiment involving the Rorschach test, as outlined by Plous [Plous1993].</p><p>You might recognize the Rorschach test from its depiction in various movies. In this test, patients are presented with cards featuring black inkblots of various shapes and are tasked with describing the objects or shapes they perceive within these abstract images.</p><p>Now, in the experiment described by Plous, participants were provided with contrived Rorschach test results. They were then instructed to search for potential correlations between the patients’ sexual orientation and their responses to the test inkblots. The catch? The data was artfully crafted in a way that avoided any genuine positive correlations between these variables.</p><p>Despite this careful manipulation, the participants managed to uncover imagined relationships, such as the belief that “homosexual individuals often interpret the inkblots as buttocks,” even though these specific instances were statistically underrepresented. Astonishingly, the mere discovery of one or two instances of these perceived correlations led the participants to disregard the abundance of cases that failed to support such relationships. It’s quite evident that these imaginary correlations, fueled by the illusion of correlation, can have significant implications, particularly when it comes to fields like recruitment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*stKXhjSCFVDLSafSuiKHmw.jpeg" /></figure><p>Within the world of recruitment, the confirmation bias takes on various forms, and together, these biases pose a significant challenge for recruiters. This cognitive machinery has a unique ability to unconsciously weave an imaginary narrative around a candidate. To shed light on this intricate phenomenon, let’s visualize it on a chart.</p><p>Imagine we’re beginning with a set of recruitment observations: the candidate’s responses, behaviors exhibited during the interview, and so on. We can represent these data points as dots on a graph, where the X-axis denotes the different areas of focus during the recruitment process, and the Y-axis quantifies the candidate’s performance in those areas:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*PupdlF5b_lxKEkbi8OKwqw.png" /></figure><p>Now, it’s essential to recognize that there’s a considerable amount of empty space between these data points. In reality, it’s nearly impossible to gather exhaustive information about every facet of a candidate’s competencies within the confines of an interview. As a result, many facts about the individual remain shrouded in mystery. However, understanding more about the candidate offers the potential for a more accurate assessment.</p><p>This brings us to a natural tendency that many recruiters face: the inclination to construct hypotheses about the candidate, which essentially create a more holistic and comprehensive portrayal than the raw data points can provide. In simpler terms, it involves crafting a narrative about the candidate.</p><p>This pivotal moment can become a turning point, as the recruiter may inadvertently steer the interview in a direction that aligns with their preconceived story. This may involve asking questions that confirm this imagined narrative, making it easier to remember facts that fit the story while conveniently forgetting those that do not. In essence, the recruiters may begin to seek confirmation of their hypothesis. This is where the danger lies. Instead of an accurate assessment, the recruiter ends up with a fabricated story, one potentially influenced by the initial impression and further fueled by the confirmation bias. On our chart, we can depict this as a red line that effectively connects the dots, a line drawn through simple interpolation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*rFWX7NSgorLDSj62xAlr2A.png" /></figure><p>Conversely, it’s crucial to realize that this constructed narrative may not necessarily reflect the true essence of the candidate. The line, symbolizing the recruiter’s mental storyline, indeed passes through all the data points but is primarily composed of the spaces between those points. These gaps are filled with intuition or recruiter fallacies, and there’s no concrete evidence to substantiate the accuracy of this narrative. The actual candidate could be entirely different, for better or worse. It’s possible that the true narrative of the candidate is better represented by the blue line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*BzvgKEh0M2w70-6I6x6cKg.png" /></figure><p>The real challenge here is that there are no tangible facts or observations that decisively determine which of these two lines is correct — both appear to align with everything observed during the interview. Nonetheless, there’s a significant risk that the recruiters might end up assessing the story about the candidate that they’ve constructed (the red line) rather than evaluating the actual candidate.</p><h3>Consideration about recruitment result correctness</h3><p>With the recruitment process now concluded, and hopefully, with minimal influence from the fallacies discussed in previous sections, it’s time to turn our attention to the quality of this evaluation.</p><h4>Regression toward the mean</h4><p>Let me start the discussion about recruitment results with a phenomenon called “regression toward the mean”. Imagine you’re conducting a test for a group of students. They are entirely unprepared and are randomly guessing the answers. Each question provides three possible choices, so, on average, you’d expect the results to hover around 33%. However, some students, driven by luck, end up with higher scores, while others, less fortunate, score below this average.</p><p>Now, let’s focus our attention on the top-performing student and the one who scored the lowest. Both were unprepared and based their answers on random guesses. Now, what if we were to administer a different test with entirely new questions, again outside the realm of their knowledge? How would these two students perform this time?</p><p>Should we expect that the top scorer will remain the best, and the lowest scorer will remain the worst? Rather, the most likely outcome is that their scores will regress toward the average, around 33%. Why? Because in our thought experiment, the deviations from the average are entirely random. It’s statistically more probable that, in the second test, the top performer’s score will decrease, while the lowest scorer’s score will improve. It’s the concept of regression toward the mean in action.</p><p>Before delving into how this connects to the world of recruitment, let’s explore an example from Kahneman’s book [Kahneman2011], featuring insights from an army aircraft instructor. The instructor emphasizes the importance of not praising pilots after they flawlessly execute a challenging manoeuvre because, in the next attempt, they are likely to make mistakes. Conversely, the instructor advocates providing harsh feedback to pilots who make errors, as this tends to enhance their performance in subsequent attempts. If by any chance, you believe that the instructor could be right, I recommend reading the great book of Miłosz Brzeziński [Brzeziński2013]. What is important for us, is that the instructor described the regression toward the mean in action while believing in a fictional theory fuelled by the illusion of correlation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*-otW5Zz57SeZq0RYSfmQ2w.jpeg" /></figure><p>Now, let’s transition back to the realm of recruitment. Have you ever encountered a situation where a candidate aced the interviews but struggled in their actual job? Conversely, have you witnessed someone who barely scraped through the interviews go on to become a superstar in the role? These scenarios, while perhaps not in such extreme forms, are common, and they are manifestations of the regression toward the mean. This time it’s not a cognitive bias; it’s simple mathematics at play. You can’t expect to replicate the same outcome when drawing from the same random distribution on two separate occasions. The key takeaway here is that we should avoid overestimating the significance of recruitment outcomes. Still sceptical? Take a look at the statistics I’ll be presenting in the following section.</p><h4>Can we predict an employee’s performance on the basis of a job interview?</h4><p>In the realm of tech recruitment, there’s a fascinating study by two researchers who condensed 85 years of research into a comprehensive analysis [Schmidt1998]. They performed a meta-analysis encompassing 32,000 employees across 515 vastly diverse job roles. The core objective was to construct a prediction model based on scores derived from interviews conducted during the recruitment process. This model aimed to forecast the actual job performance of the candidates post-employment. In simpler terms, they delved into how well recruitment results correlate with the subsequent performance of employed candidates. Now, let’s delve into the results they uncovered:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df7e1abc235eca74938d0737091b8225/href">https://medium.com/media/df7e1abc235eca74938d0737091b8225/href</a></iframe><p>The figures you see in the table quantify the extent to which these recruitment methods predict job performance. Let’s take the first row as an example. The researchers devised a prediction model that relied on the outcomes of General Mental Ability tests as its input. The model was then trained to forecast the performance of employees in their roles after the interview. An accuracy score of 100% would indicate a perfect match between the model’s predictions and actual job performance. However, when it came to General Mental Ability tests, the model achieved a mere 26%. In other words, a significant portion of real job performance couldn’t be explained by the test results.</p><p>In the source [Schmidt1998], you’ll encounter different figures, as the authors opted to present validity coefficients instead of explained variances. They argue that the former holds superior practical economic value. Nevertheless, it’s relatively simple to derive one from the other since the explained variance is the square of the validity coefficient.</p><p>What’s crucial for us to glean from this data is that none of the interview types excel at effectively predicting a candidate’s future performance. The most promising approach, work samples, only accounts for 29% of job performance. It’s worth noting that this data isn’t the most recent, and we can assume that improvements have been made in interview design since then. In practice, modern IT recruitment frequently involves a variety of interview types. It’s plausible that reanalyzing the same criteria for contemporary IT recruitment might yield more encouraging results. However, the fundamental lesson remains the same: recruitment outcomes should not be overestimated. An interview is not synonymous with the actual job. Factors such as motivations, pressure, and attitudes differ between the two contexts, potentially leading candidates to behave differently on the job compared to during the interview process.</p><h4>Intuition versus statistics</h4><p>If you’ve been following this post from the beginning, you might be arriving at the conclusion that intuition isn’t the ideal tool for assessing candidates. However, if you still have some doubts, this section aims to dispel them.</p><p>Let’s kick things off with an example from Kahneman [Kahneman2011], which happens to revolve around recruitment, making it a perfect fit for our narrative. Kahneman credits Meehl and his enlightening book [Meehl1954] as the true inspiration behind this example.</p><p>The story unfolds with Kahneman being called upon by the army to assist in revamping their underperforming recruitment process. The original approach involved trained personnel conducting 20-minute interviews to evaluate candidates for high-ranking positions. The assessments were intuitive but frustratingly weakly correlated with the candidates’ later job performance.</p><p>Meehl, as stated in his book [Meehl1954], regarded intuition as a poor tool, often outperformed by a simple linear regression. Taking a page from Meehl’s playbook, Kahneman proposed a formalized assessment that focused on six different candidate characteristics, each scored on a 5-point grading scale. When the data was quantified in this manner, it made little difference whether the final evaluation was carried out by trained personnel or a linear regression model. The results were significantly improved compared to the previous approach.</p><p>Now, let’s move from the military to an even more vivid example, this time in the world of wine. Have you ever come across the Ashenfelter’s model? You might intuitively think that assessing wine quality requires an expert meticulously examining taste, colour, and a host of other enigmatic factors. Well, that’s one way to go about it. However, there’s another approach pioneered by Professor Orley Ashenfelter from Princeton University. He meticulously analyzed data regarding wine prices in the Bordeaux region from 1952 to 1980, creating a model that correlated wine prices with factors like date and weather conditions. To the shock of wine experts, Ashenfelter’s model, despite being so simple, consistently outperformed all other wine price estimates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*54wAqKybH2gp8_75EJ8T0w.jpeg" /></figure><p>The lesson here is clear: despite what our intuition might suggest, mathematical models, even relatively simple ones, often outshine intuition. However, there are exceptions to this rule. When dealing with rare events, models face significant challenges, precisely where our intuition tends to excel. Consider a task where we need to predict whether Mr X will head to the cinema in the evening. The model would likely excel at quantifying the cinema’s offerings and correlating them with Mr X’s preferences. However, what if we discover that Mr X has broken his leg? Such a rare event is unlikely to be accounted for in the model. Yet, it’s an intuitive leap for humans to conclude that Mr X will visit the hospital, not the cinema. Nonetheless, as a recruiter, keep in mind that your candidates rarely have broken legs, and the model often provides a more reliable assessment.</p><h3>Summary</h3><p>In our journey through the intricacies of cognitive biases in recruitment, we’ve explored various pitfalls that can influence the hiring process. I’ve strived to present these biases from the perspective of candidates, in terms of what they should or should not do to avoid triggering biases. Let’s take a moment to revisit the biases we’ve covered:</p><ul><li><strong>In the process of observation:</strong><br>- <strong>First Impression</strong>: Recruiters often form an unchanging opinion of a candidate within the first 20 seconds of an interview.<br>- <strong>Recency Bias</strong>: Recent events tend to carry more weight in our minds.<br>- <strong>Halo Effect</strong>: People tend to extrapolate positive ratings of one characteristic to other unknown characteristics of the same person.<br>- <strong>Priming</strong>: Exposure to one stimulus can influence a response to another unrelated stimulus.</li><li><strong>In the interpretation of observations:</strong><br>- <strong>Conjunction fallacy</strong> — people intuitively overvalue the probability of conjunction of events.<br>- <strong>What You See Is All There Is</strong>: The quality and quantity of data seem less important than the consistency of the story created from the data.<br>- <strong>Confirmation Bias</strong>: Tendency to prefer analyse that confirms initial thought. This includes selective hypothesis testing, selective recall, selective interpretation, and the illusion of correlation, resulting in the invention of imagined narratives that feel unquestionably true.</li><li><strong>Recruitment result correctness:</strong><br>- <strong>Regression Toward the Mean</strong>: Subjects who score extremely high or low in one attempt tend to move closer to the mean in subsequent attempts.<br>- <strong>Statistical Models vs. Intuition</strong>: Statistical models typically outperform human intuition.</li></ul><p>These biases are just the tip of the iceberg. Each of them is a deep and complex topic on its own. For those looking to delve further into the subject, I highly recommend Daniel Kahneman’s book [Kahneman2011]. Additionally, most of these concepts have comprehensive Wikipedia pages that offer rich and detailed content.</p><h3>References</h3><p>[Bargh1996] Bargh, J., Chen, M., Burrows. L. (1996). Automaticity of social behavior: direct effects of trait construct and stereotype-activation on action. In <em>Journal of Personality and Social Psychology.</em></p><p>[Bock2018] Bock, J. (2018) Work Rules!</p><p>[Brzeziński2013] Brzeziński, M. (2013). Głaskologia. Faktyczne reguły motywowania i rozumienia motywacji.</p><p>[Dawes1988] Dawes, R., &amp; Kagen, J. (1988). Rational choice in an uncertain world.</p><p>[Kahneman2011] Kahneman, D. (2011). Thinking, Fast and Slow.</p><p>[Landy1974] Landy, D., Signal, H. (1974). Beauty is talent: Task evaluation as a function of the performer’s physical attractiveness. In<em> Journal of Personality and Social Psychology</em>.</p><p>[Lord1979] Lord, C. G., Ross, L., &amp; Lepper, M. R. (1979). Biased assimilation and attitude polarization: The effects of prior theories on subsequently considered evidence. <em>In Journal of personality and social psychology.</em></p><p>[Meehl1954] Meehl, P. E. (1954). Clinical versus statistical prediction: A theoretical analysis and a review of the evidence.</p><p>[Plous1993] Plous, S. (1993). The psychology of judgment and decision making.</p><p>[Prickett2000] Prickett, T., Gada-Jain, N., &amp; Bernieri, F. J. (2000). The importance of first impressions in a job interview. In <em>annual meeting of the Midwestern Psychological Association</em>.</p><p>[Schmidt1998] Schmidt, F. L., &amp; Hunter, J. E. (1998). The validity and utility of selection methods in personnel psychology: Practical and theoretical implications of 85 years of research findings. In <em>Psychological bulletin</em>.</p><p>[Snyder1979] Snyder, M. &amp; Cantor, N. (1979), Testing hypotheses about other people: the use of historical knowledge. In <em>Journal of Experimental Social Psychology.</em></p><p>[Snyder1978] Snyder, M. &amp; Swann, W. (1978). Hypothesis-Testing Processes in Social Interaction. In <em>Journal of Personality and Social Psychology</em>.</p><p>[Tversky1983] Tversky, A., Kahneman, D. (1983). Extensional versus intuitive reasoning: The conjunction fallacy in probability judgment. In <em>Psychological Review.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2bab21d72c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Software Engineering at Google”: A Review with a Critical Eye]]></title>
            <link>https://medium.com/@marian.jureczko/software-engineering-at-google-a-review-with-a-critical-eye-d596064ae168?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/d596064ae168</guid>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Fri, 22 Sep 2023 17:08:24 GMT</pubDate>
            <atom:updated>2023-10-02T06:03:38.360Z</atom:updated>
            <content:encoded><![CDATA[<p>I am prepared to offer a critical assessment of the book, but I want to clarify that my intention is not to come across as an internet troll who merely criticizes widely recognized works without cause. To provide context and establish my qualifications in this field, allow me to introduce myself. I am a software developer with nearly two decades of experience in the industry and a contributor to open-source projects, such as <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a>. I hold a PhD in empirical software engineering, and my research papers have been cited more than a thousand times, as documented on Google Scholar. Furthermore, I am a co-author of an article recognized as the most acknowledged scientific work in Polish software engineering (Szydłowska and Swacha: “Polish Contribution to Software Engineering Research 1992–2021.”) While I acknowledge that Google likely employs individuals with more impressive achievements, and some of them may have contributed to the book, I hope that my credentials suffice to establish the validity of my critique.</p><p>Let’s embark on our exploration of the book by delving into some passages that touch upon topics of lesser significance. These passages are written in a manner that may lead the reader to question the value of the information they convey. The first example is extracted from the preface:</p><blockquote>Unlike those more established engineering professions, current software engineering theory or practice is not nearly as rigorous. Aeronautical engineers must follow rigid guidelines and practices, because errors in their calculations can cause real damage; programming, on the whole, has traditionally not followed such rigorous practices. But, as software becomes more integrated into our lives, we must adopt and rely on more rigorous engineering methods.</blockquote><p>At first glance, everything appears sound, but upon closer examination, it’s a strong claim based solely on opinion, without any supporting facts provided by the authors. The assertion that engineers “must” adhere to guidelines when errors can result in actual damage seems reasonable, but the statement that programmers didn’t have to (but will in the future) because it’s not the case for software raises questions. Is it indeed the case that software errors have not caused real damage? In reality, numerous instances exist where software errors have resulted in significant real-world harm. Hence, this statement appears to be an oversimplification. Is there something inherently wrong with oversimplifying? Well, perhaps not inherently, but it does have potential drawbacks. When a viewpoint is presented explicitly, it can deter readers from exploring alternative perspectives. Did you consider an alternative after reading this passage? Perhaps there is no alternative. However, I can propose one (though I can’t confirm its accuracy, it seems more plausible to me). The key distinction between software and more established engineering disciplines may be the cost of implementing change. In software, the cost of change is relatively affordable. If you work in IT, you’ve likely heard numerous times about the expense of modifying a software system. However, I can assure you, it pales in comparison to the cost of, for instance, adding an additional floor between the 5th and 6th floors of your office building. Nonetheless, there are exceptions. When your piece of software operates on thousands of servers, the concept of “cheap” no longer readily comes to mind. However, it’s important to recognize that not everyone operates at the scale of Google. In fact, I would venture to say that the vast majority of software developers do not.</p><p>The next passage is found in the first chapter and is entirely unrelated to the book’s main topic. While the central point of interest in this chapter is the Hyrum’s Law, I couldn’t help but take note of what was mentioned about entropy. It might be my own bias, but I found it impossible to disregard:</p><blockquote>Just because entropy never decreases doesn’t mean we shouldn’t try to be efficient. Just because Hyrum’s Law will apply when maintaining software doesn’t mean we can’t plan for it or try to better understand it.</blockquote><p>There are various forms of entropy, and I’d like to discuss the one related to information theory, which is the closest concept to software engineering (although it’s not necessarily what the authors had in mind). In information theory, entropy, denoted as H(X), is calculated using the formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/223/1*1oHaH4iths5wpG1i9_iGkg.gif" /></figure><p>Here<em>, p(x)</em> is the probability of event <em>x</em>. For instance, let’s consider an urn with one red ball and one green ball. The probability of drawing the red ball is the same as drawing the green one, which is 0.5 for each. So, the entropy is calculated as follows:</p><p>H(X) = - 0.5*(-1) - 0.5*(-1) = 1</p><p>Now, if I initially draw the red ball, and afterward, only the green ball remains in the urn, there’s only one possible event: I will draw the green ball with a probability of 1. Consequently, the entropy becomes:</p><p>H(X) = - 1*0 = 0</p><p>The interesting point here is that entropy, which is supposed to never decrease according to the authors, decreased from 1 to 0. It’s worth noting that the authors might have been referring to the second law of thermodynamics, but even so, certain conditions related to system isolation need to be met for this to hold true regarding entropy.</p><p>The next passage I’d like to highlight pertains to refactoring. Unlike entropy, which can have multiple meanings, I am aware of only one definition for refactoring. Here’s how it is defined on Wikipedia:</p><blockquote>In computer programming and software design, code refactoring is the process of restructuring existing computer code — changing the factoring — without changing its external behavior.</blockquote><p>And here is the passage from “Software Engineering at Google”:</p><blockquote>Those developers might need to refactor some code, perhaps as a response to changing requirements, but it is unlikely they are being asked to deal with broader changes to their environment.</blockquote><p>How is it possible to meet new requirements without altering external behavior? It seems the authors have assigned a different meaning to the term “refactor.” They are not alone in doing so, but it certainly makes for easier reading when authors adhere to widely accepted terminology.</p><p>Have I persuaded you of anything so far? Perhaps that the authors might not be absolute perfectionists, or perhaps that I’m not an exceptionally benevolent person but rather someone who enjoys scrutinizing others’ work for minor errors. I can’t argue against those possibilities, as what I’ve described up to this point are merely subtle indicators of the book’s true weaknesses.</p><p>The real weakness lies in the commonly used flow for presenting various concepts within the book. The authors almost never provide empirical evidence to support their claims, and they rarely offer contexts of applicability beyond “it works for Google.” Moreover, they infrequently describe the mechanics behind the phenomena they discuss. As a result, readers are often left to speculate why performing action A leads to outcome B, and they may have no clarity on whether the same results can be achieved in their own projects. To compound the issue, the authors exhibit “creativity” in their nomenclature. You might recall what I just mentioned about “refactoring”; that’s just one example. Allow me to present more of them to build a compelling case.</p><p>In the Chapter 4 we can find:</p><blockquote>…our engineering population is mostly male, mostly White or Asian, and certainly not representative of all the communities that use our products. The lack of representation of such users in our workforce means that we often do not have the requisite diversity to understand how the use of our products can affect underrepresented or vulnerable users. In 2015, software engineer Jacky Alciné pointed out that the image recognition algorithms in Google Photos were classifying his black friends as “gorillas.”</blockquote><p>This is one of the rare instances where the mechanics behind the discussed phenomenon are presented, but there seems to be a logical issue. Are they suggesting that because there were no black engineers, the software couldn’t correctly detect black friends? This interpretation is further reinforced in the subsequent discussion of the “gorilla case.” However, the authors also mentioned some flaws in the training dataset, which I personally would highlight as a potential cause:”</p><blockquote>The photo data fed into Google’s image recognition algorithm was clearly incomplete. In short, the data did not represent the population.</blockquote><p>Nonetheless, the imputed relation between staff composition and “gorilla issue” is essentially the rationale behind advocating for diversity, which is the central theme of this chapter. However, there isn’t strong evidence to establish a causal relationship. Nevertheless, according to the book, Google has clients worldwide and should therefore represent all minority groups among its engineers.</p><p>Not every organization operates on a global scale. In fact, the opposite is often true. When applying logical reasoning, if the premise of an implication isn’t met (i.e. “operating on a global scale”), the conclusion doesn’t necessarily hold true (“staff diversity”). Therefore, after reading this chapter, I’m left wondering whether I should prioritize diversity if my work doesn’t extend to a global level.</p><p>Moreover, is diversity truly justified? While I haven’t worked on Google’s scale, I have developed software for users from different countries without needing representatives from those regions on my development team. When creating medical software, my company didn’t need to employ physicians, and when working on insurance software, we didn’t require insurance agents to write code. Conversely, as a Pole, I’m fully aware that I’m not intimately familiar with every aspect and nuance of Polish culture. So, if Google were to employ me to achieve Poland coverage, I wouldn’t be able to guarantee a perfect understanding of every Polish minority.</p><p>After reading the Chapter, it remains unclear to me why hiring a minority representative is necessary to develop software for that minority. I still believe this approach is suboptimal, and instead, a more effective strategy would be to thoroughly study and understand the minority group. However, the authors seem unfazed by these weak pieces of evidence. Apparently, they employ a technique they call ‘parroting,’ simply advising what to do without delving into the why and how.</p><p>In Chapter 8, there’s a passage where I strongly feel the absence of supporting empirical evidence:</p><blockquote>That’s what we strive for with our source code. Consistency is what enables any engineer to jump into an unfamiliar part of the codebase and get to work fairly quickly.</blockquote><p>This is the wishful thinking I’ve heard numerous times from managers. They often believe that they can reorganize development teams as they see fit, without significantly impacting productivity, as long as the engineers are familiar with the employed code formatting guidelines, use the same programming language, or maintain some other magical form of technical consistency. You can find a detailed explanation of why this doesn’t work in Demarco’s book, “Peopleware.” Based on my own experiences, I can affirm that developers can dive into an unfamiliar codebase only when the project domain is trivial. When this is the case, technical inconsistencies are usually minor obstacles. They become a more noticeable issue only when they are extensive, such as transitioning between imperative and functional paradigms. However, this is based solely on my personal experience. Google might have different experiences, perhaps richer ones, but we lack the possibility to read about that. When presenting a controversial thesis, it’s beneficial to support it with robust arguments and evidence.</p><p>Let’s look now into Chapter 14:</p><blockquote>Some databases contain data prepopulated into tables and used as configuration for the environment… For an SUT to be perceived as realistic, it might require a realistic set of base data at startup, both in terms of quality and quantity. For example, large tests of a social network likely need a realistic social graph as the base state for tests: enough test users with realistic profiles as well as enough interconnections between those users must exist for the testing to be accepted.</blockquote><p>The authors seem to operate under the implicit assumption that the “shared persistent fixture” is the sole solution. While the last passage regarding social networks provides some context justifying this choice, it’s crucial to note that this is only one of several potential solutions. For a broader exploration of alternatives, you can consult Meszaros: “xUnit patterns” which offers a range of options.</p><p>However, it raises questions as to why other options aren’t considered. Is there something unique about the application context that makes these alternatives less suitable? Do other approaches perform poorly, perhaps only at the scale Google operates on? Why all those questions remain unanswered?</p><p>In the same chapter, there’s another noteworthy passage:</p><blockquote>Instead of defining explicit assertions, A/B testing involves running two copies of the SUT, sending the same data, and comparing the output. The intended behavior is not explicitly defined: a human must manually go through the differences to ensure any changes are intended.</blockquote><p>This deviates from what the rest of the world typically recognizes as A/B tests. The activity defined above isn’t very widespread, but it’s commonly referred to as shadow testing (for reference, see <a href="https://microsoft.github.io/code-with-engineering-playbook/automated-testing/shadow-testing/">Microsoft’s definition of shadow testing</a>). Adding to the confusion, actual A/B testing (as described in <a href="https://en.wikipedia.org/wiki/A/B_testing">Wikipedia</a>) is acknowledged in the book but given a different name:</p><blockquote>Experimentation. A new behavior is made available as an experiment to a subset of users without their knowing. Then, the experiment group is compared to the control group at an aggregate level in terms of some desired metric.</blockquote><p>Calling this activity “Experimentation” isn’t a poor choice, but I find no compelling reason to justify inventing a new name.</p><p>The most glaring example of an inadequately described application context can be found in Chapter 21:</p><blockquote>Don’t release things without a plan (and a mandate) to support it for the long term. Whether the whole of Google engineering has learned that or not remains to be seen.</blockquote><p>It appears that even Google may not adhere to this advice, but that doesn’t deter the authors from recommending it to everyone.</p><p>I haven’t covered everything in “Software Engineering at Google” that aligns with my critique, but at this juncture, I must acknowledge that the majority of the book is quite sound and doesn’t warrant such criticism. The content is often Google specific or just not controversial and, even when it lacks properly described context or confirmatory empirical evidence, it still holds value. The authors frequently tread the well-trodden path of software engineering, offering readers well-established practices. At times, it surprises me that they don’t explicitly link their solutions to the existing body of knowledge (for instance, not mentioning GQM when presenting GSM).</p><p>Numerously, the authors provide us with a comprehensively described context. For instance, when explaining the ‘gofmt’ tool, they not only outline the expected outcomes but also discuss why they believe these outcomes are beneficial. Readers can also discover insightful observations, such as the one concerning Google’s code review approvals policy. The ‘LGTM’ (Looks Good to Me) seal, meaning that the code can be merged once all comments are resolved and the reviewer need not revisit the code, is discussed. Furthermore, the comments can be addressed without altering the source code, simply by responding to them. According to the authors, this practice builds trust among engineers, a valuable asset in a software development organization. Therefore, the next time I argue for more flexible code review policies, I can refer to this book and say, “It works for Google.” Additionally, beyond the trust discussed by the authors, I attribute to the flexible approval policy a positive impact on code ownership in terms of feeling responsible for its quality.</p><p>“Software Engineering at Google” is not a poor book, and I can genuinely recommend reading it. Just remember the title; it’s about software engineering at Google, not software engineering in general. Therefore, if you are not a Google employee, this should not be the only book you read on this topic. However, this is a statement that applies to practically every book.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d596064ae168" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flaky tests — the handy simplification]]></title>
            <link>https://medium.com/@marian.jureczko/flaky-tests-the-handy-simplification-6e4296a0d71b?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e4296a0d71b</guid>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Wed, 10 May 2023 18:45:52 GMT</pubDate>
            <atom:updated>2025-05-02T08:48:02.616Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AOpXMpu2fKupZ6t1M-Ag4A.png" /></figure><h3>Flaky tests: the handy simplification</h3><p>When googling for “flaky tests”, the first response (at least for me) is the <a href="https://docs.gitlab.com/ee/development/testing_guide/flaky_tests.html">GitLab testing guide</a> which defines them as:</p><blockquote>It’s a test that sometimes fails, but if you retry it enough times, it passes, eventually.</blockquote><p>A suspicious type, but why should we care? The reasons are clear and easy to find. In fact, there are many texts that explain the challenge. Let me use as an example <a href="https://engineering.atspotify.com/2019/11/test-flakiness-methods-for-identifying-and-dealing-with-flaky-tests/">a story from Spotify, as described by Jason Palmer</a>. He described several tools that helped to visualize the flakiness but also led to a noticeable reduction of this phenomenon. Additionally, Palmer provided us with a more descriptive definition of flaky tests, which we’ll explore further:</p><blockquote>A flaky test is a test that both passes and fails periodically without any code changes. Flaky tests are definitely annoying but they can also be quite costly since they often require engineers to retrigger entire builds on CI and often waste a lot of time waiting for new builds to complete successfully.</blockquote><p>It tells us much more about the flaky tests, but I prefer the first definition. The second one, which ties the concept of flaky tests to the challenge they pose, can muddy the waters a bit. For this post, it’s best to keep the two separate. But what exactly is the challenge of dealing with flaky tests? Well, at first glance, it may seem like the only cost is the time and resources required to run and re-run tests that don’t always produce reliable results. However, as we delve deeper into the issue, we’ll see that the true cost of flaky tests is a lack of confidence in our test suite.</p><p>Have you ever encountered flaky tests in your software development process? If so, you’re not alone. Flaky tests can be a headache for developers, and it’s important to understand their mechanics and consequences. Here’s how it typically plays out: we have a test that verifies a feature’s correctness and prevents regression. After a number of runs, the test fails. We investigate the issue but find nothing wrong in the production code. We re-execute the test and it passes, so we assume it was a false alarm and go back to our other tasks. The next time the test fails, we don’t bother investigating because we assume it’s just another false alarm. We may even decide to eliminate the re-execution step since it doesn’t give us informative results. But what if the test fails again due to a defect in the production code? We’re so used to ignoring the failures that we might miss the issue entirely. And if we have several flaky tests, we might lose confidence in the entire test suite. In general, it’s recommended to fix a flaky test as soon as it fails the first time. If fixing it is not feasible, it’s better to remove it from the test suite. This is because relying on a flaky test can erode confidence in the testing process and lead to ignoring other alarm signals. However, it’s worth exploring other solutions to address flaky tests as there is more to the story.</p><p>The description is a consistent story that illustrates causation, shows a problem and suggests and remedy. Moreover, it’s a very handy story as it often accurately describes reality and provides us with valuable guidance. Nonetheless, it is also a manifestation of the <a href="https://en.wikipedia.org/wiki/Illusion_of_validity">“what you see is all there is” bias</a>. Having a consistent story pushes away alternative reasoning paths and explanations. But let’s try to escape the bias and take a more holistic view.</p><h3>Software testing as a binary classification</h3><p>When it comes to software testing, it can be helpful to think of it in mathematical or statistical terms. In particular, we can represent it as a <a href="https://en.wikipedia.org/wiki/Binary_classification">binary classification</a> problem. Each test case is designed to exercise a specific feature, with the goal of classifying it as either correct or faulty. However, because testing is not a perfect process, there are times when the classification may be incorrect. As a result, four possible outcomes can occur.</p><ol><li>When the code under test is <strong>correct</strong>,</li></ol><p>1.1 the test may be green and we have a true negative (TN),</p><p>1.2 however, when the test is red we have a false positive (FP).</p><p>2. On the other hand, when the code under test is <strong>not</strong> <strong>correct</strong>,</p><p>2.1 and the test is green it is a false negative (FN),</p><p>2.2 and when the test is red there is a true positive (TP).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yJ77Taw3suvNEj-owyzc-w.png" /></figure><p>Using the terminology we just introduced, a flaky test can be seen as jumping over the fence. However, it’s important to remember that there are actually two fences at play. When the code is correct, we are trying to distinguish between false positives (FP) and true negatives (TN). On the other hand, when the code is incorrect, we are trying to distinguish between true positives (TP) and false negatives (FN).</p><h4>The correct production code scenario</h4><p>In the ideal scenario, our production code should always be correct, and our tests should reflect this by always being green. However, sometimes a test may fail even when the code is correct, leading to confusion and frustration. Our first instinct may be to simply re-run the test and hope that it was a false alarm. We may also try to fix the test by removing any sources of nondeterminism. However, if we are unable to resolve the issue, we risk falling into a cycle of repeated failures that can undermine our confidence in the entire test suite. This is why the common advice is to either fix the test or remove it, in order to prevent the erosion of trust in our testing process.</p><h4>The incorrect code scenario</h4><p>In such a scenario, a test in green is a liar, and a test in red is what we prefer to have. However, the incorrect code scenario is vastly different from the correct code scenario. It may be difficult to detect the defect, and even harder to find a test that can control the situation.</p><p>Will that erode the confidence in the entire test suite? The test never says that something is wrong when everything is OK. When the test fails there always is an issue. So I so no reason to decrease the confidence, for sure it is not the eroding confidence scenario described earlier. If you had a choice: a test that always passes or a test that only sometimes detects the issue, which one would you prefer? When the test always passes, it builds false confidence. I prefer the flaky one as it gives a chance to detect the issue. It is usually challenging to correct the production code when tests results are nondeterministic. But it is better to have something than nothing.</p><h4>Example</h4><p>The distinction between the two just described scenarios is crucial for this post, so let me illustrate it with an example. Let’s assume that we need to test “A user changes email address” use case and we decided to use a random value as the new email address.</p><p><strong>The correct code scenario. </strong>The production code does verification and correctly sets the new email whenever a change is requested. However, the test sometimes generates an invalid email address, causing the test to fail. For example, the test may generate an email address with an extra “@” symbol, such as “fdo@<a href="/service/mailto:fslp@ouas.xy">fslp@ouas.xy</a>”. Since this email address is invalid according to the requirements, the production code rejects it, resulting in a failing test.</p><p>However, re-executing the test usually results in a better email address that meets the requirements, and the test passes. But relying on re-execution to fix a test is not a sustainable solution and can lead to ignoring test results altogether. In other words, this scenario can erode our confidence in the test suite, which is something we want to avoid.</p><p><strong>The incorrect code scenario. </strong>The production code in this scenario has a flaw in its email verification logic, which causes valid emails to be incorrectly identified as code injection and rejected. We do not have a dedicated test case for such emails as they were not pointed out by the requirements and the implementation does not make it obvious that there exists such an <a href="https://en.wikipedia.org/wiki/Equivalence_partitioning">equivalence class</a>. However, a random data generator is generating emails with one in a hundred meeting the (faulty) criteria for rejection. As a result, the test is usually green, but fails in 1% of cases, giving an indication of a real issue. It’s not possible to fix the test in this case as it’s functioning correctly. Ignoring the failing test can damage the confidence in the test suite, but it’s important to fix the issue in the production code to ensure system quality. There are two options — ignore the failing test and have a production issue, or fix the defect and have a green test suite once again.</p><h4>Summary</h4><p>A diagram is worth a thousand words, so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wjzuigmTxniPjynOV_dd4g.png" /><figcaption>The lifecycle of a flaky test</figcaption></figure><h3>Randomness in test data</h3><p>Dealing with flaky tests can be especially challenging when it comes to tests that use random data. However, it’s important to note that flaky tests can also have a positive side. In a mature testing process, a test that sometimes fails can actually be helpful. It may indicate an issue in the production code that would otherwise go unnoticed until it causes problems in production.</p><p>It’s important to investigate any failing test, regardless of whether the issue is in the test itself or in the production code. If the issue is in the test, it’s just an additional cost that can be fixed or removed to maintain confidence in the test suite. But if the issue is in the production code, it’s an opportunity to catch and fix something before it becomes a bigger problem. By using random data, we can uncover issues that might not be immediately obvious and prevent them from causing headaches down the line.</p><p>I personally prefer using random data in my tests. I often use a tool called <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> to generate test data, and I try to make everything as random as possible. For example, if I’m testing a User class and the focus is on the email field, I initialize all the other fields with random values. Sometimes, even the email field can be randomized, i.e. if I’m testing that the email is set to the same value as the input. In these cases, I generate random input and reuse it in the assertion. While randomness can be helpful, there are also costs associated with it.</p><h4>A failing test with random data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AOpXMpu2fKupZ6t1M-Ag4A.png" /><figcaption>Sometimes fails, but usually passes</figcaption></figure><p>Tests that rely on random data can be challenging to debug when they start failing. Flaky tests can be caused by issues in the production code or the test itself, and identifying the root cause is often difficult. While random test data increases the likelihood of identifying production issues, it doesn’t necessarily make the debugging process easier. To mitigate this, it’s important to have control over the randomness and employ strategies for quickly identifying and resolving issues in the test. Here are some principles to consider.</p><h4>Make the randomness visible</h4><p>The first thing we need to determine when a test fails is the reason for its failure. We know that an assertion was not met, but what exactly caused the problem during this test execution? It is possible that the failure is related to the use of random data. Perhaps something that was not intended to affect the production code has unexpectedly become relevant.</p><p>For example, if we are testing the update of a user’s email address and the production code unexpectedly validates the user’s name during the update, it’s possible that the random user name we used for the test is invalid, causing the test to fail. Whether this is a test issue or a production code issue depends on the requirements. However, consider the perspective of a user with an invalid name who cannot update their email address. Is this acceptable? If the user was able to create an account with an invalid name (perhaps the validation rules were different at the time), should they now be blocked from other system features, such as email updates, that are not related to the invalid name?</p><p>Let me, without deciding if the example is about a bug, jump into the principle. The test failed with some fancy exception, and when reexecuted (or debugged) passes. It’s not a comfortable situation for the tester. What would make our life easier? If we only know what was the unacceptable name. So the principle is to print the random data in our tests.</p><p>However, printing all the test data can be difficult in complex enterprise systems as it may result in a large amount of irrelevant noise. Therefore, it is crucial to intelligently select the test data that could have a higher relevance to the production code than expected. For instance, in the case of testing email updates, the presence of validations on other User fields or any logic that links email with other fields could indicate that those fields are worth printing. There is no magic solution to this challenge, and it relies heavily on our intuition and experience.</p><p>There is no one solution that fits all, but it’s important to note that we primarily need to focus on the test data when a test fails. Printing out the random data is not necessary when the test passes. Fortunately, test framework creators have accounted for this. For instance, in <a href="https://assertj.github.io/doc/">AssertJ</a>, you can use the as() method:</p><pre>assertThat(actual)<br>  .as(&quot;here comes the stuff that should be printed out&quot;)<br>  .isEqualTo(expected);</pre><p>And in JUnit there is an overloaded version of assertEquals():</p><pre>assertEquals(&quot;here comes the stuff that should be printed out&quot;, expected, actual);</pre><p>Do not hesitate to use them. They may be very beneficial.</p><p>Note, that it is the “Assertion Message” pattern as described by G. Meszaros in “xUnit Test Patterns”.</p><h4>Make it possible to reproduce the test failures</h4><p>When a test using random data fails, what should you do? Firstly, it’s important to check the logs to see if the failure can be quickly identified. However, this isn’t always possible, and we may need to reexecute the test while adding additional logs or breakpoints for debugging purposes. To make the reexecution useful, we need to obtain the same test result which can be challenging for random data. The randomness has a seed which means that for a given fixed seed, we always draw from the same sequence of random data. So, the data is random but also repeatable. This makes every next test execution work on precisely the same random data.</p><p>In some cases (though not so rare if you use randomness heavily), using a fixed seed may not be enough to reproduce a failing test. For example, the test may only fail when executed as part of a large test suite, after hundreds of other tests have consumed the sequence of random data. Or there may be a random factor that cannot be controlled by the seed, such as a race condition between threads. In such cases, being able to repeat the test multiple times can be extremely useful. In IntelliJ, you can configure the test “Run/Debug Configuration” by editing the “Tests” settings and adjusting the “Repeat” option under “Modify Options”. This will allow you to run the test multiple times, and increase your chances of reproducing the failure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6B-Ecf0pdUwtzW-G0KzGaQ.png" /></figure><p>Alternatively, use your test engine features, e.g. JUnit comes with an</p><pre>@RepeatedTest</pre><p>annotation.</p><h4>Keep the tests simple</h4><p>Random data provides a unique opportunity for having straightforward test data preparation. If we are testing the email update feature again and, for the sake of this test, creating an instance of the User class using its constructor, it will look as follows:</p><pre>User givenUser = new User(<br>  &quot;John&quot;,<br>  &quot;Doe&quot;,<br>  &quot;john.doe@old.email.com&quot;,<br>  42,<br>...<br>);</pre><p>With the <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a>, it can be simplified to:</p><pre>User givenUser = some(User.class);<br>givenUser.setEmail(&quot;john.doe@old.email.com&quot;);</pre><p>There is not only less code, but we also make a significant step towards making the test intention clear. The test is about updating email, and the only relevant field in the User class is the email. Thus, when instead of setting all User fields with some irrelevant values, we rely on randomness, and the only field we touch is the email. This way, it’s easier to reason about which fields matter for the test and which don’t. As a matter of fact, the <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> sets the user name when creating an instance of User, but it happens behind the scene without boilerplate code in the test. To learn more about this idea, check out these resources: <a href="https://www.linkedin.com/pulse/start-new-year-test-arranger-marian-jureczko/">Start the new year with test-arranger</a> or <a href="https://medium.com/@marian.jureczko/on-testing-in-ddd-f250482b5717">On testing in DDD</a>, or watch my talk: <a href="https://www.youtube.com/watch?v=rAi2t05Om3Y">Test Arranger and the Second Law of Thermodynamics</a> presented at <a href="https://2022.testwarez.pl/">TestWarez</a> 2022.</p><h4>Use randomness only where it gives benefits</h4><p>I have presented several reasons to use random data in tests, but it’s definitely <a href="https://en.wikipedia.org/wiki/No_Silver_Bullet">not a silver bullet</a>. As everything, it comes with a cost. Therefore, it should provide some benefits like simplification, as mentioned earlier, to justify its use.</p><p>Let me introduce a concept with a strong analogy in production code, that is <strong>test case invariant</strong>. Everything in a particular test case can be divided into two categories:</p><ul><li>test case invariant —the elements that must remain unchanged in order for the test to work properly. For example, if we are testing the update email feature and expect an error message when updating to an email address that contains a forbidden character, the email address with the forbidden character is the invariant. Using a different email address would exercise a different code path, which could result in a different test outcome or lower test coverage. If we use an email address with valid characters but too long, the production code may still return an error, but the test would not be testing what it is supposed to.</li><li>the rest of the test case — that includes accidental complexity, things we added to our test but aren’t necessary or things that are present only because it’s how the system is designed, even though the specific test case doesn’t care about them. For example, in a test case about updating an email address, it could be the user’s name. We may need to provide a name when creating an instance of the User class, but the test case will still work the same way with names like “John”, “Mary” or even random strings like “dioh497h”. These details are not test case invariants because they don’t affect the outcome of the test.</li></ul><p>The general rule of thumb is to not use random data for the test case invariant. Instead, we should use it extensively for the rest of the test case. By doing so, we can reduce the information noise caused by accidental complexity, code that we inserted because we didn’t know that we don’t have to, or something that is there only because that’s how the system is designed. We can replace this code with hidden randomness, which will not affect the test stability since the invariant remains untouched. For example, in a test case about updating the email, the email address with forbidden characters is our invariant, and we should not use randomness to generate it. However, we can use randomness to generate the user’s name, as it is not relevant to the test case and does not affect its outcome.</p><p>There are similarities between the <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> approach and <a href="https://www.educative.io/answers/what-is-property-based-testing">property driven testing</a>, but the latter goes further with the use of randomness. Property-driven testing suggests using randomness for the invariant as well and ensures overall test stability with corresponding assertions defined as predicates. Despite the interesting benefits of property-driven testing, it has not been widely adopted. Without exploring the possible reasons, it is worth noting that property-driven testing involves more randomness than what I am recommending in this post. However, many building blocks and concepts are shared between the two approaches. Therefore, the <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a> approach to testing can be used as a preliminary step towards property-driven testing, allowing individuals to get familiar with some of the ideas in a safer and more predictable environment.</p><h3>Recommendation</h3><p>A flaky test is not desirable, but preventing them may not always be the best approach. Instead, we may get better results by addressing them when they occur. A test may become flaky due to either a non-deterministic issue in the test itself or a random and hard-to-catch defect in the production code. While the former is undesirable, the latter presents an opportunity to uncover an otherwise hidden defect. Simply preventing flakiness eliminates both scenarios. Therefore, introducing controlled randomness into our tests and carefully investigating the cause of failures can be a more effective strategy. When introducing randomness, it is important to consider the following principles:</p><ul><li>make the randomness visible,</li><li>ensure that it is possible to reproduce test failures,</li><li>keep the tests simple</li><li>use randomness only where it provides benefits.</li></ul><p>Don’t shy away from using randomness in your tests. Embrace it and use it to your advantage.</p><h3>Resources for learning more about using random test data</h3><ul><li>A tool for generating random test data: <a href="https://github.com/ocadotechnology/test-arranger">test-arranger</a></li><li>A blog post introducing the basics of using random data in tests: <a href="https://www.linkedin.com/pulse/start-new-year-test-arranger-marian-jureczko/">Start the new year with test-arranger</a></li><li>A more detailed blog post about using random data in tests: <a href="https://medium.com/@marian.jureczko/on-testing-in-ddd-f250482b5717">On testing in DDD</a></li><li>A conference talk about randomness in tests: <a href="https://www.youtube.com/watch?v=rAi2t05Om3Y">Test Arranger and the Second Law of Thermodynamics</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e4296a0d71b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test-arranger — The Asked Questions]]></title>
            <link>https://medium.com/@marian.jureczko/test-arranger-the-asked-questions-a4ec99a0f742?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4ec99a0f742</guid>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 12:14:55 GMT</pubDate>
            <atom:updated>2023-01-02T12:14:55.276Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*kLAjZZG386D6AkXXApHKzw.png" /></figure><h3>Test-arranger — The Asked Questions</h3><p>Originally published at <a href="https://sjsi.org/test-arranger-the-asked-questions/">https://sjsi.org/test-arranger-the-asked-questions/</a></p><p>Test-arranger is a tool for arranging data in tests. It was designed on top of an approach driven by randomness and several test design patterns described in xUnit Test Patterns. There is a brief introduction to the solution on the project’s GitHub page (https://github.com/ocadotechnology/test-arranger ) and a more comprehensive one in the “On testing in DDD” post (<a href="https://medium.com/@marian.jureczko/on-testing-in-ddd-f250482b5717">https://medium.com/@marian.jureczko/on-testing-in-ddd-f250482b5717</a>). In short, the test-arranger instantiates classes initialized with random data. The solution has proven successful in a couple of projects that I run at Ocado Technology so I have decided to share my experience with a wider audience. So far, my talks about the solution (<a href="https://www.youtube.com/watch?v=rAi2t05Om3Y#t=43s">https://www.youtube.com/watch?v=rAi2t05Om3Y#t=43s</a>, <a href="https://www.youtube.com/watch?v=ulW0aRmDIuE">https://www.youtube.com/watch?v=ulW0aRmDIuE</a>) generated positive interest and were followed by engaging questions to which answers may be very informative for everyone considering test-arranger adoption.</p><p><strong>What should I do when I need instances with a different set of parameters for different test cases?</strong><br>The custom arranger comes with an instance method that should be considered the default recipe for a certain class. It will be used each time while the test-arranger is creating an instance of the class. However, the custom arranger is also the place where all the factory methods for the certain class should be located. Let’s assume that a particular test case<br>needs an instance carrying values that the custom arranger instance method is not able to deliver. To address this challenge, we should ask the test-arranger to create the random instance for use and then adjust the instance to the test cases needs (we can use whatever the instantiated class offers: setters, toBuilder, Kotlin’s copy method… and if the class offers<br>nothing we can rely on test-arranger some method with the overrides parameter (<a href="https://www.javadoc.io/doc/com.ocadotechnology.gembus/test-arranger/latest/com/ocadotechnology/gembus/test/Arranger.html">https://www.javadoc.io/doc/com.ocadotechnology.gembus/test-arranger/latest/com/ocadotechnology/gembus/test/Arranger.html</a>). If there is even a slight possibility that the adjustment can be reused in a different test case, we should pack it into a factory method and locate in the arranger class to make it easy to find (and reuse) in the future.</p><p><strong>Is there a way to repeat a test with the same values?</strong><br>Test-arranger has a fixed randomization seed. So, when launching a single test, again and again, it always gets precisely the same values. Nonetheless, in the scale of a whole software system, many moving parts can affect the randomization, like tests execution order or the details of classpath processing. Therefore, when executing an entire test suite, the<br>tests may receive different values at different runs.</p><p><strong>Is it possible to generate a set of values and execute a test for each of them?</strong><br>Test-arranger is not aware of the context it is executed, and consequently, it cannot control the test execution engine. So it is not possible out of the box. Nonetheless, you can create a parameterized test (e.g. <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests)">https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests)</a> and in the values source call test-arranger to generate a set of random data.</p><p><strong>Can I use test-arranger for property based testing?</strong><br>Test-arranger is not designed for property based testing. Property testing is one of the three solutions to the problem of lack of deterministic test oracle<br>(<a href="https://en.wikipedia.org/wiki/Test_oracle">https://en.wikipedia.org/wiki/Test_oracle</a>). They are used to generate a cohort of different random values to a variable used as the object of a test and to check that the system is responding correctly for each of the values. While test-arranger is focused on the data that is not crucial for the test, it must nonetheless, be there to make it possible to execute the test. So, those are complementary approaches that are focused on optimizing two separate parts of the test.</p><p><strong>Does test-arranger support javax validation annotations?</strong><br>Test-arranger uses EasyRandom (<a href="https://github.com/j-easy/easy-random">https://github.com/j-easy/easy-random</a>) under the hood which has the Bean Validation support (<a href="https://github.com/j-easy/easy-random/wiki/bean-validation-support">https://github.com/j-easy/easy-random/wiki/bean-validation-support</a>).</p><p><strong>Maybe the noise that test-arranger eliminates from tests is a consequence of a badly designed domain and instead of optimizing the process of test data creation, we should refactor the domain.</strong><br>Indeed test-arranger eliminates a noise that comes from low cohesion between the test cases and the system’s domain under test. It lets us eliminate the code that is required by the domain but has nothing to do with the test objective. So, if our domain is perfectly adjusted to the test cases (a perfect cohesion — test uses all fields of the entity that is used in<br>the test) there would be no place for random data and, as a consequence, test-arranger. Unfortunately, that is a theoretical possibility as the domain primarily must be adjusted to business use cases. There are always test cases that use only a part of the entities. While the remaining parts, which are not needed for the test cases, need to be somehow arranged to satisfy the domain invariants and that is the place where the test-arranger comes to the rescue.</p><p><strong>Does test-arranger lead to blinking tests?</strong><br>There is no direct link between test-arranger and blinking tests. Nonetheless, test-arranger uses random data and randomness has a lot to do with blinking tests. But when considering random data as test input, we should be aware that there are two kinds of blinking tests: false positives and false negatives and there is a huge difference between them. The false<br>positives are tests that sometimes fail despite the production code’s correctness. Such tests do much harm as they undermine our confidence in our test suite. The false negatives are tests that only sometimes (usually under conditions we are not aware of) are able to detect a defect. They do nothing severe with our confidence as the failing test still means a defect.<br>There are only challenges in reproducing the issue and identifying the defect preconditions. Random test data may result in both scenarios. However, based on my own experience, I can say that both are seldom, and the second one is more frequent than the first one. To sum up, introducing the test-arranger creates an additional opportunity (but not a big one) for detecting defects that otherwise would be overlooked.</p><p><strong>Does the test-arranger work only in Kotlin?</strong><br>I believe that Kotlin is a great language for writing tests and test-arranger has a Kotlin API, but it also has a JAVA API and it is usable in most of the JVM languages.</p><p><strong>When is the test-arranger approach not the way to go?</strong><br>It is hard to point to a category of projects where the test-arranger should not be used — nonetheless, the more complex data to arrange the more significant benefit it gives. Hence, if a system is not focused on data, but rather on algorithms (e.g. a parser), the benefits of using test-arranger will be small.</p><p><strong>I already use Test Data Builders. Will be using test-arranger an improvement for me?</strong><br>Test Data Builder is a concept that is with us for quite a long time, e.g.<br><a href="http://www.natpryce.com/articles/000714.html">http://www.natpryce.com/articles/000714.html</a>, and it is a great improvement over ad hoc test data creation. Let’s consider two possibilities to compare the Builder with the test-arranger approach.</p><ol><li>The builder is part of the production code.<br>It is convenient to have the builders (or at least a copy constructor) in the production code regardless of the test-arranger presence. Anyway, the given section of our tests may differ when there are domain invariants to satisfy. A builder should check the invariants in the build() method and throw an exception when they are not satisfied. As a consequence, if our Product class requires a Category, then in every test requiring the Product there will be a .withCategory(…) line (it can be<br>wrapped in a factory method to avoid code duplication, but there will be still the additional complexity in our code ). With the test-arranger we can satisfy this requirement once in the ProductArranger and have it automatically applied to every Product instantiation. Additionally, it will introduce a separation between test cases and domain invariant support. There is also a difference in what happens under the hood. The builder does not initialize unmentioned fields (unless you implement an initialization). While having all fields initialized creates an additional opportunity for detecting unexpected defects.</li><li>The builder needs to be delivered in the testing code.<br>When implementing the builder in your testing code, you need to call a constructor or a factory method from the production code which creates coupling. Consequently, with further development, the builder needs to reflect changes from the production code. That is a maintenance cost you can avoid with the test-arranger.</li></ol><p>Test Data Builders are really helpful, and replacing them with the test-arranger will not bring such big benefits as restructuring unorganized tests. Nonetheless, there are the listed above benefits and it makes sense to at least give the test-arranger a try. You may replace a single builder with an arranger to find out if the reduction in test maintenance justifies the effort needed for replacement.</p><p><strong>How the random data generator handles complex, nested data structures?</strong><br>Test-arranger uses EasyRandom (<a href="https://github.com/j-easy/easy-random">https://github.com/j-easy/easy-random</a>) under the hood. The EasyRandom is configured with collectionSizeRange equal (1,4) and randomizationDepth equal 4. It controls how far the randomization will go in nested data structures and recursive structures (e.g. when a Product has a list of other, nested products, which has a list of other products…) You can find more details about the consequences of<br>the configuration at <a href="https://github.com/j-easy/easy-random/wiki/Randomization-parameters">https://github.com/j-easy/easy-random/wiki/Randomization-parameters</a>. It is noteworthy that on top of the solution test-arranger goes through the deepest nesting level and sets empty values there (e.g. empty List, Set…) to minimize the negative effect<br>of nulls left by EasyRandom.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4ec99a0f742" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Arranging Java Records]]></title>
            <link>https://medium.com/@marian.jureczko/arranging-java-records-d11ec9141fde?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/d11ec9141fde</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[record]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Tue, 14 Jun 2022 16:18:57 GMT</pubDate>
            <atom:updated>2022-06-30T07:50:54.220Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mH9lyL8QQKc4PMaeJgQqmw.jpeg" /></figure><p>With the test-arranger (a library helping in arranging test data by employing randomness, <a href="https://github.com/ocadotechnology/test-arranger/">https://github.com/ocadotechnology/test-arranger/</a>) you can create the test data by just calling the <em>some()</em> method:</p><pre>var given = <em>some</em>(Data.class);</pre><p>Afterwards, you can easily adjust the object to the test case requirements, e.g:</p><pre>given.value = 42</pre><p>Sometimes, when the class is immutable it can be slightly more challenging, but you can always use the Lombok’s builder with the <em>toBuilder</em> flag:</p><pre>@Builder(toBuilder = true) class Data {<br>...</pre><p>And then the arranging code is as follows:</p><pre>var given = <em>some</em>(Data.class).toBuilder()<br>        .value(42)<br>        .build();</pre><p>The Java Record corresponds with Kotlin data class which is even more convenient than the <em>toBuilder()</em> thanks to the <em>copy()</em> method. Unfortunately, Java Records did not go so far and are not only truly immutable but also offer no <em>toBuilder()</em>, <em>copy()</em> or other copy-constructor like facilities. Fortunately, there is still the Lombok’s <em>toBuilder() </em>so let’s check out what will happen once we annotate our Data record with the <em>Builder</em> annotation:</p><pre>@Builder(toBuilder = true)<br>record Data(int value, String name) {<br>}</pre><p>and then, in the test:</p><pre>var given = <em>some</em>(Data.class).toBuilder()<br>        .value(42)<br>        .build();</pre><p>Well… my IDE is not happy about this solution and says: <em>Cannot resolve method ‘value’ in ‘DataBuilder’</em>. In my case, it was only the IDE, maven was able to compile the project, but let’s tinker a bit with it. The <em>Builder</em> annotation can be applied to a class as well as to a constructor. That leads us towards an alternative solution, but before reaching it let’s first set the major facts about Java Record’s constructors. We are free to define as many constructors in the record as we pleased. Typically, they are used to instantiate the record without giving all its fields — the custom constructor may initialize some of the fields with default values. Additionally, each record has the so-called canonical constructor that is the one with all the fields the record defines. That sounds like a perfect place for our <em>Builder</em> annotation. To make things even smoother, Java Records offer a compact canonical constructor — a more concise way of declaring the constructor, i.e. by eliding the list of formal parameters:</p><pre>public record Data(String value) {<br>    @Builder(toBuilder = true)<br>    public Data {<br>    }<br>}</pre><p>After such a change, my IDE doesn’t complain about the unresolvable <em>value</em> method anymore and shows nothing in red. However, I’m not entirely sure if it is worth it to type an additional line just to make the IDE happy, but for sure it is worth it to know that there is such a possibility.</p><h3>Epilogue</h3><p>Currently (two weeks after publishing the post), I’m using IntelliJ IDEA 2022.1.3 and it does not complain about Lombok’s annotations placed directly on records anymore. As a consequence, I can rid of the canonical constructor and simplify the example to:</p><pre>@Builder(toBuilder = true)<br>record Data(int value, String name) {<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d11ec9141fde" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On testing in DDD]]></title>
            <link>https://towardsdev.com/on-testing-in-ddd-f250482b5717?source=rss-db4d040c4a05------2</link>
            <guid isPermaLink="false">https://medium.com/p/f250482b5717</guid>
            <category><![CDATA[junit]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[ddd]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Marian Jureczko]]></dc:creator>
            <pubDate>Sun, 24 Apr 2022 17:58:55 GMT</pubDate>
            <atom:updated>2025-01-16T17:29:39.108Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*IGaCEViczIQcjxtCxSs0BA.png" /></figure><p>Classes designed according to the Domain Driven Development principles may create some challenges concerning testing (e.g. <a href="https://stackoverflow.com/questions/63424912/tdd-ddd-model-abstractions/">https://stackoverflow.com/questions/63424912/tdd-ddd-model-abstractions/</a>). When instantiating such classes you need to be especially careful as typically there are invariants, that, when not fulfilled, may result in tests failures. The classes are also often rich, they contain numerous fields (this can be a challenge not only in DDD). Arranging them in tests can easily result in tight coupling between tests and production code. That means each update in the production class leads to a change in the tests that use the class. We can work this out by creating reusable factories that respect the invariants and set all obligatory fields, but that does not remove the coupling. It only gathers it in a single place and makes it manageable. I faced the challenge a couple of years ago when working on an eCom product. The tests maintenance costs were so high during development that my team found time to stop for a moment and think about how we can improve the matters. And we found a solution. We created a tool for arranging random data for tests (<a href="https://github.com/ocadotechnology/test-arranger">https://github.com/ocadotechnology/test-arranger</a>) and an approach inspired by the fixture patterns to organize the test code. It worked well for us then and since that moment I use it practically in every project I’m working on. So let me introduce you to a solution that saved me hours of boring tests maintenance.</p><h3>Introducing the test-arranger</h3><p>Let’s check how it works. The system under test is an eCom, and as every eCom it has products. For a start, they will be represented by the following class:</p><pre>@Getter<br>@AllArgsConstructor<br>public class Product {<br>    @NonNull<br>    private String id;<br>    @NonNull<br>    private String name;<br>    @NonNull<br>    private String brand;<br>    @NonNull<br>    private BigDecimal price;<br>    @NonNull<br>    private TemperatureRegime temperatureRegime;<br>    @NonNull<br>    private BackOfPack backOfPack;<br>    private List&lt;Image&gt; images;<br>...<br>    public String shortName() {<br>        ...<br>    }<br>...<br>}</pre><p>The Product contains a lot of fields, most of them guarded by invariants. Yet, there is nothing exceptionally fancy, only ordinary “nonnull”. Additionally, there is the shortName() method that deserves a decent unit test. A naive solution could be as follows:</p><pre>@Test<br>void shortName() {<br>    //arrange<br>    var name = &quot;name&quot;;<br>    var product = new Product(&quot;id&quot;, name, &quot;brand&quot;, BigDecimal.<em>TEN</em>, TemperatureRegime.<em>Chill</em>, new BackOfPack(), null);<br><br>    //act<br>    var actual = product.shortName();<br><br>    //assert<br>    <em>assertThat</em>(name).contains(actual);<br>}</pre><p>The solution is not perfect. The test requires only the name, but I gave all parameters which created a tight coupling between the test and the Product class. Tighter than it needs to be. The test uses only the name, so all remaining parameters can be considered accidental complexity that additionally blurs the intention which is assuring that the short name is correctly constructed from the name. Let’s check how it can be done with the test-arranger:</p><pre>@Test<br>void shortNameArranger() {<br>    //arrange<br>    var product = <em>some</em>(Product.class);<br><br>    //act<br>    var actual = product.shortName();<br><br>    //assert<br>    <em>assertThat</em>(product.getName()).contains(actual);<br>}</pre><p>We have there two important achievements. Loosening the coupling with production code: the signature of the production constructor is not relevant anymore and the test does not need to know anything about other Product parameters than the name. And the second thing is that the name parameter is not particularly relevant too — we do not set it explicitly. The object of the test is to assure that the shortName() method converts the name in a particular way. The exact value of the name is not relevant. Note that it is a manifestation of the rule about avoiding duplication in tests recommended by TDD (Kent Beck: <em>Test Driven Development: By Example</em>).</p><p><strong>Random data in tests can reduce the coupling with the production code and hence make the test intention more obvious.</strong></p><p>On the other hand, I simplified the task by giving out control over the name. What if our assertions cannot be narrowed to a simple predicate? What if there is some fancy natural language processing involved and we need to give a certain input value and expect a certain output to test it properly? The answers are not straightforward as the Product is immutable and hence once created cannot be altered (if the class under test is mutable, this is a no-brainer, thus despite it is not something unusual, I will not explore this scenario further). I’m used to solving this challenge with Lombok’s builder with the toBuilder parameter:</p><pre>@Getter<br>@AllArgsConstructor<br>@Builder(toBuilder = true)<br>public class Product {<br>...</pre><p>It is questionable whether we should extend production classes for the sake of tests. However, in this case, it is worth considering, as that is a change that can make the production code better. You can do much more with immutable classes with something similar to copy-constructor (e.g. in Kotlin’s data classes such feature was included by design) and that is exactly what the <a href="http://twitter.com/Builder">@Builder</a>(toBuilder = true) delivers. With this annotation, we can call toBuilder() method on the Product instance to get a builder initialized with the original Product values. Then we can change using the builder the parameter we are interested in and create an altered Product copy. Let’s use that in our test:</p><pre>@Test<br>void shortNameArranger2() {<br>    //arrange<br>    var product = <em>some</em>(Product.class).toBuilder()<br>            .name(&quot;Great Value Frozen Pineapple Chunks 500g&quot;)<br>            .build();<br><br>    //act<br>    var actual = product.shortName();<br><br>    //assert<br>    <em>assertThat</em>(actual).isEqualTo(&quot;Frozen Pineapple&quot;);<br>}</pre><p>So I use the test-arranger to create a random instance of Product (no coupling with the constructor signature or class parameters) and then I use toBuilder() method to set in the name the certain value (“Great Value Frozen Pineapple Chunks 500g”) I need for my test case. As a consequence, the coupling with the production code tighten as little as it was possible. The test case depends on the name, but it cannot be avoided as it is part of the object of the test. Additionally, there is no boilerplate code in the test. Practically, everything that is in the test is necessary. As a consequence, it is not challenging to understand the test intention just by reading the code of the test, i.e. that shortName() method should produce “Frozen Pineapple” when its input equals “Great Value Frozen Pineapple Chunks 500g”.</p><p><strong>Lombok’s @Builder(toBuilder = true) can be very handy when arranging instances of immutable classes.</strong></p><h4>Kotlin approach</h4><p>In Kotlin, there is an interesting alternative — the data classes. They come with the very handy copy() method that makes it possible to do a similar thing as with the Lombok’s builder. So if the Product was a Kotlin data class, the “arrange” part of the test could be as follows:</p><pre>val product = <em>some</em>&lt;Product&gt;()<br>           .copy(name = &quot;Great Value Frozen Pineapple Chunks 500g&quot;)</pre><p>Unfortunately, Java’s counterpart (the record) is not so powerful, and we still need to use the builder.</p><p>When data class is not an option, but the class under test does not have to be immutable, another concise Kotlin syntax can be used:</p><pre>val product = <em>some</em>&lt;Product&gt; <strong>{<br>    </strong>name = &quot;Great Value Frozen Pineapple Chunks 500g&quot;<br><strong>}</strong></pre><h3>Invariants</h3><p>We have just seen how test-arranger can be handy in simple cases, but in DDD we often face more challenging situations. The random data may not be in line with domain invariants and as a consequence, we may fail in creating instances required for the tests or create invalid objects that will be rejected by validation rules. Let’s assume that the Product needs to be extended with a list of Barcodes and a feature for checking if a given Barcode is associated with the Product:</p><pre>public class Product {<br>...<br>    private List&lt;Barcode&gt; barcodes;<br>...<br>    public boolean hasBarcode(Barcode barcode) {<br>...</pre><pre>}</pre><pre>@Getter<br>@AllArgsConstructor<br>@Builder(toBuilder = true)<br>public class Barcode {<br>    @NonNull<br>    private String code;<br>    @NonNull<br>    private BarcodeType type;<br>}<br><br>public enum BarcodeType {<br>    <em>Code39</em>, <em>Code93</em>, <em>Code128</em>, <em>Ean8</em>, <em>Ean13</em>, <em>Jan</em>, <em>UpcA</em>, <em>UpcE<br></em>}</pre><p>The Barcodes are of certain types that impose rules on the code content (code field in the listing above), e.g. the expected number of characters. If the code field is initialized with a random sequence of characters, the logic depending on Barcodes will not work as it should. And its tests will manifest the garbage in — garbage out pattern. Not what a respected tester wants.</p><p>The hasBarcode() method is not as simple as it seems at the first look. Barcode is to some extent product identifier. When paying for the shopping in a grocery, the cashier scans the barcodes to let the system find out which products were bought. However, some barcodes contain a dynamic part that encodes for instance the expiry date. The Product class represents a certain type of inventory, so it will not be suitable to store Barcodes with each possible expiry date. Instead, a so-called canonical version is used — it has no real expiry date, but it is possible to compare it against the real barcode to determine if it represents the same kind of product. So in the Product.barcodes, there are canonical versions of the barcodes. However, the hasBarcode() method should return true not only when there is an exact match, but also when in the Product, there is a canonical version corresponding with the real barcode (containing an expiry date) used as the method parameter.</p><p>That constitutes a feature that depends on the validity of the Barcode — a Barcode cannot be compared against its canonical version when it is just a random sequence of characters. Therefore, to have valid data in the tests, a recipe for creating correct Barcodes must be delivered to the test-arranger. It can be done by extending the CustomArranger class:</p><pre>class BarcodeArranger extends CustomArranger&lt;Barcode&gt; {<br><br>    @Override<br>    protected Barcode instance() {<br>        Barcode barcode = super.instance();<br>        return barcode.toBuilder()<br>                .code(generateCanonicalCode(barcode.getType()))<br>                .build();<br>    }<br><br>    private String generateCanonicalCode(BarcodeType type) {<br>        ...<br>    }<br>}</pre><p>The body of the generateCodeOfType() method was omitted for simplicity. Test-arranger is looking for the custom arranger using reflection. To not scan the whole classpath, which can cost a lot of time, there is the arranger.root property that should point to a package covering all custom arranger, e.g.:</p><p>arranger.root=com.your_company_name</p><p>The BarcodeArranger will be picked up by the test-arranger whenever a new instance of Barcode is created. The test-arranger will call the instance() method and use its result. When the instance() method isn’t overridden, a completely random instance is created. Test-arranger uses custom arranger not only when direct instantiation is requested. That means, it will be used in both:</p><pre>var product = <em>some</em>(Product.class);<br>var barcode = <em>some</em>(Barcode.class);</pre><p><strong>When a class cannot be entirely random, provide a custom arranger (should extend the CustomArranger) that instantiates the class less randomly, without breaking its invariants.</strong></p><p>Let’s go back to the test case. With the custom arranger for Barcode, it can be as follows:</p><pre>@Test<br>void hasNotBarcode() {<br>    //arrange<br>    Barcode barcode = <em>some</em>(Barcode.class);<br>    Product product = <em>some</em>(Product.class);<br>    <br>    //act<br>    boolean actual = product.hasBarcode(barcode);<br>    <br>    //assert<br>    <em>assertThat</em>(actual).isFalse();<br>}</pre><p>It will work like a charm, as the test-arranger already knows how to create valid Barcodes however, still uses random data. First, a valid but random Barcode is created. In the next line, a Product is instantiated. The Product has a list of Barcodes, each of them will be valid. But since random data is used, we will not find there the Barcode that was created in the previous line.</p><p>The positive scenario can be tested similarly:</p><pre>@Test<br>void hasBarcode() {<br>    //arrange<br>    Barcode barcode = <em>some</em>(Barcode.class);<br>    Product product = <em>some</em>(Product.class).toBuilder()<br>            .barcodes(List.<em>of</em>(<em>some</em>(Barcode.class), barcode))<br>            .build();<br><br>    //act<br>    boolean actual = product.hasBarcode(barcode);<br><br>    //assert<br>    <em>assertThat</em>(actual).isTrue();<br>}</pre><p>It definitely should be tested that the hasBarcode() method works correctly with Products containing the canonical Barcodes. Arranging data for such a test case is a bit more challenging. We need a Product associated with a canonical Barcode and a real Barcode that is in line with the canonical one. To get there it would be convenient to have a method that can generate a canonical one and a method that generates a real one, with a random code, but according to the given canonical barcode. Both methods are about arranging Barcodes, so why not put them into BarcodeArranger:</p><pre>class BarcodeArranger extends CustomArranger&lt;Barcode&gt; {<br><br>    @Override<br>    protected Barcode instance() {<br>        Barcode barcode = super.instance();<br>        return barcode.toBuilder()<br>                .code(generateCodeOfType(barcode.getType()))<br>                .build();<br>    }<br><br>    public static Barcode canonical() {<br>        ...<br>    }<br>    <br>    public static Barcode inLineWith(Barcode canonical) {<br>        ...<br>    }<br>    <br>    private String generateCodeOfType(BarcodeType type) {<br>        ...<br>    }<br>}</pre><p>Please note that the new methods are public static. They do not share any state with the BarcodeArranger class, nonetheless, I decided to put them into arranger. Such an approach makes it obvious where to look for factory methods when creating objects for tests — all of them are in the NameOfClassArranger. It is a small thing, but it helps in getting rid of duplication in tests.</p><p><strong>All methods designed for instantiating a certain class (for the sake of the tests) should be moved to the class arranger. Moreover, the methods should be reused (whenever suitable) to avoid duplication in the test data arrangement.</strong></p><p>With the convenient BarcodeArranger, I can finally create the test case:</p><pre>@Test<br>void hasCanonicalBarcode() {<br>    <em>//arrange<br>    </em>Barcode canonical = BarcodeArranger.<em>canonical</em>();<br>    Barcode barcode = BarcodeArranger.<em>inLineWith</em>(canonical);<br>    Product product = <em>some</em>(Product.class).toBuilder()<br>            .barcodes(List.<em>of</em>(<em>some</em>(Barcode.class), canonical))<br>            .build();<br><br>    <em>//act<br>    </em>boolean actual = product.hasBarcode(barcode);<br><br>    <em>//assert<br>    assertThat</em>(actual).isTrue();<br>}</pre><h4>Kotlin approach</h4><p>In Kotlin, the arranger classes can be usually simpler. Typically, different test cases require setting different subsets of the class fields. In Java, we cannot do much besides writing corresponding with the needs methods that increase arranger complexity. Fortunately, in Kotlin, there are named and default arguments. So we can come up with the following BarcodeArranger (it does not address the canonical barcode challenge we explored before):</p><pre>class BarcodeArranger {<br>    companion object {<br>        fun with(<br>            code: String = <em>some</em>(),<br>            type: BarcodeType = <em>some</em>()<br>        ): Barcode =<br>            Barcode(code, type)<br>    }<br>}</pre><p>The above BarcodeArranger creates completely random Barcodes. In real life, there would be some fancy logic (e.g. converting codes to canonical ones according to the selected type) — the purpose of creating an arranger in the first place. But my only goal here is to show how Kotlin is handy in arrangers, and hence no additional details are given. And the power of Kotlin comes from the fact that with the one simple methods from the previous listing, all those calls are possible:</p><pre>val b1 = BarcodeArranger.with(code = &quot;my_fancy_code&quot;)<br>val b2 = BarcodeArranger.with(type = BarcodeType.<em>Ean13</em>)<br>val b3 = BarcodeArranger.with(code = &quot;my_fancy_code&quot;, type = BarcodeType.<em>Ean13</em>)</pre><p>We do not face here the unwanted explosion of methods covering different combinations of necessary arguments. We can (usually) have just one, where each not given explicitly argument is assigned with a random value (the some() method call).</p><h3>Beyond single aggregate</h3><p>Till now all the examples were focused on arranging a very narrowed number of classes that can be generalized to nothing more than a single aggregate. Unfortunately, software systems usually are not so simple. Often there is more than one aggregate, and there are test cases depending on interactions between different aggregates. That isn’t something we can well address with arrangers — an arranger, by the definition, is associated with a certain class. When there are two aggregates to arrange which of the two arrangers should contain the code? Fortunately, there is a well-known tests design pattern — the fixture. It was described in many variants by Gerard Meszaros in <em>xUnit Test Patterns: Refactoring Test Code</em> (worth reading to find more details about the pattern).</p><p>Let’s imagine that there are shopping aisles in our shop and each product is assigned to one or more aisles. On the other hand, the Aisles have some attributes, like description, and create a tree-like structure. As a consequence, they were designed as a different aggregate than the Product class we already tested before. So the Product is extended with the following field:</p><pre>private List&lt;AisleId&gt; aisles;</pre><p>And a new class to represent the aisles:</p><pre>@Getter<br>@AllArgsConstructor<br>@Builder(toBuilder = true)<br>public class Aisle {<br>    private AisleId id;<br>    private String name;<br>    private String description;<br>    private List&lt;Aisle&gt; subaisles;<br>}</pre><p>When the user selects a certain Aisle, the system should render all the Products assigned to the selected Aisle and all its sub-aisles. The algorithm should go deeper and deeper into the sub-aisles till it reaches the leaves (Aisles without sub-aisles). It sounds like a feature where regression may happen and hence should be guarded by some tests (the method signature suggests that there is a design issue that may result in poor performance, but let’s ignore that and focus on the testing):</p><pre>public List&lt;Product&gt; filterProductsByAisle(<br>      List&lt;Product&gt; products, <br>      Aisle aisle) {<br>    ...<br>}</pre><p>We can easily get away with random data when testing that the method filters out Products not assigned to the given Aisle:</p><pre>@Test<br>void filterProductsNotAssignedToAisle() {<br>    <em>//arrange<br>    </em>var aisle = <em>some</em>(Aisle.class);<br>    var products = <em>someObjects</em>(Product.class, 10)<br>            .collect(Collectors.<em>toList</em>());<br><br>    <em>//act<br>    </em>var actual = sut.filterProductsByAisle(products, aisle);<br><br>    <em>//assert<br>    assertThat</em>(actual).isEmpty();<br>}</pre><p>In the test, a random Aisle is created, and then ten random Products. The first has nothing to do with the second, so we can assert that the result of filtering is an empty collection. However, to test the positive scenario we need Products containing the ids of the Aisles we submit for filtering. That’s finally the moment when the fixture comes into play:</p><pre>public class ProductsAndAislesFixture {<br><br>    public final Aisle aisle;<br>    public final List&lt;Product&gt; products;<br><br>    public ProductsOnAislesFixture(int noOfProducts) {<br>        aisle = <em>some</em>(Aisle.class);<br>        Set&lt;AisleId&gt; aislesIds = aisle.allIds();<br>        products = <em>someObjects</em>(Product.class, noOfProducts)<br>                .map(p -&gt; p.toBuilder()<br>                        .aisles(List.<em>of</em>(<em>someFrom</em>(aislesIds)))<br>                        .build())<br>                .collect(Collectors.<em>toList</em>());<br>    }<br>}</pre><p>And the corresponding test:</p><pre>@Test<br>void filterProductsAssignedToAisle() {<br>    <em>//arrange<br>    </em>var noOfProducts = <em>somePositiveInt</em>(20);<br>    var fixture = new ProductsOnAislesFixture(noOfProducts);<br><br>    <em>//act<br>    </em>var actual = sut.filterProductsByAisle(<br>            fixture.products, <br>            fixture.aisle);<br><br>    <em>//assert<br>    assertThat</em>(actual).hasSize(noOfProducts);<br>}</pre><p>When test data arrangement is covered by the fixture, the test becomes very straightforward, and its intention is quite easy to guess just by reading the code.</p><p><strong>When a test requires complex data (more than one aggregate) store the data creation procedure in a fixture.</strong></p><p>The fixture was created for only one use case, so it is a bit oversimplified. In an enterprise setup we should think about data immutability or about removing the data creation procedure from the constructor as with the forthcoming test cases we may need slight differences in the arrangement. When the data needs to be arranged in a different way, it is usually worth it to do it with a dedicated method whose name explains the peculiarities of the created data.</p><p>We can also think about the lifecycle of the fixture. When the data creation is very time-consuming, it can be justified to share the fixture between test cases. Consider creating data in an embedded database using Spring repositories… The spectrum of possible fixture variants is comprehensively covered by Meszaros in the book I mentioned a couple of passages earlier.</p><h3>Conclusions and summary</h3><p>DDD defines a number of building blocks representing different levels of size and complexity. I gave examples showing how to deal with them starting from primitives and ending with collections of aggregates which pretty much correspond with the bounded contexts. A concise summary of this approach is given in a diagram in the test-arranger readme:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*PEctvXjSTIrAlBqm1_a0nw.jpeg" /><figcaption>source: <a href="https://github.com/ocadotechnology/test-arranger">https://github.com/ocadotechnology/test-arranger</a></figcaption></figure><p>That is an approach I have used for a couple of years and it serves me well. Nonetheless, it does not contain only advantages. Let’s note the observed pros and cons.</p><h4>Test-arranger pros</h4><p><strong>Looser coupling with production code.</strong> The tests do not depend on complex constructors or implementation details (e.g. required fields) of the production classes. Under the hood, the test-arranger uses reflection to instantiate classes. Hence, it can automatically adapt to changes as long as they have nothing to do with the test goal, but that’s the level of coupling we need to accept.</p><p><strong>Less code in tests.</strong> The test-arranger does the dirty job of delivering values for all required fields. While in the test remains only what is relevant for the test goal, and we end up with less code.</p><p><strong>Clearer test intention.</strong> As it was already mentioned, with the test-arranger, we have in the tests only the relevant parts. It is much easier to discover the original intention without all the boilerplate code that vanished.</p><p><strong>Additional possibilities for detecting issues.</strong> Random data is used to cover fields irrelevant to a given test case. However, the code under tests may be using the data incorrectly, and for a certain value, a variable that should be irrelevant results in the test failure. That is a good thing because it leads to detecting defects in unexpected places. Maybe it won’t happen on the first try, but that is still better than not detecting the defects at all. Consider the case with enumerations in the contract tests: <a href="https://stackoverflow.com/questions/43089653/should-i-test-all-enum-values-in-a-contract/66040704#66040704">https://stackoverflow.com/questions/43089653/should-i-test-all-enum-values-in-a-contract/66040704#66040704</a></p><p><strong>Fewer duplications in the test code.</strong> Organizing the code for arranging test data into arrangers and fixtures makes it obvious where to look for a certain factory method. It does not prevent code duplication, but at least creates opportunities for reuse.</p><p><strong>Lower test maintenance cost.</strong> I already mentioned looser coupling with production code, less test code, fewer duplication and clearer intentions. That all makes the need to adjust tests to changes in production code less frequent, and when it occurs, the tests should be easier to understand for the maintainer.</p><h4>Test-arranger cons</h4><p><strong>More challenging debugging.</strong> The fields initialized with random data usually have fancy values, which are harder to memorize, and as a consequence, tracking the data flow may become difficult.</p><p><strong>Blinking tests.</strong> If the expectations about what is relevant and what is irrelevant are wrong (the author misunderstood the code under test) and random data is used in a place where a certain value is needed, the test eventually will fail. It will happen when unacceptable data is drawn. This drawback is a counterpart of the <em>Additional possibilities for detecting issues</em>. When a mistake is in a test, the random data may sometimes compensate for it and result in success. When the mistake is in production code, random data may shed new light on it leading to a bugfix. From my personal experience, both situations are rare, and the additional possibility of blinking only increases my confidence in non-blinking test suites.</p><p><strong>Slower tests. </strong>Generating random data is not as fast as assigning values to variables. It is still only a fraction of a second, but unfortunately, observably slower.</p><p>I hope that my ideas about organizing test data will help in solving challenges related to complex test data arrangements and reduce the number of tests that need to be adjusted in response to changed production code. If all the fixtures and arrangers look like too much fuss, try something simple. It isn’t all or nothing solution. If you use random data in a single test case, the benefit will be very small. But it will be there. So you can travel step by step at a pace that is convenient for you and your project.</p><p>Good luck in testing DDD.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f250482b5717" width="1" height="1" alt=""><hr><p><a href="https://towardsdev.com/on-testing-in-ddd-f250482b5717">On testing in DDD</a> was originally published in <a href="https://towardsdev.com/">Towards Dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>