Page 1 of 1

[1.4.0] Wrap up common code in less error-prone functions.

Posted: Wed Jan 27, 2010 4:45 am
by payflow
In all the controller files I've opened up, I've seen code like this:

Code: Select all

		if (isset($this->request->post['company'])) {
    		$this->data['company'] = $this->request->post['company'];
		} elseif (isset($this->session->data['guest']['company'])) {
			$this->data['company'] = $this->session->data['guest']['company'];
		} else {
			$this->data['company'] = '';
		}
Repeated over and over. A simple in-class function like:

Code: Select all

private function loadDefaultValue($key, $default)
{
	if (isset($this->request->post[$key])) {
		$this->data[$key] = $this->request->post[$key];
	} elseif (isset($this->session->data['guest'][$key])) {
		$this->data[$key] = $this->session->data['guest'][$key];
	} else {
		$this->data[$key] = $default;
	}
}
(Called with $this->loadDefaultValue('company', '');)

Would potentially allow hundreds of lines of code to be removed from the software, improve overall code readability, and reduce the amount of time needed to test. Of course, there might be URL "route" issues (make sure no one can do route=checkout/guest_step_1/loadDefaultValue).

Re: [1.4.0] Wrap up common code in less error-prone functions.

Posted: Wed Jan 27, 2010 7:06 am
by Qphoria
I too have played with this in the older 0.x version. Basically, since the post fields are (at this point) named the exact same name as the db field, there is really no need for statically listing each. I suppose it could open a can of worms to use a simple key = value method and assume the key name is the field name and the db name exactly. The only place you'd have problems is if you used a different name for a post field. Which could happen I suppose. Or it could be an array type field.

Something to think about tho.