views:

1449

answers:

13

When coding, what in your experience is a better approach?

  1. Break the problem down into small enough pieces and then implement each piece.
  2. Break the problem down, but then implement using a top-down approach.
  3. Any other?
+2  A: 

Yes. Do all of those things.

It may seem sarcastic (sorry, I revert to form), but this really is a case where there is no right answer.

KevDog
+1  A: 

Your 2nd option is a reasonable way to go. If you break the problem down into understandable chunks, the top down approach will reveal any major design flaws before you implement all the little details. You can write stubs for lower level functionality to keep everything hanging together.

Mike Thompson
Personally, I find this the easiest way to work, with least rework. Others have mentioned TDD, but I find it focuses me too much on bottom-up and I end up almost guessing what the higher-level client classes are going to need... and getting it wrong. That's just me, tho - do whatever works for you.
Mal Ross
+3  A: 

You might want to look over the Agile Manifesto. Top down and bottom up are predicated on Built It All At Once design and construction.

The "Working software over comprehensive documentation" means the first thing you build is the smallest useful thing you can get running. Top? Bottom? Neither.


When I was younger, I worked on projects that were -- by contract -- strictly top down. This doesn't work. Indeed, it can't work. You get mountains of redundant design and code as a result. It was not a sound approach when applied mindlessly.

What I've noticed is that the Agile approach -- small pieces that work -- tends to break the problem down to parts that can be grasped all at once. The top-down/bottom-up no longer matters as much. Indeed, it may not matter at all.

Which leads do: "How do you decompose for Agile development?" The trick is to avoid creating A Big Thing that you must then decompose. If you analyze a problem, you find actors trying to accomplish use cases and failing because they don't have all the information, or they don't have it in time, or they can't execute their decisions, or something like that.

Often, these aren't Big Things that need decomposition. When they are, you need to work through the problem in the Goals Backward direction. From Goals to things that enable you to make that goal to things that enable the enablers, etc. Since goals are often Big Things, this tends to be Top Down -- from general business goal to detailed business process and step.

At some point, we overview these various steps that lead to the goals. We've done the analysis part (breaking things down). Now comes the synthesis part: we reassemble what we have into things we can actually build. Synthesis is Bottom Up. However, let's not get carried away. We have several points of view, each of which is different.

We have a model. This is often built from details into a larger conceptual model. Then, sometimes decomposed again into a model normalized for OLTP. Or decomposed into a star schema normalized for OLAP. Then we work back up to create a ORM mapping from the normalized model. Up - Down - Up.

We have processing. This is often built from summaries of the business processes down into details of processing steps. Then software is designed around the steps. Then the software is broken into classes and methods. Down - Up - Down.

[Digression. With enlightened users, this decomposition defines new job titles and ways of working. With unenlightened users, the old jobs stay and we write mountains of documentation to map old jobs onto new software.]

We have components. We often look at the pieces, look at what we know about available components, and do a kind of matching. This is the randomest process; it's akin to the way crystals form -- there are centers of nucleation and the design kind of solidifies around those centers. Web Services. Database. Transaction Management. Performance. Volume. Different features that somehow help us pick components that implement some or all of our solution. Often feels bottom-up (from feature to product), but sometimes top-down ("I'm holding a hammer, call everything a nail" == use the RDBMS for everything.)

Eventually we have to code. This is bottom up. Kind of. You have to define a package structure. You have to define classes as a whole. That part was top down. You have to write methods within the classes. I often do this bottom-up -- rough out the method, write a unit test, finish the method. Rough out the next method, write a unit test, finish the method.

The driving principle is Agile -- build something that works. The details are all over the map -- up, down, front, back, data, process, actor, subject area, business value.

S.Lott
I interpreted the question as "the problem at hand" rather than "the entire piece of software". Even when you are building the smallest useful thing, you have to approach the problem from some perspective.
KevDog
Sorry, can't see that in the question. Also -- and more important -- the top-down/bottom-up is often because of Big Design Up Front project management. Agile folks rarely seem to ask this question.
S.Lott
+11  A: 

Here's what I do:

Understand the domain first. Understand the problem to be solved. Make sure you and the customer (even if that customer is you!) are on the same page as to what problem is to be solved.

Then a high level solution is proposed to the problem and from that, the design will turn into bubbles or bullets on a page or whatever, but the point is that it will shake out into components that can be designed.

At that point, I write tests for the yet-to-be written classes and then flesh out the classes to pass those tests.

I use a test-first approach and build working, tested components. That is what works for me. When the component interfaces are known and the 'rules' are known for how they talk to each other and provide services to each other, then it becomes generally a straightforward 'hook everything together' exercise.

That's how I do it, and it has worked well for me.

itsmatt
+1  A: 

I think there's more to consider than top- verses bottom-down design. You obviously need to break the design up into manageable units of work but you also need to consider prioritisation etc. And in an iterative development project, you will often redefine the problem for the next iteration once you've delivered the solution for the previous one.

Ty
A: 

Outside-in design.

You start with what you're trying to achieve at the top end, and you know what you've got to work with at the bottom end. Keep working both ends until they meet in the middle.

Andrew Edgecombe
+1  A: 

When designing, I like to do middle-out. I like to model the domain, then design out the classes, move to the database and UI from there. If there are specific features that are UI-based or database-based, I may design those up front as well.

When coding, I generally like to do bottom-up (database first, then business entities, then UI) if at all possible. I find it is a lot easier to keep things straight with this method.

Guy Starbuck
+2  A: 

Also in the agile way, write your test(s) first!

Then all software is a continual cycle of

  • Red - the code fails the test
  • Green - the code passes the test
  • Refactor - code improvements that are intention-preserving.

defects, new features, changes. It all follows the same pattern.

Robert Paulson
I don't think this really answers the question, that being top-down vs bottom-up design.
Mal Ross
I guess I could clarify this would fall under (3) Other; break things down, write tests, then write code. The act of writing the tests is, in its own way, an outside-in approach.
Robert Paulson
A: 

I sort of agree with all of the people saying "neither", but everyone falls somewhere on the spectrum.

I'm more of a top-down kind of guy. I pick one high level feature/point/whatever and implement it as a complete program. This lets me sketch out a basic plan and structure within the confines of the problem domain.

Then I start with another feature and refactor out everything from the original that can be used by the second into new, shared entities. Lather, rinse, repeat until application is complete.

However, I know a lot of people who are bottom up guys, who hear a problem and start thinking about all the support subsystems that they could need to build the application on top of it.

I don't believe either approach is wrong or right. They both can achieve results. I even try and find bottom up guys to work with, as we can attack the problem from two different perspectives.

toast
+9  A: 

I tend to design top-down and implement bottom-up.

For implementation, building the smallest functional pieces and assembling them into the higher-level structures seems to be what works best for me. But, for design, I need to start from the overall picture and break it down to determine what those pieces will be.

Dave Sherohman
Well said. I use this approach as well.
Dillie-O
A: 

Both are valid approaches. Sometimes one just "feels" more natural than the other. However, there is one big problem: some mainstream languages and especially their frameworks and libraries really heavily on IDE support, such as syntax highlighting, background type checking, background compilation, intelligent code completion, IntelliSense and so on.

However, this doesn't work with top-down coding! In top-down coding, you constantly use variables, fields, constants, functions, procedures, methods, classes, modules, traits, mixins, aspects, packages and types that you haven't implemented yet! So, the IDE will constantly yell at you because of compile errors, there will be red squiggly lines everywhere, you will get no code completion and so on. So, the IDE pretty much prohibits you from doing top-down coding.

Jörg W Mittag
Sort of true, but if your programming against interfaces and stubbing/mocking your dependencies for testing purposes or until there are built then this isn't an issue.
Owen
A: 

I do a variant of top-down. I tend to try and do the interface first - I then use that as my list of features. What's good about this version is, it still works with IDE that would otherwise complain. Just comment out the one function call to what's not yet been implemented.

+1  A: 

I believe that with good software designers (and in my opinion all software developers should also be software designers at some level), the magic is in being able to do top-down and bottom-up simultaneously.

What I was "schooled" to do by my mentors is start by very brief top-down to understand the entities involved, then move to bottom-up to figure out the basic elements I want to create, then to back up and see how I can go one level down, knowing what I know about the results of my bottom up, and so forth until "they meet in the middle".

Hope that helps.

Roee Adler
We have had much success using this general method. This works especially well in teams with designers and dba's. The dba will naturally start at the bottom, the designers will naturally start at the top, and coders can swing back and forth as needed.
Nick