Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · May 01, 2012 at 02:51 AM · updatereturnstyle

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);

} }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · May 01, 2012 at 04:18 PM 0
Share

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!)

avatar image ina · May 01, 2012 at 11:29 PM 0
Share

so it's mainly for readability / style, rather than any performance difference.

(80s is a tad early for me though!)

avatar image
0

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges