- Home /
Is using return in update preferred to do something only if true?
I'm looking through this script and it seems to use return a whole lot, where I would have used if (bla!=null) then evaluate
I'm wondering if using return is preferred over evaluating for a conditional and then operating? ie I would have written it like this:
if (Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit)){ // differene HERE
// Just in case, also make sure the collider also has a renderer
// material and texture
var meshCollider = hit.collider as MeshCollider;
if (meshCollider == !null && meshCollider.sharedMesh != null){ // difference HERE
var mesh : Mesh = meshCollider.sharedMesh;
var vertices = mesh.vertices;
var triangles = mesh.triangles;
// Extract local space vertices that were hit
var p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
var p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
var p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
// Transform local space vertices to world space
var hitTransform : Transform = hit.collider.transform;
p0 = hitTransform.TransformPoint(p0);
p1 = hitTransform.TransformPoint(p1);
p2 = hitTransform.TransformPoint(p2);
// Display with Debug.DrawLine
Debug.DrawLine(p0, p1);
Debug.DrawLine(p1, p2);
Debug.DrawLine(p2, p0);
} }
Answer by Bunny83 · May 01, 2012 at 03:23 AM
The compiler and the executing system doesn't really care about that since you have to do the check anyway and if the code shouldn't be executed it will branch to bypass the code.
The so called "early-exit" is often used to reduce the brackets if you have multiple conditions. However it can't be used everywhere since a return leaves the function at this point.
In this case an early exit wouldn't work since you would interrupt the Update function.
function Update()
{
if(rigidbody != null)
{
rigidbody.AddForce(....);
}
if(Input.GetKey(...))
{
// Do something
}
}
Here's an example where it makes sense:
First without early exit:
function DoSomething()
{
if (/* quite complex condition #1 */)
{
if (/* quite complex condition #2 */)
{
if (/* quite complex condition #3 */)
{
if (/* quite complex condition #4 */)
{
// Your actual action is here
}
}
}
}
}
With early exits it's much more readable:
function DoSomething()
{
if (/* quite complex condition #1 negated*/)
return;
if (/* quite complex condition #2 negated*/)
return;
if (/* quite complex condition #3 negated*/)
return;
if (/* quite complex condition #4 negated*/)
return;
// Your actual action is here
}
Basically an early exit can always be used when everything below the condition shouldn't be executed because it belongs on the condition.
An alternative is of course to combine all conditions into one big if statement, but if they are complex it's really hard to read.
edit
In general Update is a rare candidate for an early exit. Even if it might be useful at the first glance, as soon as you add more functionality you have to change it.
People used to use early-exit returns everywhere (esp. back when you had to create your own if-else's using goto's.) Because of the issues you pointed out, in the 80's there was a craze to never, ever use a return except as the very last line.
I still feel a little slimy when I write if(T==null) return;
at the start of a function (or if(paused) return;
in Update!)
so it's mainly for readability / style, rather than any performance difference.
(80s is a tad early for me though!)
Answer by Seth-Bergman · May 01, 2012 at 03:13 AM
either way, the rest never gets entered if null, so I doubt if there is a significant difference in performance here, but I'm no expert...
Your answer
Follow this Question
Related Questions
Trying to create a function that returns a bool 2 Answers
My if statement won't work 1 Answer
Animation Opposite 0 Answers
Error CS0161 : not all code paths return a value? 1 Answer
Return to a specific scene & position 0 Answers