Cirdan wrote:Here the code is 99% the same except one updates an existing entry, and one inserts a new entry. How do I reduce this duplicate code? Is it worth the extra code and complexity to write a method that can handle both cases, then wrap that method into these two methods? This particular situation only has these two methods, but in my Forum code, there are several more like this. Minor differences, but a majority of the code is the same. Maybe this duplication means something is wrong with my design?
This looks very similar to a Table Data Gateway.
The first thing you could do to remove duplication is instead of passing each variable as a separate parameter, pass it in an array, like so:
Code: Select all
public function insert($page) {
// insert
}
public function update($id,$page) {
// update
}
That frees you up to do fancy things like looping through the array to build a SET string for your INSERT and UPDATE queries.
Once you do that, however, you'll find that you have similar code between your page gateway, your post gateway, your article gateway, etc. The next step then is to take the code that builds queries from arrays, and put that into a parent gateway that all of your concrete gateways extend from. You might want to place generic queries in there like findById, insert(), update(), and delete() as well, since all you really need to generate those queries is a table name.
To start with, many of your concrete gateways will look like this:
Code: Select all
class PageGateway extends Generic_Gateway {
public function __construct($db) {
parent::__construct($db,'pages');
}
}
That should be a good start.