开发者

Function/Class Design Guidelines

I asked a fellow colleague yesterda开发者_开发技巧y if a function has too many parameters whether it would be better to create a class with properties instead. Are there any guidelines that I can follow?


I think that might depend on the language you are using, and the number of parameters in question, and are some of them allowed to be left out when calling the function.

VB has optional params, and C#3+ will allow for instantiation using params.

Will the new class have any other use except running that function, or will the state of that class be relevant later in the code?


When the number of parameters exceeds 5 I usually start thinking about refactoring the method. There's no absolute number, but this is my general rule. It may make sense to group the data in a data class, or sometimes it means that I should move the method to be closer to the data.


It all depends on the context.

Eg.

  1. If it is not a database operations we can do as per the design of the system. Break the module and try to create sub modules.

  2. If it is a database system I always prefer to write a separate bean class for the Fields and DAO class for operations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜