• 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
Question by $$anonymous$$ · May 17, 2019 at 05:46 AM · script.fpsscripting beginnerscriptingbasicsscript loading

is same code written in less lines, more beneficial ?

like is

    if(mybool) dosomething();  

faster than

   if(mybool == true)  
   {  
       dosomething();  
   }

im making mobile game, so I want to cut corners everyway possible to get 60 fps

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by hassanyawar · May 17, 2019 at 06:19 AM

I don't think it matters what way you write it. It's basically just formatting and by any chance if there is any performance benefit, it will be almost negligible but again in this case there will be no benefit. I personally like to use the second approach while writing my code. For me it increases readability. So in the end it's more of personal preference. I would suggest you to focus on writing clean and easy on eyes code. Good luck.

Comment
SunnyChow
$$anonymous$$

People who like this

2 Show 1 · 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 Bonfire-Boy · May 17, 2019 at 09:45 AM 1
Share

Certainly has no impact on performance. These snippets compile to the same thing.

IMHO the second reduces the chance of errors creeping in, so it has advantages beyond readability. To what extent it reduces those chances varies with the developer of course, but I tend to think it makes sense always to assume that at some point my code will be developed further by an idiot (and that idiot may be me).

avatar image

Answer by troien · May 17, 2019 at 09:30 AM

In short, no. Pick the one you prefer for readability


In theory, if (mybool) is more efficient as if (mybool == true) as it would be one (redundant) == operator less to execute. In my test the C# compiler optimizes this away, so there is no difference in build. It didn't optimize this though if (mybool == !true), but that is pretty bad code to read anyway and might be optimized in future or in different compilers. But even then the difference in efficiency is probably not noticeable and your time and effort is probably better spend optimizing other parts of your code ;). (See this question)


Whitespaces and comments don't make any difference in the compiled result, they are ignored. (release build atleast)


Brackets in single-line if statements are just preference, they don't change compiled results.


File size of release build should also be equal in size, no matter which version you picked. In development build, build size can be slightly (not really noticeable) different to allow you to for instance set breakpoints on those curly braces.

Comment
$$anonymous$$
FunkiestFak

People who like this

2 Show 0 · 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

Answer by Zanktus · May 17, 2019 at 07:57 AM

Except increasing your file size by a few kb if it is super huge it won't affect your performance at all. This is the least thing you should worry about to reach/keep 60fps. I personally write the shorthand whenever I can, so I keep my Script lines rather short, but that is not mandatory if you don't like the readability.

Comment
$$anonymous$$

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

161 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I transfer VS code to my Unity script? 3 Answers

is it ok to change the source scripts? 0 Answers

Trying to find the highest number than add it to itself. 2 Answers

Point Counter Works Only Once! 1 Answer

how to allow the key to only open 1 door rather than all of them? 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