<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Renaud Humbert-Labeaumaz</title>
    <description>The latest articles on DEV Community by Renaud Humbert-Labeaumaz (@rnowif).</description>
    <link>https://dev.to/rnowif</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F144128%2Ff685435c-dd88-45be-a039-a9472dde3cb8.png</url>
      <title>DEV Community: Renaud Humbert-Labeaumaz</title>
      <link>https://dev.to/rnowif</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rnowif"/>
    <language>en</language>
    <item>
      <title>Bringing Meaning to Legacy Code with Parameter Objects</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Tue, 09 Mar 2021 15:39:19 +0000</pubDate>
      <link>https://dev.to/rnowif/bringing-meaning-to-legacy-code-with-parameter-objects-lc1</link>
      <guid>https://dev.to/rnowif/bringing-meaning-to-legacy-code-with-parameter-objects-lc1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zmUX9SDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/media/posts/17/xavi-cabrera-kn-UmDZQDjM-unsplash.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zmUX9SDF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/media/posts/17/xavi-cabrera-kn-UmDZQDjM-unsplash.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of the legacy codebases I have work on suffered from primitive obsession. One of the symptoms is the presence of methods with a swarm of &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt; or &lt;code&gt;bool&lt;/code&gt; parameters. These methods harm the codebase as they are hard to understand and easy to misuse - which could cause bugs and headaches. This article aims to present a simple, yet extremely effective, solution to this issue through the use of parameter objects.&lt;/p&gt;




&lt;p&gt;Let’s take the following method signature to illustrate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;string GenerateLoginLink(
    int userId, 
    DateTime? expiryDate, 
    int? maxAllowedClicks, 
    bool notifyOnClick, 
    bool sendLinkByEmail, 
    bool redirectToHomePage, 
    string? redirectUrl
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method generates a link that would allow the user to log in to the platform and be redirected to a specific url. This link has an expiry date or a maximal number of clicks allowed. Also, we can optionally send it directly to the user by email and notify an admin when the user has clicked on it.&lt;/p&gt;

&lt;p&gt;There are a couple of business rules to be aware of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;expiryDate&lt;/code&gt; and &lt;code&gt;maxAllowedClicks&lt;/code&gt; are mutually exclusive (i.e. we can only pass one of them as an argument)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;redirectUrl&lt;/code&gt; is only useful if &lt;code&gt;redirectToHomePage&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, this method presents some issues.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Illegal combinations.&lt;/strong&gt; In the best-case scenario, providing the method with an &lt;code&gt;expiryDate&lt;/code&gt; and a &lt;code&gt;maxAllowedClicks&lt;/code&gt; throws an exception or silently gives priority to one of these fields. In the worst-case scenario, it generates an unpredictable behaviour because the case is not handled. Also, what if both are &lt;code&gt;null&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poor expressivity.&lt;/strong&gt; The business rules expressed above are far from obvious. Maybe they are documented in the method’s comment but people do not always read the documentation and it can become stale very quickly if we forget to update it (maybe that is the reason why people tend to overlook documentation?). Also, there is absolutely nothing preventing the developer to pass illegal or useless combinations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error-prone.&lt;/strong&gt; Finally, did you notice the 3 consecutive booleans in the method’s signature? How easy would it be to invert two of them by mistake? The method might work exactly as expected and be thoroughly unit tested, you will still have bugs if you don’t use it properly. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first iteration would be to use a parameter object for this method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class LoginLinkConfig 
{
    public DateTime? ExpiryDate {get; set;}
    public int? MaxAllowedClicks {get; set;}
    public bool NotifyOnClick {get; set;}
    public bool SendLinkByEmail {get; set;}
    public bool RedirectToHomePage {get; set;}
    public string? RedirectUrl {get; set;}
}

string GenerateLoginLink(int userId, LoginLinkConfig config);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This change could reduce the risk of error and would make the use of default values easier. However, it would not be very helpful to prevent illegal combinations and increase expressivity.&lt;/p&gt;

&lt;p&gt;A solution to tackle these issues is to use the builder pattern. This pattern provides an expressive API to create an object and is particularly useful when it comes to objects with optional fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class LoginLinkConfig 
{
    // Properties are now read-only
    public DateTime? ExpiryDate {get;}
    public int? MaxAllowedClicks {get;}
    public bool NotifyOnClick {get;}
    public bool SendLinkByEmail {get;}
    public bool RedirectToHomePage {get;}
    public string? RedirectUrl {get;}

    // The constructor is private to force the use of the builder
    private LoginLinkConfig(DateTime? expiryDate, int? maxAllowedClicks, bool notifyOnClick, bool sendLinkByEmail, bool redirectToHomePage, string? redirectUrl) 
    {
        ExpiryDate = expiryDate;
        MaxAllowedClicks = maxAllowedClicks;
        NotifyOnClick = notifyOnClick;
        SendLinkByEmail = sendLinkByEmail;
        RedirectUrl = redirectUrl;
    }

    // Notice that the parameter in these methods is not nullable
    public static Builder ExpiringLink(DateTime expiryDate) =&amp;gt; new Builder(expiryDate);
    public static Builder LimitedClicksLink(int maxAllowedClicks) =&amp;gt; new Builder(maxAllowedClicks);

    public class Builder
    {
        private DateTime? _expiryDate;
        private int? _maxAllowedClicks;
        private bool _notifyOnClick;
        private bool _sendLinkByEmail;
        // We can set sensible default values
        private bool _redirectToHomePage = true;
        private string? _redirectUrl;

        public Builder(DateTime expiryDate)
        {
            _expiryDate = expiryDate;
        }

        public Builder(int maxAllowedClicks)
        {
            _maxAllowedClicks = maxAllowedClicks;
        }

        public Builder WithRedirection(string redirectUrl)
        {
            _redirectToHomePage = false;
            _redirectUrl = redirectUrl;

            // Return the builder to allow chaining
            return this;
        }

        public Builder WithClickNotification()
        {
            _notifyOnClick = true;

            // Return the builder to allow chaining
            return this;
        }

        public Builder SentByEmail()
        {
            _sendLinkByEmail = true;

            // Return the builder to allow chaining
            return this;
        }

        public LoginLinkConfig Build() =&amp;gt; new LoginLinkConfig(_expiryDate, _maxAllowedClicks, _notifyOnClick, _sendLinkByEmail, _redirectUrl);
    }

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

&lt;/div&gt;



&lt;p&gt;With this builder, creating the config object goes from&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var maxAllowedClicksConfig = new LoginLinkConfig 
{
    MaxAllowedClicks = 1,
    NotifyOnClick = true,
    RedirectToHomePage = false,
    RedirectUrl = "/myaccount"
};

var expiringLinkConfig = new LoginLinkConfig 
{
    ExpiryDate = DateTime.Now.AddDays(1),
    SendLinkByEmail = true,
    RedirectToHomePage = true
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var maxAllowedClicksConfig = LoginLinkConfig.LimitedClicksLink(1)
    .WithClickNotification()
    .WithRedirection("/myaccount")
    .Build();

var expiringLinkConfig = LoginLinkConfig.ExpiringLink(DateTime.Now.AddDays(1))
    .SendByEmail()
    .Build();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This new instantiation process is arguably more verbose, but provides the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;More meaningful.&lt;/strong&gt; By using a builder, the developer can express what they mean and there is no doubt about it. Since we got rid of the ambiguity, errors are less likely to occur.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No illegal state.&lt;/strong&gt; As illustrated in the example, there is absolutely no way of representing an illegal state. For instance, if you want to create an expiring link, you cannot also set the &lt;code&gt;_maxAllowedClicks&lt;/code&gt;. Otherwise, when you provide a &lt;code&gt;redirectUrl&lt;/code&gt;, the &lt;code&gt;_redirectToHomePage&lt;/code&gt; is automatically set to &lt;code&gt;false&lt;/code&gt; and cannot be changed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simpler implementation.&lt;/strong&gt; Finally, since the business rules described above are embedded into the object creation, there is no need to enforce them into the method - which allows for simpler implementation. Also, other methods can use the config object, knowing that the business rules will always be applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, using a parameter object in conjunction with the builder pattern is a very simple way to bring back meaning into your legacy codebase and reduce bugs by making illegal states literally impossible to represent.&lt;/p&gt;

</description>
      <category>refactoring</category>
      <category>legacycode</category>
      <category>designpattern</category>
    </item>
    <item>
      <title>Untangling Legacy Code with Events</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Thu, 04 Mar 2021 17:34:41 +0000</pubDate>
      <link>https://dev.to/rnowif/untangling-legacy-code-with-events-47nb</link>
      <guid>https://dev.to/rnowif/untangling-legacy-code-with-events-47nb</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frnowif.github.io%2Fmedia%2Fposts%2F16%2Fpaper-1565157_1920.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frnowif.github.io%2Fmedia%2Fposts%2F16%2Fpaper-1565157_1920.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Legacy code is typically characterised by long methods that mix up different concepts and levels of abstraction. In this context, it can be hard to extract behaviour into proper modules because it feels like everything depends on everything and it would require a massive overhaul of the entire codebase. This article proposes to use events to reduce coupling between legacy code and other properly-bounded modules to either extract existing code or add new one.&lt;/p&gt;




&lt;p&gt;Let’s say you have a typical &lt;code&gt;UserManager&lt;/code&gt; class which features a &lt;code&gt;CreateUser&lt;/code&gt; method. Once a user is created, we might want to send a welcome email to set the password and clear a cache somewhere. The initial code looks something like this:&lt;br&gt;
&lt;/p&gt;

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

    public async Task CreateUser(string username) {
        var user = new User { Username = username };
        int userId = await _repository.Add(user);
        await ClearCacheForUser(userId);
        await SendWelcomeEmail(userId, user);
    }

    private async Task ClearCacheForUser(int userId) {
        // ...
    }

    private async Task SendWelcomeEmail(int userId, User user) {
        // ...
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first step could be to extract &lt;code&gt;ClearCacheForUser&lt;/code&gt; and &lt;code&gt;SendWelcomeEmail&lt;/code&gt; to dedicated classes. However, it would not fundamentally change the issue. Indeed, the &lt;code&gt;CreateUser&lt;/code&gt; method would still need to know to clear the cache, send the email, and all the other processes that need to happen upon user creation. Thus, the coupling is still there.&lt;/p&gt;

&lt;p&gt;A simple solution to remove this coupling is to use events. Before you raise an eyebrow, it’s important to note that using events does not necessarily mean implementing a full-blown event-sourcing architecture with an event store and whatnot (keep in mind that event-driven and event-sourcing are &lt;a href="https://martinfowler.com/articles/201701-event-driven.html" rel="noopener noreferrer"&gt;two separate things&lt;/a&gt;). As a matter of fact, starting with a very straightforward in-process event bus can go a long way to help untangle your legacy code.&lt;/p&gt;

&lt;p&gt;The following code is a complete implementation of an in-process event bus that you can use.&lt;br&gt;
&lt;/p&gt;

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

public interface IEventBus
{
    Task Publish&amp;lt;T&amp;gt;(T @event) where T : EpEvent;

    void Subscribe&amp;lt;T&amp;gt;(IEventHandler&amp;lt;T&amp;gt; handler) where T : MyEvent;
}

public interface IEventHandler&amp;lt;in T&amp;gt; where T : MyEvent
{
    Task Handle(T @event);
}

public class InProcessEventBus : IEventBus
{
    private readonly IDictionary&amp;lt;Type, IList&amp;lt;object&amp;gt;&amp;gt; _subscriptions = new Dictionary&amp;lt;Type, IList&amp;lt;object&amp;gt;&amp;gt;();

    public async Task Publish&amp;lt;T&amp;gt;(T @event) where T : MyEvent
    {
        if (_subscriptions.TryGetValue(typeof(T), out IList&amp;lt;object&amp;gt; handlers))
        {
            foreach (IEventHandler&amp;lt;T&amp;gt; handler in handlers.OfType&amp;lt;IEventHandler&amp;lt;T&amp;gt;&amp;gt;())
            {
                    await handler.Handle(@event);
            }
        }
    }

    public void Subscribe&amp;lt;T&amp;gt;(IEventHandler&amp;lt;T&amp;gt; handler) where T : MyEvent
    {
        if (!_subscriptions.ContainsKey(typeof(T)))
        {
            _subscriptions[typeof(T)] = new List&amp;lt;object&amp;gt;();
        }

        _subscriptions[typeof(T)].Add(handler);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using this minimalist event bus, we can re-write our &lt;code&gt;CreateUser&lt;/code&gt; method like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class UserCreatedEvent : MyEvent {
    public int UserId { get; }
    public string Username { get; }

    public UserCreatedEvent(int userId, string username) {
        UserId = userId;
        Username = username;
    }
}

public class UserManager {
    // ...
    private readonly IEventBus _eventBus;

    public async Task CreateUser(string username) {
        var user = new User { Username = username };
        int userId = await _repository.Add(user);

        await _eventBus.Publish(new UserCreatedEvent(userId, username));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can create handlers to process this &lt;code&gt;UserCreatedEvent&lt;/code&gt; and do what needs to be done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ClearUserCacheEventHandler : IEventHandler&amp;lt;UserCreatedEvent&amp;gt;
{
    public ClearUserCacheEventHandler(IEventBus eventBus)
    {
        eventBus.Subscribe(this);
    }

    public Task Handle(UserCreatedEvent @event)
    {
        // Clear cache
    }
}

public class SendWelcomeEmailEventHandler : IEventHandler&amp;lt;UserCreatedEvent&amp;gt;
{
    public SendWelcomeEmailEventHandler(IEventBus eventBus)
    {
        eventBus.Subscribe(this);
    }

    public Task Handle(UserCreatedEvent @event)
    {
        // Send welcome email
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This event bus will not help with scalability as the events are processed synchronously and the &lt;code&gt;Publish&lt;/code&gt; method only returns once all the events are processed. However, it can dramatically reduce coupling within the codebase and can act as a stepping stone to make your architecture more &lt;em&gt;event-driven&lt;/em&gt;. Moreover, if you need to go further (e.g. two services communicating through events), you already have the abstractions in place to switch the &lt;code&gt;InProcessEventBus&lt;/code&gt; for an out-of-process implementation.&lt;/p&gt;

</description>
      <category>refactoring</category>
      <category>design</category>
      <category>architecture</category>
    </item>
    <item>
      <title>In Defence of the Liskov Substitution Principle</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 22 Jan 2021 23:17:11 +0000</pubDate>
      <link>https://dev.to/rnowif/in-defence-of-the-liskov-substitution-principle-45p9</link>
      <guid>https://dev.to/rnowif/in-defence-of-the-liskov-substitution-principle-45p9</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CpcvSvPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/media/posts/14/stormtrooper-1343877_1920.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CpcvSvPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/media/posts/14/stormtrooper-1343877_1920.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Liskov Substitution Principle (LSP for short) is the “L” in the &lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;SOLID&lt;/a&gt; principles. Even though many people know this principle, at least by name, I cannot keep track of how many times it is skipped when the SOLID principles are being explained. Most of the time, the explanation of the LSP is reduced to stating its name and showing a half-assed example to which nobody can relate!&lt;/p&gt;

&lt;h2&gt;
  
  
  Perceived Complexity
&lt;/h2&gt;

&lt;p&gt;I think that the LSP is often overlooked because of its perceived complexity, starting with its name. Indeed, this principle is named after the computer science pioneer and Turing Award winner &lt;a href="https://en.wikipedia.org/wiki/Barbara_Liskov"&gt;Barbara Liskov&lt;/a&gt;, who laid the base for our understanding of modules and abstraction. As such, it is the only SOLID principle that has not a self-explaining name and I believe that it could throw people off.&lt;/p&gt;

&lt;p&gt;Funnily enough, Barbara Liskov explained - during a &lt;a href="https://youtu.be/oeknOtb0gzQ?t=1339"&gt;conference talk&lt;/a&gt; she gave in 2020 - that someone asked her once what this principle meant and she was pretty confused because she wasn’t even aware of its existence.&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s Just Semantics
&lt;/h2&gt;

&lt;p&gt;In a nutshell, the LSP means that there must be a &lt;strong&gt;&lt;em&gt;semantic&lt;/em&gt;&lt;/strong&gt; relationship between a class and a subclass - sharing methods with the same name is not enough. For instance, stacks and queues may both have &lt;code&gt;add&lt;/code&gt; or &lt;code&gt;remove&lt;/code&gt; methods but these classes should not inherit from each other as these methods mean completely different things: a stack would work in a &lt;em&gt;last-in-first-out&lt;/em&gt; (LIFO) fashion as opposed to &lt;em&gt;first-in-first-out&lt;/em&gt; (FIFO) for a queue. Consequently, if your program is built using a queue and you &lt;em&gt;substitute&lt;/em&gt; it for a stack, it might not work anymore.&lt;/p&gt;

&lt;h2&gt;
  
  
  Temper your Expectations
&lt;/h2&gt;

&lt;p&gt;The idea behind the LSP revolves around expectations and contracts. If I use a stack, I &lt;em&gt;expect&lt;/em&gt; it to be a LIFO and would not be happy if it turned out to be a FIFO instead. Any subtype of the stack should respect this &lt;em&gt;contract&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can therefore infer that a subtype violates the LSP if&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it expects more than what the contract states (e.g. non-null input, specific concrete type for an interface, etc.)&lt;/li&gt;
&lt;li&gt;it does less than what the contract requires (e.g. return null when it shouldn’t, does not return a list in proper order)&lt;/li&gt;
&lt;li&gt;it does not implement a method defined in the base class or interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The LSP also means that the client code should not rely on something that is not in the contract.&lt;/p&gt;

</description>
      <category>design</category>
      <category>solid</category>
    </item>
    <item>
      <title>Purposeful Testing or Why the Test Pyramid is a Scam</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 15 Jan 2021 17:29:22 +0000</pubDate>
      <link>https://dev.to/rnowif/purposeful-testing-or-why-the-test-pyramid-is-a-scam-5lp</link>
      <guid>https://dev.to/rnowif/purposeful-testing-or-why-the-test-pyramid-is-a-scam-5lp</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H8RAJHK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71u0szk3bayxch1iu2hr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H8RAJHK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71u0szk3bayxch1iu2hr.jpg" alt="anthony-mcgee-iOZOydyIw3M-unsplash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://martinfowler.com/articles/practical-test-pyramid.html"&gt;test pyramid&lt;/a&gt; is a metaphor that illustrates the “ideal” weight of tests depending on their granularity. It goes from the unit tests at the base - which are more numerous - to the end-to-end tests at the top - which should be scarce.&lt;/p&gt;

&lt;p&gt;It is often opposed to the “ice cream cone” &lt;a href="https://abstracta.us/blog/test-automation/best-testing-practices-agile-teams-automation-pyramid/"&gt;anti-pattern&lt;/a&gt; that features a few unit tests and a massive amount of manual end-to-end tests.&lt;/p&gt;

&lt;p&gt;I think that this pattern is a good heuristic - especially when your test suite looks like an ice cream cone - but it also yields a lot of sterile discussions, such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is a unit test? What is an integration test?&lt;/li&gt;
&lt;li&gt;Do I test private methods?&lt;/li&gt;
&lt;li&gt;Do I test pass-through methods? Do I test getters, setters?&lt;/li&gt;
&lt;li&gt;What is ideal code coverage? Should I go to jail if I have less than 100% coverage?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Purposeful Testing
&lt;/h2&gt;

&lt;p&gt;Behind this click-bait title, I would like to go &lt;em&gt;beyond&lt;/em&gt; the test pyramid and start reasoning in terms of &lt;em&gt;purpose&lt;/em&gt; rather than &lt;em&gt;form&lt;/em&gt;. Whenever I write tests, I make sure to ask myself the following questions, to be sure that my tests are fit-for-purpose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What is the purpose of this test?&lt;/strong&gt; &lt;em&gt;Knowing what the test is supposed to check prevents me from trying to test too much and ultimately write something that will hurt me in the long run. Let’s remember that tests are similar to production code in the way that they require maintenance.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What would make this test fail?&lt;/strong&gt; &lt;em&gt;By trying to answer this question, I often realise that this test’s failure does not mean that it has achieved its purpose. Worse, sometimes the test simply cannot fail. Tests are not supposed to check everything, they should keep passing when something that they are not supposed to cover is broken.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Will it impede our ability to refactor?&lt;/strong&gt; &lt;em&gt;A test can too heavily be coupled with the production code can actually be counterproductive. Indeed, it would incentivise not to refactor and hurt maintainability. A typical symptom of a test that impedes refactoring is the overuse of mocks.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Test Taxonomy
&lt;/h2&gt;

&lt;p&gt;By applying this reasoning, I identified the following types of test (this list is far from complete and will get updated in the future).&lt;/p&gt;

&lt;h3&gt;
  
  
  Support Tests
&lt;/h3&gt;

&lt;p&gt;These tests aim to &lt;em&gt;support&lt;/em&gt; the development of an algorithm or standalone class (i.e. no dependency). You would typically start with a very straightforward code and incrementally add test cases to increase the scope and add new edge cases. I have found this type of test to be the perfect use case for &lt;a href="https://www.youtube.com/watch?v=RWYvBNX9wcU"&gt;Test Driven Development&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Plumbing Tests
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Plumbing&lt;/em&gt; tests ensure that several classes, external libraries, or third-party services work together as intended. In the test pyramid, these tests fall into the generally accepted definition of &lt;em&gt;integration tests&lt;/em&gt;. The goal here is not to check all the edge cases but rather the assumptions made about the various APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Case Tests
&lt;/h3&gt;

&lt;p&gt;These end-to-end tests make sure that a user scenario works as intended. &lt;em&gt;Use case tests&lt;/em&gt; typically include roundtrips to the database and virtually no mocks. They are good candidates for &lt;a href="https://www.youtube.com/watch?v=Qe84jbwyZ3U&amp;amp;t=47s"&gt;double-loop TDD&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Orchestration Tests
&lt;/h3&gt;

&lt;p&gt;Unless use case tests, &lt;em&gt;orchestration&lt;/em&gt; tests rely heavily on mocks. They are relevant for pieces of code that will just enforce a workflow and call other dependencies. For instance, you may want to check that the code triggers a method call when some conditions are met. I usually make sure to keep these tests are extremely lean as too many mocks can seriously impede refactoring.&lt;/p&gt;

&lt;h3&gt;
  
  
  Developer Experience (DX) Tests
&lt;/h3&gt;

&lt;p&gt;As I always say to everyone willing to listen to me, tests do not only check your code’s behaviour, but also its design and usability. &lt;em&gt;DX&lt;/em&gt; tests are extremely valuable when you are creating an API and want to verify that its usage makes sense and is straightforward. These tests should treat the API as a black box and make absolutely no assumptions about its internal behaviour.&lt;/p&gt;

&lt;h3&gt;
  
  
  Crutch Tests
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;crutch&lt;/em&gt; tests are particularly useful when you want to tackle messy legacy code and refactor it. The objective is to extract a chunk of code, test it from the outside and rewrite it entirely without touching the tests at all. We can readily notice that these tests will typically include roundtrips to the database. The main challenge of these tests is to find &lt;a href="https://biratkirat.medium.com/working-effectively-with-legacy-code-changing-software-part-1-chapter-4-b997b78fc0a2"&gt;seams&lt;/a&gt; to separate the code you want to rewrite from the rest. These seams are a good place to put mocks in place and inject data in the system under test. Once the refactoring is over, some of these tests might become redundant and can be removed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Confidence Tests
&lt;/h3&gt;

&lt;p&gt;We often read that we should not test third-party libraries because they are already massively tested and it’s not our place to do it. While this may be true, I think it can be useful to test our &lt;em&gt;assumptions&lt;/em&gt; about the libraries we use. The &lt;em&gt;confidence&lt;/em&gt; tests make sure you understand how an external library works an ensure that it won’t change its behaviour under you (e.g. during an upgrade). By the way, these tests can also apply to your own legacy code!&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation Tests
&lt;/h3&gt;

&lt;p&gt;We should always thrive to write the simplest code possible. However, it is sometimes not possible and we have to slip some hacky hacks into the codebase (e.g. optimisations, libraries or language shortcomings, etc.). In this case, I like to write &lt;em&gt;documentation&lt;/em&gt; tests that will explain the code’s behaviour so that other developers (or me, in a week) can understand what it does.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scratch Tests
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Scratch&lt;/em&gt; tests are short-lived tests that I sometimes use to check an assumption I have about the code. I use these tests to ask the code questions and see what is its answer. I typically delete them as soon as my assumption is verified or discarded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;p&gt;For years, I struggled with the granularity of my tests because I had the feeling to write some tests for the sake of it. Also, I was refraining myself from writing other tests that would have been useful, because they did not fit in any of the boxes that the test pyramid provides.&lt;/p&gt;

&lt;p&gt;When I started to step aside this test pyramid, I realised that I did not care that much about the number of tests or the balance between unit, integration and end-to-end tests. I just write them when I feel that they could bring something to the table.&lt;/p&gt;

&lt;p&gt;Also, I noticed that I am using way less mocks than I used to because I don’t force myself to write “pure” unit tests anymore. As a consequence, I feel that tests enable refactoring instead of impeding it.&lt;/p&gt;

</description>
      <category>testing</category>
    </item>
    <item>
      <title>The Blind Golden Master</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 01 May 2020 21:46:38 +0000</pubDate>
      <link>https://dev.to/rnowif/the-blind-golden-master-67h</link>
      <guid>https://dev.to/rnowif/the-blind-golden-master-67h</guid>
      <description>&lt;p&gt;As part of my work, I regularly have to refactor/rewrite some part of our system that has little test coverage, if any. Most of the time, the code is too big or complex to write a test suite comprehensive enough to give me the confidence I need in a timely manner. Consequently, I have come to use a characterisation test technique that I called the “Blind Golden Master”.&lt;/p&gt;

&lt;h2&gt;
  
  
  Characterisation Tests
&lt;/h2&gt;

&lt;p&gt;If you know what a characterisation test is, you can skip this section. If not, bear with me for a moment. A characterisation test aims to check that an already existing piece of code always keeps &lt;strong&gt;the same behaviour&lt;/strong&gt; throughout the changes you make. This behaviour does not need to be correct - if there is a bug in the code, these tests will check that it is still here at the end! Characterisation tests are then extremely useful during refactoring efforts since it is not necessary to understand (or even read) the code to write them.&lt;/p&gt;

&lt;p&gt;One of the most usual characterisation test techniques is the Golden Master. The idea is to exercise the code and record its output somehow (e.g. in a file). Then, the test will exercise the code with the same input and check the output against the previously recorded one - the golden master. See &lt;a href="https://dev.to/rnowif/trivia-kata-a-refactoring-story-4bb9"&gt;here&lt;/a&gt; for an example of this technique.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Blind Golden Master
&lt;/h2&gt;

&lt;p&gt;Sometimes, it is impractical to store the output of the code - or we simply couldn’t be bothered to do it. Sometimes, the new implementation and the legacy one have to co-exist (and evolve) for a while and the only goal is to check that they always both behave the same.&lt;/p&gt;

&lt;p&gt;With this technique, the test simply calls both the new and the legacy code and assert that their output is identical. This approach is extremely effective when the method has a limited set of parameters and no side effect.&lt;/p&gt;

&lt;p&gt;For instance, I used it to change the template engine for our codebase (C#).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;newResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_newEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;legacyResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_legacyEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;templateName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;Minify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newResult&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;BeEquivalentTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Minify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;legacyResult&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I also used it to change an algorithm that determines the file extension of a document (see the Java codebase &lt;a href="https://github.com/rnowif/refactoring-with-tests/tree/refacto/socrates"&gt;here&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;FileExtension&lt;/span&gt; &lt;span class="n"&gt;legacyValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getExtension&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;FileExtension&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getExtensionV2&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;legacyValue&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the algorithm is crucial to the application and there are a lot of potential combinations to test, it is even possible to run the test in production: every time the code is called, it can invoke both the algorithms, compare their output and raise an error if they don’t match, and always return the legacy result. When there is no more error, it is time to switch to the new implementation. This &lt;a href="https://github.blog/2015-12-15-move-fast/"&gt;article&lt;/a&gt; explains how GitHub rewrote its merge algorithm using this technique.&lt;/p&gt;

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

&lt;p&gt;The Blind Golden Master is an extremely simple technique that provides confidence during refactoring without having to spend hours writing tests for the legacy code. It is a powerful enabler for improvement that does not require any specific tooling or overhead. Since I started using it a few years ago, it has proven a precious addition to my testing toolbox.&lt;/p&gt;

</description>
      <category>test</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>ASP.Net Clean Architecture</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Sun, 21 Oct 2018 21:04:23 +0000</pubDate>
      <link>https://dev.to/rnowif/aspnet-clean-architecture-4eap</link>
      <guid>https://dev.to/rnowif/aspnet-clean-architecture-4eap</guid>
      <description>&lt;p&gt;When creating a new project, it is always a challenge to design a clean, coherent and modular architecture. There are guidelines out there to help us achieve this goal but the implementation is not always straightforward. In this blog post, I will propose an implementation of Uncle Bob’s &lt;a href="https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html"&gt;Clean Architecture&lt;/a&gt; on an ASP.Net project. The source code of this project can be found on my &lt;a href="https://github.com/rnowif/Expenses"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The main idea of clean architecture is to reduce the coupling between the core business code and the external world (Web, Database, Frameworks). In order to do that, the project can be divided into 3 main modules, that will be described below: &lt;code&gt;Domain&lt;/code&gt;, &lt;code&gt;Web&lt;/code&gt; and &lt;code&gt;Data&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Domain
&lt;/h2&gt;

&lt;p&gt;This module contains all the core business code. It does not depend on anything else than the .NET SDK and contains sub-modules.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Entities&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;They are the building blocks of the domain and encapsulate the business concepts. Entities are not coupled with any ORM framework. Indeed the domain model can be quite different from the database model!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Use Cases&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Following Uncle Bob’s definition, they contain application specific business rules and orchestrate the flow of data to and from the entities and implement higher level business rules.&lt;/p&gt;

&lt;p&gt;It is important to understand that the domain model should not leak outside of this module. In order to do that, use cases should not take entities are arguments for their methods, but a list of raw arguments. For instance, the use case that submits a new expense looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ISubmitExpense&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Guid&lt;/span&gt; &lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;priceWithoutTax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;priceIncludingTax&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is responsible to build an &lt;code&gt;Expense&lt;/code&gt; object and apply the relevant business logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Repositories&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In the domain module, repositories are only interfaces that are used by the use cases to access data without any knowledge of the concrete implementations: data can be retrieved from databases, files or external Web APIs, it should not affect the core business logic at all. To reinforce this, repositories take entities as arguments and return entities as well. It is the responsibility of the concrete implementation to handle conversion if needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Data
&lt;/h2&gt;

&lt;p&gt;The data module contains the database and ORM configuration and the repositories implementations. Typically, we find the EntityFramework configuration in this module as well as the annotated classes that will be mapped with database entries. Repositories implementation are responsible for converting “database objects” in domain objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Expense&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Convert the domain object "Expense" into a database object "DbExpense"&lt;/span&gt;
  &lt;span class="n"&gt;_dbContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Expenses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DbExpense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromExpense&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="n"&gt;_dbContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveChanges&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This prevents the ORM framework to leak into the domain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web
&lt;/h2&gt;

&lt;p&gt;The web module contains all the controllers. It is responsible for handling HTTP requests, converting JSON or XML payloads to objects and invoking use cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;HttpPost&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SubmitExpense&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;FromBody&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;SubmitExpenseCommand&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// The controller just invoke the use case with data extracted from the body of the HTTP request&lt;/span&gt;
  &lt;span class="n"&gt;_submitExpense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PriceWithoutTax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expense&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PriceIncludingTax&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It should not contain any business logic whatsoever. As a consequence, controllers are very lightweight and easy to test.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency injection
&lt;/h2&gt;

&lt;p&gt;In order to achieve low coupling between the modules, interfaces are injected into the constructor of the different classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SubmitExpense&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ISubmitExpense&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IExpenseRepository&lt;/span&gt; &lt;span class="n"&gt;_repository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;SubmitExpense&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IExpenseRepository&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_repository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The plumbing is handled by the &lt;code&gt;Startup.cs&lt;/code&gt; class where all the implementations of the interfaces are declared:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ConfigureServices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IServiceCollection&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;

  &lt;span class="c1"&gt;// Register "SubmitExpense" as the implementation of "ISubmitExpense"&lt;/span&gt;
  &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ISubmitExpense&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SubmitExpense&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddTransient&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IExpenseRepository&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ExpenseRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;p&gt;In this architecture, the emphasis is put on the domain. Every other module should adapt to it but it does not depend on anything. This results in lightweight classes that are very easy to understand and test in isolation.&lt;br&gt;&lt;br&gt;
Moreover, the low coupling makes it very easy to change the infrastructure. Indeed, the domain module would not change a bit if we decided to have a CLI instead of a Web App or if the data should be retrieved from an external Web API instead of a PostgreSQL database.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>design</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>C# for the Java Developer: Enums</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Sat, 20 Oct 2018 17:54:27 +0000</pubDate>
      <link>https://dev.to/rnowif/c-for-the-java-developer-enums-2864</link>
      <guid>https://dev.to/rnowif/c-for-the-java-developer-enums-2864</guid>
      <description>&lt;p&gt;Enums is a very common concept. It exists, of course, in Java and C# as well. However, Java and C# enums do not have the same capabilities. This blog post aims to show their differences.&lt;/p&gt;

&lt;p&gt;In Java, enums are very much like regular classes: they can implement interfaces and have methods. However, they cannot inherit other classes or be explicitly instantiated. They can be viewed as &lt;code&gt;final&lt;/code&gt; classes (or &lt;code&gt;sealed&lt;/code&gt; classes in C#) that already inherit the virtual “enum” class, only have private constructor(s) and a set of pre-defined instances (the values of the enum).&lt;/p&gt;

&lt;p&gt;For instance, let’s take the example of the HTTP status codes. In Java, it is possible to write this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="no"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;SERVER_ERROR&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// This is a regular instance method&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// This is the implementation of the Comparable interface&lt;/span&gt;
  &lt;span class="nd"&gt;@Override&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;compareTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Http statuses will be sorted by status code&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;compare&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;HttpStatus&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpStatus&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;OK&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The 'code' method can be invoked like any other method&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In C#, enums are just integers in disguise. The previous snippet can be simulated in C# only because the &lt;code&gt;code&lt;/code&gt; attribute happens to be an &lt;code&gt;int&lt;/code&gt;. Otherwise, it would be very complex to have the same behaviour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;HttpStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// int value of the enum can be forced to a specific value&lt;/span&gt;
  &lt;span class="n"&gt;OK&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;NOT_FOUND&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;SERVER_ERROR&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;500&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;HttpStatus&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// The enum can be casted to an int to get its value&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To sum up, Java enums are much more powerful than their C# counterparts. I often use these features when I write Java code and I think I would miss them if I had to write C# code on a daily basis.&lt;/p&gt;

</description>
      <category>java</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# for the Java Developer: Lambdas</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Sat, 20 Oct 2018 11:37:12 +0000</pubDate>
      <link>https://dev.to/rnowif/c-for-the-java-developer-lambdas-1b6o</link>
      <guid>https://dev.to/rnowif/c-for-the-java-developer-lambdas-1b6o</guid>
      <description>&lt;p&gt;A lambda is an anonymous function that can be assigned to a variable, passed as an argument of a method and invoked at any time. We can find lambdas in Java and C# and the resulting code is very similar. A Java lambda can be viewed as the implementation of an interface with only one method (called a &lt;em&gt;functional interface&lt;/em&gt;) whereas a C# lambda can be assigned to a &lt;code&gt;delegate&lt;/code&gt;, which is a concept that does not exist in Java. This article aims to explain how lambdas work in Java and C# and highlight their differences and similarities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Java Functional Interfaces
&lt;/h2&gt;

&lt;p&gt;In Java, a lambda can simply be viewed as an anonymous function that implements an interface with only one method. This kind of interface is called a &lt;em&gt;functional interface&lt;/em&gt; and can be annotated with the &lt;code&gt;@FunctionalInterface&lt;/code&gt; annotation that tells the compiler to enforce the only-one-method rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The function interface is implemented with a lambda&lt;/span&gt;
&lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="n"&gt;standardPrinter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// The print method of standardPrinter can be invoked&lt;/span&gt;
&lt;span class="n"&gt;standardPrinter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the previous snippet, you should note that the way the interface is implemented makes absolutely no difference. It can be a lambda, a concrete class or even an anonymous class. Anyways, you can simply invoke the &lt;code&gt;print&lt;/code&gt; method of the interface.&lt;/p&gt;

&lt;p&gt;There are some pre-defined &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html"&gt;generic functional interfaces&lt;/a&gt; in the JDK that can be used directly off the shelf. The main ones are described below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html"&gt;&lt;code&gt;Function&amp;lt;T,R&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;R apply(T t)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and returns an object of type R&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html"&gt;&lt;code&gt;Consumer&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;void accept(T)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and does something useful (typically with side effects)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html"&gt;&lt;code&gt;Predicate&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;boolean test(T)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and returns a boolean (similar to &lt;code&gt;Function&amp;lt;T, Boolean&amp;gt;&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html"&gt;&lt;code&gt;Supplier&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;T get()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns an object of type T&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  C# Delegates
&lt;/h2&gt;

&lt;p&gt;In C#, a lambda can be assigned to a delegate which is a type that encapsulates a method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;delegate&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// A lambda is assigned to the delegate&lt;/span&gt;
&lt;span class="n"&gt;Print&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// print can be directly invoked as a method&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like in Java, some delegates are already defined in the .NET framework, the main ones are described below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.func-2"&gt;&lt;code&gt;TResult Func&amp;lt;in T,out TResult&amp;gt;(T arg)&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and returns an object of type TResult&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.action-1"&gt;&lt;code&gt;void Action&amp;lt;in T&amp;gt;(T obj)&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and does something useful (typically with side effects)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.predicate-1"&gt;&lt;code&gt;bool Predicate&amp;lt;in T&amp;gt;(T obj)&lt;/code&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Takes an argument of a given type T and returns a boolean (similar to &lt;code&gt;Func&amp;lt;T, bool&amp;gt;&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;From a Java perspective, using lambdas in APIs (like Linq for instance) is pretty straightforward. However, when digging a little bit deeper, there are some subtle differences to understand. I find the Java approach simpler as it does not introduce another concept but the C# approach is cleaner because the delegate can be invoked directly like a method.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# for the Java Developer: Generics</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 19 Oct 2018 16:57:36 +0000</pubDate>
      <link>https://dev.to/rnowif/c-for-the-java-developer-generics-1c72</link>
      <guid>https://dev.to/rnowif/c-for-the-java-developer-generics-1c72</guid>
      <description>&lt;p&gt;In my journey into the C# world, I wanted to talk about generics. Generics exist in both Java and C# languages but their implementation is &lt;em&gt;very&lt;/em&gt; different. This blog post aims to explain the differences and similarities between the two.&lt;br&gt;&lt;br&gt;
TL;DR Java generics are a lie, C# generics are not.&lt;/p&gt;


&lt;h2&gt;
  
  
  Java Generics are a Lie
&lt;/h2&gt;

&lt;p&gt;Generics have been introduced in Java 5. Before that, you had to manipulate &lt;code&gt;Objects&lt;/code&gt; and cast them to the desired type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Apple&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// This is really a list of objects, so the cast is required&lt;/span&gt;
&lt;span class="nc"&gt;Apple&lt;/span&gt; &lt;span class="n"&gt;firstApple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Apple&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the previous snippet, you should note that there is absolutely nothing that prevents you from adding a &lt;code&gt;Banana&lt;/code&gt; into the list and make the program crash at runtime. Also, this code is still valid in the latest version of Java (which is Java 11 as we speak). To say the least, this approach is not very safe and do not leverage the type system as much as we could expect.&lt;/p&gt;

&lt;p&gt;Since Java 5, it is then possible to use the generic version of the &lt;code&gt;List&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Apple&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Apple&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Apple&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;// The cast is not required any more thanks to the generics&lt;/span&gt;
&lt;span class="nc"&gt;Apple&lt;/span&gt; &lt;span class="n"&gt;firstApple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The thing is that, at runtime, the two previous snippets are strictly equivalent. Indeed, Java generics are removed during compilation and the resulting bytecode only manipulate &lt;code&gt;Objects&lt;/code&gt; and casts. This is called &lt;em&gt;type erasure&lt;/em&gt;. As a consequence, it is not possible, in Java, to write code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Filter objects of the given type
 */&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;filterObjectsOfType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Indeed, the type of &lt;code&gt;T&lt;/code&gt; is erased at runtime and the &lt;code&gt;instanceof&lt;/code&gt; operation cannot be performed. That is why a class object is often passed as an argument of the method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Filter objects of the given type
 */&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;filterObjectsOfType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;clazz&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;clazz&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method can be invoked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stringsOnly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filterObjectsOfType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// stringsOnly contains only "hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  C# Generics is a Runtime Feature
&lt;/h2&gt;

&lt;p&gt;C# generics, on the other hand, is a totally different beast. Indeed, the real type is kept at runtime and it is possible to use this type to write this kind of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Filter objects of the given type
 */&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FilterObjectsOfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;filteredObjects&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method can be invoked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;stringsOnly&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FilterObjectsOfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="c1"&gt;// stringsOnly contains only "hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In Java, generics are used to write type-safe code but this feature is limited and the code can be awkward sometimes due to the type erasure mechanism. In C#, it is a runtime feature and its usage is much more straightforward.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PS: I realize that the code I wrote in the snippets is not really idiomatic but I wanted to have Java and C# code as similar as possible to be able to focus only on the usage of generics.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>C# for the Java Developer: Extension Methods</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 19 Oct 2018 12:20:24 +0000</pubDate>
      <link>https://dev.to/rnowif/c-for-the-java-developer-extension-methods-n69</link>
      <guid>https://dev.to/rnowif/c-for-the-java-developer-extension-methods-n69</guid>
      <description>&lt;p&gt;After spending several years crafting Java code, I recently decided to dive back into C# and share what I learn in the process. In this blog post, I will talk about extensions methods. This concept, which exists in some JVM languages (like &lt;a href="https://kotlinlang.org/docs/reference/extensions.html"&gt;Kotlin&lt;/a&gt;) but not in Java, let the developer add methods to a class without touching its code (hence the name &lt;em&gt;extension&lt;/em&gt; methods).&lt;/p&gt;

&lt;p&gt;Let’s say you want to create a method that puts the first letter of a string in upper case and returns the new string. You cannot modify (or even inherit) the &lt;code&gt;String&lt;/code&gt; class in Java to add this method. Since you don’t necessarily want to create your own custom flavour of the &lt;code&gt;String&lt;/code&gt; class for your code base, you would probably create a static function that takes a &lt;code&gt;String&lt;/code&gt; as an argument and return another &lt;code&gt;String&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringExtensions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can simply invoke this function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;capitalizedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StringExtensions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, creating a static method is also possible in C#. However, C# has a feature that allows adding new methods to a class without having to modify its code. This feature is called &lt;em&gt;extension methods&lt;/em&gt;. In order to create an extension method, you have to create a top-level static class and implement a static method. The first argument of this method specifies the type that the method operates on and should have the &lt;code&gt;this&lt;/code&gt; modifier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringExtensions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Note that this cannot access any private data in the String class. &lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToUpper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to use it, the namespace that contains the class must be specified with a &lt;code&gt;using&lt;/code&gt; directive. Afterwards, the method can be invoked as if it was an instance method of the type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;capitalizedWord&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Capitalize&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extension methods are very powerful. Besides the fact that the code looks “cleaner”, it is also possible to seamlessly plug new behaviours in existing types by simply importing a namespace. The extension code can be in separate specific modules that can be imported only when needed.&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>csharp</category>
    </item>
    <item>
      <title>What is a Volatile Variable in Java?</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Fri, 21 Sep 2018 10:13:33 +0000</pubDate>
      <link>https://dev.to/rnowif/what-is-a-volatile-variable-in-java-35ef</link>
      <guid>https://dev.to/rnowif/what-is-a-volatile-variable-in-java-35ef</guid>
      <description>&lt;p&gt;The &lt;code&gt;volatile&lt;/code&gt; keyword is one of the less known and less understood keywords of the Java language. The goal of this article is to explain what it is and when to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory Architecture
&lt;/h2&gt;

&lt;p&gt;In order to understand the value of &lt;code&gt;volatile&lt;/code&gt;, one must first understand the memory architecture of a computer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LawCJKjx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/images/memory_architecture.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LawCJKjx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://rnowif.github.io/images/memory_architecture.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each CPU contains its own registers, which are basically in-CPU memory. Accessing these registers and performing operations on variables here is very fast. Each CPU also has cache memory layers. It can access this cache memory fast, but not as fast as the registers. Finally, there is the main memory (also called RAM). All CPUs can access this memory. The main memory is much bigger than the cache memories of the CPU.&lt;/p&gt;

&lt;p&gt;Typically, when a CPU needs to read something from the memory, it will read it into its CPU cache memory and perform operations on it. It can even read it into its registers. When the CPU needs to write it back to the memory, it will flush its registers to the cache memory and eventually, this cache memory will be flushed back to the main memory.&lt;/p&gt;

&lt;p&gt;In general, the flush is performed when the CPU needs to make room for other information. Thus, it seems clear that one cannot make any assumption about &lt;em&gt;when&lt;/em&gt; this flush will occur.&lt;/p&gt;

&lt;h2&gt;
  
  
  Visibility of Shared Variables
&lt;/h2&gt;

&lt;p&gt;If a computer has 2 CPUs or more, it will be able to run several threads at the same time. It means that, if each thread wants to access the same variable, each CPU will have a “copy” of this variable into its cache memory and this could lead to a major synchronization issue.&lt;/p&gt;

&lt;p&gt;Take this code for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyServer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;running&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Do some very interesting stuff...&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is very likely that the &lt;code&gt;stop&lt;/code&gt; method will be called from a different thread than the one executing the &lt;code&gt;run&lt;/code&gt; method. If these threads are executed on 2 different CPUs, the &lt;code&gt;run&lt;/code&gt; method will not stop until the CPU has flushed its cache memory to the main memory. And we saw that there is no way to predict when this will happen.&lt;/p&gt;

&lt;p&gt;In Java, the &lt;code&gt;volatile&lt;/code&gt; keyword explicitly ask for a variable to be directly written to the main memory, and thus becoming instantly available for all CPUs.&lt;/p&gt;

&lt;p&gt;The code then becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyServer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Runnable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;volatile&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;running&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Do some very interesting stuff...&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that a volatile variable can be a primitive or an object and can be &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Use &lt;code&gt;volatile&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A volatile variable is guaranteed to have its last version always available from every CPUs. It is typically used for flags that are modified by a thread and read by another one (like the &lt;code&gt;running&lt;/code&gt; boolean in the previous snippet). Globally, you may use a &lt;code&gt;volatile&lt;/code&gt; variable if the variable may be accessed by several threads and you don’t need to perform a sequence of operations in an atomic manner (you should not increment a &lt;code&gt;volatile&lt;/code&gt; variable for instance). If you need to do so, you should consider using a synchronization mechanism.&lt;/p&gt;

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

&lt;p&gt;The &lt;code&gt;volatile&lt;/code&gt; keyword is seldom used, maybe because messing with threads is frowned upon in a JEE (or Spring) application, but it can be very useful to know it and understand its usage. Indeed, it could make your code safer and prevent you from writing algorithms that rely on locks and synchronization where it’s not really needed.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Intersection Types in Java</title>
      <dc:creator>Renaud Humbert-Labeaumaz</dc:creator>
      <pubDate>Wed, 19 Sep 2018 09:29:15 +0000</pubDate>
      <link>https://dev.to/rnowif/intersection-types-in-java-1jk8</link>
      <guid>https://dev.to/rnowif/intersection-types-in-java-1jk8</guid>
      <description>&lt;p&gt;This blog post aims to explain how we can use intersection types in Java when we expect an object that implements different interfaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface Segregation Principle
&lt;/h2&gt;

&lt;p&gt;The Interface Segregation Principle (ISP) stipulates that interfaces should contain the least amount of methods as possible. In other terms, a client of an interface should use all the methods of this interface.&lt;/p&gt;

&lt;p&gt;For instance, let’s take this &lt;code&gt;File&lt;/code&gt; interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface File {
  Collection&amp;lt;String&amp;gt; readLines();
  void write(String line);
  void deleteFile();
}

class LocalFile implements File {
  // ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As a client of this interface, it is highly unlikely that I need all methods. I may want to just read the file and, in that case, I certainly don’t want to be able to delete it. If I just want to delete the file, I probably don’t want to read all its lines.&lt;/p&gt;

&lt;p&gt;In order to avoid that, it is a good idea to split this interface into 3 separate ones:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface FileReader {
  Collection&amp;lt;String&amp;gt; readLines();
}

interface FileWriter {
  void write(String line);
}

interface FileDestroyer {
  void deleteFile();
}

class LocalFile implements FileReader, FileWriter, FileDestroyer {
  // The concrete class can implement all 3 interfaces
  // ...
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, a client can just require the interface it needs and ignore the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface Combination
&lt;/h2&gt;

&lt;p&gt;Writing tiny interfaces is good to enforce ISP and lower the coupling of the code. However, what happens when a client wants to read a file &lt;em&gt;and&lt;/em&gt; write at the same time?&lt;/p&gt;

&lt;p&gt;The first two snippets won’t compile because one of the interfaces is not implemented:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void readAndWrite(FileReader reader) {
  reader.readLines();
  reader.write("Hello"); // That won't compile since reader does not implement FileWriter
}

void readAndWrite(FileWriter writer) {
  reader.readLines(); // That won't compile since writer does not implement FileReader
  reader.write("Hello");
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As an alternative, it is possible to pass an instance of &lt;code&gt;LocalFile&lt;/code&gt; but it introduces a high coupling between the method and the &lt;code&gt;LocalFile&lt;/code&gt; concrete class, defeating the whole purpose of interfaces entirely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void readAndWrite(LocalFile file) {
  file.readLines();
  file.write("Hello");
  // That will compile but it is not recommended
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since Java 1.5, and the introduction of generics, a feature, known as Intersection Types, allows to combine interfaces in this kind of situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intersection Types to the Rescue
&lt;/h2&gt;

&lt;p&gt;The following code uses intersection types to solve the issue of needing an object that implements several interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;T extends FileReader &amp;amp; FileWriter&amp;gt; void readAndWrite(T file) {
  file.readLines();
  file.write("Hello");   
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;amp;&lt;/code&gt; symbol means that the method expects a type &lt;code&gt;T&lt;/code&gt; that implements both the &lt;code&gt;FileReader&lt;/code&gt; and &lt;code&gt;FileWriter&lt;/code&gt; interfaces.&lt;/p&gt;

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

&lt;p&gt;The intersection types is a feature that is not widely used in Java. However, it is very powerful as it allows to write very tiny interfaces and combine them on demand. From now on, there is no excuse to write big fat interfaces that have dozens of totally unrelated methods!&lt;/p&gt;

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