F
F
foonfyrick2021-03-11 16:25:21
Kotlin
foonfyrick, 2021-03-11 16:25:21

How to understand that in the code, when refactoring, it is better to replace the code with a lambda, inline, generic?

How to understand that when refactoring I should replace the code with a lambda? What problem does she solve?
How to understand that when refactoring it is better to use an inline function? What problem does she solve?
How to understand that when refactoring it is better to use a generic? What problem does it solve?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DollyPapper, 2021-03-11
@foonfyrick

The question doesn't make sense. You've lumped together 3 different tools in a language, and you're asking when to use one of them over the other. In which case will a hammer make it easier to build a house? Obviously in the case when it will be necessary to hammer in a nail. When will generic simplify the code? When it is necessary for example to generalize a method. Let's have a sort method. It is possible to write n different methods that will sort an array that contains a certain type of data. For example today we need to sort an array of strings. We are writing an array that takes the String data type as input. And tomorrow we need to sort an array of integers. And we again write a method that sorts the numbers. In fact, these are two identical methods that will differ only in the accepted data type, and this is code duplication, which is not good.T , i.e. generic. And then instead of 10 methods we will have one universal one. The code will immediately become clearer, because there will be less of it. Here is the simplification. When will lambda simplify code? For example, when passing a callback function to a method, in order not to write a separate method that will do some work once and will not be used anywhere else, we can use an anonymous function (aka lambda) to write this code in place. Unfortunately, I don’t know what inline functions are, I’m not familiar with Kotlin, so I can’t say about where they can help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question