Design a site like this with WordPress.com
Get started

Day 58: Shorthand

Just a little something I found interesting – in VS Code when the quick fix lightbulb suggests to encapsulate a field, it automatically inserts something like this:

public float DebuffDuration { get => debuffDuration; set => debuffDuration = value; }

…instead of the longer code below which was in the tutorial:

public float DebuffDuration
{
     get
     {
           return debuffDuration;
     }

     set
     {
           this.debuffDuration = value;
     }
}

It seems a lot cleaner & easier to read having the accessors all in one line instead of spread over a dozen.

As far as I can tell, they both do the exact same thing as it hasn’t messed up the game or thrown any errors.

I don’t know if that’s what’s referred to an Auto-Implemented Property, expression lambdas or just a shorthand way of typing the same code. Either way, I didn’t realize the same code could be written in different ways.

Between this little realization and what I’ve read thus far in ‘Clean Code’, programming well can be considered an art.

Current Progress Status

  • Tower Defense Tutorial: 56/73
  • Clean Code by Robert C. Martin (pg 185/464)

Onward!

Advertisement

Author: Mike@PAUSED

Aspiring indie game developer. Devlog: www.paused.ca

2 thoughts on “Day 58: Shorthand”

  1. The vast majority of properties don’t need a lot of backing code, hence the shortcut. It is an auto-implemented property that uses an expression lambdas. The only time you need to use the more verbose way is when you need to do some sort of validation before setting the value.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: