return-path

Better Java method Syntax? Return early or late? [duplicate]

安稳与你 提交于 2019-11-29 15:28:09
问题 Duplicate: Should a function have only one return statement? Often times you might have a method that checks numerous conditions and returns a status (lets say boolean for now). Is it better to define a flag, set it during the method, and return it at the end : boolean validate(DomainObject o) { boolean valid = false; if (o.property == x) { valid = true; } else if (o.property2 == y) { valid = true; } ... return valid; } or is it better/more correct to simply return once you know the method's