What's with the final/const craze in Flutter?
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
add a comment |
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
add a comment |
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
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
dart flutter
edited Nov 13 '18 at 20:43
mFeinstein
asked Nov 12 '18 at 22:22
mFeinsteinmFeinstein
2,25372777
2,25372777
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
Why Widgets have to havefinal
for their fields then? I could just omitt thefinal
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
add a comment |
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
2 characters because of autocomplete right?
– mFeinstein
Nov 13 '18 at 3:56
No becausevar
versusfinal
– 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Why Widgets have to havefinal
for their fields then? I could just omitt thefinal
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
add a comment |
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.
Why Widgets have to havefinal
for their fields then? I could just omitt thefinal
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
add a comment |
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.
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.
edited Nov 13 '18 at 13:02
answered Nov 12 '18 at 22:54
chemamolinschemamolins
2,5391917
2,5391917
Why Widgets have to havefinal
for their fields then? I could just omitt thefinal
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
add a comment |
Why Widgets have to havefinal
for their fields then? I could just omitt thefinal
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
add a comment |
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
2 characters because of autocomplete right?
– mFeinstein
Nov 13 '18 at 3:56
No becausevar
versusfinal
– 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
add a comment |
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
2 characters because of autocomplete right?
– mFeinstein
Nov 13 '18 at 3:56
No becausevar
versusfinal
– 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
add a comment |
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
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
answered Nov 13 '18 at 2:09
Rémi RousseletRémi Rousselet
27.5k35887
27.5k35887
2 characters because of autocomplete right?
– mFeinstein
Nov 13 '18 at 3:56
No becausevar
versusfinal
– 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
add a comment |
2 characters because of autocomplete right?
– mFeinstein
Nov 13 '18 at 3:56
No becausevar
versusfinal
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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