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.

Discussion Wrong Way to Script Things

Discussion in 'Scripting' started by BlackSabin, Jul 22, 2022.

  1. BlackSabin


    Feb 27, 2021
    ...Is there one?

    I mean, I've been working on my game for a while now, and each and every time I look for either someone else's implementation or interpretation of how the code should look, etc... Theres almost always a different way of going about a solution. Thats just the nature of programming I suppose, that theres an absurd amount of different ways to solve a given problem, but are there any wrong ones? And I don't mean "less performant" per se, maybe something that is just absolutely not something you should do when it comes to trying to use something in the future... Maybe this is just me rambling.

    This is my question to the community here; what have you learned from experience that is the wrong way to do something in Unity?
    mopthrow and GuirieSanchez like this.
  2. Kurt-Dekker


    Mar 16, 2013
    Yeah! PERFECT Friday chat topic! :)

    I suppose that if code works and is finished and the game is published and making money, then code is good.

    But in practice one cares more about code than "is it presently shipped."

    For instance you might care about:

    - will this work in all my predicted cases?
    - are there easily-determinable things that would make this fail?
    - does this code work even in cases I didn't think of?
    - can I understand what this does?
    - can I understand it six months from now? <---- personally my most important
    - can my team understand what I wrote? <---- professionally my most important
    - can my team take this and modify it even if I am run over by a train?
    mopthrow and GuirieSanchez like this.
  3. mopthrow


    May 8, 2020
    My little bit of experience tells me that if I have to spend as much time trying to understand how an old script works as I did building it in the first place, I did something wrong.

    It's especially bad if I have to do this multiple times of the life of the codebase. "It would have been faster just to rebuild it" is the situation I try to avoid getting in to, sometimes at the expense of performance or cleverness of the solution (though I have to admit it pains me to delete a 'clever' solution 5 minutes after I finished it and realise there's an easier way).

    'Wrong' for me means unnecessarily punishing my future self. Anything else I can live with or iterate upon :)
    GuirieSanchez and Kurt-Dekker like this.
  4. GuirieSanchez


    Oct 12, 2021
    @Kurt-Dekker Like the tragic "get run over by a train". I agree on all of those.

    @BlackSabin And yes, I wonder the same each time I implement things of my own (which is probably +95% of the time). In those cases, I wish I had a tutor, or maybe had studied programming in college instead of something else. But there's something good and rewarding in studying on your own and seeing how everything gets together and works nicely (as long as you have some basic knowledge and don't do anything catastrophic (even performance-wise, such as calling expensive functions in an update method when you only need to call them on a trigger/event. Or worse: creating dependencies all over your game (which I did when I started learning)).
    mopthrow likes this.
  5. _geo__


    Feb 26, 2014
    Ha, that reminds me of this article on joelonsoftware. A bit dated but still a worthy (and funny) read :)

    What are your go-to websites with meta articles about programming?
  6. GuirieSanchez


    Oct 12, 2021
    Many things in that article are true and really relatable :D
  7. passerbycmc


    Feb 12, 2015
    i target readability first (for future me, and co-workers), and try not to waste time abstracting and thinking of cases that may never happen and just try to write code that is loosely coupled and easy to remove and rewrite if things change in the future and it is not longer suited for purpose.
    _geo__, mopthrow and Kurt-Dekker like this.