I love nested functions. When b() is called only from a(), it should, in my opinion, be defined inside a(), like
fun a(){
fun b(){
//blablabla
}
b()
//blablabla
}
This is great encapsulation: You won’t see b() from outside of a(), and you easily find b() in a() when you are looking for it.
I like to write functions within functions within functions. A mess, you say? Not at all, as long as you always define the nested functions at the top of a function and don’t use local variables, it works really nice. I think this is great design! The problem is just testing: I can’t test the inner functions
I know this old saying that “you shouldn’t test private methods because they are implementation details, you should rather test functionality”. Well, I guess then you should just test only the main-function manually, as that’s the only thing that is public in the end… Also implementation details matter. You want to test one single unit of the code. Often it’s not possible to test just a public interface and at the same time spot what exactly goes wrong. This is especially the case if you write like me: Functions within functions within classes… (And this is a brilliant design, but that’s another discussion).
Why can’t it be possible to just break through everything as long as you are in the /test directory? It could, for example, be possible to do something like this:
€a(){ b() }
The € is used to break through a(). Then we are in a()'s scope, and can call b(). If a() takes an argument, and this argument is used in b(), we would need to do something like:
€a(10){ b() }
The same should of course be allowed for private methods:
€myInstance.myPrivateMethod()
What do you think? Why can’t we just break through everything from the test directory?