Constructors and Init blocks in Kotlin - BEST PRACTICES

Поделиться
HTML-код
  • Опубликовано: 24 ноя 2024

Комментарии • 27

  • @SuboptimalEng
    @SuboptimalEng 3 года назад +15

    Rahul: Deliberately made it suboptimal...
    Me, a suboptimal engineer: Nice

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад +4

      hahaha I thought of you while editing. Was hoping you'd comment 🥰

  • @dantran6192
    @dantran6192 3 года назад +4

    Your example is so simple and concise. Straight to the point.

  • @gajendrapandeya5081
    @gajendrapandeya5081 3 года назад +2

    Awesome. Loved it

  • @shubhamXmandal
    @shubhamXmandal 3 года назад +1

    Exactly to the point video!
    thanks brother

  • @damercy
    @damercy 3 года назад +3

    More videos on best practices please! ✨🙌

  • @19Olcay97
    @19Olcay97 3 года назад

    Sir, your videos are awesome.

  • @jihedbouchnak
    @jihedbouchnak Год назад

    good job Rahul could you made us video about code optimisations fro more clean code ?

  • @RishiRajxtrim
    @RishiRajxtrim Год назад

    Thanks

  • @DevlogBill
    @DevlogBill Год назад

    Thanks for the excellent video, I learned a lot!
    Question? I come from JavaScript and I was wondering does Kotlin have High Order Arrays? Examples would be forEach, map, filter, reduce. Or what about arrow functions? I am spoiled with JavaScript and so far I haven't seen other programming languages with these features. Thank you.!!

  • @LokeshwarPG
    @LokeshwarPG Год назад

    what if the doesSmile is false
    will it throw an error for nickname undefined

  • @adamdeane4675
    @adamdeane4675 3 года назад +1

    Can you do a video on keyboard shortcuts? I do everything the hard way and I would like to be more efficient.

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад +2

      here you go :) ruclips.net/video/HLQUtLbjW98/видео.html

    • @adamdeane4675
      @adamdeane4675 3 года назад

      Dude, you're the best.

  • @nikithab.m7755
    @nikithab.m7755 3 года назад +1

    Hi. Why is doesSmile not the member variable? Could you please elaborate?

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад +3

      Yep sure. In general, you should try to minimize the scope of variables as much as possible. If you make doesSmile a member variable (i.e. a property) it will be accessible across all methods in the class. However, we only need it in the constructor as a way to compute the nickname property. So it's better to reduce the visibility of doesSmile to *only* be in the constructor like we do here.

  • @roelias
    @roelias 3 года назад +1

    Hi Rahul! Nice video!
    I was wondering what are your thoughts on the kind of logic that should go into the init{} method.
    I'm interested in some cases where instances of ViewModels have their init{} performing some sort of communication with repositories. I have a feeling that init{} shouldn't perform such implicit actions (which may also introduce side effects). Do you have any thoughts on it?
    Again, thanks for the content! Have a good day.

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад

      I agree, ideally we should minimize logic in the constructor, otherwise things like testing become difficult.

  • @harwinderbenipal5332
    @harwinderbenipal5332 Год назад

    If we write the smily code wothout int it still will be working. So whats the purpose of init here i didnt get it?

  • @muhammadwasim8787
    @muhammadwasim8787 3 года назад +1

    I tried to create the Person class in a separate file. It goes under the resources directory. And I'm not able to import it in my main.kt file. How can I import that kotlin Person.kt file?

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад

      The Person class should go into the src/java folder, not resources. How are you creating the new file?

    • @muhammadwasim8787
      @muhammadwasim8787 3 года назад

      @@RahulPandeyrkp Hi Rahul, thanks for the reply. I have created the file by right clicking on the src folder and New File. As I didn't see any option for the Kotlin Class file there. And it goes directly to the test/kotlin folder. But then I relocated it to the src/main/resources folder. also attaching the screenshot. But after your reply I tried and check by right clicking on the other folders. And it gives the kotlin class option on the Kotlin directory and the resources directory only.
      screenshot: ibb.co/C1r1Hwy

  • @sagarlokhande247
    @sagarlokhande247 3 года назад

    Sir can you teach us how REST API is used in one more fragment plz

    • @RahulPandeyrkp
      @RahulPandeyrkp  3 года назад

      I have a COVID tracking app series which shows how to do this in an activity. It should be similar for a fragment