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!

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
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
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
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’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
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
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
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
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
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
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
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New