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

joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
brian-m-ops
#book-python-testing-with-pytest-second-edition Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
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
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
New
gorkaio
root_layout: {PentoWeb.LayoutView, :root}, This results in the following following error: no “root” html template defined for PentoWeb...
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

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
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
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
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
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: