Wednesday 29 December 2010 2:35:16 pm - 6 replies
I was thinking about how boolean function/method arguments are currently handled in eZ Publish.
Calls to methods with boolean arguments will look like this:
method( $stringArgument, true, false )
When reading this call, without using code tools like code insight, you don't know what true & false are for. Within PHP itself, these days, we see a lot of mask arguments:
$iterator = new FilesystemIterator( $path, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO );
While it requires writing more code, I feel like it strongly improves code readability, and actually improves the API itself by allowing more than one value for one option.
With the proper API in the framework itself, it would be easy to use these in our code. Any comment ?
Thursday 30 December 2010 11:24:00 am
>it actually improves the API itself by allowing more than one value for one option.
Then it is not a boolen anymore, if you have many options. For true boolean useage I would still keep TRUE / FALSE / YES / NO for the quickness of writing. A boolean is self explainable. The downside is that you would need to lookup "FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO" each time in the DOC if you do not use the code that much.
In areas where you need many values it is fine.
Also do not forget to update the coding guide lines.
Tuesday 11 January 2011 11:35:21 am
I agree with Bjorn here: use constants whenever options are more than one, but for simple booleans a global rewrite is imho not necessary - we have more important things to fix
Besides, why are booleans specially harder to understand than passing a value of say 25 or "hello" as function parameter?
A weird syntax I have seen in use is this:
$result = func( $do_this = true )
where the variable "do_this" servers no other purpose than reminding the reader the name of the parameter.
Not sure everybody would like it though: not only it is unconventional, but it might be error probe, too.
You must be logged in to post messages in this topic!