c# - Why would we need void return type from methods in OOP languages? -
it's not i've started learning java yesterday, thought, why ever use void methods, if can return this instead? way can chain method calls on object , make code more readable (i know approach gaining popularity already, immutable objects, , lets forget java beans convention). case think of void being required static methods.
presumably accept methods need tell - kind of return value. seems artificial , obtuse "return value want return, unless don't want return anything, in case return this instead, unless static method, in case return void".
how about:
- if appropriate return something, return it
- if if isn't, don't
- (with wriggle room cases "fluent" api genuinely makes sense)
also: think inheritance; if have virtual method foo(), return type have foo's declaring type:
public virtual sometype foo() {...} now imagine subclass sometype, bar : sometype , have instance of bar:
bar obj = new bar(); obj.foo().someothermethodonbar(); // error hey, did bar go!?!?! polymorphism not respect fluent apis.
as final thought: think of "pop"s when don't want chain methods...
Comments
Post a Comment