views:

116

answers:

4

I have a large system that I have coded and I wish to make the code (dare I say) more simple and easy to read. Unfortunately before this, I haven't used functions much.

I have many different MySQL Queries that are run in my code, I feel that if I make the various displays into functions and store them in a separate file it would make the code much easier to maintain (actually, I know it would).

The only thing I am wondering is, if this is a common practice and if you think that it is going to hurt me in the long run in terms of performance and other factors. Here is an example of what I currently am using:

$result = mysql_query("SELECT * FROM table");

while($row = mysql_fetch_array($result)){

/* Display code will go here */

}

As you can imagine this can get lengthy. I am thinking of making a function that will take the result variable, and accomplish this, and then return the results, as so:

$result = mysql_query("SELECT * FROM table");

dsiplayinfo($result);

Do you think this is the right way to go?

[Edit]

The functions would be very different because each of them needs to display the data in different ways. There are different fields of the database that need to be shown in each scenario. Do you feel that this approach is still a good one even with that factor? AKA Modular design is not being fully accomplished, but easy maintenance is.

+1  A: 

Using more functions can be helpful, and it can be hurtful. In theory it moves you more towards modular design, meaning you can use a function over and over in multiple apps without having to re-write it.

I would honestly encourage you to more toward larger conventions, like the MVC Frameworks out there. Kohana is a great one. It uses things like Helpers for additional outside functionality, Models to query the database, and Controllers to perform all of your logic - and the end-result is passed on to the View to be formatted with HTML/CSS and spiced up with Javascript.

Jonathan Sampson
A: 

Yes, also consider looking into an ORM or some database agnostic interface. This might help reduce duplication as well (and certainly make porting to a new DB easier, should that ever come up).

Basically any time you see similar looking code (be it in structure or in functionality) you have an opportunity to factor it out into functions that can be shared across the application. A good rule of thumb is Don't Repeat Yourself (DRY)

Ben Hughes
+1  A: 
  • Don't use "SELECT *" -- enumerate the fields you want for performance reasons, as well as maintainence reasons.
  • In your example, the display code will likely be pretty closely coupled with the SQL query, so you may as well encapsulate them both together
  • You might consider some sort of MVC framework with an ORM (like CakePHP) which will facilitate Model reuse much better than writing a bunch of functions
  • You are on the right track! Write your code, then refactor it to make it better--very smart.
mgroves
+4  A: 

One thing you want to keep in mind is the principle of DRY: Don't Repeat Yourself.

If you are finding there is a block of code that you are using multiple times, or is very similar that can be made the same, then it is a ideal candidate for being moved into a function.

Callum