0

I often write code which makes an object and then use static methods in static classes to manipulate said object.

Am I missing the point of OOP? How do I know whether I'm thinking in the OOP mind set? I doubt I am...

1

Yes, you are missing OOP principles in your coding. OOP is not about bearing primitive types, OOP is about having an object that has responsibilities(methods essentially).

GRASP Principles are a good start to learn where which method belongs in OOP.

In OOP you want to treat most instances as objects that have responsibilities and instance fields(instance specific variables). However, sometimes(and I mean sometimes) you would find a need to have a static class, which is perfectly valid as long as you really sure this class has no need for an instance. But before you settle down with creating one, think - "Am I missing an object?".

Your Functional Way:

var alice = new Person("Alice");

ChangeName(alice, "John");

// Somewhere in your static code
function ChangeName(Person person, string name) {
    person.name = name;
}

OOP Way:

var alice = new Person("Alice");
alice.ChangeName("John");

public class Person {
    private _name;    

    public Person(string name) {
        _name = name;
    }

    public void ChangeName(string name) {
        _name = name;
    }
}
  • I'm sorry but in what universe could the first code you wrote be considered "functional" in any way at all? – sara Jun 28 '16 at 15:31

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