2

In the article Active Record vs Objects Bob Martin points out that:

  • Object hide data and expose behavior
  • Data structures expose data and has no behavior

He then continues to say

In languages like C++ and C# the struct keyword is used to describe a data structure with public fields.

To me it looks like he is using data structure as a synonym for struct and is saying we should use the struct keyword for data and the class keyword for implementing objects with behaviour. That sounds strange because mutable structs are evil and data in most systems need to change (it would mean most data are evil).

Could someone point out the flaw in my reasoning?

Community
  • 1
  • 1
Petew
  • 150
  • 7
  • 1
    I think there is no 'flaw in your reasoning'; object-oriented design decisions and conventions are not 'facts' which guarantee a good design. The subject also seems a bit opinion-based; all design ideas trade one thing for the other thing. – Codor Apr 17 '14 at 11:07

3 Answers3

1

I don't think that this is what Bob Martin's trying to say. I understand that he's saying:

  1. Use an class if you need behaviour along with data, while the actual data may (or may not) be of interest.
  2. Use a struct if you do not want to perform any operations on data, but still want to group individual values of data (which are all public)

Examples for 1) The classic OO example of a vehicle - it has features (data) and operations (like drive, stop, shift gear, etc.)

Examples for 2) One example would be an address. It is immutable and there are no immediate operations to be performed. Or take a point in 2D/3D space - it has three coordinates and that's it.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • One can absolutely perform operations on an address, or a point. There are also plenty of structs that have lots of operations. – Servy Apr 17 '14 at 16:22
  • I'm not saying it is not possible. I'm just saying that this is what I understand from the quoted text by Bob Martin. Personally I rarely create structs at all, but use classes in most cases. – Thorsten Dittmar Apr 18 '14 at 10:03
  • Whether or not you would plan to have operations on the type is of no consideration when determining whether to use a struct or a class in C#. If you don't use structs, and aren't familiar with either the differences or what factors should be taken into consideration, then you shouldn't be answering a question on the topic. – Servy Apr 18 '14 at 13:42
  • I'm not quite sure who entitled you to be that arrogant, but if you know so well, feel free to answer the question yourself instead of commenting like that. I have no idea why you think I don't know the difference between a class and a struct and I also have no idea why you think that *I* differentiate between "things that have operations and things that don't", but I like to repeat what I said before: The OP quotes text from another author. All I'm saying is that I think he misunderstood what the author is saying. – Thorsten Dittmar Apr 19 '14 at 10:05
1

Bob makes a good point about active records but his decision to mix in the struct keyword (as opposed to the concept) is a bit confusing..

First of all, it is important to understand that struct means absolutely different things in C# and C++.

Here is a rough explanation of the difference:

In C# the keyword struct is used to define a custom value type. The only thing that this says about this type is that it is always passed by-value to methods (as opposed to reference types, that are passed by reference. In other words, when you pass a value type to a method- a copy of it is actually passed and any changes to the copy inside the method are not visible outside of it.

In C++, a class and a struct are pretty much the same. A struct is essentially a class with all members being public by default.

So when you talk about evil properties of mutable structs, keep in mind that this spans C# only and has nothing to do with what Bob is talking about. The main issue being breach of semantics and very strange bugs when using them, but that is a discussion for another question.

Vitaliy
  • 8,044
  • 7
  • 38
  • 66
  • *all* parameters in C# are passed by value by default, and all parameters can be passed by referenced if used with the `ref` keyword. This has nothing to do with whether they are value types or reference types. What the type of the Type is a value or reference type affects whether the value passed is itself a value or a reference to a value elsewhere. – Servy Apr 17 '14 at 16:24
0

I don't think he mentioned whether the data struct is mutable or not. You can always make your struct immutable by only allowing to instantiate the members via the constructor.

public struct Foo
{
    private int _bar;

    public Foo(int bar)
    {
       _bar = bar;
    }

    public int Bar { get { return _bar; } }
}

I think using structs as a way of grouping data has its uses particularly for small short lived objects which require value type semantics.

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61