Private and public methods - how to organize them?

Do you think sometimes about how organize your code? I think about it a lot of time, when I have time to analyze it. I don't know how you, but I sometimes think that private is all what we don't use on another space of code and public is whatever what we used. It is organized without thinking about what the function does. The problem is when we want to test that function. If you have public is easy to test, but when you have a private function my question is why you want test it? Maybe it should be public or package private? As you see I had a lot of questions. I will show you my answers for it.

Access modifiers

As you know well, we have four access modifier (in Java): private, package private, protected and public.

  • A private is only class access.
  • A package private is for class and package, but doesn't for subclasses.
  • A protected the same as package, but with subclasses.
  • A public is access for all.

I am showing you these just for the reminder.

My previous wrong way for it.

I don't think that good organization of our code is "If I use from another part of the system I will set public, another private. If I want change something on subclasses I will set protected.", but a lot of time I do it like that. Why? Because It was automatic and clearly for me, but another step to change from private to public function was unfounded. I started to think about it and I got a lot of ideas.

What is a package?

It is determined by package organization, but I think this should be used for constructors, builders and change object state. If you want to limit it, because you want to create objects only by factory or change its state only by service, this is a good way to do it.

What is a protected?

This is good only for inheriting. If you have any public method in your class, which use some operation for realizing it and that operation can be changed by inherit class it should be protected. I suggest it is potentially private method, but can be changed by inherit class.

So what is a private?

Private methods are the implementations of the public method. It should be changeable without breaking down and change the tests. It is good for organizing your public method code and named parts of your code.

For the end public

Public is the method what you want to test. What you want to use to integrate with other systems. You can easily create data input and get a response. This is a function which uses other non-public functions. This is the place for communication and working with other places of the system.

Additionally

The good flow for thinking about access is always start from private method and slowly increase its access. The flow is looking like private -> package final -> package -> protected -> public final -> public. I heard it from my colleague and I agree with it in 100%.

Finally

I don't think about this way is the best for organizing, but I don't know another way for it which focuses on what the function is. We should create and organizing code focused on functionality. That helps us create better code.

Comments

Popular posts from this blog

Why TDD is bad practice?

How correctly imitate dependencies?

Software development using Angular 5 with Spring Boot