In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
and
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, a passive data structure (PDS), also termed a plain old data structure or plain old data (POD), is a
record, in contrast with objects. It is a
data structure
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
that is represented only as passive collections of
field values (
instance variable
In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e., a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similari ...
s), without using object-oriented features.
Rationale
Passive data structures are appropriate when there is a part of a system where it should be clearly indicated that the detailed logic for data manipulation and integrity are elsewhere. PDSs are often found at the boundaries of a system, where information is being moved to and from other systems or persistent storage and the problem domain logic that is found in other parts of the system is irrelevant. For example, PDS would be convenient for representing the field values of objects that are being constructed from external data, in a part of the system where the semantic checks and interpretations needed for valid objects are not applied yet.
In C++
A PDS type in
C++, or
Plain Old C++ Object, is defined as either a scalar type or a PDS class. A PDS class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PDS. Moreover, a PDS class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no virtual base classes and no virtual functions. The standard includes statements about how PDS must behave in C++. The
type_traits
library in the
C++ Standard Library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the origina ...
provides a template named
is_pod
that can be used to determine whether a given type is a POD. In C++20 the notion of “plain old data” (POD) and by that
is_pod
is deprecated and replaced with the concept of “trivial” and “standard-layout” types.
In some contexts, C++ allows only PDS types to be used. For example, a
union
in C++98 cannot contain a class that has
virtual function
In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part ...
s or nontrivial constructors or destructors. This restriction is imposed because the compiler cannot determine which constructor or destructor should be called for a union. PDS types can also be used for interfacing with
C, which supports only PDS.
In Java
In
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, some developers consider that the PDS concept corresponds to a class with public data members and no methods (Java Code Conventions 10.1),
i.e., a
data transfer object. Others would also include
Plain old Java object
In software engineering, a plain old Java object (POJO) is an ordinary Java object, not bound by any special restriction. The term was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000:
The term "POJO" initially den ...
s (POJOs), a class that has methods but only getters and setters, with no logic, and
JavaBeans to fall under the PDS concept if they do not use event handling and do not implement added methods beyond getters and setters. However, POJOs and Java Beans have
encapsulation, and so violate the fundamental definition of PDS.
Records (introduced in Java 16, in 2021) are shallowly immutable carriers of data without encapsulation, and therefore they can also be considered PDS.
In other languages
In
PHP, associative arrays and
stdClass
objects can be considered PDS.
Other structured data representations such as
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
or
JSON
JSON (JavaScript Object Notation, pronounced or ) is an open standard file format and electronic data interchange, data interchange format that uses Human-readable medium and data, human-readable text to store and transmit data objects consi ...
can also be used as a PDS if no significant semantic restrictions are used.
In
Python, dataclass module provides dataclasses - often used as behaviourless containers for holding data, with options for data validation. The dataclasses in Python, introduced in version 3.7, that provide a convenient way to create a class and store data values. The data classes use to save our repetitive code and provide better readability.
In
C, structs are used in the same manner.
See also
*
Plain old CLR object
Notes
References
{{Reflist
C++
Composite data types