Configuring function call

In programming, everywhere in the code we use procedural abstractions in the form of function/procedure or method (in object-oriented programming).  These procedural abstractions (later in the article I will call them just functions) usually have parameters. It is interesting that if you fix one or more parameters to some particular value, the function can acquire additional meaning and can be used as a dedicated function with a separate name.  For example, consider function add(x,y).  When you take the first parameter and fix it to some number, for example, x=1, then this function can acquire new meaning – incrementing. Function add(1, y) now can be named increment(y). The example is a bit unpractical, but this is done for the sake of simplicity. Although developers don’t pay much attention to such situations much, they often have to make design decision: what parameter to fix and what should be the name of the new function. After this decision is made, there are several techniques which can be used to implement this decision.

The first one is classical: using the object or just plain parameter passing. (in object-oriented programming sense). When the object is constructed, the value to be fixed is passed into the constructor. Here is C# implementation of incrementing functionality based on addition function:

In some sense, it is not the most elegant solution because Of method actually takes increment parameter implicitly. It could be done even in C by explicit passing increment parameter to Of function: Of(increment,5). It is not elegant because 1) caller should always know about increment parameter and apply it to Of function; 2) new semantics is conveyed through parameter name, that is “increment” which is the name of the parameter, not function. The function itself has a unexpressive name.

Much more interesting way to do the same thing is using closure. The closure is basically a function that has a special ability to access other variables local to the scope it was created in. Here is implementation of increment using closures in C# (yes, this great language actually support closures!):

If syntaxis of Evaluator method looks weird to you, it’s just C#6 feature – expression-bodied function. In this snippet, Evaluator takes argument x and then it looks like evaluation function gets called with an x parameter. But it is not what happens. In fact whole function y => evaluate(x, y) returns as a result and x is “closed” in this function, that is when Evaluator function returns, x variable is not destroyed as it normally would, it continues its life incide y => evaluate(x, y). That is why increment(5) gives 6. The parameter x still lives in object referenced by increment variable. Sounds cool? Not as cool as the next method!

Even more interesting, terse and elegant way to implement increment is using currying. Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example evaluation of function, add(x,y) can be considered as two steps process. 1) The parameter x is applied to add which yields another function, which takes one argument. The value of x is “embedded” inside returned function in the same way as it was done with closures. 2) Parameter y is applied to the just returned function which yields the sum of x and y. Unfortunately, C# does not support currying and hardly ever will. Therefore here is implementation of increment with currying in F#:

So, everything you do is “let increment = add 1”! It is hard to imagine something even more expressive than that.

It is not that currying is impossible to do in C# at all. You still can do some ugly hacks, the question, of course, is whether you want to do it. But who knows, maybe one day you will need it:

JavaScript is an interesting language. It does not support much stuff, but it is so flexible and dynamic, that one can introduce lacking concepts into the language using libraries. There is a very nice library, named Underscore.js, which facilitates very much functional programming in JavaScript. See how it is relatively easy to do currying in JavaScript using Underscore.js (actually I demonstrate partial function application here, but the subtle difference between partial function application and currying is off topic):

If it is hard to you to wrap your head around all these concepts, check out some course on functional programming. Sooner or later in your career you will encounter them again.
I can recommend a very nice introductory course from Delft university on edX.

Facebooktwittergoogle_pluslinkedinmailFacebooktwittergoogle_pluslinkedinmail

Why should you use popular technology to build software for business?

Our industry is overwhelmed with a number of programming languages. Every good computer scientist or a programmer interested in programming languages strives to create his own language. Today you don’t even need to be extremely smart to do it. Take a look at JetBrains MPS beast. With this tool, you can put together your own language literally in few hours. I believe it is important to distinguish languages which you can use for businesses and language which you can have fun playing with. And it’s better to avoid crossing the line. If you stop your education on widely used language like Java it is bad for you. But much worse things will happen to you and your company if you start or continue using for your business some exotic or old language, like COBOL or SmallTalk. Many people think that it does not matter what language you use because each of them can provide sequential, conditional and repeating operations and this is pretty much all you need. I was among those people few years ago. And while this is true, you can write almost any program in any Turing complete programming language, after trying working in both, a popular and exotic language, I would firmly assert: it matters a lot. And here is why.

Popular programming languages tend to form whole ecosystems around them containing tooling, plug-ins, frameworks, libraries, platforms, developers, vibrant communities with support forums, open-source foundations, vendors and companies which build solutions using all these components. This is millions of people and dollars, huge moving, innovating, self-maintaining organisms. A lot of energy and manpower. You can probably easily guess what languages managed to build such ecosystems: C, C++, C#, Java, JavaSsript, PHP, Ruby, Python. All others are lagging behind. If you develop software with programming language, which does not have powerful ecosystem, the following will inadvertently happen to you.

Main rule of software economics, which doesn’t build stuff which you can avoid building, will be broken. You will have no choice and will build all frameworks and tools on your own. Sounds great, but it is a very sad situation. You will spend too much of resources on maintaining these things, which are not related to your business directly. And the worst thing is that all these spendings will not help you with hopeless lagging behind others, who can cheaply buy or even take for free all those tools and frameworks. New programming technique has established? Like TDD or refactoring? Those who are in ecosystem just download tooling and start using these techniques. However you spend resources, which you could have spent on developing apps for your customers, on this tooling. Results are of the low quality, of course, since your resources are limited. All this leads to… Isolation. Since you can not cope with the pace of the industry, based on vibrant software ecosystems, you will isolate yourself from the rest of the industry. You will not introduce TDD, refactoring, continuous integration, continuous delivery, micro-services or any other development practice or architectural pattern into your organization. Consequences:

  1. StackOverflow will never help with any technical problem. Your developers will constantly distract each other asking for help because the world community is simply absent;
  2. Your developers’ effectiveness will drop considerably, they will spend most of their time fighting with the code and bugs;
  3. The technical debt will grow in frightening speed;

What comes next? Demotivation. Your people will start with great motivation at your company. They will try to bring the best practices they know about making software to your company. But very soon they will discover that they just can do nothing without proper tool support, which you can’t give because you have to focus on your customers, not on tools for you developers. People will stop learning and developing themselves. They will stop refactor and test their code well. Who cares? The code and architecture is a mess anyway. This situation will slowly formulate your technical culture. And this is a dangerous situation because best people will avoid working in such a culture while it will attract people, who don’t care about their craft. This will make your organization even less efficient.

From individual developer’s perspective, your career will be put in danger. If you develop software in isolation from the industry and use unknown, home-grown, low-quality technology, no one will be interested in paying you for your services. You will eventually become uncompetitive.

This same idea works for any other technology like framework or library or platform, not only for a programming language. For example, I have built this site using WordPress, which is extremely popular. Before that, I was maintaining my site made by myself. It was a total pain, I spent hours to make minor changes. This site, nechai.net, was made during two evenings after work. And it has absolutely EVERYTHING I can imagine putting on my site. All free and high quality. If I will need something specific, an army of companies and freelancers will be ready to help me for very small payment. Amazing, is not it?

Now, you can object, that giving such an advice puts innovation at risk. Yes, I agree. If you only do popular stuff, innovation will not happen. But is your business is to bring something new to the world of technology? If not, why should you or me care? My business was to create this site. I could have given a use case for some new progressive content management system, but I did not. Instead, I just seamlessly and fastly created what I needed to bring some content to you.

I would make an exception though for the case when you want to make some investment in a new language. When you believe it will become popular and will build a powerful ecosystem. In case of success, you might end up to be a big fish in this a future ecosystem. But be careful, there are so little count of really popular languages and a huge count of forgotten and niche ones. Your risks become lower if your language is a part of some other’s language ecosystem like TypeScript and CoffeeScript are the parts of JavaScript ecosystem, Scala and Groovy are the parts of Java ecosystem.

All right, so what is the conclusion? Well, it is in the name of this article. Program in popular languages for your business and remember, our job is to provide software, not to write code. Teach yourself programming using exotic languages. Don’t mix things up, unless your business is technology and you want to invest into some new raising shiny language (or framework or library or platform). Luckily most companies understand this. See how most of them stick to Java or C#.

Facebooktwittergoogle_pluslinkedinmailFacebooktwittergoogle_pluslinkedinmail

Refactoring old C#5 code to C#6 code

This post is not the usual one. I wanted to show you how it looks and feels refactoring and improving old C# 5 code using features of C# 6. Doing it in the traditional way by simply writing and giving code snippets is not very productive and I have decided to try out other media – actually doing a screencast. I have done it for the first time and therefore quality is not as good as you might want, but I promise to do it better in the future.
Regarding C# 6 I would like to point out that its most features are about making the code more succinct and terse and feel like functional. And since functional programming paradigm is taking over the programming world, I believe it is a very good move for C#.

Features, which I apply in the video:

  • Expression Bodied Methods/Properties
  • Null propagation
  • Auto-Property Initializers
  • Using static members
  • String Interpolation
  • Operator nameof
  • Dictionary Initializer
  • Exception Filters

If you like what I do in the video and you would like to play with this code yourself, then go to my GitHub repository, check the old code out and do some C#6 refactoring. Visual Studio 2015 community edition is just fine for this.

Facebooktwittergoogle_pluslinkedinmailFacebooktwittergoogle_pluslinkedinmail