How to use Scratch files in Android studio

While working on Android project, you’ll run across situations where you just want to prototype a new idea or validate output of small code block from given code.

For example, you want to apply multiple operators to a list but want to validate the intermediate output.

Let’s say you have a list of fruits and you’ve applied multiple operations on it

val list = listOf("Apple", "Banana", "Mango", "Orange"
val modifiedList = list.filter { it.length > 5 }.map { "Fruit: $it" }.asReversed()
println(modifiedList))

Now, if you want to validate the output of below operation separately, how would you do that?

list.filter { it.length > 5 }

Meet Scratch Files

Scratch files are fully functional, runnable, and debuggable files that support syntax highlighting, code completion, and all other features for the corresponding file type. You can use scratch files to draft Kotlin Java code constructs, HTTP requests, JSON documents, and so on. Scratch files are not related to a specific project.

To create a scratch file, 

1. Select the code block to be validated. Skip this step if you wish to start from scratch

2. Press Ctrl+Alt+Shift+Insert (or Go to File -> New-> Scratch File)

3. Select the language of the scratch file.

You can conduct your experiments in the newly opened Scratch file.

Share with your friends

Leave a Reply

Your email address will not be published. Required fields are marked *