Forums / Developer / RFC: handling of boolean arguments in methods

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

eZ debug

Timing: Jan 29 2025 13:41:20
Script start
Timing: Jan 29 2025 13:41:20
Module start 'content'
Timing: Jan 29 2025 13:41:20
Module end 'content'
Timing: Jan 29 2025 13:41:20
Script end

Main resources:

Total runtime0.1074 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 588.0625180.8203
Module start 'content' 0.00560.0056 768.8828102.0703
Module end 'content' 0.01110.0962 870.9531531.1094
Script end 0.1073  1,402.0625 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00292.6676200.0001
Check MTime0.00121.0945200.0001
Mysql Total
Database connection0.00080.790510.0008
Mysqli_queries0.071466.49361410.0005
Looping result0.00080.77691390.0000
Template Total0.096089.410.0960
Template load0.00060.565310.0006
Template processing0.095488.817610.0954
Override
Cache load0.00040.411710.0004
Sytem overhead
Fetch class attribute can translate value0.00060.582010.0006
XML
Image XML parsing0.00020.186110.0002
General
dbfile0.00575.2731200.0003
String conversion0.00000.004030.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs