What's with the final/const craze in Flutter?










3














Java have final as well as Dart, but as far as I have seen, most Java people avoid using it all the time, since it can make your code less readable. For example, final is used all the time in class constants such as public static final int, but most people avoid using it in a method variable, since it's just seen as "excessive code correctness" by many developers, adding to boilerplate code.



C++ also has const and it can get crazy with it:



char ** const * const x // declare x as const pointer to const pointer to pointer to char


Now I am starting to learn Flutter and I am seeing final and const all over the place. Are those really necessary, like when they say:




Fields in a Widget subclass are always marked "final".




Or can they be treated as "excess of code correctness" and be removed?



Sorry if maybe my question is too stupid, I am really new to Dart and Flutter and I don't know all the side effects/benefits of using final and const, to justify the additional attention of when to remember to use them in my code.










share|improve this question




























    3














    Java have final as well as Dart, but as far as I have seen, most Java people avoid using it all the time, since it can make your code less readable. For example, final is used all the time in class constants such as public static final int, but most people avoid using it in a method variable, since it's just seen as "excessive code correctness" by many developers, adding to boilerplate code.



    C++ also has const and it can get crazy with it:



    char ** const * const x // declare x as const pointer to const pointer to pointer to char


    Now I am starting to learn Flutter and I am seeing final and const all over the place. Are those really necessary, like when they say:




    Fields in a Widget subclass are always marked "final".




    Or can they be treated as "excess of code correctness" and be removed?



    Sorry if maybe my question is too stupid, I am really new to Dart and Flutter and I don't know all the side effects/benefits of using final and const, to justify the additional attention of when to remember to use them in my code.










    share|improve this question


























      3












      3








      3







      Java have final as well as Dart, but as far as I have seen, most Java people avoid using it all the time, since it can make your code less readable. For example, final is used all the time in class constants such as public static final int, but most people avoid using it in a method variable, since it's just seen as "excessive code correctness" by many developers, adding to boilerplate code.



      C++ also has const and it can get crazy with it:



      char ** const * const x // declare x as const pointer to const pointer to pointer to char


      Now I am starting to learn Flutter and I am seeing final and const all over the place. Are those really necessary, like when they say:




      Fields in a Widget subclass are always marked "final".




      Or can they be treated as "excess of code correctness" and be removed?



      Sorry if maybe my question is too stupid, I am really new to Dart and Flutter and I don't know all the side effects/benefits of using final and const, to justify the additional attention of when to remember to use them in my code.










      share|improve this question















      Java have final as well as Dart, but as far as I have seen, most Java people avoid using it all the time, since it can make your code less readable. For example, final is used all the time in class constants such as public static final int, but most people avoid using it in a method variable, since it's just seen as "excessive code correctness" by many developers, adding to boilerplate code.



      C++ also has const and it can get crazy with it:



      char ** const * const x // declare x as const pointer to const pointer to pointer to char


      Now I am starting to learn Flutter and I am seeing final and const all over the place. Are those really necessary, like when they say:




      Fields in a Widget subclass are always marked "final".




      Or can they be treated as "excess of code correctness" and be removed?



      Sorry if maybe my question is too stupid, I am really new to Dart and Flutter and I don't know all the side effects/benefits of using final and const, to justify the additional attention of when to remember to use them in my code.







      dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 20:43

























      asked Nov 12 '18 at 22:22









      mFeinstein

      2,20872776




      2,20872776






















          2 Answers
          2






          active

          oldest

          votes


















          2














          const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.



          Since the value is known at compile time, you can make the necessary optimisations.



          final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.



          If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.






          share|improve this answer






















          • Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
            – mFeinstein
            Nov 12 '18 at 23:17










          • Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
            – chemamolins
            Nov 12 '18 at 23:25


















          2














          All these finals are not here just for fun. Flutter revolve around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.



          They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort






          share|improve this answer




















          • 2 characters because of autocomplete right?
            – mFeinstein
            Nov 13 '18 at 3:56










          • No because var versus final
            – Rémi Rousselet
            Nov 13 '18 at 5:52










          • Ohh right lol, I am still beginning to learn, still on a Java mindset
            – mFeinstein
            Nov 13 '18 at 5:53











          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%2f53270955%2fwhats-with-the-final-const-craze-in-flutter%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









          2














          const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.



          Since the value is known at compile time, you can make the necessary optimisations.



          final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.



          If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.






          share|improve this answer






















          • Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
            – mFeinstein
            Nov 12 '18 at 23:17










          • Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
            – chemamolins
            Nov 12 '18 at 23:25















          2














          const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.



          Since the value is known at compile time, you can make the necessary optimisations.



          final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.



          If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.






          share|improve this answer






















          • Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
            – mFeinstein
            Nov 12 '18 at 23:17










          • Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
            – chemamolins
            Nov 12 '18 at 23:25













          2












          2








          2






          const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.



          Since the value is known at compile time, you can make the necessary optimisations.



          final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.



          If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.






          share|improve this answer














          const means that the value of the variable is known at compile time and it is going to be constant for the whole duration of the application.



          Since the value is known at compile time, you can make the necessary optimisations.



          final means that the value will be constant or immutable from the moment it is set. But it is set at runtime. So you don't know it at compile time and you can't optimise it.



          If you don't use final you lose the immutability feature to what you should adhere in Flutter. You should always create a widget, not modify it. And the way to enforce that is to make all its fields final.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 13:02

























          answered Nov 12 '18 at 22:54









          chemamolins

          2,3341816




          2,3341816











          • Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
            – mFeinstein
            Nov 12 '18 at 23:17










          • Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
            – chemamolins
            Nov 12 '18 at 23:25
















          • Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
            – mFeinstein
            Nov 12 '18 at 23:17










          • Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
            – chemamolins
            Nov 12 '18 at 23:25















          Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
          – mFeinstein
          Nov 12 '18 at 23:17




          Why Widgets have to have final for their fields then? I could just omitt the final and have less boilerplate
          – mFeinstein
          Nov 12 '18 at 23:17












          Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
          – chemamolins
          Nov 12 '18 at 23:25




          Yes, you can omit them and it will work. But then you lose the immutability feature to what you should adhere. You should always create a widget, not modify it. And a way to enforce that is to make all its fields final.
          – chemamolins
          Nov 12 '18 at 23:25













          2














          All these finals are not here just for fun. Flutter revolve around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.



          They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort






          share|improve this answer




















          • 2 characters because of autocomplete right?
            – mFeinstein
            Nov 13 '18 at 3:56










          • No because var versus final
            – Rémi Rousselet
            Nov 13 '18 at 5:52










          • Ohh right lol, I am still beginning to learn, still on a Java mindset
            – mFeinstein
            Nov 13 '18 at 5:53
















          2














          All these finals are not here just for fun. Flutter revolve around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.



          They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort






          share|improve this answer




















          • 2 characters because of autocomplete right?
            – mFeinstein
            Nov 13 '18 at 3:56










          • No because var versus final
            – Rémi Rousselet
            Nov 13 '18 at 5:52










          • Ohh right lol, I am still beginning to learn, still on a Java mindset
            – mFeinstein
            Nov 13 '18 at 5:53














          2












          2








          2






          All these finals are not here just for fun. Flutter revolve around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.



          They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort






          share|improve this answer












          All these finals are not here just for fun. Flutter revolve around immutability. final is a neat way to enforce that immutability, ensuring you are correctly following the different design patterns.



          They are definitely not "excess of correctness" no. They exists to assure a maintainable app. 2 characters is absolutely worth the effort







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 2:09









          Rémi Rousselet

          24.5k24581




          24.5k24581











          • 2 characters because of autocomplete right?
            – mFeinstein
            Nov 13 '18 at 3:56










          • No because var versus final
            – Rémi Rousselet
            Nov 13 '18 at 5:52










          • Ohh right lol, I am still beginning to learn, still on a Java mindset
            – mFeinstein
            Nov 13 '18 at 5:53

















          • 2 characters because of autocomplete right?
            – mFeinstein
            Nov 13 '18 at 3:56










          • No because var versus final
            – Rémi Rousselet
            Nov 13 '18 at 5:52










          • Ohh right lol, I am still beginning to learn, still on a Java mindset
            – mFeinstein
            Nov 13 '18 at 5:53
















          2 characters because of autocomplete right?
          – mFeinstein
          Nov 13 '18 at 3:56




          2 characters because of autocomplete right?
          – mFeinstein
          Nov 13 '18 at 3:56












          No because var versus final
          – Rémi Rousselet
          Nov 13 '18 at 5:52




          No because var versus final
          – Rémi Rousselet
          Nov 13 '18 at 5:52












          Ohh right lol, I am still beginning to learn, still on a Java mindset
          – mFeinstein
          Nov 13 '18 at 5:53





          Ohh right lol, I am still beginning to learn, still on a Java mindset
          – mFeinstein
          Nov 13 '18 at 5:53


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53270955%2fwhats-with-the-final-const-craze-in-flutter%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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線