Javaru

Javaru

Kotlin Coroutine Confidence: Add formatter off/on comments (pg 162)

As a minor suggestion, in timers/v19/src/main/kotlin/com/example/timers/TimerApplication.kt, you might want to consider adding // @formatter:off and (optionally) // @formatter:on comments:

val timer = java.util.Timer()

// @formatter:off
fun main() {                               /* Secret hidden brackets */
  println("3…")
  timer.schedule(1000)                         {
  println("2…")
  timer.schedule(1000)                         {
  println("1…")
  timer.schedule(1000)                         {
  println("Liftoff!")
  timer.cancel()                               } } }
}
// @formatter:on

While this appears fine in the book, and should appear as desired in the code in someone’s IDE, there is the possibility that someone might reformat the source code at some point, such as when first starting out. (I have been known to do that sometimes.) In such a case, the non-standard formatting being used in this example would be lost. You could even just put the // @formatter:off comment before the import statement so it is out of the way, and wouldn’t show in the book’s snippet.

Of course this assumes the user has the “Turn formatter on/off with markers in code comments” option enabled. (IntelliJ IDEA documentation). And to be honest, I can’t remember what the default is. You could potentially add a .editorconfig file to the root of the downloaded source with this enabled:

ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = true

The EditorConfig plugin is a bundled plugin, and so this file will take effect.

If you do add the .editorconfig file, you may also want to include the indenting settings since the 2-space indent being used may conflict with a user’s more typical 4-space default:

[*]
indent_size = 2
indent_style = space
tab_width = 2
ij_formatter_off_tag = @formatter:off
ij_formatter_on_tag = @formatter:on
ij_formatter_tags_enabled = true

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

Good point, I understand why you bring it up! I’m the same, I’m constantly running the autoformatter on autopilot using keyboard-shortcut muscle memory.

Even though running this code example in the IDE isn’t too important—since the visual appearance of the source code is more important than what it actually does—I can see why testing it out would still be instructive. Including the // @formatter:off comment will make that easier for many people, and won’t do any harm for the rest, so I’ll certainly consider it—along with a note in the text. I’ll stop short of the editorconfig step, though. Anyone who has changed the settings away from their defaults is probably already aware, as you are, of how to fix the problem for themselves.

Thank you for the detailed suggestion!

Where Next?

Popular Pragmatic Bookshelf topics Top

jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
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
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
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
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1032 17402 381
New
AstonJ
Or looking forward to? :nerd_face:
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
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
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
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Sub Categories: