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
1
Question by Stardog · Feb 23, 2015 at 07:42 PM · styleconditional

Coding style - Check conditional inside function or before running it?

Should the IsAttacking() be checked inside the function, or before running the function? How do you normally do it?

     if (Input.GetButtonDown("Action"))
     {
         if (!attackScript.IsAttacking())
             attackScript.DoAttack();
         else
             Debug.Log("Player tried to attack, but is already attacking.");
     }

     public void DoAttack()
     {
         // ATTACK CODE
     }

Or...

     if (Input.GetButtonDown("Action"))
     {
         attackScript.DoAttack();
     }

     public void DoAttack()
     {
         if (!IsAttacking())
             // ATTACK CODE
         else
             Debug.Log("Can't attack, already attacking.");
     }

Comment
Add comment · Show 1
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 · Feb 23, 2015 at 08:39 PM 1
Share

How many different places and scripts will DoAttack be run from? How much chance is there that someone will forget to run the check? Will you ever want to force an attack?

The most official answer I know of, like out of a textbook, is to check BOTH places. It doesn't cost much, crashes are bad, and it saves lots of "but I thought you did it?" problems.

3 Replies

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

Answer by steakpinball · Feb 23, 2015 at 09:16 PM

Is the calling code concerned with whether it is attacking? If not the check should be inside. If the outside code should not be concerned with the implementation of the method you are calling it should be inside.

A more thorough explanation is on programmers stack exchange.

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
avatar image
1

Answer by gfoot · Feb 24, 2015 at 02:39 AM

One rule of thumb for reusable, library-style classes is that private methods can make assumptions about internal state and/or the state of arguments, because their callers know everything, but public methods cannot because their callers often can't even see the internal state. Encapsulate the constraint in the class that best understands it.

Extending that, it can be important for public methods to perform these checks in order to guarantee that the internal state of the object remains consistent. It is less important (but still often valuable) for private methods to perform these kinds of checks, because they can trust the caller to understand the internal state itself.

However, for the case you give, I think your class really ought to let the calling code see that attacking right now is futile. Then the calling code has an opportunity to choose to do something more useful instead, rather than just issuing an attack order that is ultimately ignored. For this kind of thing, think of the principles behind the HATEOAS model - code shouldn't assume it knows what can be done next, it should ask the thing it's going to talk to what options are available, then pick one. This needs to be applied selectively, but I think game "AI" is one area that this logic applies well.

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
avatar image
0

Answer by DGKN · Feb 23, 2015 at 07:48 PM

I'd say first method is better as it doesn't go to the function unless the condition is met. You save time and time is precious :)

Comment
Add comment · 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 Stardog · Feb 23, 2015 at 08:23 PM 0
Share

I guess it's more flexible too, because the conditions aren't hardcoded into the function.

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

23 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

Related Questions

time machine game with Unity3D 2 Answers

Changing background, font and style of buttons 1 Answer

Text Colour Gradually Change Animation 0 Answers

Is it not possible to include "if-then" conditionals within foreach loops? 1 Answer

Conditional Breakpoints - Can't Enter an Expression? 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