Monads for OO Programmers

I’ve been writing Scala for 5 years (At time of writing this post). Let me explain in my own layman terms without category theory what a Monad is.

A Monad is an object instance which contains another object instance and adds some context to it.

The word “context” here means to add other information to something in order to give it meaning. If you saw the word Drisenburg in a story book it’s meaning wouldn’t necessary be apparent out of context, but in the context of the book, you would come to understand as you read the whole book it is a name for a character or a location or treasure of something.

Context is new information that changes the meaning of something. A joke might be objectively funny but it might not be a funny thing if said at someone’s funeral.

We may know a Royal wedding is on TV at 5pm in UK Time but we need to know our own timezone or location, for that time to be relevant to us. In this example the time is the thing or object and the timezone is the context that makes the time relevant for us.

As a general phrase many people say “to put things into context…” and in the Object Oriented design world it makes exactly the same sense to model this context as something that wraps something else.

case class UKWeddingEvent(
   dayOfYear :Int,
   hour :Int,
   minute: Int
)

case class LocalTimeUKWeddingEvent(
   ourOffsetFromUKTimeInHours :Int,
   weddingEvent :UKWeddingEvent
)

Here I have a UKWeddingEvent and a LocalTime which I’m calling the Context in this example.

We could arguably model our solution by using a Tuple to attach our timezone to our wedding event

val ukWeddingEvent = UKWeddingEvent(364, 12,00)    // thing
val localOffsetFromUKHours = 4                     // context

val myLocalisedWeddingEvent = (localOffsetFromUKHours, ukWeddingEvent)

Or we could just have some sort of Context object to pair them

case class Context[A, B](mainThing :A, extraData :B)

The last two proposed solutions aren’t ideal as they allow tinkering with the mainThing outside of its context, without the full facts about what’s changing.

If the UK Wedding is pushed back two weeks, you don’t want to tamper with the UKWedding directly outside of the context of the localising information as you will cause a bug. The clocks go forward an hour in the summer in the UK one week before the Americans’ put their clocks forward so this would introduce a bug into the system. This is why the best approach was the original design, one that controls the object to restrict it’s access, which was very close to our first example:

case class UKWeddingEvent(
   dayOfYear :Int,
   hour :Int,
   minute: Int
)

class LocalTimeUKWeddingEvent(weddingEvent :UKWeddingEvent, offset :Int) {
   private val weddingEvent :UKWeddingEvent        // thing
   val offsetHoursFromUK :Int = 6                  // context
}

We still need to be able to change the wedding event in a controlled manor and in a way where the change can be captured. One way to do this is to allow people to provide an update function which changes a UKWeddingEvent into a new UKWeddingEvent, mapping it from an old value into a new value and allowing us to handle context changes:

def map(function :UKWeddingEvent => UKWeddingEvent) = {
   val newEvent = function(weddingEvent)
   
   val wasBST = weddingEvent.dayOfYear > 65 && weddingEvent.dayOfYear < 135

   val nowIsBst = newEvent.dayOfYear > 65 && newEvent.dayOfYear < 135

   val newOffset = (wasBst, nowIsBst) match {
        case (false, false) => offset
        case (true, true) => offset
        case (false, true) => offset - 1
        case (true, false) => offset + 1
   }
   
   new LocalTimeUKWeddingEvent(newEvent, newOffset)
}

So the function above allows users of my Context class to manipulate the inner object is a way that allows the context to retain control safely. Callers may use it like this:

def delayWeddingByTwoWeeks() = {
    localWeddingEvent.map(event => event.copy(dayOfYear = event.dayOfYear + 14))
}

This has safely pushed back the wedding whilst letting the context stay accurate and relevant. Whilst these next two examples may seem a little contrived for our code, it’s time to step back a little a look at the big picture.

I’ve been calling a Monad a generic context object so there’s little surprise we are going to make this UKWeddingEvent use the generic terms that monads use.

I’m going to eventually make this UKWeddingEvent in a Monad. Two other common things we want to do with our context object apart from map over it, is change the context by simply taking an update to itself it:

def flatMap(function :LocalWeddingEvent => LocalWeddingEvent) = {
   val replacementForWholeContext = function(this)
   this.offset = replacementForWholeContext
}

Flattening a monad is a way of merging contexts. In our example, it would be applying two timezones to the event. Convert a UK Time to French time, and then on to German (providing a new context and thing).

So lets look at our LocalUKWedding object and talk about those few operations

class Context {
   def pure(in)   // put a thing into some context
   def map(f)     // apply a function to a thing
   def flatMap    // replace a thing + context
   def flatten    // reduce to a single context
}

someOtherFunction   // get something out of the monad and realise it's value.

A Tour of real Monads.

Future

Functions return values and the Future Monad abstracts away the details of how functions are turned into values. Understanding the context of running code on this thread or that thread and have a slightly irritating need to refer to ExecutionContexts.

So Futures provide values and allow you to manipulate values that may not have even been generated yet by exposing it’s map function to say “apply this function to this value when it becomes available”.

// code example required.

I’m going to talk about Futures again when I get near the bottom. For now understand it is a Monad that provides an execution context.

Options

The context is whether something can be missing. map() here takes a function but runs it if the thing exists, thus your code can be ignorant of null pointers and missing variables and run safely regardless because it provides the context of whether or not those commands would fail.

What’s nice about abstracting away whether something exists or not is that you can write really terse code with practically no if statements.

You can think of Option’s implementation as a list of 0 or 1 items. If you can write a LinkedList collection class in Java, you can easily write your own Monad for Option. map takes a function and only runs it if the item exists.

Either

An either is two values bundled together. Either the left is empty and the right is populated or the right is empty and the left is populated. A little surprising thing about Either is that it’s “right biased”. map() takes a function that applies to the thing on the right. map does nothing to the left. Scala developers put good values in the right and errors in the left and map works like a “if this is successful, continue with this…”.

Getting things out

There’s no secrets or cheats here but it seems to be a point of frustration or confusing for people. You can put things into Monads, operate on them in context (whilst ignoring the context) and then pull them out. Every monad allows you at runtime to realise it’s value in a concrete way. Even future. You simply “Await.result”. Every program using a Future either has one await.result in it or it prematurely ends. No Exceptions. In Play framework the framework does it. Akka and http4s too. You should avoid it completely and chain your operations using map and flatMap. This will give you an application that has flexible concurrency and little chance of deadlocks. But there is no magic, eventually the whole app is flattened into one future per event handler on which await.result is invoked. The function isn’t in Monad category theory language but is called Await.result.

Programs using Option either eventually call .get or .getOrElse or they use if statements to control the flow but somewhere they are realised.

Next steps

Once you’re familiar with the four operations of a Monad. Put it in (pure), apply a change to the value safely (map), replace one (flatmap) or flatten nested Monads (flatmap) then you’re ready to look at some of the other guides with more confidence and experiment!

Scala has a very nice set of keywords that allow you to write a series of maps followed by a single flat map in a pretty style. It’s a syntax variation on a for loop called a for-yield loop.

You are probably going to want to explore that at some point but it’s beyond the subject of this blog post and frankly is just pretty boilerplate that shouldn’t deter you from using Monads directly. Hopefully this guide compliments your understanding of Futures and Monads from other articles and you can play with them having more confidence.

I believe in Findology

I believe in science. I believe in psychology. I believe in Findology!

There is a science and method to looking around your house for things that once you’ve learnt will make your life more enjoyable and less stressful.

The first rule of Findology is that you cannot find something unless it’s you acknowledge it’s missing. You need to move from panic, into a productive state. Go into your front-room and loudly proclaim “My house keys are missing and I can only find it, if I look for it”. Once you’ve done this, stand still for 10 seconds and contemplate emotionally that you have a small, easy task to perform. A quick search.

Here are some simple methodical steps

  1. The missing item is in the place you think it is. Why wouldn’t it be? Statistically it is probably within 18 inches of where you think it is. It has fallen behind something. Something is draped over it.
  2. No? That’s fine. It’s in the last place you saw it. You memory is the problem here, not the item. The item is not lost, you need to find your memories.
  3. Everything has a home. Every piece of sellotape, every pen, every ornament. There’s a “default” place you would put it, had you bought it home new. Your brain is a database and there’s also an algorithm to put new things in new homes. If your memory fails, your algorithm to assign something a home should select the correct place. Its where you think it should belong.
  4. You’re now going to have to adopt “the algorithm”. The algorithm is to search your house. You are going to search each place thoroughly, so that you only search it once. You will search things in the likely-hood of it being there. Think about your house having “hotspots” and “coldspots” of activity. A table is a hotspot. The bathroom is a cold spot (typically).
  5. Consult Dr. Solomon’s advice, using the link below.

With practice and discipline, you can become fast, organised and committed at performing a single thorough house search. It no longer becomes a chore. It is a simple, quick and reliably successful exercise you have to do, comparable to taking the bin out. Searching is easy.

As you accept Findology as your belief system, you more consciously consider the “homes” of your items. You pick up, fondle and lovingly gaze upon things like Glue sticks and say

“hello mr glue stick, you live in the draw, let’s put you there now”.

As you adopt Findology life is less stressful.

I didn’t invent Findology, it was invented by a real scientist. What I’ve written is an experience based variation of the original and more respectable official Findology advice. Please see this article for the genuine, and more hilarious, truth to happiness.

https://www.dailymail.co.uk/femail/article-1301444/12-tips-worlds-findologist-help-lose-again.html

Some excerpts:

Sometimes, in fact, you are looking right at it. That’s Solomon’s sixth principle. ‘It is possible to look directly at a missing object and not see it,’ he says.

‘This is due to the agitated state of mind that often goes with losing something – especially when you’re in a hurry. Go back and look again. It may be staring you in the face.

‘Sometimes, not only do we overlook an object – we forget what we’re looking for. To avoid this, repeatedly murmur the name of the object.’

Counterstrike’s raison d’être

The little CSGO team at Valve work on making CSGO the best game everyday. It’s their job to. With that said.. Valve is the platform business. It’s biggest and most important product is Steam. To make Steam attractive, to encourage adoption and retention of game developers Valve release many products that tie games to Steam. CSGO exists, for the larger company, purely to advertise those Steam features:

  • Making a competitive game? We offer a matchmaking service using Glicko. Look at how matchmaking increases retention rates and players invest to make it to the top.
  • Worried about cheaters using free 2 play accounts? We’ve got Trustfactor spanning multiple games so we can look at a gamer’s history to see if they’re likely to cheat.
  • Need anti-cheat? VAC bans thousands of CS players and you can enable it with 2 shakes of a lambs tail
  • AI Anticheat? Look at how CSGO detects and bans aimbots.
  • Want an overlay, with web browser, chat and want your gamers to share game screenshots on the community? Just hit F12 on any Steam game and get it free!
  • That’s not all. We got desktop SDK, Valve Index SDK for VR games and a host of other services you might want to enjoy.
  • Community market? Check out Counterstrikes loot boxes, even operating in France (with a novel legal interpretations)!
  • Look at Counterstrike running on a “steam deck”. Pointless but respectable from a technical standpoint none the less.
  • Want the community to build your product for you? How about Steam Workshop. Our millions of Counterstrike users are upvoting the same poorly designed aim-maps for decades.
  • Want to target the Esports arena? We’re the experts with Counterstrike. Check out Steam TV where gamers watch streams together and we collect the metrics and sell them back to you like Adsense for your game.
  • Worried about going free 2 play? We did it with Counterstrike.

We love and are invested in Counterstrike and I certainly believe the devs are too. CSGO has to dance to the tune of those selling Steam as a platform. Those below Gaben who want to milk the product and use it as an advertising tool for the rest of Steam. That’s why CSGO is getting Steam deck support even though we all know a controller isn’t as good as a mouse. Ho hum, tis the world of business.

How I stay organised using Vim

I spend all day in VIM. Either editing configuration, writing plans or using it as a full blown IDE.

I use nvim-metals to get IDE features like showing variable types when editing Scala code.

Whilst every morning I give a standup update, through out the day I keep all my notes in a text file. Its just a text file and looks roughly like this

Where ever I am in Vim or whatever I’m doing, I can immediately open the file with `G I’m using the feature called “marks”. In short m will mark the current cursor position so ma will remember the cursor position and associate it with the letter a. Typing` awill take you to the line your mark was on. (The quote character is under escape on a UK keyboard).

It’s an amazingly useful feature. I use it the most when I’m half-way through writing some code and I need to jump to the top of the file to add an import. I mark the cursor position, jump to the top and then return once I’m done. Lowercase letters are local to your file so you can store 26 unique positions per file. Using an uppercase letters operate at the global level opening my notes file regardless of where I am, or if the notes file isn’t opened yet. It just opens it and puts me in.

If I need to write an essay on a particular topic or the notes get long and ugly, the second trick I use is to create a specific text file for that thing and keep the filename in my notes file. Using gf in vim opens the file path under the cursor, so it takes me hardly anytime to navigate through my collection of notes, drilling in and out when necessary.

Vim’s Ctrl+o feature helps me move back to where I was when I’m finished with the notes, speeding up navigation. I also have this bind in my vim rc:

noremap <silent><leader>bd :MBEbd<CR>

When I type \bd the file I’m looking at is close and vanishes. This is my trick to keep my workspace clean and tab list short and focused. It’s not uncommon for me to spend a minute every now and again closing all the unused files like this, just as you might also do when you periodically close those browser tabs you weren’t using.

Finally, the benefit of just using a text file instead of some plugin solution is the flexibility. It’s in my sync’d cloud files so it’s backed up and can be opened in other programs. Sometimes i open it in Kate if i’m not in vim at that particular moment.

Open In JIRA – 2021 Edition

If you’re developer who uses Linux for work and has an appreciation for command line tools, it’s likely you spend all day in apps like IRC, Mutt, use Vim, emacs, and git commands. All of which, are probably littered with references to your project management code. My company uses Jira but yours may very. 6-7 years ago I “hacked” KDE’s Konsole to add an “open in Jira” link to the Konsole and it was like enriching dozens of programs at once! Now IRC, Vim, git all benefited at once from a single change. See the image for screenshots

Konsole augments email
Konsole augments bash
Konsole augments IRC Chat
Konsole augments git log command

Checking out the latest dev version of Konsole on my path to reimplement the feature again I spot a new feature. The feature allows selected text to be used to open a URL:

KDE Konsole’s new search option

It appears to have a simple configuration for building simple urls

The feature is called Web Shortcuts and appears in Konsole 20.12.2 (and possibly earlier)

Huge thanks to the KDE Konsole team for this awesome feature! As a Debian Stable user I have no idea how long this has been around, but I love it! I don’t know how much publicity Konsole gets but I wanted to share!

As a serious command line user, like many Linux users, Konsole’s 3 killer features for me are:

  1. This open in JIRA thing
  2. Infinite scrollback
  3. Search output

Tour of my working environment

User interface: Tiling window manager DWM

Resizeable windows that you drag around the screen, that overlap helps people using a computer for the first time to recognise that opening a new window doesn’t lose the old one, and that the computer can do multiple things at once, and so can they.

Once you know that however, moving and resizing windows is fiddly and unnecessary. DWM manages and resizing windows for me and means I use the mouse less which makes me more efficient too. Essentially I have 9 workspaces. If I have one app per workspace then each app is full screen. I keep my webbrowser on Alt+1, my editor on Alt+2, chat on Alt+7. If I open a second window I get a vertical split which helps me do things side-by-side if I need to, and if I open more they stack up on the right hand window, automatically sized and I can still cycle between windows with Alt+j and Alt+k like a Windows user might switch between windows with Alt+tab.

The DWM development team has some quirks in that DWM provides no separate configuration. You edit the source code, apply and patches directly to the source code and compile it into a binary yourself, keeping the size and complexity of the window manager small. The custom config I’ve applied (clients per tag, some colour changes and some keybindings) were first committed to a git repo I created in 2013 and still use to this day.

Operating System: Debian Stable

I don’t have much to say about Debian Stable. It’s stable and care is taken by the Debian team to make sure things don’t break. Aptitude is straight forwards, the documentation is good. Many people complain that Debian Stable isn’t very “up to date” but whatever is released brand new today will be in Debian in 2 years. After 5 of these 2 year upgrade cycles in my ten years of using Debian, there’s nothing new or looming that makes me feel like I’m missing out.

Vertical tabs instead of horizontal ones

One thing that’s important to keep up to date in Debian is your web browser. I downloaded Firefox directly from Mozilla, unpacked the tar ball into ~/opt (~/opt is in my home directory) and because it lives outside of root’s permissions it can keep itself up to date and doesn’t damage the stability of my machine. I use the tree-tabs plugin since it provides cleaner visibility when you have 30+ tabs open at once.

To keep my machine clean in other ways I use docker heavily to build applications I develop. I haven’t installed perl, nodejs, mongodb onto my system (which I need for work). Instead I maintain a bunch of docker images that can be thrown away or reset whenever I fancy.

Spotiamb

One thing that will no doubt stick out as odd or niche in my setup is my music player. Created as a little treat by the Spotify team in 2013 this Winamp clone connects to Spotify to play music and has a lot of nice functionality. Yes, including the visualisations you may remember. Winamp made them remove the “amp” hence it’s now “Spotiamb”. It’s not been very easy to maintain this application but I love it so much I persist. It’s only written for Windows and it’s a 32 bit application. To keep it alive I’ve held on to the original installer and milkdrop plugin all these years. I tracked down a docker image designed to do X11 and audio forwarding and so I didn’t have to add 32 bit architecture support to my main desktop. I hope to share this in a blog post soon.

Terminal: Hacked KDE Konsole

I still use a patched copy of KDE Konsole that has hardcoded regex’s embedded inside it so it ingrates with JIRA, the program that tracks the work I do, or should be doing. Read the full post for more information but I feel like it’s a win for open source if end users can make their little changes.

Editor, IDE: Vim / Neovim

Neovim with Scala-metals

I wrote Python and Perl professionally from like 2008-2018 and honestly vim is a fantastic editor. I used to believe in the slogan “Unix is my IDE”, often using Ctrl-Z to put vim into the background, returning to bash and using find, grep etc to help me develop the code. For those lightweight languages that don’t benefit from large IDEs, vim had become the editor I use all day. I had to adopt JetBrains IDEA when I learnt Scala just because of the complexity of the type system. I had tried and failed to readopt vim when working Scala but it didn’t really happen until scala-metals came out a year ago. To be honest I only really wanted accurate “go to definition”, “find references” and “show type” and it gives me those. I use a separate install of neovim as an IDE to keep open all day and use the regular lightweight default vim for quick editing sessions everywhere else.

Physical Setup

Desk with Chair, light, background Bookcase for show
Work laptop on desk, Gaming desktop underneath
So much more keyboard and comfort when a monitor stand is replaced with a mount. Get one!
  • Monitor: 24″ Asus 144Hz Gaming Monitor, 1080p
  • Programming Keyboard: Steel Series M400 UK – Blue Switches
  • CSGO Keyboard: HyperX Origins Alloy Core – Red Switches
  • Mouse: Roccat Kone Pure
  • Gaming Desktop: Core i7 6700K (4GHz) + Geforce 1660Ti
  • My desk arm gives me tons of space to push keyboard around and be comfortable. I literally get the whole desk. See before/after pictures. I couldn’t recommend it enough!

One thing I want to draw attention to (the pictures are too old to show) is a Usb-C hub costing £20. My mouse, keyboard and monitor are connected to the usb-c hub and so is a power charger. It means I have just one cable to put into my laptop and it connects to all the devices and even charges from the same cable. I can just freely come and go between my desk and my living room with only a single cable to connect/remove. Super convenient

16TB Synology Diskstation, Fibre Internet Terminator, 1Gbps Switch

Supporting Equipment

I use next-cloud sync to my self-hosted next cloud instance to backup a dozen files and folders, although I don’t backup the operating system itself. My work laptop is a Dell Inspiron 13 7000 2 in 1 I bought in 2018. I’ve upgraded constantly from Debian 9 without issues and if I need to reinstall so be it, but it’s likely I’d be moving to a new machine at the same time anyway since my environment seems to be very reliable. In my house I have a Synology diskstation with 2x8TB disks (mirrored) and also backed up using the Synology cloud backup service. That provides 3 copies of my important files.

How I got banned from Reddit’s r/Linux forum

I wake up one morning to find this drivel “stickied” to the top of r/Linux. A post from the moderators stating:

  • Readers should stop using social media (such as reddit)
  • your usage of social media spreads mis-information even if you don’t share misinformation

The headline alone is confusing for me. I’ve been a Reddit user for 14 years and actually considered it a “news aggregator” rather than social network. I don’t have any friends on the platform, it’s just a good place to see critiques of news stories. In fact if anything it’s THE place to ensure you don’t swallow misinformation.

I’m not on Facebook, Instagram or Twitter. I stopped using What’s App after Facebook bought it. It’s always interesting for someone on social media to tells others to drop it, but why Reddit allows a moderator to have such an anti-reddit views is a bigger question.

So the author wants to stop people using Reddit, stop people using Social Media and they want to control misinformation. Why then suggest people use decentralised channels to communicate where misinformation is harder to control?

In a moment of not caring I drop this insignificant comment into the chat:

Then I receive my ban

Pathetically authoritarian if you ask me. You do decide what’s appropriate for linux, I just threw out an opinion. Suspecting this was a single mod acting in bad faith I try to appeal my ban but it seems to be the same person

When you ban people for no reason and act like this you embarrass yourself and destroy the community. I think Reddit has a problem with these sorts of moderators who don’t actually contribute to the community, have no investment in a community and rate their own contribution by the number of bans they hand out.

One week later they sticky a “read the rules” post with this gem:

Top violations of this rule are trolling, starting a flamewar, or not “Remembering the human” aka being hostile or incredibly impolite.

I’d classify calling someone “a waste of time” as incredibly impolite. I’m feeling very much like I’m the forgotten human at the end of this ordeal.

Easily Format JSON using KDE Kate (easy technique)

For a long time I’ve wanted to format confidential JSON to easily inspect it without pasting it online sites. I’m not keen on installing huge tools for such a simple action but as a Debian stable user, finding KDE documentation that works for me isn’t always simple, so this method is very low complexity.

I had tried copying scripts and custom commands but for some reason that hasn’t worked for my version of KDE so I present a alternative method that works.

Requirements:

  • Install the lightweight command line jq program
  • Enable the Text Filter app that already ships with Kate in Debian

First, check you have the plugin available and enable it if you don’t

Enable the Text Filter plugin

Paste your json payload into Kate and select it all and then go Tools > Text Filter

Remember to use SELECT ALL

Enter the command jq ‘.’ into the dialogue and press enter. Voila it’s formatted it.

The result

Phillip Taylor

Quickly configure a keycloak server for Single Sign On

I’m writing a tutorial on how to make Single Sign on work with the Play Framework in Scala and how to integrate Silhouette authentication library with Keycloak. (this isn’t published yet though) One part of that tutorial is spinning up a Keycloak server you can run your app against. These are the minimal steps required to get something running.

  1. Spin up a keycloak server in a local docker instance
  2. Add a client app with a secret
  3. Add a test user with a username/password combo

1. Start and configure a local Keycloak server for testing.

The following command requires docker to run. This will start Keycloak locally, listening on port 8080 with the username and password of admin/admin. It uses an InMemory database so be aware that changes are lost when the container is stopped. For this tutorial however this is acceptable.

docker run -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -e DB_VENDOR=H2 -p 8080:8080 -p 9990:9990 jboss/keycloak

Once it’s running you should be able to navigate to http://localhost:8080 to access the main page (shown below)

Click on the “Administration Console” link above and use the username admin and the password admin to log in. Below is a screenshot of the main screen “The Master Realm”. It’s too much work for me to explain how Keycloak and Realms work except to say that one Keycloak instance can manage dozens of upstream and downstream auth providers and applications and each realm is a segregation of users and permissions in some way. The master realm typically controls the auth for the Keycloak instance itself and each app would have it’s own realm. For this tutorial we are going to breeze over this and do the minimum.

Click on “Clients” in the left hand navigation menu. A client is an application that can use the Realm. Think of Keycloak as a database of users. Clients are programs that login to query it. (So clients are user accounts to log in to the “user database” where users are just data.) We’re going to write a Keycloak client app in the next tutorial below so we need to tell Keycloak who we are and how we’re going to connect. Click on Clients and then click Create in the top right corner of the table. See the image below.

Here I’ve entered the ClientId. Make a note of this! I’ve also said what the Root URL for the project is, and for Play applications it’s port 9000 by default. Keycloak needs this because it checks “referers” [sic] and redirects users to and from our site. Therefore it needs to be http://localhost:9000 if that is where your app is running and you’re following my Scala guide. Once you save this new client you will be taken to a screen for configuring it (shown below):

When this page opens you will not have a “Credentials” tab but you need one! You should change the Access type from public to confidential and hit save. Then the credentials tab will appear. The credential page is shown below:

On the credentials tab we can now see the secret. Make a note of this secret.

Now we have a working Keycloak server with a clientId and secret that enables another program to login to this server. What we need next is an actual user account we can use for testing and logging into the app we write ourselves. Lets click on the “Users” tab (Under Manage) and click “Add User” in the top right corner of the users table.

You can fill this form in however you want, I don’t really care, but make a note of the username and email! Save the user. Then go to the Credentials tab shown below

First turn off the temporary option and then enter a password. I recommend “pwd” but whatever you choose make a note of it. Then click Reset Password and confirm the prompt when it opens.

Now you’ve done this, I highly recommend you put an email address against the admin user. It can lead some people who are both configuring the app and testing it at the same time to run into some confusion and doing this helps. For the Scala tutorial you might want to create a user called Sinclair who has the exact email “sinclair@example.com” in order to make the Scala example work right-out-of-the-box but this isn’t that important.

Your system is now minimally functional for the Scala tutorial. I haven’t written this tutorial yet.. so please wait.. If you wander off and change other settings do remember all those settings are lost when you stop the container! When we’ve finished setting up Keycloak we need to take away some details from it to use in our application. So far we should have:

  • A client id (keycloak-seed)
  • A client secret (different for everyone, mine is 45cb055e-d93c-4a14-a4ce-43c2bc0c1414)
  • A user account with a username name and password (mine are sinclair/pwd)

What we need are the special keycloak urls to connect to. Click on Realm under Configure to go back to the main page.

See this link next to Endpoints that says OpenId Endpoint Configuration. Click it and read the json (use a formatter to help you if your browser sucks)

We need and care about the following URLs that we’re going to use in our app:

authorization_endpoint: http://localhost:8080/auth/realms/master/protocol/openid-connect/auth
token_endpoint:         http://localhost:8080/auth/realms/master/protocol/openid-connect/token
userinfo_endpoint:      http://localhost:8080/auth/realms/master/protocol/openid-connect/userinfo
end_session_endpoint:   http://localhost:8080/auth/realms/master/protocol/openid-connect/logout

We now have a running server and the following information:

  • Authorization Endpoint
  • Token Endpoint
  • UserInfo Endpoint
  • End Session Endpoint
  • ClientId
  • ClientSecret

This is everything we’re going to need in our application, so now we’re ready to move to the Scala part of my tutorial (which is not available yet but will be published soon).

IF == BAD

This post is essentially a write up of a talk my friend Johan Lindstrom did years and years ago, which in turn are ideas stolen from other people. This advice is aimed at really novice programmers who heavily rely only the initial pieces of knowledge they leverage when they start out. I don’t see this advice shared a lot online despite being common knowledge in some circles so please forgive me if you think it’s overly simplified beginner stuff.

IF statements in programming are bad. Johan and I worked on an warehouse backend system. One that involved taking orders, reserving stock, doing stock checks etc. At the time we had two warehouses, DC1 in England and DC2 in America, so code would often look like this (examples are transposed from Perl into Scala):

if (warehouse == DC1)
    startConveyorBelt()
else
   showManualInstructions()

Our code was absolutely full of these bad boys. Hundreds upon hundreds of separate statements throughout enormous legacy monstrosity. This code base will celebrate it’s 20 year anniversary next year.

def printInvoice(warehouse :String) = {
    val address = if (warehouse == "DC1") "England" else "America"
    val papersize = if (warehouse == "DC2") USLetter else A4
    val invoice = generateInvoice(address, papersize)
    ...
}

Of course, when we added a third warehouse nothing worked and it took an enormous effort to isolate all the behaviour and fix it. Some of the changes were in little blocks that went together. IF <something> assumes a key exists in a map etc or that a function had already been called. Adding the third DC didn’t result in a random blend of features. Just unpredictable crashes and a world of pain.

The way == or != were used would shape the way the default behaviour would play out. Stringification and easy regexs in Perl also made it harder to track down where comparisons or warehouse specific logic even resided.

warehouse.toLowercase == "dc1"    // lowercased alternatives

wh == "DC2"                       // alternative names

warehouse.matches("1")            // regexes are seamless in Perl
                                  they aren't so unnoticeably odd

if (letterSizeIsUSLegal)          // warehouse derived from something
                                  set earlier and not passed through

Perl doesn’t have the support of rich IDEs to help track references and all these different programming styles that have grown over 20 years means the process of finding these errors involves dozens of GREPs, lots of testing and a lot of code base inspection.

It didn’t take too long to realise that our IF statements should be based on small reusable features (ie. modular reusable components) and not switch on a global “whole warehouse” value. This code would have been much easier to manage:

if (warehouseHasConveyorBelts)
    sendRoutingMessage()
else
    showDestinationPageOnScreen()

if (shipmentRequiresInvoice) {
   val invoice = getInvoiceTemplateForCountry(
         getWarehouseCountry(warehouse)
   )
   Printer.print(invoice)
}

Ultimately however, the problem also extends passed this modularity and the realisation that IF statements themselves are bad. Necessary in a few places and possibly the simplest fundamental building blocks of all programs… but still bad… Lets look at a comparison to find out why.

The history of goto

Many languages like C, C++, Java, VB, Perl etc support the GOTO keyword, which is a language construct that allows you to jump around a function by providing a label next to a statement. GOTO will jump to the named label. Here is an example.

#include <stdio.h>

int main(void) {
	
	int someNumber = 0;
	int stop = 0;
	
	BEGIN:
	
	if (someNumber < 23)
	  goto ADD_THIRTEEN;

	  printf("hello. app finished with someNumber = %d", someNumber);
	  stop = 1;
	
	ADD_THIRTEEN:
	  someNumber += 13;
	  if (stop == 0)
		goto BEGIN;
	
	return 0;
}

The code is really difficult to read since execution jumps around all over the place. You may have difficulties even following the simple example above. Tracking the state of variables is really hard. Pretty much everyone is in agreement that GOTO statements are too low level and difficult to use and that IF, FOR/WHILE/DO loops and a good use of function calls actually make GOTOs redundant and bad practice.

Foreach loops are so much more elegant than GOTO statements because it’s obvious that you’re visiting each element once. It really speaks to the intent of the programmer or algorithm. Do-while-loops make it obvious the loop will always execute at least once. Scala supports .map, .filter, .headOption, dropWhile, foldLeft which all perform very simple well defined operations that convey intent to other people reading that GOTO simply cant.

So if a construct like GOTO is confusion, leads to spaghetti code, and can be replaced with more elegant solutions should we not prefer those alternatives? Of course! IF statements scatter your business logic around and leave it in disjointed locations across your code base that are hard to track, follow and change. They make refactoring hard. IF statements are bad for the same reasons that GOTO statements are bad, and that’s why we should aim to use them as little as possible.

Switching it up

Here’s a collection of constructs that can be used instead of IF statements to keep your application more readable, and more easy to follow and maintain.

Switch Statements

Not exactly much of an improvement, especially in most languages, but Scala’s specifically can be. If your choices extend a Sealed Trait, Scala can warn you which switch statements aren’t exhaustive. No DC3 slipping into DC2’s warehouse code paths!

sealed trait Warehouse
case object DC1 extends Warehouse
case object DC2 extends Warehouse
case object DC3 extends Warehouse

val myWarehouse :Warehouse = DC1

myWarehouse match {
   case DC1 => println("europe")
   case DC2 => println("america")
}

// scala reports: warning: match may not be exhaustive.
// It would fail on the following input: DC3

Option.map

A super common one, especially for Scala is to map over an optional value only doing something if it exists and doing nothing if it isn’t. This is the functional equivalent of an “if null” check.

invoices.map { invoice => invoice.print() }

Map is way more generic than this. It applies a function to a value inside a Monad and is commonly used to manipulate lists. Please don’t punish my brevity, it’s just an example for my own ends.

Inheritance

Inheritance allows you to override the behaviour of an existing object to do many specific things so it’s absolutely perfect at reducing the use of IF.

trait Warehouse {
  def hasAutomation() :Boolean
  def address() :String
  def isInEurope() :Boolean
}

class DC1 extends Warehouse {
  override def hasAutomation = true
  override def address = "England"
  override def isInEurope = true
}

class DC2 extends Warehouse {
  override def hasAutomation = false
  override def address = "America"
  override def isInEurope = false
}

class DC3 extends Warehouse {
  override def hasAutomation = false
  override def address = "Europe"
  override def isInEurope = true
}

// App is set up once.
val warehouse = if ("DC1") new DC1 else new DC2.

// use in code
if (warehouse.hasAutomation && warehouse.isInEurope)

 ...

When it comes to adding DC3, we have an interface to extend so we know exactly which methods we need to define in order to specify how a warehouse behaves. Our behaviour is vastly centralised. We only have to extend the initial warehouse setup once as well since we’ve bought everything together.

We can also go a step further and make the Warehouse class responsible for doing things. This removes IF statements even more!

object Printer { def print() = ??? }
object Browser { def handle() = ??? }
case class RoutingInstruction(destination :String)
val REDIRECT = 303
type Invoice = String

trait Warehouse {
  def packItem() :Either[String, Boolean]
  def generateInvoice() :List[Invoice]
  def maybeRouteItem() :Option[RoutingInstruction]
  def getNextWebpage() :Option[(Int, String)]
}

class DC1 extends Warehouse {
  override def packItem() = Right(true)
  override def generateInvoice()  = List.empty // no invoice since we are in england
  override def maybeRouteItem() = Some(RoutingInstruction("PackingArea11")) // we have automation
  override def getNextWebpage() = Some((REDIRECT, "/confirmation/place-on-conveyor"))
}

val warehouse :Warehouse = new DC1

// look, no if statements yet lots of diverse functionality
// being used.

warehouse.packItem()

warehouse.generateInvoice.map { Printer.print }

warehouse.getNextWebpage.map { Browser.handle }

There are some variations of Inheritance I won’t cover, such as Mixins and Traits or Interfaces. They all follow the same theme so I won’t list them individually. The code might be a little crap here because I’m trying to be slightly language independent in my samples.

Function Pointer Tables

You can effectively have cheap object orientation by having a Hash/Map of functions and passing around whole “collections of decisions” together.


def accessGranted() = println("granted!")
def accessDenied() = println("denied!")
val permission = "allowed"

// old, redundant.
if (permission == "allowed") accessGranted() else accessDenied()

// single place for logic.
val mapOfAnswers = Map(
    "allowed" -> accessGranted _,
    "denied" -> accessDenied _
)

val func = mapOfAnswers(permission) // no if here

func() // executes function which causes println to run

Partial Functions / Closures

Partial functions allow us to build functions using composition which can help mix up and select the appropriate logic without actually having to use IF statements.


def makeAddress(inEurope :Boolean)(country :String)(addressLines :String) =
    println(s"$addressLines\n$country\ninEurope: $inEurope")

val europeanFactory = makeAddress(true) _    // variables type
                                             // refers to a function
val britishFactory = europeanFactory("UK")

britishFactory("London")

Closures are functions that reference variables outside of their direct scope. It allows you to do something like this:

def setTimeout(timeMs :Int, onTimeout :Unit => Unit)

val myVariable = 66
def doingMyThing() = println("myVariable")

setTimeout(500, doingMyThing) // setTimeout doesnt have any logic
                                 but does the right thing

Lambdas are typically short hand syntax for functions so this general class of ideas can be used to encapsulate decision making without callers having to use IF statements everywhere.

Dependency Injection

Dependency injection is generally a technique to remove global variables from an application and is just an application of inheritance to a certain degree but it’s perfect for dynamically changing the behaviour of code without using repetitive IF statements.

// Old code with embedded IF statements

class FetchData {
   def fetchOrders() :List[Order] = {
      if (testMode == true)
        List(sampleOrder1, sampleOrder2)
      elseif (DC == 1)
        httpLibrary.httpGet("http://backend-1/")
      else
        httpLibrary.httpGet("http://backend-2").andThen(doConvert)
   }
}

// New version simply trusts whatever is passed in.

class FetchData(httpLibrary :DCSpecificHttpLibrary, convertor :Option[Order => Order] = None) = {

    def fetchOrders() :List[Order] = {
       val order = httpLibrary.httpGet() // was built knowing which DC
       convertor.map { c => c(order) }.getOrElse(order)
    }
}

// testing code would make a fake httpLibrary and pass it in before the test. Real code would use the real one.

Summary

I’m going to stop list alternatives now but hopefully you go away with some interesting thoughts on the subject and possibly an idea that sometimes IF statements can be detrimental if overused.

Some of my examples are really poor, especially my Inheritance one. I was going to model lots of subprocesses of a warehouse like ScreenFlowChartManager, StockCheckManager and make a warehouse point to them but the code was getting too big for a simple example.

I would accept some criticism that some IF statements can’t be avoided and I would accept that some of these alternatives only move the IF statement to another place in the code base. Certainly dependency injection only moves things to when the application starts. Still armed with this knowledge you can write applications which are easier to maintain and move your variables and mutable state around into places that make it easier to work with.