Is there a school of thought on putting focus on using generic names for functions, opposed to naming functions based on the things they do?
Example
Say we have a Bill / Price Sheet
object, that has line items
Bill:
* Shipping Charge $5.00
* Crate Charge $6.00
* Tax Charge $8.00
Generic function names would be:
//function's code knows how to add a line item
bill->addLineItem("Shipping Charge", 5);
bill->addLineItem("Crate Charge", 6);
bill->addLineItem("Tax Charge", 8);
Specific function names would be:
//function's code reflects knowledge of this specific line item
bill->addShippingCharge(5);
bill->addCrateCharge(6);
bill->addTaxCharge(8);
This can be extended to any set of similar actions where something similar is being done. A more generic function takes the actual parameter, and can be deemed more flexible - any new line item can be added using the same function. A more specific function can only be used for that particular line item. Adding a new type of line item requires a new function. Something tells me that the specific way is more preferred, but I can't verbalize why, and maybe I am wrong. But perhaps it makes the code more clear as to what this line item is intended to do.
So if specific naming is superior, and I am writing code for a restaurant with 600 items on the menu (such restaurants do exist), I am not sure I want to have 600 unique functions for each menu item. But in that case maybe a different approach is warranted. So what I am asking here, is if I have a reasonable class of items (i.e. 10-20) for my specific possible line item list, would you recommend any particular approach from the ones described (specific vs general), or does it all depend on my project specifics?
bill->addCharge(ICharge newCharge)
so that you can add new charges by callingbill->addCharge(new ShippingCharge(5))
orbill->addCharge(new TaxCharge(5))
? ShippingCharge and TaxCharge both implement the ICharge interface. – valenterry May 7 at 15:30bill
class because they have to be handlend different later (AFAIK PHP doesn't support pattern matching yet), then i would suggest using method overloading so that you have multipleaddCharge(...)
with different signatures as there is one method then for each kind of charge. – valenterry May 7 at 15:39func_get_args()
) then i guess you can't express this without violating SOLID / DRY. This means you would have to use one of the options you suggest in your question. Your feeling is correct that the specific function names are better then because it is more clear and better supported by many IDEs if you are calling a non existing method name than calling a method name with an syntactic correct but method contract violating String. – valenterry May 7 at 15:56