Top 5 Mistakes That Make Your Kotlin Code a Mess

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

ความคิดเห็น • 87

  • @vaibhav47sharma
    @vaibhav47sharma 2 ปีที่แล้ว +39

    Another readability issue is not using trailing commas in method signatures and their invocations if they span multiple lines. The problem with this is that if we are to add a new parameter in the future the reviewer will see 2 lines of diff instead of 1 making the review harder. Furthermore the git blame will say that we are the one who added the previous parameter. As kotlin supports trailing commas I highly recommend thinking about using them.

  • @laOLIVAesVERDE
    @laOLIVAesVERDE 2 ปีที่แล้ว +4

    I would create sealed interface to express authentication result.
    - Success
    - Error with message
    By the way, I enjoyed your CI/CD course👍
    It was so great!

  • @makkhan3153
    @makkhan3153 2 ปีที่แล้ว +1

    It really give you pleasure, when you have written the code in this manner.

  • @piotr8731
    @piotr8731 2 ปีที่แล้ว +2

    If you already took the time to extract if conditions to variables you might as well create third one "val shouldHighlightMessage = isLocalUserAdmin || areUserFriends", and call your sendMessage() function once, instead of two times. This will speed up reading process even more as developer would not need to go trough 2 "sendMessage" logics

  • @myrabohikechi2168
    @myrabohikechi2168 2 ปีที่แล้ว +2

    Thanks so much Phil, most time I just use some stuff without really knowing the negative side of it, maybe because I see senior devs use it.

  • @agungwatanabe318
    @agungwatanabe318 2 ปีที่แล้ว +6

    I always use named parameter except for short functions that are self describing. Example: Modifier.height(56.dp) instead of Modifier.height(height = 56.dp)

  • @tch.777
    @tch.777 2 ปีที่แล้ว

    I just enjoy to hear every word of you in every single video...
    Thank you!! 🙏

  • @ug333
    @ug333 ปีที่แล้ว +3

    Love the video, and totally agree with your notes. Thanks!
    One small difference for me: I would pull the logic for local user admin and is friend to functions. I tend towards boundary testing, and that's much easier when those are broken into functions. There are other reasons as well, but I would imagine you would have already made it a function in those cases
    Again, thanks!

  • @chase-2-2
    @chase-2-2 2 ปีที่แล้ว +2

    Talking about named parameters:
    Intellij supports adding "parameter names" to java functions. It will then add comments that give you the name of the java parameter and while this does not give you any of the kotlin features, it's still very helpful

  • @denisoluka
    @denisoluka 2 ปีที่แล้ว

    Thank you for the tip. I used inline lamda functions a couple of times. I finally realize my mistake. Thanks again

  • @domonk7450
    @domonk7450 2 ปีที่แล้ว

    I really like videos like this that go over general improvements

  • @anasfarhad2845
    @anasfarhad2845 2 ปีที่แล้ว +2

    I am soo happy to discover that I already take care of these code readability mistakes but I got to learn some new tricks as well :)

    • @anasfarhad2845
      @anasfarhad2845 2 ปีที่แล้ว

      @Philipp_Lackner1 Omg really? I can't believe it

    • @anasfarhad2845
      @anasfarhad2845 2 ปีที่แล้ว

      How Can I claim my prize? Im soo excited

  • @emmanuelmtera5936
    @emmanuelmtera5936 2 ปีที่แล้ว

    I once faced this problem but as time goes i learn and now create better code.

  • @pablovirus
    @pablovirus ปีที่แล้ว

    Thanks for the tips! These are the things you only get from experience or from someone mentoring you

  • @dennisgithuku9340
    @dennisgithuku9340 2 ปีที่แล้ว

    Great content Phil.

  • @adambickford8720
    @adambickford8720 2 ปีที่แล้ว +8

    I almost *never* use local variables like this; i'd much rather extract them into functions. You still end up with a nice 'named/semantic ' chunk of code but it's:
    1. more readable/abstract; don't bother me with the implementation of deriving friends or admin, i only care about the result
    2. it clearly limits scope of that result, reducing cognitive load
    3. its reusable/extensible/composable/etc

  • @OlegGolubev_yolo
    @OlegGolubev_yolo 2 ปีที่แล้ว +1

    i wish more people will follow this practice, sometimes joining a new projects is a very difficult process :|

  • @AntonioGonzalezsanchez
    @AntonioGonzalezsanchez ปีที่แล้ว

    Holy stuff my man, this video is GOLD. Where were you when I was learning Android some 4 years ago?

  • @pinoy_marites_beh
    @pinoy_marites_beh 2 ปีที่แล้ว

    A good analogy for the null argument are use-cases like a calculator or a some monetary use-case, numbers should start with null instead of 0 or 0.0, but for a counter/counting use-case 0 should be a default value

  • @aquilesorei
    @aquilesorei 2 ปีที่แล้ว

    Thanks for your advices 🙏🏾

  • @gori_maheswari8994
    @gori_maheswari8994 2 ปีที่แล้ว

    Thank you so much❤

  • @SriHarshaChilakapati
    @SriHarshaChilakapati 2 ปีที่แล้ว +2

    I'd also avoid passing boolean parameters to functions as much as possible. Most of the times, it makes a lot more sense to create an enum and pass that instead.

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +7

      Why would you pass an enum if you have something that has a clear true and false value 🥴

    • @tashilapathum3709
      @tashilapathum3709 2 ปีที่แล้ว +1

      @@PhilippLackner I think it's better if we had to extend the functionality later. For example, to add another user type BOT, other than FRIEND or ADMIN

    • @mv2e19
      @mv2e19 2 ปีที่แล้ว

      Kotlin supports named parameters, so boolean parameters aren't that bad. For example, "Hello, World!".contains("hello", ignoreCase = true) is quite readable.

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      @@mv2e19 Enums make sense if you have a boolean where the negated value is unclear. For example isLeft. It's not clear what it means if isLeft is false, could be isRight, isTop, isBottom.
      But if the negated value is clear like for isLoading, isDropDownShowing etc. please use booleans

    • @mv2e19
      @mv2e19 2 ปีที่แล้ว

      @@PhilippLackner oh absolutely, I agree. I just wanted to add that booleans have their place. Not everything should be an enum

  • @danieldawson8018
    @danieldawson8018 2 ปีที่แล้ว

    What formatter do you use? Seems a lot better than my setup.

  • @emmanuelpregnolato5026
    @emmanuelpregnolato5026 2 ปีที่แล้ว

    Thank you Phil, I did enjoyed this capsule.

  • @mustafaammar551
    @mustafaammar551 2 ปีที่แล้ว

    very cool video thank you bro
    wish you all the best👍👍👍👍

  • @DaleKingProfile
    @DaleKingProfile ปีที่แล้ว

    When you extracted the expression to a variable you should have done that using the built in receptions instead of doing it by hand so you cannot make a mistake.

  • @frank.koenig
    @frank.koenig 2 ปีที่แล้ว

    Hello Philipp. I am curious. Could you show some big projects you were involved in?

  • @aptemkov
    @aptemkov 2 ปีที่แล้ว

    Hey, Phillip, can you say, what is your Theme in Android studio and what is your font?

  • @ekstrapolatoraproksymujacy412
    @ekstrapolatoraproksymujacy412 2 ปีที่แล้ว +1

    Maybe you should say something about whether performance is compromised by adding unnecesary variable definitions or other instructions that do nothing? is compiler getting rid of those automatically etc?

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      You won't ever notice a performance difference by introducing a variable 😅 and yes the compiler optimizes a lot

    • @ekstrapolatoraproksymujacy412
      @ekstrapolatoraproksymujacy412 2 ปีที่แล้ว

      ​@@PhilippLackner Unless this function is called a billion times? ;) I don't know much about kotlin jvm etc, but I know a thing or two about "lowerish" level programming for very low power microcontrollers when you have to think 10 times if you really need this float or it can be done with just an int, I'm aware that CPU in average android device is at least million times faster than low power microcontroller and such thing in almost all cases will not be even detectable, but I think it should be said why that is, because otherwise we will end up with new generation of programmers that don't actually know what they are doing and that starts to be a plague in my biased opinion. I don't expect that high level android programmer will be fluent in ARM assembly, but it is important to know what you don't know.

    • @dankal444
      @dankal444 2 ปีที่แล้ว

      @@ekstrapolatoraproksymujacy412 readability is most important, at the end you check performance of whole application (or your part of it) and only if you find a bottleneck you can sacrafice a bit of readability for faster code. Happens very rarely

  • @rhen4610
    @rhen4610 2 ปีที่แล้ว

    this is pretty helpful on technical interview

  • @FreedivingTrainer
    @FreedivingTrainer 2 ปีที่แล้ว

    Hi! Thanks for your videos! Why in your example projects you don't use preview of the whole screen during developing?

  • @Torte-Roblox
    @Torte-Roblox ปีที่แล้ว

    Good video! I just want to add, when using intellij IDE you can use the .val - shortcut to create a new val of an expression. Simply put ".val" behind the expression et voila.

  • @syedovaiss
    @syedovaiss 2 ปีที่แล้ว

    Woah! Didn't doing these mistakes ♥️

  • @victorlapin2080
    @victorlapin2080 2 ปีที่แล้ว

    Philipp, what approach would you recommend for localized strings in error messages?

  • @pratikdhage3491
    @pratikdhage3491 2 ปีที่แล้ว

    Thanx Philip bro 🤟

  • @vladdiachuk562
    @vladdiachuk562 ปีที่แล้ว

    We need same video for Compose!

  • @ПавелЗубко-ц8ч
    @ПавелЗубко-ц8ч 2 ปีที่แล้ว

    Thanks man!!!! Cool!!!

  • @bombinaround
    @bombinaround ปีที่แล้ว

    If highlighted messages are a first class feature, would it not make more sense to have two send methods in the interface - send normal message and send highlighted message. Then you can lose the Boolean altogether

  • @soodersoaehnlichlol
    @soodersoaehnlichlol ปีที่แล้ว

    The „areUsersFriends“ is not a good refactoring, you should rather move the list of „localUsers“ from the first part of the expression to a variable and keep the rest wirhin the condition. Not a fan of moving conditions to variables just to name them either, it bloats the code; just like the named parameters do. „checkIfAuthenticated“ is also bad naming, „checkAuthentication“ is suitable.

  • @HARSHSHARMA-ic6qo
    @HARSHSHARMA-ic6qo 2 ปีที่แล้ว +1

    Great video.
    Waiting for the result off the giveaway. I hope will be replacing my 7 year old laptop.

  • @annunzarizzle
    @annunzarizzle 2 ปีที่แล้ว

    why is his x-code dark theme so chill, when i download the one on android studio the colors are very bright

  • @peterbrown4516
    @peterbrown4516 2 ปีที่แล้ว

    Excellent

  • @robchr
    @robchr 2 ปีที่แล้ว

    Variable names are important but I don't think these examples are more readable with the added variables. I still will need to parse logic to see what logic is actually occurring.

  • @shahzamanrai2617
    @shahzamanrai2617 2 ปีที่แล้ว

    Which Font are you using?

  • @technicholy1299
    @technicholy1299 2 ปีที่แล้ว

    Disagree on the empty string for error message. As long as it is consistent across the app, then the .isEmpty() function does all you need, safely.

  • @sabuta100
    @sabuta100 2 ปีที่แล้ว +6

    In a more general suggestion (despite being Kotlin or not), I would add; try to avoid very large files, not doing so makes the code reading hard/confusing/boring 😅, probably keeping the number of lines between 1k and 1.5k is still a good option (do so ONLY when needed, I mean, if your file is shorter than that, it's totally fine and great). Also large files might kill your Android Studio 🥲.

    • @TheLatvianboy
      @TheLatvianboy 2 ปีที่แล้ว +11

      1k is already 3x what you would want from a file.

    • @TheTuxtrons
      @TheTuxtrons 2 ปีที่แล้ว

      With 500 my AS is already agonizing lol

    • @JimPekarek
      @JimPekarek ปีที่แล้ว +4

      On the other extreme, don't go full enterprise Java and spread logic across 75 different files with 10 layers of inheritance so you have to go on a treasure hunt to figure out what every line of code actually does. There's usually a happy medium.

  • @hossamqandel5638
    @hossamqandel5638 2 ปีที่แล้ว

    Could you tell me how can we mix between UiEvent and UiText sealed classes to show a Snackbar with translated message?
    @Philipp Lackner

  • @Daaaaaaavid
    @Daaaaaaavid 2 ปีที่แล้ว

    Hey, I wanted to ask what monitor do you use for Android Studio? I have a 27" 4k only, and it's not enough to work with 4 columns in AS, so I'm wondering which one to buy to use as a central monitor or as second one to the current one.

  • @pavelschannel-alittleoutof3532
    @pavelschannel-alittleoutof3532 ปีที่แล้ว

    This is just standard coding practice. Can be for any language....

  • @yasserakbbach7342
    @yasserakbbach7342 2 ปีที่แล้ว

    That would be a useful Playlist 👌

  • @KotlinBek
    @KotlinBek 2 ปีที่แล้ว

    thanks

  • @argahutama
    @argahutama 2 ปีที่แล้ว

    I spotted 1 more mistake:
    You don't rearrange your code (CMD + Option + L)

  • @tashi7160
    @tashi7160 2 ปีที่แล้ว

    read uncle bob clean code, it will change/improve your coding style drastically.

  • @peterwestlin8052
    @peterwestlin8052 2 ปีที่แล้ว

    Named parameters.

  • @scottbiggs8894
    @scottbiggs8894 6 หลายเดือนก่อน

    "Don't be afraid to introduce more variables to make code more readable." I wish I could tattoo that on the heads of some of my old co-workers.
    I completely disagree with removing the comment. Keep it. If there's a bug, it's nice to know what the programmer was TRYING to do. This way we can tell if the algorithm is faulty or the implementation of it. Comments explain what you're trying to do; the code shows how it is accomplished. I've wasted hundreds of hours of my life debugging people's code; knowing the difference between the programmers' intent vs how it was implemented is invaluable.

  • @sacarymoviesamu
    @sacarymoviesamu ปีที่แล้ว

    i like this, as i am a noob developper, lol

  • @theonline5703
    @theonline5703 2 ปีที่แล้ว

    Not gonna a lie i didn't know you but i just started watching your videos after you posted the giveaway! ✊

  • @mkc0321
    @mkc0321 2 ปีที่แล้ว

    amazing

  • @hossamqandel5638
    @hossamqandel5638 2 ปีที่แล้ว

    by the way Philipp .. Could you build a new project for example chat app?❤

  • @programaths
    @programaths ปีที่แล้ว

    for the null, better add a boolean alongside or an enum when relevant. The rational behind that is that one variable should not encode two things.
    In the case of errorMessage being null, it encodes two things: whether there was an error and if so, the error message.
    With a boolean, you can do:
    if(auth.isError){ display(auth.errorMessage) }
    instead of:
    if(auth.errorMessage!=null){ display(auth.errorMessage) }
    Also, "==true" or "==false" should not be used.
    As for comments explaining "what", they also indicate that some code pertain to a function. I used that when doing QC ^^
    And with Kotlin, you don't lose much with inline functions. So, it's worth to even put a one liner in a function.

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว

      With that argument nullables shouldn't be used at all, since they ALWAYS encode the existence of a value and the value if it exists. Can't agree with that 😅

    • @programaths
      @programaths ปีที่แล้ว

      @@PhilippLackner Yes, null is the one million mistake!!!

  • @pradyumnx
    @pradyumnx 2 ปีที่แล้ว

    ❤️❤️❤️❤️

  • @swedishpsychopath8795
    @swedishpsychopath8795 หลายเดือนก่อน +1

    kotlin is messy as hell. Better use java and OOP

  • @thangaduraiselvaraj9318
    @thangaduraiselvaraj9318 2 ปีที่แล้ว +3

    First Cmt🥳

    • @JagdishOnYT
      @JagdishOnYT 2 ปีที่แล้ว

      Not actually 🤣

  • @JagdishOnYT
    @JagdishOnYT 2 ปีที่แล้ว +1

    I am waiting for Giveaway announcement. 😂

  • @cristianowinter4382
    @cristianowinter4382 2 ปีที่แล้ว +1

    Hey Phil, congrats on all the work you have done. Your job is fantastic, and you have been helping many developers with your content!! Maybe, another improvement, what about moving it.isLocalUser && it.isAdmin to a method inside the User class as one of its members, something like users.values.any {it.isLocalUserAdming()}? I noticed that people are no longer adding behaviours or using the state of an Object.

  • @UmerFarooq-vk9be
    @UmerFarooq-vk9be 2 ปีที่แล้ว +2

    No matter what you do... Kotlin syntax is a total mess by itself 🤦