The question is simple & hard to understand at the same time. Why do I need to mark all my variables in the Statefull Widget as private?
Now when I declare a variable I declare it like private.
bool _isOpened = true;
String _currentUserUID;
...
But why do I need this? I do not access these variables from other widgets. I do not override. I know my app logic & totally insured in the safety of my code in all widgets.
So why do I need private variables? Is there any impact of using private variables & methods? Maybe performance benefits?
The prefix _ makes the variable private within the .dart code file you are in. The IDE (or at least VSCode, that I use) will then be able to tell you if you are using the variable or not. It also tells me, as the developer, that I need look no further than the code file that I am working in for changes to this variable. All important information as far as I am concerned.
The same is true when making methods private with the _ prefix. This is also highly recommended.
However, imho, this question relates to the the use of private variables in general so here is a link to a more general question and a much better set of answers than mine https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables
Related
I'm still learning Flutter and trying to understand best practices.
One of them seems to be that global variables are bad, and recommends using something like Provider instead.
In my case, I have a bunch of settings that are read from the back end when the app first starts. The variables in globals.dart are assigned values on startup, and then don't change after being assigned.
int maxLoginAttempts...
String welcomeMessage...
...
My confusion comes from the fact that most people who've asked this question (that I have found, sorry if I missed a post somewhere) are creating variables whose value may change as the application is used. I can understand something like Provider and ChangeNotifier would work well for that.
But in my case the values are assigned once - and don't change thereafter - they're only read.
So are globals still bad in my case?
In your case no provider/state management is needed if you are just assigning some value to global variable and and its value change does not require widget rebuild.
Good afternoon,
Is it possible to create constants in Progress-4GL?
The same question has been asked here, but there the question is based on object oriented programming (which I'm not doing).
There is no constant keyword in ABL.
The simplest way to create constant values is using static properties. These are available in any code, even procedural.
class ConstantValues:
define static public PI as decimal initial 3.14159 get.
end class.
You could add a private setter and do the assignment in the static constructor, instead of the initial value.
If you can't or don't want to use this approach, you can use preprocessors. If you need these values shared then define the preprocessors in includes and use those in your programs (even classes).
But that's - to me - more work than it needs to be if you're creating new constant values.
Even if not a constant and also possibly quite old school you can define precompiler statements that can work as a constant.
There's a possibility for global (&GLOBAL-DEFINE) and not global (&SCOPED-DEFINE)
Its also possible to undefine, check defined and other basic things.
These are define on compilation time so they cannot be changed dynamically when the program is running.
&SCOPED-DEFINE const1 1
&GLOBAL-DEFINE const2 hello
DISPLAY {&const1} "{&const2}".
I am beginner to flutter/dart and has read that we don't have public/private/protected access specifier for dart but if we want to make private instance variable, we can make the use of underscore(_) operator but it will not make the variable private to the class but to its own library, So what does it actually mean ?
Dart privacy is indeed only on a per library basis.
A name which begins with _ is a library private name. A private identifier, like _tmp, is considered a different name than a similarly spelled identifier, also _tmp, which occurs in a different library.
That means that code in a different library cannot access the private name _tmp because it can't even express it. If it tries to write _tmp, it can only refer to a private name of its own library instead.
The choice of embedding access control in the name makes sense when you remember that Dart has dynamic invocations. If you write dynamic x = ...; x.foo();, then this should call the foo method of x if there is one. To do this efficiently, it would be too much of an overhead if each dynamic invocation should also figure out where the name originally comes from and whether it's accessible to the caller. Dart avoids this overhead by making all public names visible, and all private names inexpressible.
The goal of privacy is to separate public interface API from internal implementation API, and to avoid naming conflicts.
You can write your private names without fear that they conflict with someone else's names, and without risking someone thinking they are meant for public use.
Dart does not try to protect code from other code in the same library. It's supposed to be the same author anyway, so they can be trusted to use the API responsibly (and if not, it's on themselves).
What it means for you as a user is: The library is the unit of code. You can make libraries which contains only a single class. The library privacy is class privacy for that class. Or you can create libraries with many classes and top-level functions which can all see each other's private names.
That means you should base your modularity on the need of classes to share implementation, and not really anything else. You can always build a larger API by exporting other libraries.
When creating a Pub package, I would create your own internal libraries, inside the lib/src/ directory, giving them whatever size is convenient, and then export your public API from the package main file in lib/.
a library/module/model refferes to the class itself, so any variables/methods are only accessable to the class itself. any class from the outside world trying to access this property, will not be able to. basically its own library means the class itself
I'm new to Go and couldn’t find a good solution for my problem.
I have 2 types, the first one is private, because I want the programmer to use a constructor. The 2nd type has the first one inside it.
screenshot of foo bar example
Sorry, I am not only new on GO, but also on stackoverflow question asking...so that’s why no code, but a screenshot
Don’t do that then. Private in go is private to that package, so it’s working as designed. Try working with the tools and see where you end up.
Instead you could:
Trust the user of your package and leave foo open
Put both types in the same package
Have a private field using a public type
Make the zero value useful so you don’t need a constructor
Lots of options, but also ask yourself do you really need this complex structure of nested structs for the problem at hand?
I've done some searching on this, but I cannot find info. I'm building an application inside sinatra, and using the coffeescript templating engine. By default the compiled code is wrapped as such:
(function() {
// code
}).call(this);
I'd like to remove that using the --bare flag, so different files can access classes and so forth that I'm defining. I realize that having it more contained helps against variable conflicts and so forth, but I'm working on two main pieces here. One is the business logic, and arrangement of data in class structures. The other is the view functionality using raphaeljs. I would prefer to keep these two pieces in separate files. Since the two files wrapped as such cannot access the data, it obviously won't work. However, if you can think of a better solution than using the --bare option, I'm all ears.
Bare compilation is simply a bad practice. Each file should export to the global scope only the public objects that matter to the rest of your app.
# foo.coffee
class Foo
constructor: (#abc) ->
privateVar = 123
window.Foo = Foo # export
Foo is now globally available. Now if that pattern isn't practical, maybe you should rethink your structure a bit. If you have to export too may things, you nest and namespace things better, so that more data can be exposed through fewer global variables.
I support Alex's answer, but if you absolutely must do this, I believe my answer to the same question for Rails 3.1 is applicable here as well: Put the line
Tilt::CoffeeScriptTemplate.default_bare = true
somewhere in your application.