Returning a Local Variable in Get Method

I tried writing my (Get) method to assign a local variable with the Volume Key and return it after.
Are there any advantages to using the method in this way? Or is it a bad idea?

(Rick’s Code)

return PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);

(My Attempt)

    public static float GetMasterVolume()
    {
        float volume =  PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
        Debug.Log("Volume is " + volume);
        return volume;
    }

It’s the same thing from the point of view of Unity.

Generally if you need to access two or more times the same value it is a good approach to store such value into a local variable (to avoid code repetition and hence errors) as you did.

That said, usually a getter method should simply return the value of the private variable, so from the point of view of style I suggest keeping Rick’s one liner.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms