Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Self-Modifying Code in JavaScript

DZone's Guide to

Using Self-Modifying Code in JavaScript

For the Thanksgiving holiday, Zone Leader John Vester demonstrates how thankful he is JavaScript providing the ability to write self-modifying code.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

When I started working in Information Technology (over 25 years ago), I met a guy named Dave who had been at the large insurance company (where I accepted employment) for a few years.  Dave's job focused on program code supporting a core product called "Individual Life Insurance."  If you bought a personal life insurance policy from an agent representing this company, Dave's code was running in the background to support the needs of that business line.

One of our initial conversations was regarding a task he had recently completed.  That task was to remove all the self-modifying code that was running for years on the mainframe system which handled the individual life insurance family of products. For those who are not aware, self-modifying code is defined by Wikipedia as:

Self-modifying code is code that alters its own instructions while it is executing - usually to reduce the instruction path length and improve performance or simply to reduce otherwise repetitively similar code, thus simplifying maintenance. - Wikipedia

In the case Dave was removing, self-modifying code was used to alter the program logic at run-time to maximize the efficiency of the available memory on the mainframe. I believe I remember my boss telling me they had only 8k of RAM on the mainframe when it was purchased, years before I cared about computer technology.

So, for this Thanksgiving holiday, I thought I would provide an example of using self-modifying code using the JavaScript language.

JavaScript Example

Thursday, November 23, 2017, is the day when the United States celebrates Thanksgiving.  A majority of corporations recognize this as a holiday, providing employees the opportunity to be thankful for one's blessings. With this in mind, the following JavaScript can be created:

selfModifyingCode = function() {
   var turkeyDay = new Date("November 23, 2017 01:00:00");
   var date = new Date();

   if (date.getDate() === turkeyDay.getDate()) {
      selfModifyingCode = function() {
         console.log("Eat some turkey");
      };
   } else {
      selfModifyingCode = function() {
         console.log("Go to work");
      };
   }

   selfModifyingCode();
};

selfModifyingCode();

When the selfModifyingCode function is initialized, the value is equal to the following function:

function selfModifyingCode() {
   var turkeyDay = new Date("November 23, 2017 01:00:00");
   var date = new Date();

   if (date.getDate() === turkeyDay.getDate()) {
      selfModifyingCode = function() {
         console.log("Eat some turkey");
      };
   } else {
      selfModifyingCode = function() {
         console.log("Go to work");
      };
   }

   selfModifyingCode();
}

When the function is called via the selfModifyingCode() command, the two date variables (turkeyDay and date) are set and then evaluated to see if the date versions are equal.

If the two dates match (which means today is Thanksgiving), the selfModifyingCode function is re-written to be as noted below:

function selfModifyingCode() {
   console.log("Eat some turkey");
}

If the two dates do not match (today is not Thanksgiving), the selfModifyingCode function is re-written to be as follows:

function selfModifyingCode() {
   console.log("Go to work");
}

As a result, when program logic calls the  selfModifyingCode()  function, the results will be different - depending on the date in which the program is called.

Conclusion

Self-modifying code can be an effective way to handle logic that needs to be evaluated at run-time.  However, it is recommended that use is restricted to only cases where it makes sense.  

Back when my former employer bought their mainframe, they had to use self-modifying code to maximize the amount of memory available for processing requests. Once the memory issue was resolved, my friend Dave was assigned the task to remove this logic, because it was difficult to support and maintain.

The same would be true with the simple example above. There are much better ways to handle the end-result that I provided in the examples above. Certainly, it would be confusing for someone to support my example, where every day of the year (except one), the function code within the selfModifyingCode() function is the same.

Have a really great day!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
javascript ,web dev ,self-modifying code ,web application development ,web app performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}