Best practice with function parameters

I have a question regarding the use of function parameters.

In the past, I always wrote down my code so that all the information needed by the function was passed as a parameter. That is, global parameters are not used.

However, looking at other people's code, functions without parameters seem to be the norm. I should note that they are intended for private functions of a class and that the values ​​that would be passed as parameters are actually private member variables for this class.

This leads to a more streamlined code, and I'm starting to lean towards this for private functions, but would like other people's views.

eg.

Start();  
Process();  
Stop();  

is tidier and more readable than:

ParamD = Start(paramA, ParamB, ParamC);  
Process(ParamA, ParamD);  
Stop(ParamC);  

It destroys encapsulation in terms of method, but not in terms of class.

+3
8

- (, ) , , .

. , , -. : -, - , - , , , . , , Publish(), , , - , . . ( #):

// Notice the consuming class needs only know what it does, not how it does it
public interface IInvoicePublisher {
  pubic void Publish(Invoice anInvoice);
}

, :

public class DefaultPrinterInvoicePublisher
  DefaultPrinterInvoicePublisher _printer;
  public DefaultPrinterInvoicePublisher(DefaultPrinterFacade printer) {
    _printer = printer
  }
  public void Publish(Invoice anInvoice) {
    printableObject = //Generate crystal report, or something else that can be printed
    _printer.Print(printableObject);
  }

, , IInvoicePublisher , .

+1

, , , , , , .

, - :

void Start() {
    // read FieldA, FieldB, and FieldC
    // set the value of FieldD
}

void Process() {
    // read FieldA and do something
    // read FieldD and do something
}

void Stop() {
    // read the value of FieldC
}

Start() FieldD . , , , Process() , Start(). . , , FieldD. .

, , . , , , , - - , , .

, , , , , . , .

+6

, . , . , , . . ( ), . , , .

+4

- ​​ : myobject.someMethod() ( ) .

+2

, .

, , , , ? , / .

OTH , ? , , Static , .

, " ".

+2

, . , .

, .

+1

. . , , , :

  • ( , , . , , , )
  • . , . , , .
+1

, .

, , , .

Public DoTask( string jobid, object T)
{
 DoTask1(jobid, t);
 DoTask2(jobid, t);
}

private DoTask1( string jobid, object T)
{
}

private DoTask2( string jobid, object T)
{
}
0

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


All Articles