Skip to content

Files

Latest commit

 

History

History
37 lines (26 loc) · 644 Bytes

AlsoCouldBeApply.md

File metadata and controls

37 lines (26 loc) · 644 Bytes

Pattern: also could be apply

Issue: -

Description

Detects when an also block contains only it-started expressions.

By refactoring the also block to an apply block makes it so that all its can be removed thus making the block more concise and easier to read.

Example of incorrect code:

Buzz().also {
  it.init()
  it.block()
}

Example of correct code:

Buzz().apply {
  init()
  block()
}

// Also compliant
fun foo(a: Int): Int {
  return a.also { println(it) }
}

Further Reading