informa
/
2 MIN READ
Blogs

Don't Show me your Private Parts

What's a private method, why use them? Their purpose is to hide implementation details, the way we accomplish a task. We generally don't want to share these details so we limit its scope and who can call it...

What's a private method, why use them? Their purpose is to hide implementation details, the way we accomplish a task. We generally don't want to share these details so we limit its scope and who can call it.

Use private methods to share common implementations so a service can have different APIs for different client's needs. But also use private methods whenever there's any bit of functionality that you can name but don't need to expose to the outside.

When I mark something as private I'm saying the world doesn't need to know or care. I'm telling other developers they don't have to worry about what's inside. Of course, private methods don't bloat an object's interface and can provide an extra measure of security as private methods can't be called from outside the object that contains it. Keep as much as you can private and only expose what is required to use your service.

Try not to expose implementation details so that you're free to change them later without impacting the system. Do this by following the advice from the Gang of Four (GoF): design to interfaces, prefer aggregation, and encapsulate variation.

Offer your services through an interface based on the callers’ needs then adapt it. Present what you want, not how to get it. Code to abstractions rather than concrete classes. As you do these things, classes get defined responsibilities and the domain model remains crisp.

But don't tell me how you'll do something, don't even imply it. That forces you to commit to a particular implementation. Keep those details private instead.

How do you test private methods? The short answer is you don't, at least not directly. If you can exercise the behavior of a private method by calling its public interface then your private method will have code coverage and you can consider it tested. If you feel that the private method does so much that it really needs its own test then it probably shouldn't be private. In those cases, you can put the behavior in a public method in its own class that's held privately. This gives you testability and potential reusability of the behavior without bloating the interface of the client.

Latest Jobs

IO Interactive

Hybrid (Malmö, Sweden)
3.02.23
Gameplay Director (Project Fantasy)

Arizona State University

Los Angeles, CA, USA
2.27.23
Assistant Professor of XR Technologies

IO Interactive

Hybrid (Copenhagen, Denmark)
3.02.23
Animation Tech Programmer

Purdue University

West Lafayette, IN, USA
3.02.23
Assistant Professor in Game Design and Development
More Jobs   

CONNECT WITH US

Explore the
Advertise with
Follow us

Game Developer Job Board

Game Developer

@gamedevdotcom

Explore the

Game Developer Job Board

Browse open positions across the game industry or recruit new talent for your studio

Browse
Advertise with

Game Developer

Engage game professionals and drive sales using an array of Game Developer media solutions to meet your objectives.

Learn More
Follow us

@gamedevdotcom

Follow us @gamedevdotcom to stay up-to-date with the latest news & insider information about events & more