DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • A Comprehensive Guide To Working With JSON in JavaScript
  • The Advantage of Using Cache to Decouple the Frontend Code
  • How I Used Swift Script in Electron Browser Natively
  • Comprehensive Guide on Integrating OpenAI ChatGPT API With React JS

Trending

  • Querydsl vs. JPA Criteria, Part 5: Maven Integration
  • How To Deal With AWS’s Ban on Reserved Instances Resale
  • Demystifying Databases, Data Warehouses, Data Lakes, and Data Lake Houses
  • You’re Wasting Time With Your Daily Standup
  1. DZone
  2. Coding
  3. JavaScript
  4. Using Self-Modifying Code in JavaScript

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.

John Vester user avatar by
John Vester
DZone Core CORE ·
Nov. 22, 17 · Tutorial
Like (10)
Save
Tweet
Share
91.4K Views

Join the DZone community and get the full member experience.

Join For Free

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!

JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • A Comprehensive Guide To Working With JSON in JavaScript
  • The Advantage of Using Cache to Decouple the Frontend Code
  • How I Used Swift Script in Electron Browser Natively
  • Comprehensive Guide on Integrating OpenAI ChatGPT API With React JS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: