Javaru

Javaru

Kotlin Coroutine Confidence: Add location indicators to output (pg 80)

For coffee/v3/src/main/kotlin/com/example/coffee/CoffeeApplication.kt, on pg 80, it states that the output will be:

Done launching two child jobs!
coffeeJob.isActive is: true
coffeeJob.isActive is: true
coffeeJob.isActive is: true
Prepared some piping hot water
Prepared some freshly ground coffee
coffeeJob.isActive is: false

However, for me, the first two lines are consistently in the opposite order:

coffeeJob.isActive is: true
Done launching two child jobs!
coffeeJob.isActive is: true
coffeeJob.isActive is: true
Prepared some freshly ground coffee
Prepared some piping hot water
coffeeJob.isActive is: false

This is likely just one of those mutlithreading things. The variance might even be OS related, (Or maybe one of my CPU cores is just extra hyper today after sampling too much of the coffee we have it brewing :slight_smile: ) But it got me wondering about if that first output was from the loop, or if it was from that last println() in the main() method. (I was 99% sure it was the loop, but wanted to verify.) So I made a code change to add a location indicator:

  while (coffeeJob.isActive) {
    println("(loop) coffeeJob.isActive is: ${coffeeJob.isActive}")
    delay(2.seconds)
  }
  println("(last) coffeeJob.isActive is: ${coffeeJob.isActive}")

This gave me the output of:

(loop) coffeeJob.isActive is: true
Done launching two child jobs!
(loop) coffeeJob.isActive is: true
(loop) coffeeJob.isActive is: true
Prepared some freshly ground coffee
Prepared some piping hot water
(last) coffeeJob.isActive is: false

Which cleared things up. I think this change might be a useful change in the example. I mentor some high school students in programming, and I always recommend to them when using logging or println() statements – especially when using it to trace a program – to avoid having the exact same message in two places. That will prevent confusion, or a bad assumption, as to which line is printing that message. I think that is practical suggestion here. I think it just makes what is happening clearer to the reader.

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

That’s a great suggestion, thanks Mark! I’ll have to remember that one for my future work too. I can definitely see why having two identical println() calls in the same program would make it harder to match up the code against the output.

I’ll definitely make a change to this code example so that it’s clear which line of code corresponds to which line of output. I might also add a note to let readers know that their output might not match the book’s exactly.

Great feedback!

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New

Other popular topics Top

AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New