two ways creating functions compared in kotlin





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have this snippet of function



fun getNewIntent(context: Context, following: Boolean, userId: String): Intent {
val intent = Intent(context, UsersActivity::class.java)
intent.putExtra(FOLLOW, following)
intent.putExtra(USER, userId)
return intent
}


that can be written also like this



fun getNewIntent(context: Context, following: Boolean, userId: String): Intent =
Intent(context, UsersActivity::class.java).also {
it.putExtra(FOLLOW, following)
it.putExtra(USER, userId)
}


and example in a case it was only 1 parameter



fun getNewIntent(context: Context, userId: String): Intent =
Intent(context, UsersActivity::class.java).apply { putExtra(USER, userId) }


which one is better? and why?










share|improve this question































    0















    I have this snippet of function



    fun getNewIntent(context: Context, following: Boolean, userId: String): Intent {
    val intent = Intent(context, UsersActivity::class.java)
    intent.putExtra(FOLLOW, following)
    intent.putExtra(USER, userId)
    return intent
    }


    that can be written also like this



    fun getNewIntent(context: Context, following: Boolean, userId: String): Intent =
    Intent(context, UsersActivity::class.java).also {
    it.putExtra(FOLLOW, following)
    it.putExtra(USER, userId)
    }


    and example in a case it was only 1 parameter



    fun getNewIntent(context: Context, userId: String): Intent =
    Intent(context, UsersActivity::class.java).apply { putExtra(USER, userId) }


    which one is better? and why?










    share|improve this question



























      0












      0








      0








      I have this snippet of function



      fun getNewIntent(context: Context, following: Boolean, userId: String): Intent {
      val intent = Intent(context, UsersActivity::class.java)
      intent.putExtra(FOLLOW, following)
      intent.putExtra(USER, userId)
      return intent
      }


      that can be written also like this



      fun getNewIntent(context: Context, following: Boolean, userId: String): Intent =
      Intent(context, UsersActivity::class.java).also {
      it.putExtra(FOLLOW, following)
      it.putExtra(USER, userId)
      }


      and example in a case it was only 1 parameter



      fun getNewIntent(context: Context, userId: String): Intent =
      Intent(context, UsersActivity::class.java).apply { putExtra(USER, userId) }


      which one is better? and why?










      share|improve this question
















      I have this snippet of function



      fun getNewIntent(context: Context, following: Boolean, userId: String): Intent {
      val intent = Intent(context, UsersActivity::class.java)
      intent.putExtra(FOLLOW, following)
      intent.putExtra(USER, userId)
      return intent
      }


      that can be written also like this



      fun getNewIntent(context: Context, following: Boolean, userId: String): Intent =
      Intent(context, UsersActivity::class.java).also {
      it.putExtra(FOLLOW, following)
      it.putExtra(USER, userId)
      }


      and example in a case it was only 1 parameter



      fun getNewIntent(context: Context, userId: String): Intent =
      Intent(context, UsersActivity::class.java).apply { putExtra(USER, userId) }


      which one is better? and why?







      android kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 13:54







      gmetax

















      asked Nov 23 '18 at 13:29









      gmetaxgmetax

      2,55422036




      2,55422036
























          2 Answers
          2






          active

          oldest

          votes


















          1














          They are basically equivalent. I would go for whatever is the most readable to you and/or your team.



          I personally like the direct assigments if it is clear enough what is done in the function. Usually that's the case if it's a one-liner or if an initialization is followed by something like .apply (so basically just relatively short). For your functions I would actually introduce something like the following:



          inline fun <reified T> newIntent(context: Context, applyToIntent : Intent.() -> Unit = {}) = Intent(context, T::class.java).apply(applyToIntent)


          which is then reusable for several activities. Usage could then be as simple as:



          newIntent<UsersActivity>(context) {
          putExtra(FOLLOW, following)
          putExtra(USER, userId)
          }


          Or if you really require your current function (I don't see a real reason for it though), it stays still simple enough:



          fun getNewIntent(context: Context, following: Boolean, userId: String) = newIntent<UsersActivity>(context) {
          putExtra(FOLLOW, following)
          putExtra(USER, userId)
          }





          share|improve this answer
























          • the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

            – gmetax
            Nov 23 '18 at 14:28













          • companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

            – gmetax
            Nov 23 '18 at 14:51













          • it is not easy to write it in comment :)

            – gmetax
            Nov 23 '18 at 14:52











          • I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

            – Roland
            Nov 23 '18 at 15:20











          • Private consts but removed the word private from comment

            – gmetax
            Nov 23 '18 at 15:27



















          0














          No difference. Second variant must be use if you can write return in one line instead better use first. Also in second variant you can remove this part : Intent.



          Also can check this - https://kotlinlang.org/docs/reference/functions.html






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447622%2ftwo-ways-creating-functions-compared-in-kotlin%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            They are basically equivalent. I would go for whatever is the most readable to you and/or your team.



            I personally like the direct assigments if it is clear enough what is done in the function. Usually that's the case if it's a one-liner or if an initialization is followed by something like .apply (so basically just relatively short). For your functions I would actually introduce something like the following:



            inline fun <reified T> newIntent(context: Context, applyToIntent : Intent.() -> Unit = {}) = Intent(context, T::class.java).apply(applyToIntent)


            which is then reusable for several activities. Usage could then be as simple as:



            newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }


            Or if you really require your current function (I don't see a real reason for it though), it stays still simple enough:



            fun getNewIntent(context: Context, following: Boolean, userId: String) = newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }





            share|improve this answer
























            • the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

              – gmetax
              Nov 23 '18 at 14:28













            • companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

              – gmetax
              Nov 23 '18 at 14:51













            • it is not easy to write it in comment :)

              – gmetax
              Nov 23 '18 at 14:52











            • I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

              – Roland
              Nov 23 '18 at 15:20











            • Private consts but removed the word private from comment

              – gmetax
              Nov 23 '18 at 15:27
















            1














            They are basically equivalent. I would go for whatever is the most readable to you and/or your team.



            I personally like the direct assigments if it is clear enough what is done in the function. Usually that's the case if it's a one-liner or if an initialization is followed by something like .apply (so basically just relatively short). For your functions I would actually introduce something like the following:



            inline fun <reified T> newIntent(context: Context, applyToIntent : Intent.() -> Unit = {}) = Intent(context, T::class.java).apply(applyToIntent)


            which is then reusable for several activities. Usage could then be as simple as:



            newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }


            Or if you really require your current function (I don't see a real reason for it though), it stays still simple enough:



            fun getNewIntent(context: Context, following: Boolean, userId: String) = newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }





            share|improve this answer
























            • the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

              – gmetax
              Nov 23 '18 at 14:28













            • companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

              – gmetax
              Nov 23 '18 at 14:51













            • it is not easy to write it in comment :)

              – gmetax
              Nov 23 '18 at 14:52











            • I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

              – Roland
              Nov 23 '18 at 15:20











            • Private consts but removed the word private from comment

              – gmetax
              Nov 23 '18 at 15:27














            1












            1








            1







            They are basically equivalent. I would go for whatever is the most readable to you and/or your team.



            I personally like the direct assigments if it is clear enough what is done in the function. Usually that's the case if it's a one-liner or if an initialization is followed by something like .apply (so basically just relatively short). For your functions I would actually introduce something like the following:



            inline fun <reified T> newIntent(context: Context, applyToIntent : Intent.() -> Unit = {}) = Intent(context, T::class.java).apply(applyToIntent)


            which is then reusable for several activities. Usage could then be as simple as:



            newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }


            Or if you really require your current function (I don't see a real reason for it though), it stays still simple enough:



            fun getNewIntent(context: Context, following: Boolean, userId: String) = newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }





            share|improve this answer













            They are basically equivalent. I would go for whatever is the most readable to you and/or your team.



            I personally like the direct assigments if it is clear enough what is done in the function. Usually that's the case if it's a one-liner or if an initialization is followed by something like .apply (so basically just relatively short). For your functions I would actually introduce something like the following:



            inline fun <reified T> newIntent(context: Context, applyToIntent : Intent.() -> Unit = {}) = Intent(context, T::class.java).apply(applyToIntent)


            which is then reusable for several activities. Usage could then be as simple as:



            newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }


            Or if you really require your current function (I don't see a real reason for it though), it stays still simple enough:



            fun getNewIntent(context: Context, following: Boolean, userId: String) = newIntent<UsersActivity>(context) {
            putExtra(FOLLOW, following)
            putExtra(USER, userId)
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 14:11









            RolandRoland

            10.5k11442




            10.5k11442













            • the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

              – gmetax
              Nov 23 '18 at 14:28













            • companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

              – gmetax
              Nov 23 '18 at 14:51













            • it is not easy to write it in comment :)

              – gmetax
              Nov 23 '18 at 14:52











            • I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

              – Roland
              Nov 23 '18 at 15:20











            • Private consts but removed the word private from comment

              – gmetax
              Nov 23 '18 at 15:27



















            • the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

              – gmetax
              Nov 23 '18 at 14:28













            • companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

              – gmetax
              Nov 23 '18 at 14:51













            • it is not easy to write it in comment :)

              – gmetax
              Nov 23 '18 at 14:52











            • I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

              – Roland
              Nov 23 '18 at 15:20











            • Private consts but removed the word private from comment

              – gmetax
              Nov 23 '18 at 15:27

















            the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

            – gmetax
            Nov 23 '18 at 14:28







            the only way that you still need to have the getNewIntent() in your companion object is to have there the consts of your arguments

            – gmetax
            Nov 23 '18 at 14:28















            companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

            – gmetax
            Nov 23 '18 at 14:51







            companion object of class UsersActivity is like that ` companion object { const val FOLLOW = "FOLLOWING" const val USER = "USER" fun getNewIntent(context: Context, following: Boolean, userId: String) = ... } `

            – gmetax
            Nov 23 '18 at 14:51















            it is not easy to write it in comment :)

            – gmetax
            Nov 23 '18 at 14:52





            it is not easy to write it in comment :)

            – gmetax
            Nov 23 '18 at 14:52













            I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

            – Roland
            Nov 23 '18 at 15:20





            I think I got you.... well... that sample should work nonetheless... you just need to ensure that you import that constant values, if not already done so...

            – Roland
            Nov 23 '18 at 15:20













            Private consts but removed the word private from comment

            – gmetax
            Nov 23 '18 at 15:27





            Private consts but removed the word private from comment

            – gmetax
            Nov 23 '18 at 15:27













            0














            No difference. Second variant must be use if you can write return in one line instead better use first. Also in second variant you can remove this part : Intent.



            Also can check this - https://kotlinlang.org/docs/reference/functions.html






            share|improve this answer




























              0














              No difference. Second variant must be use if you can write return in one line instead better use first. Also in second variant you can remove this part : Intent.



              Also can check this - https://kotlinlang.org/docs/reference/functions.html






              share|improve this answer


























                0












                0








                0







                No difference. Second variant must be use if you can write return in one line instead better use first. Also in second variant you can remove this part : Intent.



                Also can check this - https://kotlinlang.org/docs/reference/functions.html






                share|improve this answer













                No difference. Second variant must be use if you can write return in one line instead better use first. Also in second variant you can remove this part : Intent.



                Also can check this - https://kotlinlang.org/docs/reference/functions.html







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 13:52









                Ivan KrasilnikovIvan Krasilnikov

                664




                664






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447622%2ftwo-ways-creating-functions-compared-in-kotlin%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini