5

The reason I am asking this question is I want to know how to properly call an architecture where classes have either data or logic but not both. I know this goes against object orientation and encapsulation. I want to use the proper terms for discussing this architecture.


EDIT: By reading the answers I have received, I can see my question wasn't clear. I also feel the edit that was made didn't push the question in the direction I was going for.

I will restate the question in other terms.

Suppose you are working in C#, Java or some other OO language.

You have 2 designs:

DESIGN A:

class Car
{
     // data
     int numberOfWheels; 
     Engine carEngine;

     // methods
     public void StartCar();
}

DESIGN B:

class Car
{
     // this class contains only data (be it entity, DTO, whatever)
     int numberOfWheels;
     Engine carEngine;
}

class CarStarter
{
     public void StartCar(Car aCar);
}

In DESIGN B, some classes are used as data structures/DTO/entities (they contain only data and should not contain methods). Other classes contains logic and work with the data classes.

Now suppose you have a design/architecture where DESIGN B, should be applied throughout rather than design A.

I was looking either for a term that described DESIGN B, or the OOP term/notion that consist of DESIGN A. Preferably both.

For the OOP term I guess encapsulation could be it. From Wikipedia Encapsulation

A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

I still don't know how to call DESIGN B though.

1
  • 1
    Is this by any chance related to the Von Neumann architecture, i.e. one in which data and instructions are stored together, and are in fact indistinguishable? AKA the stored program
    – WaelJ
    Sep 16, 2011 at 16:04

8 Answers 8

4

Do you see how many different answers you're getting here?

You know what the conclusion is?

There are no standard, well-known and actually-common names for this kind of thing.

If you want people to understand you, simply call them as they are: data structures and classes (or business rules). If you want to make a clear distinction somewhere, explain it - verbosely.

Sticking to extra-formal terms always seems so unpragmatic to me.

0
29

What is the term, in relation to OO programming, to describe objects that represent both logic and data.

Object

The question could be turned upside down, what would be the term to describe objects that contain only logic

Procedure if it has side-effects, Function if it is referentially transparent.

or only data.

Data

The reason I am asking this question is I want to discuss an architecture where objects only either have data or only have logic and I want to use the proper terms to do so.

This is called functional programming if the logic is referentially transparent and the data is immutable, and procedural programming otherwise.

15
  • 1
    A common (IMHO more common even though I have some FP background) definition of procedure/function is that a procedure doesn't return a value while a function returns a value, no matter if it's pure/referentially transparent/etc. or not.
    – user7043
    Sep 16, 2011 at 16:11
  • 6
    @StuperUser: an object that has no state and only a single method is isomorphic to a procedure/function. (In fact, this is actually how first-class procedures/functions are implemented in Scala, Python, Ruby and Smalltalk, for example.) A stateless object with multiple methods is isomorphic to a set of procedures/functions in a namespace. An object with no methods, only data is isomorphic to a struct/record (plus inheritance). An object with data and a single method is isomorphic to a closure. Sep 16, 2011 at 17:11
  • 3
    @StuperUser: And an object with data and multiple methods (i.e. a bog-standard object) is isomorphic to record containing a set of closures that all close over the same lexical environment. (In fact, that's how objects are typically implemented in Haskell, ML, Scheme (and thus also ECMAScript) and other procedural/functional languages.) Sep 16, 2011 at 17:13
  • 2
    @KeithS: where did I claim that an object containing only logic were a method? Sep 16, 2011 at 17:14
  • 3
    @KeithS to be pedantic whether a procedure or function is an object depends on the language in question.
    – Davy8
    Sep 16, 2011 at 21:38
5

Objects have both state and behavior.

Objects that have only state and no behavior are data or data transfer objects. I've also heard them referred to, incorrectly, as entities.

Objects that have only behavior but no state are services.

3

Data-only objects are commonly known as Data Transfer Objects.

Data-free objects are probably best called Stateless Objects, as there is no state within them to be maintained.

2

Classes contain both logic in form of Members(Functions) and data in form of Fields(variables). The object representing the class at run-time is called Instance

2

Objects that contain and expose both logic (methods, properties) and data (fields) are simply "objects". Often, the overall behavior of this object is a model for some real-world concept or thing (this is after all the point of O-O design; to structure programs in terms of code objects that model real-world ideas), which means that an object must encapsulate both data about the state of a particular instance of that object, and also logic representing what an object can do; these stateful yet "rich" objects, as a collection in your codebase, are usually called the "domain" or "model".

An object containing only logic is rare; most objects need to store some sort of state. If you need one term for all of these, There are three major subcategories I can think of that are more commonly heard of:

  • Static classes that organize "pure" (stateless and/or side-effect free) utility methods; these are generally "helpers" or "utility classes".
  • Instance classes that exist as "bridges" for communications between classes or architectural layers; these are often called "controllers" because their usual function is to direct more stateful objects to perform overarching tasks that the stateful objects should not know how to do by themselves.
  • Instance classes that provide a means to get state information out of an external resource. The instance class itself does not "own" the state data that is exposed; it is simply a means to an end. These are generally called "repositories"; some of them may be called "proxies" depending on the basic design pattern in which the object is used.

Classes that contain only data are generally referred to as DTOs, or Data Transfer Objects. You may also hear them called "structs", but that term is usually reserved in the C-style languages for "complex value types" like DateTime, Money, Point, Vector, etc. Sometimes, data-only classes are used as the "domain", with other classes built to perform all manipulations of that data, but this is known as an "anemic domain model" and is generally considered an anti-pattern.

1

Neither design does anything with the Car other than reference it. The Car is a black-box representation. I assume that is just an omission.

You should call the second design Procedural Programming, since that is what it is without a doubt.

0

Homoiconicity.

In computer programming, homoiconicity is a property of some programming languages, in which the primary representation of programs is also a data structure in a primitive type of the language itself, from the Greek words homo meaning the same and icon meaning representation. This makes metaprogramming easier than in a language without this property. To put that another way, homoiconicity is where a program's source code is written as a basic data structure that the programming language knows how to access.

The most obvious example being Lisp. "Code is Data."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.