1

Should I prefer getting variable from other class directly(int number = something.number;) or should I use a function for getting that number(like in example below)? What is the difference?

class someclass
{
    private int number;
    public float GetSomething()
    {
        return number;
    }
}
class otherclass
{
    someclass something;
    private void somefunction()
    {
        int number = something.GetSomething();
    }
}
Gintas_
  • 4,940
  • 12
  • 44
  • 87
  • I think [property](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx) is a better approach – Doan Cuong Apr 04 '13 at 14:26
  • This is a very frequently asked question. There's lots of resources online on the subject. – Servy Apr 04 '13 at 14:43
  • @Servy I don't think it's a duplicate; the other question is about properties vs. fields; this question is about fields vs. methods. However, I agree that it would be easy to search for an answer to this question. – feralin Apr 04 '13 at 16:16
  • @feralin A property is virtually identical to a method, barring very few minor differences. The arguments between a field vs property almost all apply to a field vs method. – Servy Apr 04 '13 at 16:26
  • 1
    @Servy I know that properties and methods are similar, but they still are different (therefore, so are the questions). And anyway, this question is about whether fields or methods are better; the other, about what the difference between fields and properties are. IMO, they are different questions. – feralin Apr 04 '13 at 16:38
  • @feralin And yet the answers to that question answer this question, which is the criteria used for marking a duplicate. – Servy Apr 04 '13 at 16:41
  • @Servy ah, ok then. I mistakenly thought that the criterion was "other _question_ is very similar to this question". – feralin Apr 04 '13 at 16:49

4 Answers4

2

The difference between using a field reference or a getter method is that if you create a method that you expect "client code" to use, then you can always change the method code later and the client will not have to change his code. If you use a field, then the client will have to update their code from using the field to using a method, if you decide that you want, for example, validation in the method. So, in short, it is better practice to use getter methods for future-proofing. However, in a language like C#, you can also use properties, which act like methods but look like fields, so you can have the best of both worlds: nice syntax (fields), and future-proofing (methods).

feralin
  • 3,268
  • 3
  • 21
  • 37
1

Direct accessing to a class variable outside of a class is not a good practice so it's strongly recommended to use methods (also include properties). When there is no direct access to your class variables, other classes can use it and whenever you change the internal structure of your class you can do it with less effort. Consider you class:

class someclass
{
    // it's a field
    private int number;

    // it's a property
    public int Number
    {
        get{return this.number;}
    }

    //or you can use method
}

EDIT: If after a while you found that it was better to change the number's type to int?, you can do it because never outside the class anyone uses number so simply you can make changes to number and change your property this way

class someclass
{
    private int? number;

    public int Number
    {
        get{return this.number.Value;}
    }

    //or you can use method
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
1

for that type of data, you'd better use a property :

class someclass
{
    private int number;

    public int Number 
    {
        get {return number;}
        set {number = value;}
    }
}

then you can use someclass.Number anywhere else

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
0

Exposing fields is bad practice because it less extensive than expose method or property. For example you want to change this field's calculation logic depending on other fields values. That will be possible with both approaches but if you will use methods or properties it will be easier and cleaner to implement.

Vladimirs
  • 8,232
  • 4
  • 43
  • 79