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!

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using 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
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25582 756
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Sub Categories: