Is it better to pass class data internally or directly?

Example:

// access fields directly
private void doThis()
{
    return doSomeWork(this.data);
}

// receive data as an argument
private void doThis(data)
{
    return doSomeWork(data);
}

The first option is related to the value in this date, while the second option avoids this connection. I feel that the second option is always better. This contributes to free connection in the classroom. Access to the data of the global class, perforce, seems to be poor. Obviously, at this point, the data of this class must be obtained directly. However, if access to global class data can be eliminated by passing parameters, it seems that this is always preferable.

The second example has the advantage of working with any data of the corresponding type, while the first is connected with working with data of a simple class. Even if you don't need extra flexibility, it seems nice to leave it as an option.

, . ? , - .

+3
3
  • , / . 1 - .

  • , / vars , . / .... , 1 - .

1 ...

  • , (var scoping). , . / var, .
  • , , . , .
  • / vars , " ", vars, .

2 !: -)

+3

IMHO. , . , . , , , , .

+2

, # 1 , ( , / - Java ).

.

+1

Source: https://habr.com/ru/post/1741538/


All Articles