I used a Method...I like how tidy it looks but can it be a problem in the long term?

Hi All,

So I successfully assigned the Random.Range but I used a Method thinking it looks cleaner. However, after viewing the solution it talks about “removing cache”…

I am now thinking, YES a Method can be use but it will add “Cache” and as a result it needs to be considered when creating Method when writing code?

Obviously after watching the solution, a Method is probably not necessary, but i wonder if it will affect anything else if I add Method every where?

Cheers
image

I don’t think I understand your concern. I’ll simply say that creating methods is important for many reasons, such as: clean code, efficient code, readable/maintainable code…

For an example, while I have actually taken several steps to make this function “readable”, it’s still a rather large mess; In fact, it’s going to get much worse, because I’m in the middle of refactoring it to also utilize the “low” raycasts; I’ve added remarks to help, but… yeah… good luck rapidly digesting this method:

private void DetermineTurn(RaycastHit hitPort, RaycastHit hitPortLow, RaycastHit hitStarbordLow, RaycastHit hitStarbord)
   {
      const float RAYCAST_CORRECTION_FACTOR = 9.0f;
      const float REVERSE_DELAY = 6f;

      // Turn more sharply when a neighbour is detected.
      if (hitPort.distance > 0 || hitStarbord.distance > 0)
      {
         lastContact = Time.time;
         correctedTurnRate = turnRate * RAYCAST_CORRECTION_FACTOR;
         if (hitPort.distance > 0 && hitStarbord.distance > 0)
         {
            // When there are neighbors on both sides, if course is toward closer target then invert course (but not too often).
            if (((hitPort.distance > hitStarbord.distance && correctedTurnRate > 0)
               || (hitPort.distance < hitStarbord.distance && correctedTurnRate > 0))
               && revTime < Time.time)
            {
               correctedTurnRate = -correctedTurnRate;
               revTime = Time.time + REVERSE_DELAY;
            }
         }
      }
      else correctedTurnRate = turnRate;
   }

By contrast, my Init() function was such a mess I had to break it down into a handful of “sub-functions” (my word) to help keep it readable/maintainable:

   private void Init()
   {
      Vector3 scale = GetNewScale;
      SetLocalScale(scale);
      TuneAnimationSpeed(scale);

      SetNewTurnRate();
      SetNewOrientation();
      SetNewRandomSpeed();
      speed = newSpeed;
      if (animator.isActiveAndEnabled) animator.SetFloat("stateSpeed", speed * scaleFactor * ANIMATION_SPEED_FACTOR);

      lastContact = 0;
      revTime = 0;
      caution = false;

      group = GetGroupID();
   }

The fact that I made all those other methods, i.e. SetNewTurnRate, SetNewOrientation, SetNewRandomSpeed, SetLocalScale, etc… doesn’t really change the final product; by the time the compiler turns it all into machine language the results will likely be identical between the original source code and the source-code after the extra methods were added.

The real difference is that before I added all those methods, the Init() function was huge and messy, difficult to digest, but now with a glance I am reminded exactly what it’s responsible for. This methodology can go layers deep too, of course; for example, the SetNewTurnRate method, also calls on a “property” (a different type of method, basically) called FiftyFifty… this makes the SetNewTurnRate function just that bit more “readable”:

   private void SetNewTurnRate()
   {
      const float TURNRATE_MAX = 10.0f;
      const float TURNRATE_MIN = 5.0f;

      turnRate = Random.Range(TURNRATE_MIN, TURNRATE_MAX);
      if (FiftyFifty) turnRate = -turnRate;
   }

For what it’s worth, here is the ‘property’ FiftyFifty:

private bool FiftyFifty { get { return (Mathf.FloorToInt(Random.Range(0, 2)) == 1); } }

Yes, that’s right, I even went as far as making a “one-line” method. If it makes the code more maintainable, it’s worth it. What you want to avoid, is making extra methods simply for the sake of making extra methods. If that were the only time and place that I wanted something to happen ~half of the time, it might have been going too far to make it (FiftyFifty) a method to itself, but since I use it several times, it’s actually important to make it it’s own method; doing so ensures that if I need to change it, I only have to change it one place, rather than N places.

2 Likes

Thank you for your insight. I really like using the method. It make sense and help to get more clarity.

Privacy & Terms