With Java, do you use private methods? Do you use static methods?
I have tended to avoid private methods because t开发者_C百科hey are difficult to test the functionality and/or override that functionality in a sub class implementation.
Do you use private methods? Do you favor protected methods instead of private methods?
Private methods are perfect for hiding operations in a class that neither client classes nor subclasses should rely on. The only other way to hide such operations is to in-line the functionality where you need it.
For testability :
First it depend of what you want to test. If you want to do integration tests, you don't care about that. You call public services, with all the stack activated and then see the results. You integrations test can run thousands line of code. This is perfectly fine.
If you are thinking more about unit tests, here you need you code to be simple to be easily testable. That's it you don't want to test to much code at a time, or several features at a time.
If code executed by each public method is not too big/complex, you are perfectly fine.
If the called code end up being too complex and you want to split the tests you can hack : - mark private methods as protected. - call private methods using introspection.
But the best maybe to define an helper class that would take care of some of the behaviour, test this class (as it has public methods), and then test the other class with a mock.
Anyway, i do not agree with this "Sacrificing that role to simplify testing is wrong, IMO.".
In fact making you code testable introduce lot of more change to the design than one might think. You'll need interfaces, inject mocks everywhere, ensure that all objects allocations are overridable, avoid statics... Make smalller class sometime... And if you code is not easily testable, it will not be tested at all.
You can be sure that if the only problem for the method to be easiliy stubbed/tested is a protected visibility instead of a private that the private will vanishes as soon that somebody is asked to test it.
Do you use private methods?
Yes.
Do you favor protected methods instead of private methods?
No.
I don't share your concern about testability of private methods. Any private method's behavior should testable via one or more visible methods or constructors. If it is not, then the chances are that it is dead code.
Private methods serve a critical role in hiding the internal details of the abstraction. Sacrificing that role to simplify testing is wrong, IMO.
Creating a good API meaning that you expose to all programmers public methods that they need and prevent other methods that they do not need.
in some cases, a private function needs to be provate because it can harm the proccess.
private methds are one of the most used feature that a programming lanuage can give you.
I prefer "private static" as a helper methods. That will make it clear to the reader that they will not modify the state of the object and most IDE will show calls to static methods in italics, so I will know it is static without looking a the signature.
I would definitely favor private methods if we go by basics of oops. Just take an object body for example. There are many private functions going on but we never see them. And its not necessary that they have to be static because they can change the state of the object. The breathing process involves many internal process. If any of them go wrong it may cause problem in breathing (changing the state) in a body (object). They are private because the user has nothing to know about them. There might be situations where a small process is expected to be used in many different bigger process. They may still not be directly useful for a user. So it is good to separate them as a private function and used in other function of the object.
精彩评论