"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding Clean Code: Objects and Data Structures⚡

Understanding Clean Code: Objects and Data Structures⚡

Published on 2024-08-24
Browse:436

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

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

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!

Release Statement This article is reproduced at: https://dev.to/alisamir/understanding-clean-code-objects-and-data-structures-j4f?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3