Set, Get and Destroy

Java was a beautiful programming language for me to learn. I still love the way it forced me to write the neat and organized code, though I didn’t like the idea of writing extra  lines just to write a “Hello world” and also the performance of the language. I think, it became popular because of Android adoption and will fade in upcoming years because of legal disputes and strategical changes of Google to replace it with some other language like Kotlin or Dart.

Among a lot of beautiful things from Java, one common goodie is setter-getters. I assume, you are familiar with the idea of setter-getters by now.
In case you are not- it is just a function allows us to set some value to a variable, which can be later retrieved using getter function.

Getter-setter is not the only way to data passing, but I believe its most commonly used among them. Obviously getter-setters don’t serve to all problems and others approaches have their significance in the case.

Anyway, in Java, a normal Class could have Constructor, Getter, Setter and Destructor. Every method/function is self-explanatory in general-
Constructor, to initializes required vars. Setter sets values to vars of Class. And Getters are used to retrieve the values of vars of the Class. And finally Destructor to clean up the vars, you don’t want to keep the vars in memory after you are done using it.

I used Java mostly in my college days for Android app development and class projects. Now, I have been using Javascript from more than 1 year now for mainstream development as working in a software firm.

Javascript, is not OOP and doesn’t have Classes like Java, if you know. But get-set is just an approach and developers use it a lot in there. Recently while working on some memory critical task, I had have few observations regarding this.
Javascript has Constructors inbuilt, but it lacks proper Destructors. I don’t mean it because Class approach is only not available in JS and it makes sense of not having Destructor then.

But, as a developer we might need it, if you use get-set. I believe it’s must to destroy all set vars if your framework is not taking care of it.

We, in company, use AngularJS in frontend and NodeJS in backend. While angular has destroy method and it takes care of scope vars but there is no such things in Nodejs inbuilt. More, if you set something globally, you need to remove it after you finish with it.

I find get-set everywhere but Destructors are not there for efficient uses. It was a complete combo in Java as they are designed, but in Javascript we need to do it manually.
So you do destroy the data after you are done with it, otherwise those set vars will be in memory and Javascript won’t collect it for garbage because you have set it globally in JS file.

Approach is simple…
Set when you start controller.
Get as per need of the code.
Destroy when controller ends its task.
Or your code will end up storing a lot of unnecessary data in memory and will be slowing down the system.

And it can lead to worse things if your code is memory critical.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *