I am currently in the process of writing a (custom) Minecraft server application in C#. I figured it's a good way to teach me a lot of important things like concurrency and especially memory efficiency (due to simply the vastness of the object space in Minecraft).

While on the train yesterday, I was finding it hard to really "think" of a way to organize the network module. I wanted a simple external interface that would give out the necessaries (Let users react to a connection being received, message being received, and when a connection is dropped). However, I also wanted it to be fairly abstract such that I could allow for either real game clients to connect or simple fake clients.

I started over-engineering things a la IConnection, INetworkModule, IConnectionFactory, IConnectionProtocol, IMessageEncoder, IMessageDecoder and so on. Kingdom of nouns galore. Not only that but I wasn't really getting anywhere - it just felt like I was putting the fundamental problem behind other layers of abstraction in that I had no idea how I wanted to organize the communication between all these modules.

Then I came to the thought - why don't I just use verbs (actions/functions/methods/whatever) instead of nouns (types) - let's focus on what I want to achieve for now, and split it up into nouns later!

And so, I came up with something similar to this:

    private static Func<IDisposable> Listen(
        Action<TcpClient> onConnectionReceived,
        Action<TcpClient> onConnectionDropped,
        Action<TcpClient, int, byte[]> onBytesReceived,
        Func<bool> isOpen)
        // todo: 25565 needs to be injected
        // todo: give opportunity to supply listener
        // todo: refactor listener into role interface
        var listener = new TcpListener(IPAddress.Any, 25565);
        return () => Observable.FromAsync(listener.AcceptTcpClientAsync)
            .Subscribe(client =>
                var stream = client.GetStream();
                // on subscribe invoke the delegate first
                // the delegate has the option to disconnect the client
                // based on whatever criteria it see fit.
                // we should now create an observable to 
                // listen for messages on this client.
                Observable.Defer(() =>
                    // 8kb buffer for each connection
                    // this is actually fairly small
                    var buffer = new byte[8024];
                    // we can handle the 'doing' of things with these bytes
                    // inside the passed function. This function will be doing too much otherwise
                    return Observable.FromAsync(ct => stream.ReadAsync(buffer, 0, buffer.Length, ct))
                        .Select(n => new {Read = n, Bytes = buffer});
                }).Subscribe(a =>
                    // drop connection if nothing was read
                    if (a.Read <= 0)
                        onBytesReceived(client, a.Read, a.Bytes);

Really simple. Well, I mean, not quite, but this basically does the role of all of the nouns (except IMessageEncoder/Decoder that I listed earlier), but only making assumptions of the fact that we need a TcpClient (obviously, that isn't what I want right now, but I can work to that in iteration 2!)

However, my problem with this is, is that this isn't really typical C# code.. and it surely breaks the SRP in that this function is returning a function for the entire execution of a server.

But - it makes sense to me.

So my question is, is there any real inherent downside to using a functional-esque paradigm like this as opposed to traditional Kingdom of Nouns OOP inside of C# - traditionally a multi-paradigm language? And if there are downsides, what would I face and why?

For the record, I intend for this to be OSS, so there's another issue in that some developers might not understand the code style because it's simply not OOP orientated, just pure functions.

  • Just for future reference (since here, it doesn't actually affect the correctness of anything but a comment), 8KB is either 8000 or 8192 bytes. (And when you're dealing with chunks of RAM, it's almost always 8192. Kibibytes notwithstanding.)
    – cHao
    Jul 31, 2014 at 9:21
  • Ah, thanks for that. I don't know why I used 8024 instead of 8192 (seeing as 8192 is 1024 x 8)
    – Dan
    Jul 31, 2014 at 9:25

2 Answers 2


If there's something bad about mixing programming paradigms, then it's clearly lost on the C# designers; Linq is essentially a functional programming pipeline.

Clearly you already know about Steve Yegge's post, since you're referring to the Kingdom of Nouns. Of course, the world didn't suddenly see the error of its ways and stop using Java. Why?

  1. Because Java is already widely used and well-understood.
  2. Because the Kingdom of Nouns is well-suited for building data-based business systems (the efforts of Architecture Astronauts to overtake the plumbing notwithstanding).
  3. Languages like Java have lots of ceremony like classes, interfaces, methods, scope and so on that programmers can use to understand the program's architecture. This ceremony is largely absent in most functional languages, or at least not overtly visible.

So does that mean that you abandon functional programming altogether? Of course not. The lack of ceremony allows certain things to be done more quickly and more intuitively in a functional paradigm than in the Kingdom of Nouns. This is especially true of math-like operations.

Mixing programming metaphors is a perfectly valid way to program. Were this not true, we'd only need the One True Programming Language to Rule Them All.

  • This is a fairly good answer. I intend to accept this answer unless there are any more I would consider better, however I would like to keep the question open for a few hours to see what discussion can be had
    – Dan
    Jul 31, 2014 at 8:55

I believe you should be extremely careful when mixing programming paradigms in one place. The main reason is that when you mix paradigms, it will become problematic to apply "idiomatic" solutions on one paradigm to problems, that mix multiple paradigms.

I do have problem with your example, because it is not inherently functional. You could easily replace those "Actions" and "Funcs" with "Commands". It would become pure OOP code, when you start composing functions and start using higher-order functions.

Example where I found mixing to be problematic was, when once in code, we needed to "decorate" some method with pre and post-running action. The first obvious way was to create function, that would get Action, run it between pre and post code. But then, it become extremely problematic to slightly change this pre and post behavior and we had to introduce function parameters and mix multiple behaviors in single method. If we instead implemented it as class which could decorate the method, then it would be easy to use inheritance to change the pre and post actions. The slight problem with this solution is that it needs you to create a new class, which has little bit more of code than simple method.

So for me, it is great to have functional capabilities for something like LINQ, but I would think hard before using functional composition as piece of architecture or design in .NET/C# application.

I would also like to emphasize on Command pattern. I believe this pattern is heavily underappreciated and underused. Both functional and object design is all about composing behaviors. It is just that both use slightly different tools to get to the same end result. And it is probably problem of education, that many people doing OOP design don't understand that.

  • It doesn't necessarily have to be a part of the architecture. I use Linq all the time in Repository classes that already have an OOP API, and because the Linq is an implementation detail, it doesn't affect the architecture at all. Jul 31, 2014 at 8:42
  • @RobertHarvey Well, I have seen argument against what you are doing and some people even say IQueryable is quite leaky abstraction. Also, I didn't mean it like that. LINQ is only one way to use functional features of C#, and one of the few I find OK to use.
    – Euphoric
    Jul 31, 2014 at 8:46
  • 2
    I actually taught myself most of what I know of OOP from reading the internet and.. poking things until they went bang. I am aware of the command pattern, but it feels that that would be slightly over-engineering the solution. Why should I need to wrap my function inside a command object when I could just pass the function directly, for example?
    – Dan
    Jul 31, 2014 at 8:47
  • However I will say that I made a mistake by saying my code is functional. It is not functional just because it uses functions, but I think the better way of saying something was that it wasn't common OOP in that I was passing functions as callbacks/delegates, rather than, say, returning an instance of an interface implementor that had event hooks for OnBytesReceived etc.
    – Dan
    Jul 31, 2014 at 8:48
  • 2
    but there might be time in the future - YAGNI! I don't need that complexity now. And there's potentially no chance I will ever need that complexity. I believe the saying is that don't code anything unless you need it right now.
    – Dan
    Jul 31, 2014 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.