Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Best Practice for Writing Scripts?

Discussion in 'Scripting' started by Pumkitten, Mar 17, 2020.

  1. Pumkitten

    Pumkitten

    Joined:
    Feb 17, 2020
    Posts:
    2
    I'm just starting to get into Unity and game development in general. I started off by watching a few different tutorials. The folks doing the tutorials handled writing their scripts differently. In particular, two of the three wrote the scripts related to player movement directly in Update whereas the third wrote separate functions for each part of movement (getting input, calculating move direction, etc.) and called them in a certain order in Update.

    I was just wondering, is either of these two methods "better?" Like I said, I'm a newbie with all of this and I don't want to learn to do things the "wrong" way and have to teach myself the "right" way later.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    It easier to prototype with everything in update, but easier to debug and maintain with each bit of functionality in different methods, or even classes, and call them in update. With that said, the former is good when you're still learning, since the benefits of the latter can only be felt after learning hardships from the former.
     
    Pumkitten likes this.
  3. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    210
    Do not be afraid of the wrong way. You will walk on it, anyway. Embrace it.

    Extracting methods instead of writing everything in Update has a few advantages:
    - it can be more readable and the method name can serve as documentation. Tend to write lots of comments everywhere to compensate for cryptic variable names? Not a good idea.
    - your methods can be used from elsewhere, so you can reuse code. Want to change your Attack() behaviour, but it is called from 10 places? No problem, just change it once, everything uses the same code.
    - it can be easier to change and extend later. Want to change order of Attack() and Jump()? Just swap two lines.

    However, it also comes with disadvantages:
    - it can be less readable, if you fragment your code too much. Have a method that consists of just one line? Probably useless.
    - it can be harder to change and extend, because the code is all over the place. Your Attack() method now needs a local variable from your Rotate() method? Well, you now have to refactor stuff.

    So, you see, it really just depends.

    On a general note:
    When you keep coding, you will come across several more approaches to the same problem. Usually, all of those have their pros and cons. Some are certainly better than others, but many are equally good and it really just depends on your needs.
    I agree with @Laperen . Spent effort in architecture and design only when you need, not before. Personally, I overdesign all the time, because I like it. If you want to make progress, though, do whatever works for you. You will run into problems either way. Solve them then.
     
    Pumkitten likes this.
  4. Pumkitten

    Pumkitten

    Joined:
    Feb 17, 2020
    Posts:
    2
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    o Traditional game developers like things very simple. Functions are things you use, not things you write. Most successful games aren't all that complicated, so why work extra hard.

    o But people with some programming background naturally write functions for everything. It would feel weird not to. Plus, some of the examples you saw may have been trying to teach functions.

    o Then some people figure that since Unity currently uses C# (it didn't always) they have to follow official C# guidelines.

    If you're more interested in playing with all the stuff in Unity, then don't even bother with how you "should" program -- your favorite game probably breaks all of those rules. But if just using code to move around a box is super fun, eventually get a very basic non-Unity programming book.
     
    Pumkitten likes this.
  6. GMore

    GMore

    Joined:
    Nov 16, 2015
    Posts:
    5
    No one can tell you more about writing good code than uncle Bob. If you want to be a good programmer, who is able to maintain his code easly, I would recommend you reading this book - Clean Code Robert C. Martin.
     
    Pumkitten likes this.
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    That's aimed at programmers. It would be like reading Hulk Hogan's book to learn how to wrestle.
     
    Pumkitten likes this.
  8. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    210
    You will not get a consensus on this. It is a passionate topic for many.
     
    Pumkitten and Laperen like this.
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,386
    Moving code to a separate function can be done for a few reasons. One is simply an organizational tool- to make your code easier to read by breaking it up into separate logical sections.

    Another reason is if you have some code that runs multiple times and at different places. Then you can write the code just once in a function and then call that function each time you need to.
     
    Pumkitten likes this.
  10. Dexter_Ryan

    Dexter_Ryan

    Joined:
    Jun 8, 2020
    Posts:
    1
    You definitely can't avoid the "wrong" way. This is passed by all, without exception.