views:

181

answers:

9

Obviously, "Hello World" doesn't require a separated, modular front-end and back-end. But any sort of Enterprise-grade project does.

Assuming some sort of spectrum between these points, at which stage should an application be (conceptually, or at a design level) multi-layered? When a database, or some external resource is introduced? When you find that the you're anticipating spaghetti code in your methods/functions?

+7  A: 

when a database, or some external resource is introduced.

but also:

always (except for the most trivial of apps) separate AT LEAST presentation tier and application tier

see:

http://en.wikipedia.org/wiki/Multitier_architecture

b0x0rz
+2  A: 

Here are some criteria of when to...

  1. Any time you anticipate the need to replace one part of it with a different part.
  2. Any time you find yourself need to divide work amongst parallel team.
Jimmy Chandra
also: when you feel you are creating a generic component that can be used in other projects.
DanDan
+1  A: 

I'd say in most cases dealing with multiple distinct levels of abstraction in the concepts your code deals with would be a strong signal to mirror this with levels of abstraction in your implementation.

This does not override the scenarios that others have highlighted already though.

jerryjvl
+2  A: 

There is no real answer to this question. It depends largely on your application's needs, and numerous other factors. I'd suggest reading some books on design patterns and enterprise application architecture. These two are invaluable:

Design Patterns: Elements of Reusable Object-Oriented Software

Patterns of Enterprise Application Architecture

Some other books that I highly recommend are:

The Pragmatic Programmer: From Journeyman to Master

Refactoring: Improving the Design of Existing Code

No matter your skill level, reading these will really open your eyes to a world of possibilities.

hobodave
A: 

My generic rule of thumb is to at least to separate the problem into a model and view layer, and throw in a controller if there is a possibility of more than one ways of handling the model or piping data to the view.

(Or as the first answer, at least the presentation tier and the application tier).

Extrakun
+3  A: 

Layers are a mean to keep a design loosely coupled and highly cohesive.

When you start to have a few classes (either implemented or just sketched with UML), they can be grouped logically, into layers - or more generally packages, or modules. This is called the art of separating the concerns.

The sooner the better: if you do not start layering early enough, then you risk to have never do it as the effort can be too important.

philippe
+1  A: 

I think once you ask yourself "hmm should I layer this" the answer is yes.

I've worked on too many projects that probably started off as proof of concept/prototype that ended up being full projects used in production, which are horribly written and just wreak of "get it done quick, we'll fix it later." Trust me, you wont fix it later.

The Pragmatic Programmer lists this as the Broken Window Theory.

Try and always do it right from the start. Separate your concerns. Build it with modularity in mind.

And of course try and think of the poor maintenance programmer who might take over when you're done!

Jack Marchetti
A: 

Thinking of it in terms of layers is a little limiting. It's what you see in whitepapers about a product, but it's not how products really work. They have "boxes" that depend on each other in various ways, and you can make it look like they fit into layers but you can do this in several different configurations, depending on what information you're leaving out of the diagram.

And in a really well-designed application, the boxes get very small. They are down to the level of individual interfaces and classes.

This is important because whenever you change a line of code, you need to have some understanding of the impact your change will have, which means you have to understand exactly what the code currently does, what its responsibilities are, which means it has to be a small chunk that has a single responsibility, implementing an interface that doesn't cause clients to be dependent on things they don't need (the S and the I of SOLID).

You may find that your application can look like it has two or three simple layers, if you narrow your eyes, but it may not. That isn't really a problem. Of course, a disastrously badly designed application can look like it has layers tiers if you squint as hard as you can. So those "high level" diagrams of an "architecture" can hide a multitude of sins.

Daniel Earwicker
A: 

Loose coupling is all about minimising dependencies, so I would say 'layer' when a dependency is introduced. i.e. a database, third party application, etc.

Although 'layer' is probably the wrong term these days. Most of the time I use Dependency Injection (DI) through an Inversion of Control container such as Castle Windsor. This means that I can code on one part of my system without worrying about the rest. It has the side effect of ensuring loose coupling.

I would recommend DI as a general programming principle all of the time so that you have the choice on how to 'layer' your application later.

Give it a look.

R

Rob Ellis