Friday, August 21, 2009

Fat is Better

I have recently had discussions with some colleagues about what architecture they prefer and, while they seem to favor thinly sliced services, I have come to the conclusion that the overhead that comes with slicing services thin is not worth the extra time that it takes to setup, verify and test the complex, internal communication that comes with this kind of architecture. Fat is better!

If I am designing a system that should work in a coherent way, I want it all in my big, fat, juice object model. This enables me to put the functionality where it is most cohesive and, therefore, gives me the best design possible. Every object should carry its own weight.

If there are external services they must, by necessity, be outside the model, but the internal representation of the external service should be inside my model.

An example of an architecture that relies on thinly sliced services is REST. REST is very elegant and it definitely has a place when publishing resources. But REST models are anemic. They rely on you to GET the information from the resource, do things to it and then replace the information of the resource with a PUT. It is CRUD for the web. It is not intended to take advantage of what is good in object-oriented and functional programming, like sending behaviors into an object and have it perform the calculations for you.

The elegance of map and reduce (fold) is the essence of functional programming. How do you model map and reduce with REST? You can't! Polymorphism and encapsulation is the essence of object-oriented programming. Where does it go when everything is a resource? It disappears!

I have worked on projects where the goal has been to design every little part of the system as a free standing module with its own life and versioning, that can be switched in and out, but the artifacts have mostly been deployed together and have rarely given any extra value standing on their own. But they have given us a lot of grief when we tried to build a DRY system.

So, a fat model is the way to go. How fat? As fat as possible, but no fatter. How fat is that? As always this is a judgment call but, err on the side of fatter.

If, by luck or skill, my fat system reaches a workload where it will have to be split over multiple processors or machines, it will not be very difficult to split the system, since the system will be well factored, cohesive and DRY!

Note: "Fat is better" is somewhat related to worse is better by Dick Gabriel

Wednesday, August 12, 2009

What Eric Evans Would Have Changed in the DDD Book.

Here are my notes of Eric Evans talk What I've learned about DDD since the book

Essentials

Emphasize that the collaboration with the domain experts is essential. It is up to you to show the domain experts how valuable their collaboration is. If they cannot see the value of participating in the project, they will not to a good job, and they will try to avoid it.

Always produce at least three bad models. These models help to emphasize what is important.

The chapters "Distillation of the Core Domain" and "Context Mapping and Boundaries" should have been moved to the start of the big since these are the most essential areas.

Changes to Building Blocks

Domain Events

There is one new Building Block and it is the Event Object. The Event Object is an object the represents an Event that is significant to a domain expert. A benefit of domain events is that they give clearer, more expressive, models.

The events can be used for: - Representing the state (history) of entities. - Decouple systems with event streams (publish-subscribe) - Enable high-performance system.

Aggregates

Evans want to emphasize som things of aggregates. The boundaries of an aggregate need to contain transactions, distribution and concurrency.

When modelling the aggregates it is important to not over-specify on what part of the aggregate the properties and invariants are placed. Even though they are commonly placed on the aggregate root, this is not essential and it gives you a freedom if you not specify it too hard and too early.

Strategic Design

Large-Scale Structure does not come up very often and would probably have been left out of the book.

It is important to not spread modelling to thin, and to focus on the core domain and to create a clean bounded context.

Collaboration Patterns

There is two new collaboration pattern called Partners. This pattern differs from the other collaboration patterns in that it is cooperative and mutually dependent.

Another pattern is the Big Ball of Mud"_. This pattern is known as an anti-pattern since it implies that the code is just fixed as things go. This pattern is utterly pragmatic but the point Evans is making is that if there is a Big Ball of Mud in the system, it is important to define the boundaries of it, so that it doesn't spread to other systems that are more rigorously architected.

Context Mapping

  • What models do we know of?
  • Where does each apply? Define boundaries in words?
  • Where is the information exchanged?
  • The service interface may define a context boundary.

DDD and SOA

  • The service interface must be defined in some context.
  • Internals also, but often not the same one.
  • The service interface may define a context boundary.

Precisions Designs are Fragile

Sophisticated design techniques are wasted in a ball of mud. It must be isolated with an Anti-Corruption Layer.

Not all of a large system will be well designed. Figure out what part of the system is most critical and benefits most from a really nice design.

Monday, August 10, 2009

Notes on The User Illusion, part Two

This is part two of a summary of the fantastic book. The User Illusion

Our conscious mind consists of symbols that map into the rest of our mind. Every second more than 11 million bits are reduced to less than 50 bits of meaningful information at apparently no time at all.

Half a second before we try to do something consciously our brains has started its activity. Half a second!

Experiment
  • Sit down and hold one finger up.
  • Whenever you feel like it, bend your finger.

Half a second before your finger is bent, your brain has started its activity! It does not feel like half a second does it? There is no way! 0.1 second is more likely. But the data has been reproduced and it is undeniable.

The consciousness of wanting to do something appears almost half a second after the brain has initiated its activity.

Our actions are initiated unconsciously.

Benjamin Libet

Benjamin Libet performed some experiments, in the sixties, while a friend of his was performing brain surgery. Libet stimulated the brain with electrical impulses and found that if the brain was stimulated less the half a second, it was not noticed at all. If the brain was stimulated more than halv a second the patient felt it. It appeared to the patient as if a certain area of his body had been touched, since there are no normal sensors inside the brain.

Libet then performed some other experiments. He stimulated a part of the brain that related to the left hand and at the same time he stimulated the right hand and the patient was to say which hand was stimulated first.

The hand had to be stimulated half a second after the brain to give the appearance to the patient that they where stimulated simultaneously.

The conscious experience is projected back in time so that the conscious mind believes that they are almost simultaneous. The brain is fooling itself.

The conscious is delayed, but it does its best to hide it. For itself. It is very convenient since it gives the mind the time to perform the reduction of the sensory data to what is needed to get a full experience.

Everyone knows that it doesn't take half a second to remove a hand from a hot stove, but it takes half a second to get the conscious back-dated experience.

So does this mean that we don't have free will? Libet does not think so. He says that there is time for the conscious mind to veto the unconscious decision.

The conscious is not a top-level unit that gives orders to its underlying processes. It is a selecting mechanism that chooses between the different options that the unconscious provides.

As long as the unconscious action-proposals work in sync with the conscious thoughts and feelings, they are hardly noticed. They are merged together with the conscious motives that take all the credit! --Harald Høffding

The conscious veto is often associated with an uncomfortable feeling, we usually feel best when we are not making conscious decisions. Compare this with for example Flow

I and Me

Nørretranders defines I as the conscious mind and Me as the rest of us.

I have free will, but it is not my I that has it, it is my Me.

The conscious mind is allowed to use its veto in situations where the Me allows it to. In situations where speed is critical, for example when something is threatening us, the conscious mind is left out of the loop.

A comfortable state of mind is when the Me is allowed to do what it does automatically without interference of the I. This state is neither associated with nervousness nor shyness, but with comfort and carelessness.

The User Illusion

The user illusion is a term coined by Alan Kay of Smalltalk fame. He used it to describe the metaphor of the desktop that was invented by him and his colleagues at Xerox PARC. His great idea was that it does not matter what really happens inside the computer as long as the interface presented to the user is useful and consistant.

Nørretranders argues that our consciousness is our user illusion. Our consciousness is our map into ourselves and our possibilities to affect this world. I am the user illusion of Me.

The conscious mind may appear when the brains' simulation of the world has become complete enough to require a model of itself. --Richard Dawkins

Experiments with split-brain patient has shown that the mind doesn't think twice about making up a story that is consistent with something that does not have anything to do with reality. An example of this is:

  • A split brain patient is shown two images
    • The right eye was shown a snowy landscape
    • The left eye was shown a chicken's foot.
  • The patient pointed to two images.
    • The right hand a snow-shovel.
    • The left hand a chicken.
  • When the patient was asked why he pointed at the two images:
    • He said that he picked the chicken because it matched the chicken's foot
    • And that he picked the shovel because you need the shovel to clean out the chicken house.

The mind lied without hesitation to make the selection of images consistent.

Our consciousness is delayed to allow it to perform the required calculations. First we sense, then we simulate, then we experience. But we have no notion of the simulation. Consciousness is depth appearing as shallow.

Learning and Knowing

There are lots of things that we know how to do that we cannot explain. How do you ride a bike? How do you walk? How does your mother look? Its impossible to explain, but simple to know, once it has been learned.

The I's role in learning is to have the disciplin to learn by repetition, and the foresight that when I have learned this I will enjoy practicing it. The consciousness, usually, gets in the way while learning and, certainly, gets in the way when we practice what we have learned.

Science, usually, clarifies what we already know. It explains what we already know but cannot explain to each other.

Science is just refinement of everyday knowledge --Albert Einstein

The relationship between the conscious learning and the non-conscious ability is similar in both practical areas, such as ballet, and theoretical areas such as science; in both cases we must put in a lot of hard work for something we don't completely understand, but that we still can share with others.

The Rise of Consciousness and Religion

Nørretranders brings up another researcher, Julian Jaynes, who argues that the consciousness of man didn't exist 3000 years ago. He has various theories to prove this. Among other things he brings up the Oracles of Ancient Greece and the voices religious people hear as examples of people who are acting without a consciousness. A pre-conscious man is just a me, and a conscious man thinks he is just an I.

He also makes an interesting point about religion. The I has to recognize that there is something greater than itself. But it is not a god it is the me. God is the part of man that the I cannot explain. Religion is too important to be left to the religious. It is the struggle between the carefree me and the worrying I.

The Limits of Consciousness

Most of the processes that go on inside our bodies we know nothing about. We cannot feel the blood flowing in our legs or our immune system attacking viruses, until the problems become large enough for the body to raise the temperature to increase the effect. We cannot hold our breaths long enough to kill ourselves. The unconscious part of us wont let us

The Beginning of the Universe

In the Universe there is as much negative matter as positive matter. The sum of it all is Zero. According to the laws of quantum mechanics it is possible for Nothing to split into something for a very short while. The less it is the longer it is allowed to exist. So, it the sum of everything in the Universe is Nothing can exist forever.

Emergence

Emergence is the appearance of group characteristics when the number of elements increase. An example of this is Temperature. There is no temperature when there are only a few molecules but when the number of molecules increases the Temperature property emerges. In the book Gödel, Escher, Bach, Douglas Hofstadter talks about the emerging properties of the brain, the thoughts that cannot be described at a lower level.

Gödels proof suggests the possibility that an view from a higher level may have an explanation capacity that is totally missing at a lower level. --Douglas Hofstadter

A larger system consisting of simple rules can show characteristics that cannot be deduced from the rules.

It is impossible to know how yourself or another human will react, because it requires you to have access to all the information yourself or this other person has had and this is impossible since human being mostly function unconsciously.

The Liars Paradox, "I'm lying" is not a liars paradox. It is the truth about our consciousness. --Thor Nørretranders