Refactor for optimal Kotlin

idiomatic kotlin
kotlin android
kotlin medium
java to kotlin
kotlin cheat sheet
kotlin articles
kotlin vocabulary
31 days of kotlin

I have this piece of validation code and do not think its in the spirit of good Kotlin,

In this code I compare to Data Classes content networker: Networker and worker: Networker

    if (networker.name != worker.name && networker.name != null) {
        worker.name = networker.name
    }
    if (networker.lastName != worker.lastName && networker.lastName != null) {
        worker.lastName = networker.lastName
    }
    if (networker.phone != worker.phone && networker.phone != null) {
        worker.phone = networker.phone
    }
    if (networker.email != worker.email && networker.email != null) {
        worker.email = networker.email
    }

How can I improve this with better & more elegant Kotlin code please

If elegance is your main concern here, then you could write it this way.

worker.name = networker.name ?: worker.name
worker.lastName = networker.lastName ?: worker.lastName
worker.phone = networker.phone ?: worker.phone
worker.email = networker.email ?: worker.email

This acheives the same result as your code (copy values from networker to worker, except nulls), but I think it looks nicer.

Learn how to refactor an app with Kotlin, As Kotlin is fully interoperable with Java, refactoring existing If you are doing all the katas one by one, the best thing to do is starting from your� Refactor for optimal Kotlin. Ask Question Asked 15 days ago. Active 15 days ago. Viewed 60 times 0. 0. I have this piece of validation code and do not think its in

Since following conditional assignment is pointless

if (X != Y) {
    Y = X
}

as without this check the result of assignment would be same, you can omit it and only keep null-check via .let {}

networker.name?.let { worker.name = it }
networker.lastName?.let { worker.lastName = it }
networker.phone?.let { worker.phone = it }
networker.email?.let { worker.email = it }

From Java Programming Language to Kotlin — the idiomatic way , Then we go to the next level — make it idiomatic, teaching best practices and useful tips along the way. The Refactoring to Kotlin codelab starts� As Kotlin is fully interoperable with Java, refactoring existing applications by using Kotlin will be more frequent than creating them from scratch. These practices come a little closer to what we usually do every day at work. The application to refactor. The application is simply a list of movies and their corresponding detail screen.

In addition to the low-level improvements addressed in other answers, I'd also consider where you put this code.

Since all these lines are accomplishing a single task — updating one object from another — it would make sense to bundle them into a function; not only does that clearly associate them, it also lets you give them a descriptive name.

The obvious place for that would be inside the data class itself; perhaps a method called updateFrom(other: Networker) which updates the object it's called on.  That would make it obvious to the caller which object was being mutated.

Sometimes you want to keep data classes as simple data holders, though, or don't have control over them.  In that case, it could be an extension function.

Refactoring utility classes with Kotlin : Shared Preferences, In below steps, we will first try to refactor our Java class and then convert it to Kotlin. Step 1: Higher-order functions. The first step we can do is to� Working as a group, we'll take existing Java code, convert it to Kotlin in IntelliJ, and then refactor the results to idiomatic Kotlin. We'll see how to take advantage of the language to make code smaller, faster and more expressive, whilst at the same time interoperating with your existing application and Java libraries.

Help Provide Feedback: Vote Up Kotlin Refactoring Issues relevant , Kotlin YouTrack Refactoring Issues. KT-15679 - Rename refactoring for header/ impl interface/class/function/typealias. Kotlin is a relatively new programming language developed by JetBrains for modern multiplatform applications. Nowadays, Kotlin is widely used for Android development instead of Java. It is because Kotlin is safe, concise, and fun to read and write. Our Kotlin tutorial will guide you to learn Kotlin one step at a time.

Refactoring to Kotlin Codelab, Learn how to convert Java to Kotlin using Kotlin's idioms with Google Developers Codelabs. Refactoring to Kotlin, which is available in English,� Integration Tax. Kotlin has a great interoperability with Java, but it’s not completely free and there is a list of rules to follow when mixing the two.Therefore, there is a “tax” for integration points between Kotlin and Java in the codebase.

Idiomatic Kotlin. Best Practices., Classes should know as little as possible about each other. For more info: - " Design patterns" GoF - "Refactoring" M. Fowler - "Refactoring to� The Node installation is only required for Create React Kotlin App itself. Using React with Kotlin. To develop applications in Kotlin that use React you need to use a Kotlin wrapper for React. You can find a documentation for it and examples in the module's repository. Adding Kotlin/JS packages

Comments
  • I am comparing the two classes because the one might have changed, it is as important to see if it changed as it is to see if the new field is null Background: I am getting the changed (or not) class from a spring/thymeleaf HTML with a doAjaxPost() or js script but the fields not in the HTML form get set to null and overwrite the data set when I persist it, with my test above I copy only the changed data after testing for change and null to the original data then persist it to preserve the other fields not used in the HTML. I guess you are correct the change test is not needed.
  • Interesting Idea, I like to do it but have not enough experience to implement it yet, Can you point me to sample/lesson some ware