Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Funtion naming and actions
#1
Hello,

A recent forum reference to this wiki page, showing new functions appearing in v3.1, prompted this request.

On that wiki page, there are three naming styles for functions that fundamentally do the same thing: get, return and echo.

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.

Further, if this same idea could be extended to all core functions, they would all provide the flexibility of choice – to echo or return the result – making them more useful for more sophisticated data manipulation (rather than simply echoing content to output).

I know that changing core functions is a painful process, but feel that it would provide a much more solid foundation for future GS development, as well as a rather less frustrating experience for GS developers and site developers alike. At least, if new functions can be more consistent there will be less work to do in the future!

Discuss. ;-)
--
Nick.
Reply
#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
#3
I agree so much on the $echo parameter!

WordPress does it well by using the following syntax:

get_something() to return a value &
the_something() to output it.

This should become standard.
Reply




Users browsing this thread: 1 Guest(s)