Skip to content

Kotlin Basics : Using Scopes in kotlin

Devrath edited this page Dec 24, 2023 · 1 revision

About Scoping

  • The scope is the boundary of an operation or a block.
  • Any variable defined in scope is accessible inside the block only but it is not accessible outside the block.
  • Scopes are important for performance and code clarity.
  • We should not create the variables at the top of the program, cluttering the very beginning of the program.
  • We should scope the variable in a relevant part where it is needed.

Advantage of scoping

  • Using the scoping the advantage is that, Say if the control of the code does not reach a particular part of the scope. In such a scenario, the memory is not created for the variables inside the scope thus saving the memory needed to create the variables in the first place.
Clone this wiki locally