0

I contribute to a couple of open source software, and I've noticed two types of patterns when classes act on data. I am interested in the name of these patterns.

The first is something that goes like this:

class A{
public:
    void compute_something(int* var){
        *var = 2;
    }
};
int main() {
    A a;
    int result;
    a.compute_something(&result);
}

Basically, the variable is initialized outside the class and then passed into the class method to be "filled in" with a value.

The second pattern is the usual:

class A{
public:
    int compute_something(){
        return 2;
    }
};
int main() {
    A a;
    int result = a.compute_something();
}

Here, the method's return type is used to return the value and filling in the variable is made outside the class.

Now is there any name for these types of patterns? And is there a prefered pattern than the other?

  • 1
    You might want to adjust your examples. The first is currently just stupid. "Output parameters" (a term I associate with this) are mainly used when there are multiple (sometimes even independent) outputs of different kind. Think a result and a status code. – Daniel Jour Mar 24 '18 at 10:41
  • 1
    Only use out parameters if you already are making use of the return value for something else. – Erik Eidt Mar 24 '18 at 15:19
  • FYI, in your first example, the variable is not being initialized outside the class, but rather merely declared. – Erik Eidt Mar 24 '18 at 15:20
1

Yes, the short version is that the two patterns in the question termed "pass by reference" and "pass by value." Different languages have different levels of support, and different idioms and preferences, for these and other patterns of data management. Stack vs heap-based memory allocation is a related implementation detail.

The deeper answer is that there is a fundamental challenge for programmers around the ownership of memory and lifecycle of memory management. Pass by value is easy but limiting and expensive. Pass by reference is magical but it can become extremely messy very quickly. Innumerable expensive bugs, including most of the most severe security bugs, ultimately arise out of failures to properly manage ownership.

Of popular languages, Rust has gone furthest in describing a set of semantics and giving explicit tools to the programmer to team up with the compiler to make sure that the ownership and lifecycle of particular variables are managed correctly. You don't just pass a pointer, you allow other code to borrow a variable, ownership of which gets tracked by the compiler. The Rust book's treatment of the issue is very good:

https://doc.rust-lang.org/book/second-edition/ch04-01-what-is-ownership.html

  • hmm is the salient part of the example really the return statement or type? I read it as the calling methodology – Ewan Mar 26 '18 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.