Android Concepts: Definition, Pros, and Cons

Simplified Insights into the Android Concept

Hari Prasad
4 min readAug 31, 2023

MVVM

MVVM is an architectural pattern that separates an application into three distinct components:
Model — data and business logic
View — UI representation
ViewModel — mediator between Model and View

Pros

MVVM promotes the Separation of concerns so the code base becomes more Maintainable, Scalable, and Testabile
— ViewModel
is lifecycle aware, can hold UI data, and can survive configuration changes.
ViewModel & LiveData updates the UI through lifecycle aware and observable patterns.
— ViewModel & View are not tightly coupled with a view so it can be reused with other views
— ViewModel & Data Binding, works well with data binding libraries so reduces the boilerplate code needed to update.

Kotlin

Kotlin is a JVM language that is concise, expressive, and fully interoperable with Java

Pros

  • Conciseness: Kotlin reduces boilerplate code, making it more concise than Java. This results in cleaner and more readable code.
    Ex:
    simple class declaration —
    class A(val a:String)
    data class —
    data class A(val a: String)
    object class — object A{}

    scope functions — .let{}, .run{}, .apply{}, .also{}
    Extension Functions — A.abc(){}
    Lambda Expressions
    infix notations —
    a add b

    Property Syntax
    : val a=”abc”
    Type inference — val name = “John” // infers that ‘name’ is of type
    Smart Casts

    StringSimplified Control Flow — for(x in 1..10), if()-else-, when(){}
    if and when as expressions — val a=if(i>10) “Big” else “Small”
  • Interoperability: Kotlin is fully interoperable with Java, allowing developers to use Kotlin alongside existing Java codebases.
    Ex: @JvmStatic, @JvmOverloads, @JvmFiled, @JvmName
  • Null Safety: Kotlin has built-in null safety features, which help prevent null pointer exceptions and improve code reliability.
    Ex: ?.,!!, ?:
  • Coroutines: Kotlin provides first-class support for coroutines, making asynchronous and concurrent programming more manageable and readable.
  • Functional Programming: Kotlin supports functional programming features, such as lambdas, higher-order functions, and immutability, which can lead to more expressive code.

Coroutines

A coroutine is a multitasking technique that lets handle multiple tasks efficiently.

  • Enables a piece of code to temporarily pause its execution, allowing other tasks to proceed, and then resume where it left off.

Pros

  • Lightweight and Efficient: Coroutines are lightweight, allowing for the creation of thousands of concurrent tasks without consuming excessive resources. They are more efficient than traditional threads.
    — Resource Efficiency: Coroutines are managed by a coroutine dispatcher, which is responsible for scheduling and executing coroutines on a limited number of threads. Unlike traditional threads, which each require their own stack and memory allocation, coroutines can share the same thread resources, resulting in lower memory overhead.
    — Context Switching: Context switching refers to the process of switching between different threads or tasks. Coroutines involve fewer context switches because they can be paused and resumed at specific points, allowing for more efficient task switching compared to traditional threads.
    Cooperative in nature: Coroutines are cooperative in nature. This means that they rely on explicit cooperation from the developer to pause and resume their execution, rather than being preemptively managed by the system like traditional threads.
    In a cooperative multitasking model, a coroutine yields control voluntarily to other coroutines. This cooperation is achieved using suspension points, where a coroutine can pause its execution and give an opportunity for other coroutines to run. This enables efficient context switching between tasks without the overhead of thread context switching.
    Performance: Coroutines are designed to be lightweight, making them efficient for resource utilization. They use fewer system resources compared to traditional thread-based concurrency.
  • Structured Concurrency: Coroutines are launched within specific scopes and are automatically cancelled when the scope ends (e.g., a lifecycle scope in Android). This structured approach ensures proper cleanup and avoids runaway threads, leading to more efficient resource utilization and helps prevent resource leaks.
    — Scoped Lifetimes: Coroutines are launched within a specific scope, such as a function, a coroutine scope, or a lifecycle scope in Android. This scope defines the boundaries within which the coroutines operate.
    — Automatic Cancellation: When a scoped coroutine’s parent scope is cancelled or completed, all the coroutines within that scope are automatically cancelled. This helps in preventing resource leaks and ensures proper cleanup.
    — Cancellation Propagation: Cancellation is propagated from parent to child coroutines, ensuring that a cancelled parent coroutine leads to the cancellation of its child coroutines. This maintains consistency in the application’s behavior.
    — Exception Handling: Exception handling in coroutines is straightforward and follows regular control flow, which leads to cleaner error handling and easier debugging.
  • Readability and Sequential Code: Coroutines allow you to write asynchronous code that looks like regular sequential code. This enhances code readability and makes it easier to understand the logic.
  • Elimination of Callback Hell: Coroutines help eliminate the callback nesting (often referred to as “callback hell”) that can occur with deeply nested asynchronous operations.

RxJava & Android

WorkManager

Navigation

Room

--

--