Why writing idiomatic code is important?

Kishore Kumar
3 min readJul 4, 2018

A wise man once told me, idiomatic code is like using red bricks to build a house, otherwise you would end up with an ugly looking house.

Doesn’t make sense to you? Yeah, me neither.

But the real question is what’s the point of using the usual if constructs, for loops and the like, while working with all these new modern programming languages ? Why don’t we just use C for everything, right?

When working with modern languages, which provides so many more features, keywords, syntax sugars, I believe, using those “idioms” to do even the simplest of tasks improves the readability to a great extent.

We all build software that will be used and maintained for at least more than 3 years, in which case readability is of highest importance because it has a direct effect on the maintainability of the code.

Consider a code-snippet written in Kotlin,

fun processRequest(request: Request): Status {
requestProcessors.forEach { processor ->
(processor.canProcess(request.type)) {
val result = processor.process(request)
return result.status
throw UnknownRequestType()

Now, this is a simple example, based on the type of the request, find a processor that can process this request from a list of available processors. If nothing is found, throw an exception.

This code works and sure is readable, but this does not look “Kotliny”. It is very similar to a Java 8 or C# code. What if, we rewrote the same code using Kotlin provided features,

fun processRequest(request: Request): Status {
val result = requestProcessors.firstOrNull {
}?.process(request) ?: throw UnknownRequestType();


return result.status

Coming from a Kotlin background, you would the find idiomatic way more pleasing and also readable, because the API tells what exactly it’s doing. Get the firstOrNull requestProcessor that satisfies the predicate, and process the request. When none found, throw an exception.

One of the biggest advantages of writing idiomatic code is that, it greatly improves the readability of the code, there by making it easier to maintain and also avoids getting cursed or killed by someone who comes in later to maintain your code. After all, we all want to go back home and have a peaceful night and sleep.

Also, this helps in learning the language, you start by writing some code and then look for something provided by the language to achieve the same thing, also maybe making it less verbose. This way, you get to explore and learn the features provided by the language.

So write idiomatic code when possible and go #cleancode.

It takes practice to write idiomatic code, you will have to spend some time with the language. There might be times, you would be frustrated, wondering why the hell did I try to write that code idiomatic. But in most cases the reason for that would be that the code is breaking some SOLID principles and hence your idiomatic code is not as readable as if it were written the non-idiomatic way.

In unlikely cases like these, spend some time trying to move few things around, look for any code smells, like bad naming. When nothing works, well of course you can always go back to the non-idiomatic way.

One last example of a code found similarly in most code bases,

Non-idiomatic vs Idiomatic code(JavaScript)

Happy coding :)