For some time I’ve been publishing on my Twitter some Kotlin snippets. They all represent either awesome Kotlin usage or some kind of comment to what happens in the community. Let’s collect the last ones and discuss them.

Collection literals

My Twitting code career started from this:

Well, sorry if you thought otherwise but it is actually an irony. Many people agitate to introduce collection literals to Kotlin. It was actually the most popular feature based on the big Kotlin survey. Though there are important reasons why collection literals should not be introduced and influential personalities agitate against this feature (check out Jake Wharton response to the survey). I understand both sides, but I also find it pretty ironic that pretty minimalistic collection literals can be implemented using features that already are in Kotlin. I don’t suggest to use this. I suggest to stick to standards, but if someone is a big fan of collection literals then he can have his own if he wishes.

Awesome functional programming examples

It is not a new thing that I like to make a fun with functional programming and share cool examples. Though I found out that people like them, so I enjoy sharing :)

Many ways

As a teacher (I make workshops for companies) I often give a task to write a function. I generally don’t force any concrete solution and instead I help people implement their idea. I am often amazed by how many different ways functions can be implemented. We generally tend to think about one way, but understanding that there are many ways that have their pros and cons is essential. This is why I published 4 totally different ways how one can implement the Fibonacci number calculator:

Though I have many more examples of functions implemented in extremely different ways, and I will post some of them in the future ;)

Bit terrifying but good to know

Some ideas are powerful and a little bit terrifying. Like fake constructors. Though I think that it is good to know that they exist and that they can be used (but with caution).

In any language, there is a lot of such things. I am willing to share them. Sometimes I also share my worries a bit more directly:

I especially suggest to check out the discussion below this tweet. This is a quite popular pattern but it is often overused. Just think what if showListOffline would possibly return null . In such a case the result of these two implementations would be different. This is not clear at all. Though my main idea is that sometimes it is good to use more basic structures to keep code readable for everyone. Short code and your readability is not everything. First one is something that any developer can understand and change. The second one is clear only for Kotlin developers with some experience. We want to decrease the entry threshold into Kotlin, not increase it.

Well, that’s it for now. I will publish more code examples, and once there will be a big-enough collection, I will share another part of “Awesome and crazy Kotlin examples” ;)