Level Up Your Daily Coding: Unveiling Four Lesser-Known Tools in Android Studio | by Hadi Dortaj | Jan, 2024

This feature is incredibly powerful, revealing the tree structure of the workflow. It allows for improvements as needed. In scenarios like Jetpack Compose, with many nested composable functions, this tool can be a lifesaver. You can execute the analysis on the state of ViewModels to see how the view uses them and how the ViewModel sets values for them. For more information about this tool, refer to the official website.

3. Refactoring Tools

Refactoring is a crucial practice in software development, involving the restructuring of existing code to improve readability, understandability, and maintainability. In the past, developers undertook this task manually, ensuring changes didn’t introduce bugs. However, modern Integrated Development Environments (IDEs), such as Android Studio, provide a variety of automated refactorings, streamlining the developer’s workflow and making the process both safe and efficient.

Let’s explore the concept through a practical example. Consider the following code, generating a list of students with random scores and printing the top three based on their scores:

fun main() 
printTop3Students()

data class Student(val name: String, val score: Int)

fun printTop3Students()
(1..50)
.map
Student("ST_$it", (Random.nextDouble() * 100).toInt())

.sortedByDescending it.score
.take(3)
.forEachIndexed index, student ->
val rank = index + 1
println("$student.name achieved the $rank rank with a score of $student.score.")

Let’s apply a series of refactorings step by step:

Introduce Parameter

The initial code creates the students list inside the printTop3Students function, which may hinder its reusability. To address this, we’ll take the students list as a parameter from the call site. First select the list creation expression and press Ctrl + T in macOs and Ctrl + Alt + Shift + T in Windows to open the list of available refactorings for the selected item.

Select Introduce Parameter from the list, or opt for alternative methods such as using a keymap or right-clicking on the item and choosing it from the Refactor section. Assign a meaningful name for the parameter, and observe how Android Studio seamlessly incorporates the students parameter into the printTop3Students function, automatically passing the students list from the call site.

Introduce Variable

I want to make the code more readable by storing the sorted students in a variable. To achieve this, just select the expression and choose Introduce Variable from the refactoring list. Give it a name that makes sense. I’ll do the same thing with the take function. After these two tweaks, our printTop3Students function looks like this:

Extract Function/Method

Printing a student’s name along with their rank appears to be a distinct function to me, and I’d like to create one for it. To do so, select the print expression and, from the refactoring list, opt for Extract Function/Method, specifying a name for it. Now, our final code looks like this:

We can continue refining the code, but I think the current state adequately explains the idea. These refactorings tools can save a considerable amount of time in your daily coding tasks, and I highly recommend mastering them for enhanced efficiency. For a comprehensive list of available refactorings and additional tips, please refer to the official website.

4. Postfix Completion

The Postfix Completion feature simplifies the process of wrapping template code around a recently typed expression. For example, typing .val after an expression and pressing Tab or Enter transforms it into val [name] = expression, allowing you to specify a name for the new variable. Similarly, typing .if changes to if (expression) , and typing .fori after an integer expression converts to for (i in 0..expression). In this context, val, if, and fori serve as postfix keys.

To explore a comprehensive list of predefined templates, along with their descriptions, navigate to Settings | Editor | General | Postfix Completion. As of writing this article, you can only edit postfixes of available templates by right-clicking on them. Unfortunately, modifying or deleting their functionality is not supported.

If you’re keen on creating custom templates for supported languages, refer to this article, which provides insights into creating custom templates for GO. The process is similar for other languages as well.

Next Post

Digital Marketing in 2024: Trends, Tools, and Triumphs

As we step into the ever-evolving landscape of digital marketing in 2024, the industry is set to witness a fascinating blend of technological advancements, changing consumer behaviours, and a relentless pursuit of innovation. In this article, our small business marketing agency in Melbourne willlook at the key trends shaping the […]
Digital Marketing in 2024: Trends, Tools, and Triumphs

You May Like