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

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
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
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
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
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
New

Sub Categories: