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
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
Partner Zones Build AI Agents That Are Ready for Production
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
Partner Zones
Build AI Agents That Are Ready for Production

LIVE: “Cognitive Databases, Intelligent Data: Unified Infrastructure for Vector Search, AI-Optimized Queries, & Hybrid Workloads" Report

Join this live webinar to learn safer rollout techniques for schema changes, index testing, and database migrations.

Live Webinar: Exclusive practitioner summit on AI-powered CDN operations and real-world automation strategies

Related

  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • Boosting React.js Development Productivity With Google Code Assist
  • Why Angular Performance Problems Are Often Backend Problems

Trending

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • Grok AI API Tutorial: Chat, Image, Video, Tool Calling, and Web Search
  • Engineering Closed-Loop Graph-RAG Systems, Part 2: From Prompts to Rules
  • Engineering Closed-Loop Graph-RAG Systems, Part 3: Closing the Loop in Graph-RAG Systems
  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.

By 
John Vester user avatar
John Vester
DZone Core CORE ·
Nov. 22, 17 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
92.5K 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

  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • Boosting React.js Development Productivity With Google Code Assist
  • Why Angular Performance Problems Are Often Backend Problems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook