Avoid THIS Mistake With IF Statements in Kotlin

preview_player
Показать описание
Follow for more Android & Kotlin tips 🙌
Рекомендации по теме
Комментарии
Автор

Me thinking he was gonna combine all the if statements into one if

chibuikeoffu
Автор

They are called guard clauses and are applicable in any programming language not just Kotlin

larryd
Автор

I had coded a nested if statement, and later my senior cleaned it up using the way you showed in the video, now it is reinforced after watching

meenamurthy
Автор

Ah yes my senior used to do this and explained me same, thx

codersaurabh
Автор

“Chain of responsibility” wants to join the chat😅

yuriisurzhikov
Автор

I used to do backend code with validator with spring or struts it’s the same type of defensive code nothing new

TheBlackManMythLegend
Автор

For this example you also can use Chain of responsibility pattern

gamumu
Автор

Great tip, gonna add that to my refactor arsenal.

Aspiret
Автор

Bro, you're a Kotlin ninja. You give me something to strive for.

JDMorris
Автор

This is called a guard clause, popular from C#, also you can switch out for a when statement and implement a chain of responsibility pattern instead.

nicholasferrara
Автор

for those interested this is called a guard clause. it guards the code below it from conditions it shouldn't be running in.

Rin-qjzt
Автор

Agree! Check the invalid case first to rule out the logic and continue the next invalid one and so on.. the finally deal with the valid one!

rjamesdoe
Автор

That is one of good things which is pretty good handled by swift. Stretch for guard and guard let.

AA_Yoloist
Автор

You COULD combine all the early return statements with or ( not with and, as you said near the end) . But I would not. I prefer it like it was after you changed it to lots of early returns. I have done it this way for 40 years. Much better than the first version, especially if you need to change something. Also much easier to read any changes in git diffs if you avoid nesting wherever possible.

polarnight-no
Автор

Why fail silently when you could return an appropriate exception, e.g. IllegalArgumentException? That clearly communicates the error to the user. Otherwise, how would you know if the user was created or not?

I think it would be better to use the require function for these assertions. It also comes with the benefit that you do not need to negate the conditions.

emile
Автор

Just like the "guard" statement in Swift

apostolostsaousis
Автор

I've got Android lead who said "get rid of those returns, make fun end with braces if the condition fails". All these opinions

vladalexeev
Автор

Would precondition functions work better here?

welovfree
Автор

I personally find consecutive if statements more unreadable than nested ones. I'd probably do a when statement instead and have each case be a guard, and then if it passes all of them the desired returned logic could just be the else. But I get the idea behind guard clauses and I know not all languages are capable of a when/switch/match type statement

ThisGuyDakota
Автор

Is there a best practice of how to think about these? Like, keep disqualifying from top to bottom?

ThomasIkemann