Comparison of the variants:
In the important case - Component has no errors and returns nothing, both variants return the same.
So my only issues with Zegnåt's approach is that it is overly complex:
I'm assuming that component 'sidebar' has no errors, as it is the default and was already tested.
My approach:
Having said this and trying to keep it simple, I prefer my simple approach, because support for it is much simpler - it's easier to ask "does the component exist?" instead of "Does the component exist and compile and not return anything or at least not false?" - but I'm happy with either way.
BTW: I don't care if break or return is used - if the code works ;-)
Code:
Component Component Component Result Result
exists has errors returns mvlcek Zegnåt
===============================================================
yes yes false true false <=
yes yes nothing/not false true false <=
yes no false true false <=
yes no nothing/not false true true
no yes false false false
no yes nothing/not false false false
no no false false false
no no nothing/not false false false
In the important case - Component has no errors and returns nothing, both variants return the same.
So my only issues with Zegnåt's approach is that it is overly complex:
- It tests, if the component has errors. But will users really check the return value for this? Is anybody - except in special cases - writing code to test, if some other code compiles???
- On the other hand, in a realistic call, for which the return code of get_component was intended (by me, at least, based on feature requests in the forum) - like get_component('sidebar-'.return_page_slug()) || get_component('sidebar') - Zegnåt's approach has no advantages for testing/debugging. See below.
I'm assuming that component 'sidebar' has no errors, as it is the default and was already tested.
My approach:
- sidebar is displayed, but shouldn't => sidebar-xxx does not exist
- nothing or an error message is displayed => there is an error in sidebar-xxx
- sidebar is displayed, but shouldn't => sidebar-xxx does not exist or sidebar-xxx has an error (or sidebar-xxx returns false)
- nothing or an error message is displayed => can't happen
Having said this and trying to keep it simple, I prefer my simple approach, because support for it is much simpler - it's easier to ask "does the component exist?" instead of "Does the component exist and compile and not return anything or at least not false?" - but I'm happy with either way.
BTW: I don't care if break or return is used - if the code works ;-)