TileVania, why Rick write code like this?

Is there some reason why Rick write code like this:

{
    if (!myCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
    {
        return; 
    }
        
    Vector2 climbingVelocity = new Vector2(moveInput.x, moveInput.y * climbingSpeed);
    myRigidbody.velocity = climbingVelocity;
       
}

instead of

void ClimbLadder()
{
    if (myCollider.IsTouchingLayers(LayerMask.GetMask("Climbing")))
    {
       Vector2 climbingVelocity = new Vector2(moveInput.x, moveInput.y * climbingSpeed);
       myRigidbody.velocity = climbingVelocity;
    }
            
}

Is this for optimization or what is reason for writing longer code?

The first version is a little easier to read. No real difference otherwise. In a simple bit of code like this one, there is not a lot of difference. If you had more going on in the method then the difference in readability would be greater.

2 Likes

Hi xSerj,

edc237 is right. However, since “easier to read” is a matter of personal preference, I’d like to add that an if-statement to terminate a method early is common practice in programming. In fact, most of the time, programmers are not eager to read code, so we appreciate “shortcuts” like this. If we see an if-statement at the top of a method code block, and that if-block contains return;, we interpret the code snippet immediately as “if true, the rest is irrelevant and won’t get executed”. We don’t even have to read the code to understand the logic. That’s why it is “easy to read” for programmers. Obviously, we presume that the coding style follows this logic. Otherwise, our instant interpretation would be wrong.

You’ll encounter this style quite often if you read other people’s code.

Nevertheless, your solution is valid as well. Feel free to use it if you like it better than Rick’s. :slight_smile:


See also:

2 Likes

Thank you for your answers.

1 Like

Adding my two cents.

I found that writing code the way Rick did it is often easier to structure, for instance when having another if statement or a loop, let me give you an example.

private void MyMethod()
{
    if(someCondition)
    {
        if(!anotherCondition)
        {
            SomeCode();
        }
    }
}

An if statement nested inside another if statement means that the first condition is actively affecting the first second one, meaning you really need to think your conditions thoroughly to prevent logical errors. I’ve found that sometimes I have to add more conditions or be super careful with the naming to prevent confusion.

private void MyMethod()
{
    if(someCondition) { return; }

    if(anotherCondition) 
    { 
        SomeCode(); 
    }
}

Rick’s method makes it relatively easier to read and makes each condition independent from one another since there’s no nesting, breaking the logic in more steps which is often better, so you don’t have to be as careful with your conditions, it is also, sometimes easier to diagram.

I’m not saying that you shouldn’t nest if statements, it’s perfectly fine to do that, it’s just a matter of preference, the more you practice, the more you’ll fine your own style, coding is like drawing, there are some bases that you should follow, but once you master those, you can do whatever you want.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms