简体   繁体   English

最佳实践:默认和测试驱动开发的受保护或私有方法

[英]Best Practice: Protected or Private Methods by Default and Test-Driven Development

Similar Questions类似问题

My Question我的问题

Many people agree that protected methods should be used only when you have a reason to use them.许多人同意只有当你有理由使用受保护的方法时才应该使用它们。 How does a test-driven development model way into this?测试驱动开发 model 如何进入这个? (Especially with regard to faking objects.) I have a friend who is a big fan of TDD and now BDD and is a C# developer, and he told me he hardly ever uses the private keyword. (特别是关于伪造对象。)我有一个朋友是 TDD 的忠实粉丝,现在是 BDD,他是一名 C# 开发人员,他告诉我他几乎不使用private关键字。 After he said that, I kept using it for fields but starting defaulting all of my methods to protected .在他这么说之后,我继续将它用于字段,但开始将我的所有方法默认为protected Some people on StackOverflow also agree that protected should be used by default—could some of you please weigh in on this question? StackOverflow 上的一些人也同意默认情况下应该使用protected —— 你们中的一些人可以权衡一下这个问题吗? What is the best reason to use protected by default (since the threads above explain reasons not to)?默认情况下使用protected的最佳理由是什么(因为上面的线程解释了不这样做的原因)?

Edit : per Oded's comment, what about using protected by default and the Open-Closed Principle (a class should be open for extension and closed for modification)?编辑:根据 Oded 的评论,默认情况下使用protected和开闭原则(class 应该对扩展开放和对修改关闭)怎么样?

Here's what I consider a best practice, do with my developments and suggest to all of my clients:以下是我认为的最佳实践,与我的发展一起做,并向我的所有客户提出建议:

  1. Start with your test (or specification, if you're into BDD).从您的测试(或规范,如果您喜欢 BDD)开始。 Production classes and methods that you pull from your tests should be public .从测试中提取的生产类和方法应该是公开的。
    • Note: If you're developing in .NET, you may wish to consider using the internal keyword (and adding the InternalsVisibleTo assembly attribute to your production assembly, so that your test project can use the code) instead.注意:如果您在 .NET 中进行开发,您可能希望考虑使用internal关键字(并将InternalsVisibleTo程序集属性添加到您的生产程序集,以便您的测试项目可以使用该代码)。 Then you would make it public only if another production assembly depends on it.然后,只有在另一个生产程序集依赖于它时,您才会将其公开
  2. Any method that you create as part of the refactoring phase of your TDD should be made private .您在 TDD重构阶段创建的任何方法都应设为私有
  3. Make helper methods protected only when a derived production class needs them.仅当派生的生产 class 需要辅助方法时才保护它们。
  4. Any method created in the refactoring phase (now private or protected), that you need in another production class should be extracted to a helper class and made public (or internal in .NET, and any language that supports the concept).在重构阶段创建的任何方法(现在是私有的或受保护的),您在另一个生产 class中需要的任何方法都应提取到帮助程序 class 并公开(或在.NET和任何支持该概念的语言中公开)。
  5. If the helper class is needed in another assembly , then:如果另一个程序集需要辅助 class ,则:
    • If the other assembly already depends on the helper class' assembly, make the helper class public (if it isn't already).如果其他程序集已经依赖于帮助程序类的程序集,请将帮助程序 class公开(如果尚未公开)。
    • If the other assembly does not depend on the helper class' assembly, extract the helper class to a helper assembly (the best fit, or a new one) and make it public .如果另一个程序集依赖于助手类的程序集,则将助手 class 提取到助手程序集(最合适的或新的)并将其公开 Be sure to make the original assembly reference the new helper assembly, if it doesn't already.如果还没有,请确保使原始程序集引用新的辅助程序集。

This should pretty much cover all of your cases.这应该几乎涵盖了您的所有情况。
Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM