java - Is getOrCreate function a good or a bad practice? -
in code (hypothetical) i'd use getorcreate function. pass parameters , either new entity or existing entity database, if such entity exists.
from 1 point of view wrong approach, because function should not more 1 thing. point of view single operation, not have proper word in english , can reduce duplicities in code.
so using approach or bad practice? , why?
it's function. instance of class.
it doesn't matter outside world how function works internally.
public object getobject(int key) { object object = getobjectfromdatabase(key); if (object == null) { object = createobject(key); writeobjecttodatabase(key, object); } return object; }
every method has 1 function.
edited add: people @ methods inside out. that's need when you're writing code method. recognized getobject method had several things object.
however, when you're naming method, @ method outside. why getobject method "gets object" (pretty short javadoc description). if can't write simple declarative sentence describing function of method, method possibly complicated.
Comments
Post a Comment