2

Most of my projects start small. Many times I start with a web page (1 file) that has the code to...

select stuff from the database => display it to the user and offer editing => receive the edits (sometimes complex enough that it requires parsing) => update the database

all of this in 1 file, of course. As the project grows it always becomes annoying to have so much code in one pile, 50 if statements.

Question: What is a good, proven & intuitive data separation? I guess i'm looking for a diagram that explains which code goes in what file.

I would like to keep this language independent, but in case it matters I'm using PHP and MySQL.

3 Answers 3

3

What you're talking about is the n-Tier archiecture. The following article uses .NET as an example, but the basics are the same for any language/platform.

http://www.developerfusion.com/article/3058/boosting-your-net-application-performance/2/

2

Have you tried working with an MVC framework. I used to be in similar situations before, where having things in one file made it difficult to manage as the project got bigger.

If you suspect the project growing in phase, start with a Model-View-Controller framework method. You could create your own or start with frameworks which already exists. For PHP - CakePHP and CodeIgniter are two popular frameworks.

Following MVC method, should make your code easier to manage and scale as the project gets bigger.

Good luck

2

There is nothing wrong with starting out with a single file containing all code if the project is very small. Surfer513 has pointed to a good article explaining n-Tier architecture. How do you get from where you are to there?

The answer to that is refactoring. Once you get to the point where a single file becomes clumsy or unmanageable, you need to start separating it into pieces. There is a very good book on refactoring from Martin Fowler. That book is mostly concerned with object oriented programming, but the principles apply all the same.

The first step is often to separate the display from the rest. In the language of MVC, the display code is the view (in a web application in PHP, these could be HTML templates and CSS). For PHP applications you can use a templating engine such as smarty.

The next step is often to separate the actual business logic from the data access layer. This is generally also a good time to start thinking about using OOP (if your code isn't already using it), since this allows for easy grouping of functionality that belongs together and separating it from other functionality, plus code reuse.

After that you will probably have a good idea what the next steps should be. This is not a "one size fits all" situation, every project has to develop its own structure and architecture over time, depending on its particular needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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