RFC: handling of boolean arguments in methods

Author Message

Bertrand Dunogier

Wednesday 29 December 2010 5:35:16 am

Hi !
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 ?

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

Carlos Revillo

Wednesday 29 December 2010 6:29:47 am

+1. If it improves code readability (as it will) then it should be done :).

Peter Keung

Wednesday 29 December 2010 2:08:27 pm

+1

http://www.mugo.ca
Mugo Web, eZ Partner in Vancouver, Canada

Thiago Campos Viana

Wednesday 29 December 2010 3:26:34 pm

Never thought about this question, but it makes sense.

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

Maxime Thomas

Thursday 30 December 2010 1:56:27 am

+1

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

Björn Dieding@xrow.de

Thursday 30 December 2010 2:24:00 am

-1/+1

You say:

>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.

Looking for a new job? http://www.xrow.com/xrow-GmbH/Jobs
Looking for hosting? http://hostingezpublish.com
-----------------------------------------------------------------------------
GMT +01:00 Hannover, Germany
Web: http://www.xrow.com/

Gaetano Giunta

Tuesday 11 January 2011 2: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.

Principal Consultant International Business
Member of the Community Project Board

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 02:20:16
Script start
Timing: Jan 18 2025 02:20:16
Module start 'layout'
Timing: Jan 18 2025 02:20:16
Module start 'content'
Timing: Jan 18 2025 02:20:17
Module end 'content'
Timing: Jan 18 2025 02:20:17
Script end

Main resources:

Total runtime0.8587 sec
Peak memory usage4,096.0000 KB
Database Queries79

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 591.6641152.6406
Module start 'layout' 0.00600.0026 744.304739.4844
Module start 'content' 0.00870.8485 783.7891808.0234
Module end 'content' 0.85720.0015 1,591.812520.1250
Script end 0.8587  1,611.9375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.4016160.0002
Check MTime0.00140.1653160.0001
Mysql Total
Database connection0.00080.087910.0008
Mysqli_queries0.784791.3773790.0099
Looping result0.00100.1159770.0000
Template Total0.831896.920.4159
Template load0.00250.294520.0013
Template processing0.829396.569020.4146
Template load and register function0.00010.013710.0001
states
state_id_array0.00080.090010.0008
state_identifier_array0.00060.070720.0003
Override
Cache load0.00190.2220390.0000
Sytem overhead
Fetch class attribute can translate value0.00060.065570.0001
Fetch class attribute name0.00110.1249130.0001
XML
Image XML parsing0.00320.368970.0005
class_abstraction
Instantiating content class attribute0.00000.0020130.0000
General
dbfile0.00160.1863470.0000
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
6content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
2content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
10content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 30
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs