A Coder’s Diary 5 (How I Finished My Wizard)

Alexander Weinmann
Good Audience
Published in
3 min readMar 7, 2019

--

Thursday, March 7, 2019

Dear Diary,

I am happy, because yesterday I could finally finish my Wizard. I even published it on Github. This is how I proceeded:

After I had finished that Deviation-Topic, I just wanted to add some basic bookmarking, in order to polish things up a little bit. This implies storing the number of the current screen as a cookie, so that a returning reader is always been shown the page he has left.

My solution does not need to be universal all failsafe, so I have just chosen window.localStorage, which is part of the pretty much standardized web api. You can access it simply as a property of the window-Element

Things are a bit more complicated, as I am still using scala.js. So, in this case I thought it could be a good idea to brush up the existing Wrapper a little bit. That seems to be a good occasion show you one of my favorite features in Scala, which is implicit classes.

Basically you can improve any object in Scala using Implicits. You do not need inheritance! Implicits are nothing more than a a clever way to get a wrapper class to improve another classes interface. This functionality is provided by the compiler, so a programmer does not have to write repetitive and silly code for that purpose. The compiler does it for you.

By the way: A few days ago, I found an article describing something similar to Implicits for Java, but I cannot find the link any longer. Java has later adopted so many features and concepts from more modern programming languages, that it can be barely recognized today. I pity the people who are doomed to spent their working lifes with that language that always comes late. To be honest: I am one of them myself!I am a Java programmer. It feels like using bow and arrow, while others have a machine gun.

Let me do another digression: What do you think about inheritance in today’s programming? It is an antipattern! There are so many articles in the net stating this. (Here is one of them.) Today, when I do my little bit of programming, it feels good to be disgusted about inheritance, and to avoid it but for the most simple use cases. I still use it, and I also used it in my Wizard project, but I am being much more cautious with it today. People at my age were weaned on it. To get rid of it just means getting ride of lifelong habits. That tends to get more difficult, the older you are. But I can say it clearly today: I no longer trust the inheritance pattern.

It is much more flexible and secure to write code like I did it for the ’localStorage`-object in Scala:

implicit class PimpedLocalStorage(localStorage:Storage){
[...]

def myConvenienceMethod():String = ???
}

Now, where ever I use localStorage, myConvenienceMethod will be available. (Only as long as the implicit class is in scope).

Storing the index of the Wizard means adding two very simple methods in such a way. (You can check it in the Github-Repository, if you like.)

Storing the index of the wizard is not more than a dozen lines of code, if you are doing it like that. I love it! I love Scala. And I love the way it can be compiled to Javascript.

That is all I wanted to write about my magical friend, the wizard. I will let him sink back into the darkness of oblivion now. It was nice to meet him. Now let us move on to even more fascinating miracles in the world of programming …

--

--