“助手”方法的定义 [英] Definition of a 'helper' method

查看:71
本文介绍了“助手”方法的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们什么时候决定一个方法是'帮助'方法? - 我在这里寻找一些定义。



一段时间以来,我已经看到源代码中的区域的应用程序和API称为助手或'助手方法'。什么使方法成为'帮助'方法?



是的,授予该方法提供了一些帮助的功能 - 但是更稳固的技术定义呢?对我来说,我从来没有见过任何一致的模式 - 例如,这个方法应该是静态的还是无效的?



我经常看到各种方法被称为辅助方法但是那里他们的装饰品,访问修饰符或功能似乎没有任何一致性......



非常感谢

解决方案

< blockquote>它显然也被称为委托模式。维基百科的文章是 http://en.wikipedia.org/wiki/Delegation_pattern [ ^ ]


这里有一个很好的描述帮助方法 [ ^ ]但是,我同意Chris(解决方案1):可能每个人都有他自己对'辅助方法'的定义。


你一个有趣的问题,那里!



我怀疑你找不到任何关于帮助方法的通用定义,只有1001个不同的个人对象。


所以,这是我的...



辅助方法是一种目标或与某些方法相关的方法上课(也许完全)但出于某种原因不是作为该类的一部分实现的。通常,原因是该类是以不可修改的代码体(内部或第三方库,.Net框架等)实现的,并且额外的工作不能或不能证明实现子类。



我创建的一个帮助方法示例是一个字符串并粘贴在每个字母前面的空格中(除了在字符串的开头) )。获取属性名称或数据库列名称,并使某些内容更容易被用户看到。



*它属于我想要获取新文本的类吗?没有。



*我可以扩展System.String吗?没有。



怎么办?创建一个帮助方法。



.Net在几代之前就引入了扩展方法。我爱他们。我发现它们允许我将这些方法与消费代码分开(所以我不是'污染'我需要行为的类的代码);另外,它们出现在intellisense中用于扩展课程。



我将个人规则作为内部实现我的扩展方法的类。这样,如果他们需要使用实现它们的程序集,它们就不会成为需要为团队中的其他开发人员正确记录的公共API。


When do we decide a method is a 'helper' method? - I'm looking for a bit of definition here.

For some time now I've seen applications and APIs with regions in the source called something like 'Helpers' or 'Helper Methods'. What makes a method a 'helper' method?

Yes, granted the method provides some functionality that 'helps' - but what about more solid technical definition? For me I've never seen any consistent pattern - eg should the method be static or void?

I often see all sorts of methods referred to as helper methods but there never seems to be any consistency in their adornments, access modifiers or functionality...

Many Thanks

解决方案

It is apparently also called a delegation pattern. The Wikipedia article is http://en.wikipedia.org/wiki/Delegation_pattern[^]


A nice description here "Helper Method"[^], however, I agree with Chris (Solution 1): probably everyone has his personal definition of 'helper method'.


You've an interesting question, there!

I suspect you'll not find any universal definition of helper method, just 1001 different personal takes on the subject.

So, here's mine...

A helper method is a method that 'targets' or relates to some class (perhaps entirely) but for some reason is not implemented as part of that class. Typically, the reason would be that the class is implemented in an unmodifiable body of code (an in house or 3rd party library, the .Net framework, etc.) AND where the extra work cannot, or does not justify, implementing a subclass.

An example of a helper method I've created is one that take a string and sticks in spaces in front of every captial letter (except at the beginning of the string). Takes a property name, or database column name, and makes something moderately more acceptable for users to see.

* Does it belong in the class where I want to get the new text? No.

* Can I extend System.String? No.

What to do? Create a helper method.

.Net kindly introduced extension methods a couple of generations ago. I love them. I find that they allow me to segregate such methods away from the consuming code (so I'm not 'polluting' the code of the class where I need the behaviour); in addition, they show up in the intellisense for the extended class.

I make it a personal rule to make the class(es) that implement my extension methods internal. That way they do not become a public API that needs to be properly documented for other developers on the team to understand, if they need to consume the assembly in which they are implemented.


这篇关于“助手”方法的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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