Use public vars or public function to get info from another class? And another question :)

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.

1 Like

Thank you for your reply :slight_smile:

How would you implement something like this if I may ask?
Seperate singletons for score and strawberry and reference them directly?

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.