newism / symfony2-standard-template

2 stars 5 forks source link

Class method naming conventions #18

Open ghost opened 10 years ago

ghost commented 10 years ago

I think we could agree we've had troubles previously with class method naming conventions which have caused a few heartaches. Two issues relating to this have been introducing bugs due to methods behaving unexpectedly (i.e. a get method will perform more than just returning a value), and our code not being refactored totally correctly. (i.e. a lot of reused code in those costimator importers which perhaps could have been refactored given better 'bite size' coding).

Just thinking about it quickly tonight, could we start thinking about building a convention such as Request-Modifier-Element for our methods.

Request

Somewhat in the same vein as HTTP with safe calls as well. Just some ideas to start:

This (optional) verb describes what the method actually does. As long as this modifier is 'verby' then I don't think there is much more we can do here. Safe methods most likely won't use modifiers at all, but they will be needed most of the time on the non-safe methods.

Element

I've called this element and not property based on the idea that it might be an abstract part of the entity. For instance, within the pager we might have ResultsPage, TotalResults or PageNumber. PageNumber would be the only actual property of these three.

More points to ponder

Still pondering...

ghost commented 10 years ago

@leevigraham @iainsaxon

leevigraham commented 10 years ago

See: http://symfony.com/doc/current/contributing/code/conventions.html#method-names

leevigraham commented 10 years ago

Not sure about getSomeValue() and fetchSomeValue() being two different things. Exposing a getSomeValue() is probably enough. I understand that fetch could load some things into the entity or a cache but as someone implementing the class and calling the methods I don't really care about that *as long as the getSomeValue() is still safe.

Can you provide an example of splitSomeValue()?

RequestModifierElement or RequestElementModifier

I think this should be verb+Element+Modifier. Example: getOrderTotal() getOrderSubTotal(). It reads better to me. The other thing to consider here is count, sum, avg and other aggregate modifiers. These should also be the final prefix IMO.

So you could have:

[get|set|has|is|perform|replace|remove|all|add|register|count]
+[Property|EntityName]
+[Sum|Min|Max|Count]`

Actually looking at the line above aggregate values must only be used with get prefixes.

leevigraham commented 10 years ago

Additionally naming conventions for aggregate fields stored in the DB could reflect their collection.

Example instead of getOrderTotal() we could use getOrderLineItemCostSum()

I think we should never use getSomethingTotal. Is it total number of items in a collection or a total of one of their properties. Sum and count are much better. Lots of people smarter than us figured this out a long time ago :)

iainsaxon commented 10 years ago

If we are moving towards the idea of simple entity-value containers where the entities do not do work or change state then the fetch* method prefix may not be needed. The entities themselves will not alter their own state because a service/factory/manager has set the values.

I guess this is headed towards a system where side-effects are limited as calling get* method on the entity won't trigger state change that may be unexpected. The entities that are sent to the views/templates remain the same in the controller and the views.

This could also mean that any tax-related functions would have to be sorted out in the controller before the item is sent to the view. While debugging code would be a lot easier as you would only need to look in the controllers and managers for entity state-changes the view side could get a little trickier as returning getPriceIncludingTax would only work if the tax was calculated before the entity was sent to render.