Best of KotlinJuly 2024

  1. 1
    Article
    Avatar of medium_jsMedium·2y

    Advanced Kotlin Coroutine Cheat sheet (for Android Engineer)

    This cheat sheet provides advanced insights and best practices for using Kotlin Coroutines in Android development. Key elements like CoroutineContext, Job, Dispatcher, and CoroutineScope are thoroughly explained. It covers essential concepts such as coroutine builders, dispatcher selection, and how to manage parallel execution and cancellation of coroutines. Additionally, it offers practical advice on avoiding synchronization issues and optimizing performance.

  2. 2
    Article
    Avatar of pandProAndroidDev·2y

    Stop throwing exceptions!

    In Kotlin, it's recommended to avoid throwing exceptions and instead handle error scenarios using try/catch expressions, result objects, and functional programming paradigms. The default usage of try/catch is criticized for reducing readability and maintainability. The post suggests Kotlin's built-in Result class for better error handling, providing examples of how to wrap operations in Result objects and handle errors more effectively. This approach promotes cleaner, more testable code without relying on exceptions.

  3. 3
    Article
    Avatar of communityCommunity Picks·2y

    Ktor vs. Spring Boot: 5 Key Differences for Kotlin Devs

    Ktor and Spring Boot offer distinct advantages for Kotlin developers. Ktor, built purely with Kotlin, is lightweight, supports asynchronous server-side web apps with fast start-up times, and uses Kotlin Coroutines for concurrency. Spring Boot, while more established with extensive documentation and community support, is more suitable for larger applications with its robust ecosystem. Both frameworks offer great developer experiences, but the choice depends on the project's needs.

  4. 4
    Article
    Avatar of pandProAndroidDev·2y

    Convert Your Native Project to Kotlin Multiplatform: Why, When and How

    Kotlin Multiplatform (KMP) enables a new approach to cross-platform development without the need to rewrite existing native applications. KMP allows sharing code between Android and iOS while retaining native performance and user experience. This guide highlights the advantages of migrating to KMP, such as gradual migration, maintaining native functionality, faster feature delivery, and improved team collaboration. The migration process typically involves setting up a multiplatform module, migrating models, data sources, and business logic in an iterative manner.

  5. 5
    Article
    Avatar of pandProAndroidDev·2y

    Kotlin Lazy vs Lateinit Explained

    Explore ways to initialize properties in Kotlin using lazy and lateinit keywords. Lazy is used for immutable properties and initializes the value upon first use, promoting better resource management. Lateinit is for mutable properties, allowing initialization to occur later, but requires careful handling to avoid crashes. For primitive types, use Delegates.notNull to achieve similar functionality.

  6. 6
    Article
    Avatar of medium_jsMedium·2y

    Kotlin Sealed Class Explained

    Kotlin's sealed class is a special kind of class that restricts its inheritance to a specific package, providing a type-safe way to handle different types of data. This makes code more readable and manageable, especially when paired with Kotlin's 'when' expression, which ensures all possible types are handled at compile time. The article includes practical examples and emphasizes best practices for organizing sealed classes in your code, highlighting their utility in handling API responses and other generic data holders.

  7. 7
    Article
    Avatar of droidcondroidcon·2y

    Stack vs. Heap in Kotlin: Understanding Memory

    Discover how memory allocation works in Kotlin by exploring the concepts of stack and heap memory using everyday analogies. Learn how local variables and function calls are managed in stack memory, while objects and global variables are stored in heap memory. Understand the importance of efficient memory management for better performance and resource utilization.

  8. 8
    Article
    Avatar of jetbrainsJetBrains·2y

    Java Annotated Monthly – July 2024

    The July 2024 edition of Java Annotated Monthly features insights from Trisha Gee and covers the latest Java news, such as details on Java 23's upcoming release and new features. It also includes a variety of tutorials and tips on Java, Kotlin, and AI, along with a list of upcoming events. Highlights include articles on improving developer productivity, automated testing practices, and integrating generative AI into Java applications. Don't miss the section on how Quarkus is enhancing Java's efficiency and performance.

  9. 9
    Article
    Avatar of itnextITNEXT·2y

    Modeling Android Screen State

    The post discusses effective strategies for managing Android screen states using ViewModels and sealed classes. The approach leverages intermediate streams to create a single observable for the view state, ensuring easier maintenance, reliability, and readability of code. The method is showcased with examples from a simple map app, outlining states like Loading, PermissionDenied, and WithLocation, and demonstrating how to handle these within the ViewModel. Benefits include flexibility, the ability to move logic into sealed interfaces/classes, and better testability.

  10. 10
    Article
    Avatar of pandProAndroidDev·2y

    Kotlin reified Explained (no more type erasure)

    Kotlin solves the problem of type erasure at runtime by introducing the 'reified' keyword, which preserves type information. This keyword works in conjunction with inline functions, allowing code to be moved to the call site and maintaining type information. This approach enables writing more generic and readable algorithms.

  11. 11
    Article
    Avatar of pandProAndroidDev·2y

    Type Safe Bottom Navigation in Jetpack Compose

    This guide delves into the advanced concepts of Type Safe Bottom Navigation in Jetpack Compose. It explains how to use Serializable objects for navigation routes, thus eliminating the need for route build functions and navArguments. The guide covers setting up a BottomBar with three routes, handling active tabs, generating route patterns, and managing navigation routes with arguments. It simplifies navigation setup, ensuring type safety and enhancing readability.

  12. 12
    Article
    Avatar of droidcondroidcon·2y

    Kotlin’s @Keep Annotation: Keeping Your Code Safe and Sound

    Kotlin’s @Keep annotation is essential for protecting specific parts of your code from being removed by the code optimizer. By marking classes, functions, or properties with @Keep, you prevent unintended removal, ensuring critical code invoked through reflection or external libraries remains intact. The annotation acts like a VIP badge, signaling to the optimizer the importance of certain code elements.