Skip to main content
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

Your appeal to testability as justification for completely dropping private modifier is wrong, as evidenced eg by the answers in http://programmers.stackexchange.com/questions/135047/new-to-tdd-should-i-avoid-private-methods-nowShould I avoid private methods if I perform TDD?

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learnlearn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

Your appeal to testability as justification for completely dropping private modifier is wrong, as evidenced eg by the answers in http://programmers.stackexchange.com/questions/135047/new-to-tdd-should-i-avoid-private-methods-now

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

Your appeal to testability as justification for completely dropping private modifier is wrong, as evidenced eg by the answers in Should I avoid private methods if I perform TDD?

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

minor spelling correction
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295

<rant "I think I heard enough whining. Guess it's about time to say loud and clear..."&gt;"> #Private methods are beneficial to unit testing.

Now imagine that instead of above, I simply weaken access limitation. I skip the study of the code that uses the method and proceed straight with test that invokes my exPrivateMathodexPrivateMethod. Great? Not!

Summing up, keeping methodsticking with private method brings me a useful, reliable enhancement in unit tests. In contrast, weakening access limitations "for testability" only gives me an obscure, hard to understand piece of test code, which is additionally at permanent risk of being broken by any minor refactoring; frankly what I get looks suspiciously like technical debt.

<\rant></rant>

<rant "I think I heard enough whining. Guess it's about time to say loud and clear..."&gt; #Private methods are beneficial to unit testing.

Now imagine that instead of above, I simply weaken access limitation. I skip the study of the code that uses the method and proceed straight with test that invokes my exPrivateMathod. Great? Not!

Summing up, keeping method private brings me a useful, reliable enhancement in unit tests. In contrast, weakening access limitations "for testability" only gives me an obscure, hard to understand piece of test code, which is additionally at permanent risk of being broken by any minor refactoring; frankly what I get looks suspiciously like technical debt.

<\rant>

<rant "I think I heard enough whining. Guess it's about time to say loud and clear..."> #Private methods are beneficial to unit testing.

Now imagine that instead of above, I simply weaken access limitation. I skip the study of the code that uses the method and proceed straight with test that invokes my exPrivateMethod. Great? Not!

Summing up, sticking with private method brings me a useful, reliable enhancement in unit tests. In contrast, weakening access limitations "for testability" only gives me an obscure, hard to understand piece of test code, which is additionally at permanent risk of being broken by any minor refactoring; frankly what I get looks suspiciously like technical debt.

</rant>

minor wordsmithing
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is stable againsttolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

  1. Do I gain a test for specific usage of non-private API mentioned above? No: there was no test for nonPrivateMethod(true) before, and there is no such test now.

  2. Do outside readers get a chance to better understand usage of the class? No. "- Hey what's the purpose of the method tested here? - Forget it, it's strictly for internal use. - Oops."

  3. Is it stable againsttolerant to refactoring? No way: whatever I change in exPrivateMethod, will likely break the test. Rename, merge into some other method, change arguments and test will just stop compiling. Headache? You bet!

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is stable against refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

  1. Do I gain a test for specific usage of non-private API mentioned above? No: there was no test for nonPrivateMethod(true) before, and there is no such test now.

  2. Do outside readers get a chance to better understand usage of the class? No. "- Hey what's the purpose of the method tested here? - Forget it, it's strictly for internal use. - Oops."

  3. Is it stable against refactoring? No: whatever I change in exPrivateMethod, will likely break the test. Rename, merge into some other method, change arguments and test will just stop compiling. Headache? You bet!

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

  1. Do I gain a test for specific usage of non-private API mentioned above? No: there was no test for nonPrivateMethod(true) before, and there is no such test now.

  2. Do outside readers get a chance to better understand usage of the class? No. "- Hey what's the purpose of the method tested here? - Forget it, it's strictly for internal use. - Oops."

  3. Is it tolerant to refactoring? No way: whatever I change in exPrivateMethod, will likely break the test. Rename, merge into some other method, change arguments and test will just stop compiling. Headache? You bet!

Private methods are beneficial to unit testing...
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
Loading
added 718 characters in body
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
Loading
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
Loading