Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 19, 2023 11:50 am GMT

Object VS Data Structure : The Fight !

As developer, we need to hide code implementation for users, we don't want to expose the logic of our code. Why ? Because they don't need to know the inner code, they only need what we choose to expose !

Hiding implementation is not just putting getter and setter and make the dedicated variable in private.

A short example about that:

Should I use the first example ?

class Vehicule {  getFuelTankCapacityInGallons() {}  getGallonsOfGasoline() {}}

or this example ?

class Vehicule {  getPercentFuelRemaining() {}}

The second option is preferable, because we don't want to expose any details about our data to the user (he don't need to see that Vehicule is using Gasoline or FuelTankCapacity). He just need to know the PercentFuelRemaining.

So as you can see, data abstraction is not only putting setter or getter on private variable.

So when you need to modeling your data in some case you will be facing two choices: choose an Object design model or a Data structure model.

In this article you will learn what is this and which use depending on the context!

What is Object and Data structure

Data Structure

It exposes their data but it doesn't have meanfull function.

class Shape {}class Square extends Shape {  side: number;}class Rectangle extends Shape {  height: number;  width: number;}class Circle extends Shape {  center: number;  radius: number;}class Geometry {  PI = 3.14;  getArea(shape: Shape) {    if (shape instanceof Square) {        return shape.side * shape.side    }    if (shape instanceof Rectangle) {        return shape.height * shape.width    }    if (shape instanceof Circle) {        return this.PI * shape.radius * shape.radius    }    throw new Error ('Unknow Shape')  }}

So Shape class and subclass expose their Data without any behavior (no methods), this is the Geometry class that handle all behavior.

Pro & Cons for Data Structure

On the first hand, data structure make easy to add new behavior (new function like adding Perimeter function) on the existing data (Shape class and subclass) without editing them. Because we only need to add it in the Geometry class !

On the second hand, data structure is bad if you will need to add new shape. Imagine that Geometry class has 20 methods, if you add a new subclass from Shape, you will need to edit all function in Geometry class !

Object

It hides their data with abstraction but it expose their function.

class Shape {    getArea() {}}class Square extends Shape {    side: number;    getArea() {        return this.side * this.side    }}class Rectangle extends Shape {    height: number;    width: number;    getArea() {         return this.height * this.width    }}class Circle extends Shape {    PI = 3.14;    center: number;    radius: number;    getArea() {         return this.radius * this.radius    }}

As you can see, with abstraction we don't need the Class Geometry.

Pro & Cons for Object :

On the first hand, adding a new Shape is very easy since existing function are not impacted by this effect.

On the second hand, adding new function (like adding Perimeter function) needs to update all subclass of Shape !

TLDR; When to use Data structure vs Object ?

Use Data structure if you need to add new function to existing data, but you don't need to add new data.

Use Object if you nedd to create a lot of object and you don't need to add new function to it.

Avoid the mix-up

A lot of developer made a confusion between both structure. So they are creating a mix-up (50% data structure and 50% object).

This leads to have a structure that have all Cons.

DTO & Bean

DTO (Data Transfer Object) is a data structure with public variable and no function. They are usefull when communicating with database to convert raw data in database or during request payloads in API Call that that defines how the data will be sent over the network.

For purist, the DTO has an another form called Bean. It has private variable manipulated by getters & setters.

Bean form :

class Adress {    private street: string;    private city: string;    private state: string;    private zip: string;    public constructor(street: string, city: string, state: string, zip: string) {        this.street = street;        this.city = city;        this.state = state;        this.zip = zip;    }       public getStreet() {        return this.street    }    public getCity() {        return this.city    }}

DTO form :

class Adress {    public street: string;    public city: string;    public state: string;    public zip: string;    public constructor(street: string, city: string, state: string, zip: string) {        this.street = street;        this.city = city;        this.state = state;        this.zip = zip;    }   }

ActiveRecord

ActiveRecord are special form of DTO, but they have methods like save or find. Since we are using it for translation our database data to our source code data.

To avoid mixup, we need to treat ActiveRecord as data structure and using an object (here we called it Manager) that will contains all business logic of methods save or find, and this object will hide their internal data (that is our ActiveRecord !)

Image description

const userActiveRecord = { id: 1, name: 'CodeOz', age: 27 }const manager = new Manager(userActiveRecord)manager.save()

ActiveRecord is not linked to the manager, but the Manager needs it to save it in the database.

Conlusion

  • Hide implemention is not only use private data and manipulate it by accessor.

  • Use Data structure if you need to add new function to existing data, but you don't need to add new data.

  • Use Object if you nedd to create a lot of object and you don't need to add new function to it.

  • Don't make hybrid between Data structure & Object.

  • DTO are Data structure.

  • ActiveRecord are special form of DTO.

I used example from 'Clean Code by Robert C Martin' in this article

I hope you like this reading!

You can SUPPORT MY WORKS

You can follow me on

Twitter : https://twitter.com/code__oz

Github: https://github.com/Code-Oz

For french developper you can check my YoutubeChannel

And you can mark this article!


Original Link: https://dev.to/codeoz/object-vs-data-structure-the-fight--k0h

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To