Gluing things together

Gluing things together

One of the most important things in coding is how to hold it together. In this short article I will share some thoughts on callbacks, events, observables, promises and tasks. This is what I have experienced in my time programming and I would be glad if you could share if you don't agree with me on something.

Why callbacks are bad

When I started programming some years ago I started with C# and Javascript. While .NET already had the Event in Javascript callbacks were everywhere. Then the word "callback hell" was coined.
In short callbacks and also Events for that matter are bad because they defere the code execution to some other place which can be hard to find especially when you are following from one callback to the next for multiple steps. Therefore it pollutes the code and can separate concern which should not be separated.

Why callbacks can be good

Defered execution is not always bad. To create modulation and structure callbacks and events are a major tool I have used. They should not pollute the local code and if there is a sign that a module is internally hold together by events or callbacks it is most likely better to create an abstraction layer. It is possible that callbacks and events are used in a local module but it should not be domain code.

Event based programming with RxJs

Event based programming with RxJs is another approach to events. Or should I say its exactly the same approach except for the fancy api's RxJs provides to you. It also provides that many tools that it makes a lot of sense to use it for domain code in a local module. As it is a new model for code continuation it can be a bit confusing at first when you do not know the api in the beginning but once you know it it provides you an abundance of tools for code flow control.

Promises And Tasks

The Promise type from Javascript and the Task type from .NET have runtime support and some syntax sugar to it which means they can provide you with yet another set of tools like threading and async execution. They are backed deep into the language and are used everywhere as control mechanisms. They aren't specifically for flow control or domain code.

Always those blockages

Now sometimes what I wrote above doesn't hold true. There are always special cases which change the whole perspective. Just before I said I prefer a callback over a Task because I didn't like the sight of three lines versus one just for flow control. Maybe I already feel different when I look the next time at it.

Comments

Popular posts from this blog

Components of Tasks & Threading in .NET

Creating a cordova plugin and using it with ionic

Debugging information and visual studio