Member or Global Function?

Member or Global Function?

I have a function that takes 2 classes as arguments. Should I make the function global or inside the class? If that should be a member function, is there any rules to select which class should I place the function in?

The “rules” are nearly endless, and it’s hard to address this without seeing your code. Some OOP purists argue that every function should be a method. I’m not prepared to go that far but, in theory, some encapsulation will be better than none.

You might look at which of the two classes the function accesses the most, and try to determine which class the function is more closely related to.

In the end, even the best OOP designers run across situations where OOP doesn’t seem to quite fit a particular situation exactly.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist