Page 1 of 1

to pass, or not to pass, that is the question

Posted: Mon Apr 17, 2006 7:54 pm
by John Cartwright
Well I've been working on some small app's to familiarize myself with the Zend Framework, and when using their filter system I seem to have come to a design question which I could use some input on.

Well, when designing some modules there is a possibility for them to default to a value, or set a value.

For instance, I'll have a view which may or may not have an id to be passed to it. In the Zend Framework, their filters will return false if the value is not valid.

Code: Select all

if (($newsId = $filtered->testDigits('newsid')) == false) 
{	
	//invalid paramater
}
						
$this->view->paintNewsArticle($newsId);
$this->view->paintNewsComments($newsId);
What I want to know is, should I

- Set the default value when I know its an invalid paramter (indicated by the comment)

- Keep the false value, and pass the variable

- Pass nothing to the view and let it check the simple logic

Not really a big deal, but I am wondering if anyone has any thought on this?

Posted: Mon Apr 17, 2006 8:26 pm
by Ambush Commander
Well, if I was really really OOP I'd throw an InvalidID exception and have a higher up caller capture it and output an appropriate error message. It's the classic error bubbling problem.

Posted: Mon Apr 17, 2006 8:39 pm
by John Cartwright
Lets say I had to pass a starting point for a paginating class.. some user inputs letters in the url for some odd reason. I know it's invalid but I don't really care, I still need to paginate and there isn't much point on outputting an error message

What would I do then? Would I set the paramater to a default and pass that to the class -- or should I pass nothing to the class?

Posted: Mon Apr 17, 2006 8:44 pm
by feyd
Depending on where you want to place things, the paginating class would have input handlers that would correct any "bad" values since it knows it's inner workings the best. However you could also have general handlers but then the question comes, do you want special case code to know this field is supposed to be an integer greater than zero? Personally, I feel the class should handle the actual logistics while the general handlers simple make sure the information coming in is in a uniform (unescaped) format that everything accepts as basic input data.

Posted: Mon Apr 17, 2006 10:25 pm
by Christopher
This becomes a question about the dividing line between exceptions and expected invalid inputs. A simpler example relating to the one you gave is the first time you enter Paging code where not page number is given. This is not an exception, you want the thing to default to the first page of results if no valid page number is supplied. I have had a similar problem with lists where you can delete items. If you delete the last item in the list and it is alone on the last page, then returning to the previous page is now out of bounds, not unexpected, just invalid.

Where the line between gracefully responding to bad inputs and fending off hacking attacks is another aspect to this question.