Why you should avoid direct access to data members

Why you should avoid direct access to data members

Sometimes it may be tempting to declare class data members as public. However, this is not recommended for the following reasons:

1. Users of such class rely heavily on its implementation details, and when the class needs to be extended or changed, it can become a maintenance nightmare to them, since they have to track each and every piece of code related to that class. Infra-structure components, such as Date or String classes, can be used dozens of times within a single source file. You can imagine what it’s like when dozens of programmers, each producing dozens of source files have to chase every source line using these classes in their programs. This problem is exactly what caused the notorious Year 2000 Bug! If, on the other hand, the data members are declared as protected or private, users cannot access the object’s data directly and have to use accessors and mutators (also known as “Getters” and “Setters,” respectively) for that goal. When the implementation details of the class are changed, only accessors and mutators need to be modified, and not any other pieces of code.

2. Users can inadvertently tamper with the object’s internal data members. For example, they can delete memory which is supposed to be deleted by the destructor; change the value of a file handle, let the destructor close the wrong file, and so on. The results of such tampering may cause an undefined behavior, and hence unreliable software components and bugs that are very hard to detect.

So it’s always a better design choice to hide implementation details by prohibiting public access to an object’s data and using accessors and mutators instead.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes