Forum Moderators: coopster

Message Too Old, No Replies

How to deal with overly complicated scripts

I need to modify my code writing style

         

henry0

12:57 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I reached a point where I may not remember without drawing it the logic/synopsis of one page of script!
Too many domino effects of queries getting the result of above queries, joint, switch, session and the whole palette, the scripts duties are in direct proportion to the task at hand.

Doing a bunch of includes will add to the load and only for the look benefit will shorten the page.
My scripts are already quite commented

What I need is to write them in a way I could reuse many queries as if the were .tpl
Stored procedure comes to mind
Anything else?

WesleyC

2:51 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



Stored procedures will work well for you, as will Object-Oriented Programming. If you have access to PHP5 (even PHP4 has limited OOP support, but 5 is far better), do a bit of research into PHP classes. They should help you encapsulate things far better than your current procedural code.

Alternately, you could look into a framework, such as CodeIgniter or CakePHP--while neither is suitable for every purpose, I've had a tremendous amount of success developing complete websites with CodeIgniter.

Converting existing, non-OOP code to either framework, however, will be a very painful process. I would recommend simply converting your code to OOP to start with, obeying the best practices available (I.E. name your classes and functions appropriately, don't try to cram multiple objects' functionality into one object, don't make functions too long, etc.).

henry0

3:05 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, I am already in OOP mode
The actual site needs to be updated probably to use stored procedures, but is way too big to move out of its original structure, as using a framework will require learning and I do not have this time luxury :)
so anything else?

WesleyC

5:13 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



There's a couple ways to use or mimic stored procedures, then:

1. If you have MySQL 5.x, or MSSQL, you have access to both stored procedures and views.

Views will allow you to make faster, easier, simpler queries based on joined tables, hardcoded data, combined fields, etc.--in other words, if you find yourself constantly repeating a query to retrieve data from the users table with an inner join to payments, just create a view that combines the two tables and query directly from the view--it works just like querying from a table, but is much faster than running the full complex query with join every time you need to look at that information. This speed increase is generally because MySQL/MSSQL will "pre-compile" a view and cache its results as it would a normal table--for all intents and purposes in your code, the view is exactly the same as a normal table, but you get the advantage of having it in readily-accessible form without the need to manipulate the data every time you query it. I've used views to great success before in MSSQL, and MySQL's implementation isn't far behind in quality.

Stored procedures are also a good practice, because they allow you to assign a name to a specific query. If the query changes due to an underlying data structure change, your code will still work provided the same fields are provided by the query. Again, they're pre-compiled and will thus execute faster than an in-code query.

2. If you're running MySQL 4.x, or another DB that doesn't support views/stored procedures, use PHP "templates" instead. Use an object with a variable of $query, containing a method to include() a named file (queries\my-query.sql) containing each query you'll use: "<?php $this->query = 'SELECT * FROM table WHERE username = @user' ?>" or similar. You can then access $object->query and execute it. This certainly isn't the only way, or even a way I would recommend--if you have MySQL 4.x, I would highly recommend simply upgrading to 5.x. However, if you absolutely must use something else, this or a similar include system, with named files that contain parameterized SQL queries, would at least let you mimic a stored procedure.

Hope this helps!

henry0

5:42 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good info!
I have MySQL5
and am decided to use stored procedure.
But since I never used them I found some good tutorial from the MySQL point of view, however close to nothing on how calling those procedures and use them in php5+

Morgenhund

11:28 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



Stored procedures hamper portability, IMHO. Also make sure they are backup-ed together with other mySQL data (you do backups, not to lose everything programmed, don't you?)

There is a good book named "Code Complete" entirely dedicated to the topic how to make your code more readable. For a pity, way too dick :-)

Also to reuse queries, google for "Data Access Objects design pattern", where all your queries are stored inside specific per-table classes.