What is "this" in Software Development?

Photo by Jp Valery on Unsplash

What is "this" in Software Development?

ยท

2 min read

Recently, I was having a discussion with a friend of mine while writing code, he glanced at my code and confessed that he hates "this". No, he wasn't talking about my code ๐Ÿ˜‚. Today we're going to make this fast, "this" is one of the many keywords used in software development.

In software development, Object-Oriented Programming to be more specific, we create a lot of classes. If you're recalling an article where I mentioned classes, you're most likely remembering classes with relation to frontend development. Classes when talking about backend development take on a whole different meaning (I just may write about it tomorrow ๐Ÿ˜‰).

A man showing a little girl images on a laptop

A relatable explanation

"this" is an intuitive keyword in the sense that it's used in roughly the same way we use the word real life. If you're at the office with a colleague and you're having a discussion with him/her about the architecture or features of the building, you're more likely to say "this building...", or "this building's...." rather than "Microsoft HQ building's...".

What you should pick up is that by virtue of being in the building, you no longer have to use a unique name or description for the building when interacting with other individuals in the same building as you. If you refer to the building as "this building" or the bathroom as "this building's bathroom" the person you're talking to most likely knows what and where you're referring to.

The software engineering keyword "this" works in the same way; when you're within a class, you don't have to call the class by name or create a new unit (popularly known as instantiation) of the class before you can refer to features (popularly known as properties) of the class. The keyword "this.{insert name of property}" is just a neater way of referring to and assigning value(s) to a property of a class from anywhere within the class.

Picture of a girl walking with balloons

Finally

It's important to add that the keyword "this" isn't a one-size-fits-all keyword and some programming languages may not be very optimized for it; an example is JavaScript. A number of developers will tell you to avoid using "this". More often than not, I believe strongly-typed programming languages that greatly support OOP (Object-Oriented Programming) will not give you any trouble with "this" ๐Ÿ˜‰. To read more about the implementation of "this" click here.