blog productivity psychology

Don't trust yourself, trust your daily routine!

The power of environment

Most of our daily decisions are automatic, and most people do not even realize that. The brain is a very efficient machine, everything becomes a habit after a while, meaning that who you are today is mostly habits and a small part of it is your active thinking. If you do not believe me, go back through your day and try to find at least three events where you stopped, thought about what are you doing, or you questioned your decision, then did that thing.
So why do we ignore our habits when we want to change and reach our maximum potential?
Do you check your phone every 5 minutes while you work? Do you check your emails every 10 minutes while you work? If you do something without thinking about it, that is a habit you built over some time, and if you stack multiple bad habits on a daily basis it may be the biggest cause of your low productivity and stress. You know you have to improve your work, but you simply can’t, you end up on the same level you were a month ago, or you descend few levels.
If you do not feel any progress in quality of your work from month to month, it is time for you to seriously reconsider changing your current work environment.

Environment by design

As with anything in life, you can passively receive things, and wait for someone to shape you, your life, or you can be proactive, a habit worth acquiring, and shape your environment so you can shape yourself the way you want to be.
I used to catch myself sometimes spending few hours on browsing facebook and youtube without realizing it and I don’t know how I got to that point at all! I do not remember the point where I decided to open facebook or youtube, so how did I end up wasting the last few hours on pointless entertainment?
I realized I need to proactively block my access to those sites, so I edited my hosts file and redirected facebook and youtube to So every time I want to open facebook or youtube, I have to open hosts file, remove the redirect command, close the file, restart all browsers and access facebook and youtube. And that process cannot be automatic, I need to do that intentionally, and that was enough for me to prevent that habit of opening facebook and youtube without even thinking about it.
The next thing I did was to leave my phone in the other room, so I don’t even see the notifications when they pop up, I am free to focus on my work.
I did leave facebook or youtube available on my old laptop, so when I want to watch youtube or open facebook, I would take the laptop and access those sites, but that would be on my own terms when I am satisfied with the work I did, and I want to take a longer break.

Prevention beats cure

You can not trust yourself, your brain is on autopilot most of the time, don’t count on your brain ignoring phone notifications, don’t count on not opening youtube or facebook, you are driven by habits, facebook and youtube specifically exploit that by creating the dopamine loop that forces you to go back to those sites and engage with them frequently.

How we spend our days is, of course, how we spend our lives.

The quote above is from Annie Dillard, and there is a great article on brain pickings about her book The writing life, a great piece of worldly wisdom.
In summary, place systems that produce environments that nudge you towards high focus and productivity, and you will be more consistent with your work quality, otherwise, you are just responding to outside stimuli.

  • Environment has a stronger impact on your productivity than you think
  • The only way to make sure you are focused is to eliminate the possibility of a distraction (leave your phone in the other room, block sites)
  • Purposely create environments for your self that nudge you towards productivity (turn of wifi on your phone, leave a book on your living room desk, sell your TV)
  • How I constrain my internet usage
  • If you feel like you need to relax, don’t open youtube, take a walk or go talk to someone
  • Avoid instant gratification to get used to low dopamine input and to increase your ability to focus, deep work
  • Be aware of your decisions when you perform your work, question everything to produce quality work
blog psychology

Quit drinking coffee by quitting your job!

How to stop your coffee drinking habit

It is a well-known fact that once you repeat something enough times it becomes a habit. You may think you can not live without some lifelong habits you built, but if you manage to eliminate the habit trigger, you can actually remove that habit from your life without much effort. The quest then becomes to find the trigger, once found the process of overcoming that habit can begin.

Identify habit triggers

So how can you stop that coffee drinking habit that you know is a burden and unhealthy? One way is to find all the triggers that nudge you to think about drinking coffee. Let’s start with your job, most probably the majority of your coffee intake is during your work hours. So how many triggers can you identify at your current office that lead to drinking coffee? I think you can count at least 5 common scenarios, for example coming to the office in the morning and going straight to the coffee machine, or returning from a lunch break and seeing your coffee mug empty. So if there are so many different triggers, can you really stop drinking coffee at the office?

Willpower and behavioral change

When we think about behavioral change, what is the first thing that comes to our mind? Willpower, “yes I will resist the urge of drinking coffee next time I have it.” But how many times did that end up working? For me, it was never!
Using willpower to stop a habit is not impossible but it is very close to impossible. One problem with willpower is you have a fixed amount of it, one day you will have enough willpower to resist your temptation, another day you may not, and on that day will fall back to your old ways and realize your habits are simply stronger than you, and what is the point of fighting something that is stronger than you?

Environment and behavioral change

So if willpower does not work, what will work? There must be something that can be done about quitting habits! Let me drop a quote here:

There’s just one way to radically change your behavior: radically change your environment.
—Dr. B.J. Fogg, Director of Stanford Persuasive Lab

The idea is simple, with all the triggers at your current environment, trying to fight it with willpower will not work, it is far easier to change your environment and be careful not to create bad habits in the first place! If you are familiar with mental models, particularly the inversion mental model, you can immediately understand why would this work. Instead of fighting the habit directly, try to reverse the problem, don’t fight the habit, try to eliminate the cause of the habit, the habit trigger, once you remove the trigger, quitting a habit is an easy task.

New environment, new you

Now lets present a solution for our coffee drinking problem: instead of fighting it directly at your current office, quit your current job, get another job, which means moving to a new environment, resists drinking coffee at the new environment on your first day, and just like that, your urge to drink coffee went away! By each passing day, it becomes easier and easier to resist it! If you can hold off drinking coffee at your new job for about two months, congrats, you successfully removed the excessive drinking habit you thought you can not live without!
Actually, don’t quit your job! Wait until you get a better offer, switch jobs, then try this and let me know if it worked.
Actually, if you have health consequences from all that caffeine, then yes, quit your job, get another one and try this!
Keep on coding healthier!

blog kotlin

Quick introduction to Kotlin coroutines part 2

If you have not read part one of this series, please read it first, it explains the main idea behind Kotlin coroutines.

Error handling

If you read the part one of this series, you may notice that error handling was not covered as a topic at all, so in this post, we will go through techniques you can utilize to handle errors that may occur inside coroutines.


The main idea introduced in part one of this series is the idea of async code written as a synchronous code and if you think about it, how would you handle errors in a sync style code, you catch exceptions. Same idea here, we can surround code that throws an error inside try/catch block, and handle error states in your async code. Let’s see an example of a retrofit network call.
As we can see in the example above, we surrounded the retrofit method execute with try and catch blocks, and two exceptions are caught that execute method can throw, IOException and RuntimeException. Why just these two exceptions? If you look at the documentation for execute method, you can see that execute method can throw only those two exceptions. In case an exception is thrown, the user will be notified, and that part of the code runs on the main thread.
A better way would be to pass in an exception handler that will be called once the launch coroutine encounters and exception.

Async vs launch builder

In part 1, we mainly used launch coroutine builder as our main coroutine block, and one problem with launch builder is the exception handling, if an exception is not caught inside a coroutine, the whole app will crash. Kotlin provides another coroutine builder, Async builder, which behaves differently in few ways. One way in which it differs is exception handling, async block just ignores thrown exceptions if they are not caught resulting in a non app crash scenario. Here is a way to catch an exception with async block


What if you need to cancel a coroutine, for example, the android activity gets paused or destroyed, and the coroutine result is no longer needed?


All coroutine builders return a job, which can be canceled at any point by calling job.cancel(). What is interesting with Kotlin coroutines is the ability to specify a parent job as a context for multiple coroutines, and calling cancel() on the parent coroutine will result in all coroutines being canceled, very convinient. In all examples above you may notice the parentJob passed in in every coroutine, and calling parentJob.cancel() inside onPause() activity method results in all coroutines being cancelled.

Hooking into callback code

The last thing I want to talk about is hooking into code that is async but uses callbacks to handle the suspension of the flow. For example, firebase database uses callbacks to inform the caller an operation has completed. In that case, you need to bridge between a Kotlin coroutine and a callback listener. SuspendCoroutine to the rescue! A special coroutine which will be suspended by default, and you call resume on it inside the callback method. Below is an example from one of my apps that uses google firebase as a backend.
This getBooleanValue can be used inside any coroutine and will behave as any other suspend function, hence creating a bridge between Kotlin coroutine and callback.


I have created a sample app that uses all exception handling cases shown in this blog post on this Github repository.
If you liked the article share it if you loved it comment below :).

blog kotlin

Quick introduction to Kotlin coroutines part 1


So first, let me start this article by saying that I spent few years developing Android apps in Java, and the one thing that I used to do frequently was to separate layers of abstraction with interfaces. And for example in the networking layer, I end up writing lots of interfaces that declare methods, with a listener parameter, and this listener will implement some onSuccess and onFail methods. What this leads to is lots of nested callbacks where I go few layers deep into callbacks and onSuccess and onFail blocks, something like the code below.
Then Kotlin came along, got supported by Google officially and suddenly, everyone went mad, and for a good reason, it is a modern swift like, compiled, language, which is a huge step up from the Java world. And one thing caught my eye when I first read through the Kotlin documentation, Kotlin coroutines. I thought, well this for me, the one thing I can use right now to avoid my callback hell problem. So I jumped straight into it. Here is my journey to understanding what Kotlin coroutines are.

Asynchronous code as a synchronous code

The header above is the essence of what coroutines are, it is a way to declare a block of code in which every line suspends execution of the block, what does that mean? Well, you simply don’t have to provide a callback for long-running functions, you just add that function to a coroutine and the coroutine will suspend its execution until that method returns hence no more callbacks!
Look at the code below:
This is my interface for the data storage contract in Kotlin, no listener interface needed, you will notice the suspend keyword in front of all methods, that is a special Kotlin syntax to indicate this function should be run from a coroutine block or another suspend function, basically anything that involves a network call or a CPU intensive task should be placed inside suspend functions and then consumed from a coroutine block.

Coroutine builders

If we want to call our suspend function, we need to pass it to the coroutine builder, which will do the heavy lifting for us, and ensure that the code inside that builder will run on a thread of our choice, for example, let’s use launch coroutine builder:
That’s it, you simply consume your suspend function inside the launch block and you are good to go.  Every time a suspend function is encountered, the execution of this coroutine block suspends, waiting for the function to return. Any code above our getStringValue method call will be executed first, then our function, then the rest of code below, no callbacks! So on which thread will the above launch block execute? Obviously, we want it to execute on a background thread and we want to update the UI with values from our suspend functions.

Coroutine context

Every coroutine builder can have a context in which to run, the main thread, or some other thread, if no context is specified, like the example above, it’s the CommonPool of threads, meaning it will run on a separate thread from the main thread of the app. So if the code above is running in the background thread, how to update the UI then? To update the UI there is one more step that needs to be done, and its another launch block with a coroutine context specified, like the example below:
Everything inside the second launch will use the main thread event dispatcher, that will post our update on the main thread. What is cool about this is that the second launch block will also wait for the code above to execute and once it does execute, the value variable will be set and we can safely update our UI. Coroutine context is simply a coroutine dispatcher, which controls on what thread the coroutine block is running.

Coroutines are lightweight threads

Coroutines run on threads and can suspend its execution without blocking the thread it runs on, so the thread execution is not tied to the coroutine execution, they operate separately. If you think about it, callbacks are almost the same thing, they suspend execution and continue once the result is available, but have a boilerplate syntax and require you to repeat yourself everytime you want to suspend an execution.

Launch vs Run coroutine builder

One problem with nesting coroutines is that every coroutine block will have its own execution line, meaning the second coroutine block will not suspend the parent coroutine. There is a better way of writing the code above, since getStringValue  function returns a value, we can use a run coroutine builder to return a value from a suspend function, hence we can start on the main thread, and invoke our getStringValue function inside a background thread, the other way around from the example above.
The main difference between launch and run coroutine builder is the return value, launch has none while run has, so in this case, we will assign our variable value to whatever the result value of the run block is.
I specified a dispatcher for run block intentionally since nested coroutines take the parent coroutine context if not specified, and we do not want that in this case, we want the run block to execute on a background thread.
I believe this is all you need to know to start playing around with coroutines, don’t hesitate to open android studio and write some coroutines!

Further reading

Coroutines can do much more if you need it, there are many different coroutine builders, channels, synchronization options etc. The best introduction to coroutines is the official Kotlin coroutines guide on Github, don’t hesitate to read it, you will find lots of gems in there.


To use coroutines in your project, you need a couple of additions to your build grade file, mainly these two lines:
The coroutines-android dependency is for the special UI context that can be passed to coroutine builders that uses the main thread dispatcher to dispatch events on the main thread.
Then you have to tell Gradle that we will use coroutines in the app by adding these lines to the app build Gradle file:

Github sample project: Fibonacci counter

I wrote a simple Android app in Kotlin, that implements a Fibonacci counter with coroutines. Check it out on Github, clone the repository and play with it and most importantly have fun!


This is a part one of two-part series, check out part two of the series.