Anyone else annoyed by the "variable declaration c...
# announcements
p
Anyone else annoyed by the "variable declaration could be inlined" inspection? It wants me to inline this:
Copy code
val imageSource = suspendCoroutine<ImageSource> { cont ->
        MaterialDialog(context).show {
          listItems(
            items = ImageSource.values().map { getString(it.nameRes) },
            selection = { _, i, _ ->
              val imageSource = ImageSource.values()[i]
              cont.resume(imageSource)
            }
          )
        }
      }
      val uri = when (imageSource) {
        ImageSource.GALLERY -> {
          module<SelectPictureFromGalleryModule>().take()
        }
        ImageSource.TAKE_PICTURE -> null
      }
into this:
Copy code
val uri = when (suspendCoroutine<ImageSource> { cont ->
        MaterialDialog(context).show {
          listItems(
            items = ImageSource.values().map { getString(it.nameRes) },
            selection = { _, i, _ ->
              val imageSource = ImageSource.values()[i]
              cont.resume(imageSource)
            }
          )
        }
      }) {
        ImageSource.GALLERY -> {
          module<SelectPictureFromGalleryModule>().take()
        }
        ImageSource.TAKE_PICTURE -> null
      }
s
Yes. Its too often a matter of style and just straight up debug hostile
k
#C0B8H786P but just turn it off it's 3 keystrokes.
p
You expose
imageSource
outside of
when
scope but do not use it anywhere else that is why it reports it. But at least for me 1st snippet is easier to read.
p
I agree, it would be better if inspection is not reported if variable declaration takes more than 1 line. Maybe you can make a suggestion on https://youtrack.jetbrains.com/issues/KT ?
3
p
l
Why not use
suspendCancellableCoroutine
and handle cancellation and dismissal properly?
p
Will do, thx
t
it has been years since I read it, but if I remember correctly Clean Code (the book), would suggest to use a method rather than a variable in that case. that would also solve the problem of exposure mentioned by @Paulius Ruminas but, it is a matter of taste honestly
l
@Paul Woitaschek BTW, maybe the source of this module can be a good starting point for you: https://github.com/LouisCAD/Splitties/tree/master/modules/alertdialog-appcompat-coroutines
k
That would make the problem way worse, now the scope is the entire class.