1

I have question based on the following fragments of "Clean Code". Those have been cited before, but I wanted to ask more basic question.

1.

Data/Object Anti-Symmetry

(...) the difference between objects and data structures. Objects hide their data behind abstractions and expose functions that operate on that data. Data structure expose their data and have no meaningful functions. Go back and read that again. Notice the complimentary nature of the two definitions. They are virtual opposites. This difference may seem trivial, but it has far-reaching implications.

2.

Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.

The complement is also true:

Procedural code makes it hard to add new data structures because all the functions must change. OO code makes it hard to add new functions because all the classes must change.

3.

(...) would be a lot less confusing if data structures simply had public variables and no functions, whereas objects had private variables and public functions. (...)

4.

Hybrids

This confusion sometimes leads to unfortunate hybrid structures that are half object and half data structure. They have functions that do significant things, and they also have either public variables or public accessors and mutators that, for all intents and purposes, make the private variables public, tempting other external functions to use those variables the way a procedural program would use a data structure. Such hybrids make it hard to add new functions but also make it hard to add new data structures. They are the worst of both worlds. Avoid creating them. They are indicative of a muddled design whose authors are unsure of—or worse, ignorant of—whether they need protection from functions or types.

And the question is: according to Clean Code, is it okay to add a single constructor to a plain data structure?

I want to follow the advices very precisely, but I'm finding it quite tedious to initialize each field in a new line. On the other hand, adding a constructor is adding a method, which are unnatural to data structures. Maybe being forced to explicitly name each of the initialized fields also brings some value.

  • 5
    It's hard to understand how you managed to arrive at the conclusion that single constructors might be a bad thing from the text that you've cited. As good a book as Clean Code is, you might not be ready to read it yet. Books like this are not meant to be a mandate; they're meant to get you to think about how you might improve the way you write your programs, and if you don't have enough experience writing actual code yet to know the difference, this advice is premature. – Robert Harvey Jun 28 '16 at 16:59
  • @RobertHarvey I'm not exactly sure whether you want to help me or to discourage. I have some experience as a programmer and still it was ambiguous to me as it is to many other professionals. The whole book is filled with the examples of errors that professionals do. Very often a small amount of wrongdoing is hard to see. Sometimes the best doesn't "know the difference". Hard to understand my "conclusion"? Well it is written "no functions" and such conclusion is totally valid. – Atom Jun 28 '16 at 17:14
  • 2
    I'm trying to help you. My issue is less with your attempts at understanding what Bob Martin says, and more with your idea that there is such a thing as The One True Way According to Bob Martin™. There isn't. What you need to understand first is what constructors are, how they work, and where you should use them. You don't need Uncle Bob's book for that, unless he has a chapter in there that discusses constructors and their proper use specifically. – Robert Harvey Jun 28 '16 at 17:21
8

according to Clean Code, is it okay to add a single constructor to a plain data structure?

Yes.

Constructors are not functions in this context since they're not something that operates on data. They don't hide the data. And if the data changes, they'll need to change, but that's no different from any other initialization strategy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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