Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Funtion naming and actions
#2
hameau Wrote:I would like to propose that function naming should follow a more consistent format, so that any data-fetching function is named 'get...', with a switch as the final parameter to select 'echo' or 'return', this switch having a system-wide default value. Ideally, this philosophy should be extended to all the core functions and included in the Coding Guidelines for contributors, too.

I personally don't like this parameter $echo at all:
  • it's easier to understand echo get_it(); than get_it(true);
  • get_it(); with default to echoing is as bad as it does not get it, but output it
  • if there are more parameters, is $echo the first or the last?
  • what, if a future version adds a parameter? Now $echo is in the middle to provide compatibility?
  • there is this function get_page_url($echo);, where by default $echo is false (as opposed to all other functions) and $echo = true means, the url is NOT echoed!?!
  • if you have anything other than a string returned, echo or not is not feasible. You can return e.g. the page data (XML or other structure), but for outputting it you would have to specify more parameters on how you want to do it.

Im my plugins I'm using the 2.0 syntax:
  • return_something(...) will return the data
  • get_something(...) will output the data (and return true, if anything was output.). This allows you to have code like get_something(...) || get_something_else(...).

The prefered way IMHO would be
  • get_something(...) (or return_something(...) for compatibility reasons) to return the data
  • output_something(...) to output data
I18N, I18N Search, I18N Gallery, I18N Special Pages - essential plugins for multi-language sites.
Reply


Messages In This Thread
Funtion naming and actions - by hameau - 2011-10-16, 19:31:52
Funtion naming and actions - by mvlcek - 2011-10-16, 19:56:04
Funtion naming and actions - by lacroixca - 2011-12-05, 02:24:13



Users browsing this thread: 1 Guest(s)