What is object oriented development?

Let me show you a snippet of C# code:

public class Person {
    public string Name { get; set; }
    public decimal Salary { get; set; }
}

// some somewhere else in the code

Person p = getPerson();
p.Salary *= 1.15;   // give a 15 % raise

Is Person a class?

What a strange question! Of course this is a class, it says so right on the first line!

If it is a class, it should have an assigned responsibility. What does this Person class do?

Read more: What is object oriented development?

Well, not very much. It just exposes two properties, but there is no actual logic.

Is defining a class this way according to the object oriented design principles?

One of those principles states that data and operations on the data must be combined in a single class to achieve encapsulation. That is not the case here. So no, this snippet is not written according to object oriented design.

I agree. This snippet does not use the class to hold a responsibility, but the class is only used to structure related information. The behavior is decoupled from the data. If the whole program was designed this way, then that would not be object oriented design, even though class definitions are used, right?

I can only agree with that.

So it is very easily possible to write non-object oriented code using an object oriented language.

Is C an object oriented language?

No, it is a procedural programming language. There is no notion of classes or objects.

But you can define structs to hold data definitions. For example, you can do this:

// Person.h

typedef struct Person Person;

void RaiseSalary(Person* person);

And

// Person.c

struct Person {
    char* name;
    double salary*;
};

void RaiseSalary(Person* person) {
    person->salary *= 1.15;
}

In this example in C, is Person a class?

I’d say no, a C struct definition is really different. For example, a struct cannot have instance methods.

We just talked about object oriented design, where data and operations on that data must be combined in a single unit. That is done here, right?

In this example, there is encapsulation when viewed on the file level. Person.c is the only place where the actual fields of the struct are known, so all operations on the fields of Person must be made available by the Person.h header file. It is not even possible to implement a salary raise outside Person.c.

If you would design all code of a program this way, would that be object oriented design?

Yes and no.

What do you mean?

If you’re writing C code this way, you’ll achieve some properties of object oriented design, like encapsulation and having data and operations on that data defined together. But there are still no classes or objects since those don’t exist in C.

How would that matter?

You cannot do person->RaiseSalary(), so you cannot work with objects as such. And the methods in Person.c might even be completely unrelated to the Person struct.

Methods in Person.c are comparable with static methods in C#, but C# is still object oriented right? So the possibility of static methods seems not to be a good criterion. And about the call syntax: there is technically no difference between person->RaiseSalary() and RaiseSalary(person). The first version is just easier to read by making the first argument special. C#, being an object oriented language, makes this syntax easy to use. That those call syntax forms are the same is also shown by C# extension methods, which are basically just static methods that can be used like instance methods.

As you can see from the C example, it is the context of the method that determines what data it can access, not the method itself.

So it is possible to write object oriented code in a procedural language.

Yes, although object oriented languages provide syntax that make it easier. But ultimately, it is up to the developer, not the language, to develop according to object oriented principles and to have the benefits from it.

Leave a Reply

Your email address will not be published. Required fields are marked *