<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.3.4">Jekyll</generator><link href="https://valber.net/feed.xml" rel="self" type="application/atom+xml" /><link href="https://valber.net/" rel="alternate" type="text/html" /><updated>2025-10-21T23:21:24+02:00</updated><id>https://valber.net/feed.xml</id><title type="html">My two cents</title><subtitle>Valber's take on software development and the world in general.</subtitle><entry><title type="html">Why I don’t like extension methods (the way it is)</title><link href="https://valber.net/c%23/.net/aspnet/extension/methods/functional/2022/07/02/why-i-dont-like-extension-methods-the-way-it-is.html" rel="alternate" type="text/html" title="Why I don’t like extension methods (the way it is)" /><published>2022-07-02T10:30:55+02:00</published><updated>2022-07-02T10:30:55+02:00</updated><id>https://valber.net/c%23/.net/aspnet/extension/methods/functional/2022/07/02/why-i-dont-like-extension-methods-the-way-it-is</id><content type="html" xml:base="https://valber.net/c%23/.net/aspnet/extension/methods/functional/2022/07/02/why-i-dont-like-extension-methods-the-way-it-is.html"><![CDATA[<p>Back in the day and since then, C# 3.0 introduced a new syntactic sugar feature called extension methods. The idea is to be able to call a static method which defines as its first parameter an object of a certain type as though this static method were actually an instance method of that object.</p>

<p>So, instead of having to write</p>

<p><code class="language-plaintext highlighter-rouge">Foo(obj, arg1);</code></p>

<p>you’d write</p>

<p><code class="language-plaintext highlighter-rouge">obj.Foo(arg1);</code></p>

<p>the method’s signature being, say</p>

<p><code class="language-plaintext highlighter-rouge">public static string Foo(this MyClass obj, string arg1);</code></p>

<p>This is really a big deal for generic fluent APIs like LINQ. And since both features came with the same major version of C#, extensions methods were most probably introduced as a means of making LINQ (more specifically: LINQ extension methods) feasible and enjoyable in C#.</p>

<p>At first, this seems harmless and even a great idea, if it weren’t for a small detail. And I could only notice it after I learned about a specific feature in F#.</p>

<h2 id="so-what-my-issue-with-extension-methods-is">So what my issue with extension methods is</h2>

<p>The intention of improving the syntax of calling static methods is something I actually appreciate. What I don’t like about extension methods is how this intention was implemented.</p>

<p>Not being able to tell a legit instance method from a foreign static method can be at times (or even often) annoying and time-consuming. It also decreases the expressiveness of the language since whenever you’re reading a method call you have to ask yourself: “<em>ok, but what am I <strong>really</strong> looking at there: is this an instance method or an extension method?</em>”</p>

<p>The remedy to this disadvantage is really simple and kind of obvious, to be honest: the syntax for calling extension methods should be slightly different than the syntax for calling a normal instance method!</p>

<p>For comparison, in many functional programming languages, like in F#, there’s a syntax feature called “pipe” which basically accomplishes the goal of extension methods.</p>

<p>So, in F# a code analogous to the above C# example would be:</p>

<p><code class="language-plaintext highlighter-rouge">obj |&gt; foo arg1</code></p>

<p>the function’s signature being, say</p>

<p><code class="language-plaintext highlighter-rouge">foo: string -&gt; MyClass -&gt; string</code></p>

<p>which reads: “foo is a function that takes a string and then takes a MyClass in order to yield a string”</p>

<p>It means you could write the following F# snippet to the same effect of using the pipe as in the above example</p>

<p><code class="language-plaintext highlighter-rouge">foo arg1 obj</code></p>

<p>The pipe operator does a very simple job: it makes the function on its right (<em>foo</em>) use the value on its left (<em>obj</em>) as the function’s next argument. So, you just rearrange the argument in the call.</p>

<p>The pipe operator is therefore a syntactic sugar, just like extension methods in C#, but it doesn’t try to mislead you into thinking it’s an instance method of that object.</p>

<p>By the way, in F# you can also call an object’s instance method and when you do that, you use the dot, just like you would in C#. This is F# syntax for calling an instance method of an object:</p>

<p><code class="language-plaintext highlighter-rouge">obj.MyLegitMethod(arg2)</code></p>

<h2 id="alternative-and-wrapping-it-up">Alternative and wrapping it up</h2>

<p>An alternative to the current extension methods syntax doesn’t have to be pipes, especially because people with some experience in functional programming would then also expect C# to provide built-in currying (a.k.a higher order functions, a.k.a partial functions), it just takes an alternative to the standard “dot” syntax. I’d be fine with, say:</p>

<p><code class="language-plaintext highlighter-rouge">obj..Foo(arg1)</code></p>

<p>Or even:</p>

<p><code class="language-plaintext highlighter-rouge">obj-&gt;Foo(arg1)</code></p>

<p>most important: it just has to be different from obj.Foo(arg1).</p>

<p>This late change probably will never come, given that extension methods have been around for a long while, are already an established syntax and most people don’t even realize it’s not an optimal solution, but I hope this post makes you curious about learning more about functional programming and its clever features.</p>]]></content><author><name></name></author><category term="c#" /><category term=".net" /><category term="aspnet" /><category term="extension" /><category term="methods" /><category term="functional" /><summary type="html"><![CDATA[Back in the day and since then, C# 3.0 introduced a new syntactic sugar feature called extension methods. The idea is to be able to call a static method which defines as its first parameter an object of a certain type as though this static method were actually an instance method of that object.]]></summary></entry><entry><title type="html">Beware of Unit Tests</title><link href="https://valber.net/unit/tests/integration/2018/01/20/beware-of-unit-tests.html" rel="alternate" type="text/html" title="Beware of Unit Tests" /><published>2018-01-20T17:25:55+01:00</published><updated>2018-01-20T17:25:55+01:00</updated><id>https://valber.net/unit/tests/integration/2018/01/20/beware-of-unit-tests</id><content type="html" xml:base="https://valber.net/unit/tests/integration/2018/01/20/beware-of-unit-tests.html"><![CDATA[<p><em>Test-driven development is a well-proven best practice and it has come to stay. However, it is by no means a panacea and too many unit tests may even be prejudicial to a big project. Besides, I see the risk that too much attention may be given to them at the expense of other best practices and even other types of tests.</em></p>

<p>It has been a while since unit tests and test-driven development have hit mainstream. The idea is very tempting: write a small snippet of runnable code that tests a very basic functionality your system is supposed to offer before implementing this functionality in the first place. Run the test and watch it fail. Then, implement this functionality until, and not beyond that, your test succeeds. Implement your system this way step-by-step, one basic functionality after the other and be happy.
But, for big projects, this has consequences. Code is liability – someone already said – and test code is still code. Code that must be understood (ok, unit tests should be as clean and easy as possible, but it doesn’t always work that way) and maintained. How about when you make a major refactoring in your architecture? It’s a natural process in a system. Requirements change. Sooner or later you’ll have to extract part of the logic of a growing method into another class or even a whole new set of them. Sometimes you’ll remove a functionality which is not needed anymore or simply wrong according to new requirements. And then you’ll frown upon the hundreds of tests you once wrote, which are valuable since they do ensure that some necessary business logic still works. You’ll find yourself either adapting them, if they were really simple, well-written and relating to a good extent to the new implementation, or just throwing them away and trying to define completely new tests that hopefully will have at least the same business logic testing coverage the older ones had.</p>

<p>What about fast implementation? It’s not always easy to write code test-driven for a completely new requirement, because writing tests before production code demands manipulating something which doesn’t exist and this way of thinking, although healthy for exercising the brain and one’s programming practice, may get in the way of an urgent new feature.
At the end of the day, did your unit tests really save your neck? Nice code is beautiful and theoretically scalable, but who cares? In the end, only the results matter and it boils down to:
Can you and your team change your program with less effort?
Has your program proven to be more reliable to its users/customers?
I’m not sure if fastidiously developing test-driven and carefully applying all unit-tests good practices is enough or even necessary to achieve this. If you’re a good programmer, you will take care of writing good programming units, but while doing this, it’s so easy to miss the big picture. Bugs will not arise from your local code, but from the interaction between this local code and other code (communication with services; components; data emerging from user interaction).
Besides, religiously focusing on unit-testability will make you turn down design approaches which are elegant, nice to understand and change, but don’t fit into this wonderful unit-tests utopian world.</p>

<p><em>Unit tests are very close to implementation detail.</em></p>

<h1 id="consider-integration-tests">Consider integration tests</h1>
<p>Contrary to unit tests, integration tests don’t enjoy the same appeal of unit tests. They’re harder to write; take more time to run and depend on many real components (not mock objects). So, even when they fail, the failure cause is not obvious as opposed to unit tests. However, once you set up an integration test, it will take much longer to touch it again compared to unit tests. Removing an integration test altogether will be even rarer. When a new requirement comes into play, you probably will need just to adjust the corresponding integration test by defining a new “arrange”, “act” or “assert” step. If you have a good integration tests base, you have a stronger safety net that gives you a lot of flexibility to change your code.
Think about upgrading the framework you use to a new, state-of-the-art, major version of it. For example, upgrading your ASP.NET MVC 3 application to ASP.NET 1.0 Core. It may even seem straight-forward if you make a careful analysis of the relationships between architectural points of one version of the framework to the other. But, how do you guarantee that everything is working as expected? And ever heard of analysis paralysis?
Think about refactoring a part of the system into a design pattern which makes sense to apply to your code today.
Of course, all these considerations are based on the assumption that your software is expected to have high reliability. By the way, reliability is, in my opinion, the greatest asset of a software. Everybody hates software that crashes or that gets worse with new updates. Everybody also hates slow software etc. But software is a means to an end. And users will love a piece of software which won’t let them down.</p>

<p><em>Integration tests are very close to domain-level requirements.</em></p>

<h1 id="so-is-this-a-integration-tests-vs-unit-tests-question-we-have-here">So is this a integration tests vs unit tests question we have here?</h1>

<p>Yes and no. The point I’m trying to make is not at all that unit tests are useless. Unit tests still have the advantage of being very performant and straight-to-the-point and they can be very helpful and sometimes they really suffice as a single safety net. The point I’m making is that unit tests are not the only game in town as I’ve seen a great deal of software developers suggest, neither they are absolutely necessary in all cases.
In my view, unit tests should only be used to test specific business logic requirements, which is almost always the case for number and string manipulation.
They shouldn’t be used as often to test “code logistics”. Not only that, no team should consider to have a good safety net just because they flooded their codebase with unit tests.
Instead, there should be at least some integration tests covering the most important and/or common use cases and execution paths for the team to feel “safe” and for the production code itself to be receptive for major changes.</p>]]></content><author><name></name></author><category term="unit" /><category term="tests" /><category term="integration" /><summary type="html"><![CDATA[Test-driven development is a well-proven best practice and it has come to stay. However, it is by no means a panacea and too many unit tests may even be prejudicial to a big project. Besides, I see the risk that too much attention may be given to them at the expense of other best practices and even other types of tests.]]></summary></entry></feed>