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.
Related Posts
Leave a Reply Cancel reply
Categories
- ! Без рубрики (1)
- ++PU (1)
- 1 (1)
- 1w (1)
- 1win Brazil (1)
- 1win India (1)
- 1WIN Official In Russia (1)
- 1win Turkiye (1)
- 1xbet egypt (1)
- 2ankarafayansustasi.net_may (1)
- ankarafayansustasi.netsiteai apr (1)
- Artificial intelligence (1)
- Arts & Entertainment, Photography (1)
- belugasitesi_mAY (1)
- BH_TOPsitesi apr (1)
- BHsitesy_may (2)
- Blog (3)
- Bookkeeping (14)
- Bootcamp de programação (2)
- Bootcamp de programación (2)
- BT_TOPsitesi apr (1)
- casino (5)
- casinom-hub (1)
- casinom-hub.comsitesi apr (3)
- colombian mail order brides (1)
- Cryptocurrency exchange (2)
- Dinamobet_next (1)
- Disease & Illness, Colon Cancer (1)
- Dumanbet (1)
- Dumanbet_next (1)
- Finance, Insurance (1)
- FinTech (5)
- Forex Trading (11)
- Galabet (1)
- Health & Fitness, Fitness Equipment (1)
- Hitbet (1)
- Home & Family, Crafts (1)
- Home & Family, Gardening (1)
- Internet Business, Audio-Video Streaming (1)
- Internet Business, Ecommerce (1)
- Internet Business, Email Marketing (1)
- Internet Business, Internet Marketing (1)
- IT Вакансії (1)
- IT Образование (5)
- IT Освіта (1)
- latin women dating (1)
- mail order bride (1)
- Mars bahis (2)
- Matadorbet (1)
- minimiri.comsitesi apr (3)
- Mobile App Development (771)
- Mostbet Russia (1)
- New Post (1)
- News (1)
- PB_TOPsitesi apr (1)
- PBsitesi_may (1)
- Pusulabet (1)
- redmirepool.bizsitesi apr (2)
- redmirepoolsitesi_may (1)
- Reference & Education, College (1)
- Reference & Education, Sociology (1)
- Rokusitesi apr (1)
- Sober living (6)
- Society, Divorce (1)
- Software development (7)
- Superbetin (1)
- Tempobet_next (1)
- thelongeststride.comsitesi apr (1)
- tipobet-turkiyesitesi apr (1)
- Ultrabet (1)
- Uncategorized (1)
- Игра (2)
- казино (1)
- Криптовалюты (1)
- Новости Криптовалют (1)
- Финтех (7)
- Форекс Брокеры (9)
- Форекс обучение (2)