函数参数最佳实践 [英] Function Parameter best practice

查看:80
本文介绍了函数参数最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在过去,我一直编写我的代码,以便函数所需的所有信息都作为参数。即没有使用全局参数。



然而,通过查看其他人的代码,没有参数的函数似乎是常态。我应该注意到这些是针对类的私有函数的,并且作为参数传入的值实际上是该类的私有成员变量。



导致整洁的代码,我开始倾向于这个私人功能,但希望其他人的意见。



例如

 开始(); 
Process();
Stop();

更整洁,更易读:

  ParamD =开始(paramA,ParamB,ParamC); 
过程(ParamA,ParamD);
Stop(ParamC);

它从方法的角度打破了封装,而不是从类的角度来看。在面向对象的语言中,传递依赖关系(这个类将与之通信的类)和配置值是常见的。构造函数,只有在函数调用中实际操作的值。

这实际上可以更具可读性。考虑代码,你有一个生成和发布发票的服务。可以有多种方式来进行发布 - 通过将它发送到某种集中式服务器的Web服务,或者通过发送给仓库中某个人的电子邮件,或者也可以将其发送到默认打印机。但是,调用Publish()的方法通常不会知道发布的具体细节 - 它只需要知道发布顺利完成即可。这样可以让您一次只想较少的事情,并更好地专注于问题。然后你只是简单地使用一个服务接口(在C#中):

  //注意消费类只需知道它是做什么的,而不是它是如何做的
public interface IInvoicePublisher {
pubic void Publish(Invoice anInvoice);
}

这可以通过多种方式实现,例如:

  public class DefaultPrinterInvoicePublisher 
DefaultPrinterInvoicePublisher _printer;
public DefaultPrinterInvoicePublisher(DefaultPrinterFacade打印机){
_printer =打印机
}
public void Publish(发票anInvoice){
printableObject = //生成水晶报表或其他内容可打印
_printer.Print(printableObject);
}

使用它的代码也会将IInvoicePublisher用作构造函数参数该功能可供整个使用。


I have question regarding the use of function parameters.

In the past I have always written my code such that all information needed by a function is passed in as a parameter. I.e. global parameters are not used.

However through looking over other peoples code, functions without parameters seem to be the norm. I should note that these are for private functions of a class and that the values that would have been passed in as paramaters are in fact private member variables for that class.

This leads to neater looking code and im starting to lean towards this for private functions but would like other peoples views.

E.g.

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

is neater and more readable than:

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

It does break encapsulation from a method point of view but not from a class point of view.

解决方案

In an object-oriented language it is common to pass in dependencies (classes that this class will communicate with) and configuration values in the constructor and only the values to actually be operated on in the function call.

This can actually be more readable. Consider code where you have a service that generates and publishes an invoice. There can be a variety of ways to do the publication - via a web-service that sends it to some sort of centralized server, or via an email sent to someone in the warehouse, or maybe just by sending it to the default printer. However, it is usually simpler for the method calling Publish() to not know the specifics of how the publication is happening - it just needs to know that the publication went off without a hitch. This allows you to think of less things at a time and concentrate on the problem better. Then you are simply making use of an interface to a service (in C#):

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

This could be implemented in a variety of ways, for example:

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);
  }

The code that uses it would then take an IInvoicePublisher as a constructor parameter too so that functionality is available to be used throughout.

这篇关于函数参数最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆