Using C# 7 and Succinc<T> to give F# a run for its money

Yesterday, I saw on twitter a link to a blog post entitled “An F# rewrite of a fully refactored C# Clean Code example”, by Roman Bossart. The F# code is compact and easily understood. So I was curious as to what the “fully refactored C# Clean Code” looked like and followed the links therein to have a look.

Continue reading “Using C# 7 and Succinc<T> to give F# a run for its money”

C# 7, “out var” and changing variable scope

C# 7 is due to be released along with Visual Studio 2017, which is currently available as an “RC” release. I had previously regarded it as a thoroughly unexciting “meh” release, for – beyond new syntactic tuple features – the changes were minor and not really worth getting excited (or even angry, depressed etc) about. Then just before that RC release, the language team made a change to the way the out var feature works. And suddenly that all changed. This minor feature has become a significant change to the language. And many people aren’t at all happy about it.

Continue reading “C# 7, “out var” and changing variable scope”

Why declarative programming is often better than imperative, even in C#

SuccincTIf you are a C# programmer, the chances are, you use an imperative approach to coding. You may have heard of the declarative (or functional) programming approach offered by F#, but are you aware that the same approach can be used in C# too? This article attempts to show how C# can be used in this way, and to explain why it often leads to shorter, simpler solutions to any programming requirements.

Continue reading “Why declarative programming is often better than imperative, even in C#”

Arnolyzer: adding clean-code static analysis to C# in VS2015

arnolyzer-logoArnolyzer is a Roslyn-based analyzer for Visual Studio 2015 that provides a set of compiler rules that encourage modern, functional-orientated, coding standards in C# 6. Pure functions; no inheritance; no global state; immutable data classes and variables; and short, concise sections of code.
Continue reading “Arnolyzer: adding clean-code static analysis to C# in VS2015”

Generic variance in C#, part 3 – Covariance

csharp_genericsIn part 2 of this series, we looked at contravariance, which is a special case of generic variance, for interfaces that only allow instances of T, for ISomeInterface<T>, to be passed in, either via method parameters or through property setters (though good code will not contain the latter, of course). It’s perhaps no surprise that covariance is the opposite. Only when ISomeInterface<T> allows instances of T to be passed out (either via method returns, or property getters), can that interface be covariant.
Continue reading “Generic variance in C#, part 3 — Covariance”

Posted in C#

Generic variance in C#, part 2 – Contravariance

csharp_genericsContravariance is perhaps the hardest of the three types of generic variance to understand (at least I found it so). This article hopefully takes the reader through two examples of its use to explain what it is, before showing how it’s used for real in the .NET Framework.

This is part 2 of a three part series on generic variance in C#. Part 1, covers invariance. The final part, covers covariance.
Continue reading “Generic variance in C#, part 2 — Contravariance”

Posted in C#

Generic variance in C#, part 1 – Invariance

csharp_genericsC#’s generic types are, by default, invariant. Special cases of covariant and contravariant can be defined though. So what does that previous gobbledygook even mean? Hopefully this three-part article will help explain these three terms, and why they even exist in the first place.

The second part of this series, looks at what is probably the least well understood of the three terms: contravariance. If you feel you understand the other two terms, feel free to jump straight to that part therefore. The third, and last, part, looks at covariance and finishes with a summary of the difference between the three. But first, we start at the beginning: invariance.
Continue reading “Generic variance in C#, part 1 — Invariance”

Posted in C#

Using Succinc<T> to overcome IEnumerable code smells

SuccincT
The IEnumerable<T> interface has numerous extension methods associated with it, including a number that return a single element if a match occurs, or return the default value for the type if that fails: FirstOrDefault, LastOrDefault, SingleOrDefault and ElementAtOrDefault. These methods create two code smells due to the nature of .NET’s type system. As of version 1.5.0, Succinc<T> offers alternatives to these methods that overcome those smells.
Continue reading “Using Succinc<T> to overcome IEnumerable code smells”

An example of a benign Service Locator

In my last post, I examined whether or not the Service Locator is an anti-pattern. The conclusion was that it isn’t. Instead it’s a code smell that all too often causes another anti-pattern to occur: hindering easy testing.

One of the reasons cited for why it’s only a code smell is due to edge cases where a service locator can be used internally within an assembly as an implementation detail that doesn’t affect testing. It’s natural to ask what such a test-friendly service locator might look like. The Visual Studio/MSBuild and .NET framework combination supplies such a locator: .resx files and the ResourceManager class.
Continue reading “An example of a benign Service Locator”

Do you SHOF? Higher order functions in OO languages

If you live in the world of OO languages, you likely fall into one of two categories:

  1. You are still happily using inheritance, switch statements, nested if/else collections, have methods that are hundreds of lines long etc. You possibly haven’t heard of TDD or DI. There’s a very good chance that, if this is the case, you’ll likely never see this post as you likely do not read blogs by other developers.
  2. You do read around, you are aware of why inheritance, switch, deeply nested and long code blocks are frowned upon by the well-informed. You are at least aware of TDD, even if you haven’t quite started using it yet. Also, you are likely aware of the growing excitement around functional languages and perhaps look on with envy at some of their features. If this is you, you possibly already use SHOF’s without knowing it.

Continue reading “Do you SHOF? Higher order functions in OO languages”