DEV Community

Cover image for Understanding Clean Code: Objects and Data Structures⚡
Ali Samir
Ali Samir

Posted on

Understanding Clean Code: Objects and Data Structures⚡

Understanding the distinction between objects and data structures is crucial when writing clean code.

Both have their place in software design but serve different purposes and are best suited for various scenarios.

In this article, we'll dive into the differences between objects and data structures, and explore when to use each, using JavaScript examples to illustrate the concepts.


📌 What Are Objects?

Objects are the foundation of object-oriented programming (OOP).

They encapsulate both data and behavior, meaning that they not only hold information but also provide methods to interact with that information.

The core idea behind objects is to bundle data with the functions that operate on that data, ensuring that the internal state of an object is manipulated only through its methods.

Example of an Object:

class Rectangle {
  constructor(width, height) {
    this.width = width;
    this.height = height;
  }

  getArea() {
    return this.width * this.height;
  }

  getPerimeter() {
    return 2 * (this.width + this.height);
  }
}

const myRectangle = new Rectangle(5, 10);
console.log(myRectangle.getArea());       // Output: 50
console.log(myRectangle.getPerimeter());  // Output: 30
Enter fullscreen mode Exit fullscreen mode

In this example, the Rectangle class is an object that encapsulates the width and height data, along with the methods getArea() and getPerimeter().

The internal data (width and height) is protected and can only be accessed or modified through these methods.


📌 What Are Data Structures?

Data structures, in contrast, are collections of data without any associated behavior.

They focus on exposing the data rather than protecting it, making it accessible for external functions to manipulate.

Data structures are more about storing and organizing data in a way that makes it easy to retrieve and modify.

Example of a Data Structure:

const rectangle = {
  width: 5,
  height: 10
};

function getArea(rectangle) {
  return rectangle.width * rectangle.height;
}

function getPerimeter(rectangle) {
  return 2 * (rectangle.width + rectangle.height);
}

console.log(getArea(rectangle));       // Output: 50
console.log(getPerimeter(rectangle));  // Output: 30
Enter fullscreen mode Exit fullscreen mode

Here, rectangle is a data structure. It exposes its data directly, and the functions getArea() and getPerimeter() operate on this exposed data.

Unlike objects, there is no encapsulation, and the data can be freely accessed and modified by any external function.


📌 When to Use Objects

Objects are ideal when you want to encapsulate behavior along with the data.

This encapsulation allows you to control how the data is accessed and modified, providing a layer of protection.

Objects are also well-suited for situations where different types of objects need to interact with each other through well-defined interfaces.

⚡ Use objects when:

  • You need to protect the internal state and ensure that it can only be changed through specific methods.
  • You want to define behavior that is closely related to the data.
  • You are working with a complex system where encapsulation and abstraction are important.

📌 When to Use Data Structures

Data structures are useful when you need to simply store and organize data without attaching behavior.

They allow for easy and direct access to the data, which can be beneficial in scenarios where performance and simplicity are key.

⚡ Use data structures when:

  • You need to expose data directly for external functions to operate on.
  • You want to keep the design simple and straightforward.
  • The behavior is separate from the data and can be implemented in external functions.

Key Takeaways 💯

  • Objects encapsulate data and behavior, protecting the internal state and providing controlled access through methods.
  • Data Structures expose data directly, allowing external functions to operate on it without any encapsulation.
  • Use objects when you need encapsulation and want to bundle behavior with data.
  • Use data structures when you need to store data without attaching behavior, and when simplicity and direct access are priorities.

Understanding the distinction between objects and data structures is essential for writing clean, maintainable code.

By choosing the right approach based on the needs of your application, you can create systems that are both efficient and easy to understand.

Happy Coding!

Top comments (0)