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

Is there a definition of weird way of code

Discussion in 'Scripting' started by MikeyJY, Apr 7, 2020.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I'm asking this because I started a big project and I have some dilemmas about my way of coding. All c# scripts are working and do what I want to do, but I feel like my code is kinda weird and I'll show you some examples:

    First I tried adding ragdoll. I didn't search on web and I tried this:
    Code (CSharp):
    1. public void EnableRagdoll(){
    2. for (i = 0; i <= 12; i++)
    3.         {
    4.             rigidbodies[i].useGravity = true;
    5.             rigidbodies[i].isKinematic = false;
    6.             colliders[i].enabled = true;
    7.          
    8.         }
    9. }
    10. public void DisableRadoll(){
    11. for (i = 0; i <= 12; i++)
    12.         {
    13.             rigidbodies[i].useGravity = false;
    14.             rigidbodies[i].isKinematic = true;
    15.             colliders[i].enabled = false;
    16.          
    17.         }
    18. }
    That's working but after that I searched how to do this and I found this:
    Code (CSharp):
    1. public Ragdoll(bool state){
    2. for(i = 0; i<= 12; i++){
    3.    rigidbodies[i].useGravity = state;
    4.    rigidbodies[i].isKinematic = !state;
    5.    colliders[i].enables = state;
    6. }
    7. }
    1 method instead of 2 and even if my code worked I felt like all my scripts can be replaced with something cleaner. And thats only one example
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, it's a good idea to try to reduce the amount of code, as long it does not make things more convoluted.
    And for sake of simplicity and flexibility you could use something like this:

    Code (CSharp):
    1. public SetRagdoll(bool value)
    2. {
    3.     foreach(Rigidbody rbody in rigidbodies)
    4.     {
    5.         rbody.useGravity = value;
    6.         rbody.isKinematic = !value;
    7.     }
    8.  
    9.     foreach(Collider coll in colliders)
    10.     {
    11.         coll.enabled = value;
    12.     }
    13. }
    Don't hardcode values for no reason. What if you have a Ragdoll system that has a different amount of bones? And generally magic values don't look good in code to my eye if they're not some mathematical/physics constants etc. (like Pi, gravitational constant or such.)
     
    Yoreki likes this.
  3. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Ok, thanks, anyway my game has some constants like Vector3 that store the item position in inventory, Vector3 for equip items in hand...
     
  4. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    210
    For a grab position, I would recommend an empty GameObject. You reference that instead of the Vector3 and use its position. The advantage is that you can easily position it in the editor. Also, you can use the rotation.
     
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Thanks, that's a good idea
     
  6. jasonatkaruna

    jasonatkaruna

    Joined:
    Feb 26, 2019
    Posts:
    64
    If you're like me and spend an embarrassing amount of hours at night thinking "What is clean code?" then there's a good series of books called "The Clean Coder" and "The Clean Architecture" by Robert Martin that will get you some sleep.
     
  7. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    :) Thank you I ll search on YT
     
  8. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    Most code will need to change, several times. Maybe you'll go up to 14 colliders, or have to do extra things to springs. Once you know that, a lot of ways of writing code make more sense. In your 1st way, changes take extra work, since both functions need the same changes.

    But suppose enable and disable are completely different. Putting them in 1 function would have a messy-looking IF. It might be better to have 2 functions in that case.