I read somewhere (or maybe I dreamt it or w/e) that you should always get another class variable through a function
Also I could not find a clear answer on this other than people just doing it because it is possible - I however would like to code in such a way that I am always using best practices.
So:
if (otherClass.IsTrue ()) {
And not
if (otherClass.isTrue == true)
Is this true or did my crazy brain tell me this?
And while we are on the subject of best practices I have another question (or should I post that in a new thread?):
If I want multiple global type classes to track letâs say score and strawberries, should I make one global class that links to all others like:
public class GlobalClass : Monobehaviour {
public static GlobalClass instance;
public ScoreClass x;
public StrawberryClass y;
void Awake () {
if (instance != null) {
GameObject.Destroy (instance);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
x = this.gameObject.AddComponent<ScoreClass> ();
y = this.gameObject.AddComponent<StrawberryClass> ();
}
}
And then to access from other classes:
GlobalClass.instance.x.DoStuff ();
Or should make both ScoreClass and StrawberryClass global and access them directly?
I ask this because I read static stuff is bad so perhaps it is better to have only one?
The explicit comparison does not depend on whether you access a field, a property or method in any way. Using methods (and/or properties in C#) is a way to encapsulate implementation details, that the other types should not be concerned about. Methods and properties are part of a types interface and determine which operations can be done from the âoutsideâ of the class (which also depends on the access modifiers).
Back to the samples:
Some people prefer the explicit comparison with the boolean literals, others prefer the one without (and negation with â!someBooleanâ instead of âsomeBoolean == falseâ or âsomeBoolean != trueâ).
So in most languages, itâs rather a matter of preference, readability and general conventions in your team and/or company.
In some languages, such as C & C++, thereâs a lot more to that, as (e.g.) integral values can also be checked for being equal/not equal to 0 without further (explicit) comparison. This also leads to the consequence that a mistakenly written assignement ( a = b instead of a == b ) can represent a valid expression that yields a boolean value.
As for C#, all of the operations in the following sample are valid and this also applies to any combination you can think off using âfalseâ and the negation operator.
public class BooleanSample
{
private bool _b;
public bool BooleanField;
public bool BooleanProperty { get { return _b; } }
public bool BooleanMethod() { return _b; }
}
// somewhere else in your application:
public void Foo(BooleanSample sample)
{
if (sample.BooleanField == true) { /* ... */ }
if (sample.BooleanProperty == true) { /* ... */ }
if (sample.BooleanMethod() == true) { /* ... */ }
if (sample.BooleanField) { /* ... */ }
if (sample.BooleanProperty) { /* ... */ }
if (sample.BooleanMethod()) { /* ... */ }
}
This is also a matter of preference but more importantly a matter of proper software design.
The âprosâ are obvious: itâs easy, very-fast and reliable to resolve the references. This often convinces people to use this throughout the whole source code. Itâs a very dirty way.
The âconsâ are more important though: This design couples a lot of types to the type âGlobalClassâ, theyâre highly dependent on it.
And worse, that âGobalClassâ type appears to be very application specific, which is turn makes all dependent types some sort of application specific as well. Why?
The problem arises when you decide to re-use your components or change the overall architecture:
If you ever decide to drop the âGlobalClassâ type or change its interface, everything that depends on it (i.e. everything resolving itâs references using this type etc.) will be condemned to break and will no longer be valid source code.
An overuse of singletons tends to result in alot of dependecies as well, because itâs gonna lead to the same result as mentioned in the previous post: due to convenience, everything would access the singleton of everything else.
Sometimes theyâre useful and do not harm your code structure, but they should be used wisely and sparingly.
I donât know a lot about the project, so I cannot really suggest anything in regards to implementation.
However, most of the time you can implement components that are self-contained and if thatâs not possible, try to only make use of components or modules that are less application specific, those which could be put into a âlower layerâ.
Yes, itâs easy to say that, but itâs probably the best you could aim for. Thatâs one of the difficulties in software engineering.