Skip to main content

Performance benefits of declaring Apex variables as Final and dynamic dispatch .

Dynamic dispatch is the process of selecting which implementation of a polymorphic method to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems. 

Polymorphism in object-oriented programming is the phenomenon where objects of the class expose an operation of the same name but possibly differing in behavior. Dynamic dispatch contrasts with static dispatch, in which the implementation of a polymorphic operation is selected at compile time. The whole purpose of dynamic dispatch is to defer the selection of an appropriate implementation until the run time type of a parameter is known.

A polymorphic operation has several implementations, all associated with the same name. With dynamic dispatch, one particular implementation of an operation is chosen at run time. 

In apex programming methods and classes are final by default. You cannot use the final keyword in the declaration of a class or method. This means they cannot be overridden. Use the virtual keyword if you need to override a method or class. The virtual definition modifier allows extension and overrides. 

Use final when you know that a declaration does not need to be overridden.

The final keyword is a restriction on a class, method, or property that indicates that the declaration cannot be overridden. This allows the compiler to safely elide dynamic dispatch indirection. 

@ Overriding:
  • It is an object-oriented programming that enables the child class to provide a different implementation for a method that is already implemented in its parent class.
  • This is possible through only inheritance
  • Multiple methods containing the same name, the same signature is inherited (virtual) and another is originated (override) in the child class
Lets run a example of dynamic dispatch :

A class that extends another class inherits all the methods and properties of the extended class.  The extending class can override the existing virtual methods by using the override keyword in the method definition. Overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behavior of a particular method is different based on the object you’re calling it on. This is referred to as polymorphism.

A class extends another class using the extends keyword in the class definition. A class can only extend one other class, but it can implement more than one interface.

This example shows how the YellowMarker class extends the Marker class. To run the inheritance examples in this section, first create the Marker class.



Then create the YellowMarker class, which extends the Marker class.



This code segment shows polymorphism. The example declares two objects of the same type (Marker). Even though both objects are markers, the second object is assigned to an instance of the YellowMarker class. Hence, calling the write method on it yields a different result than calling this method on the first object, because this method has been overridden. However, you can call the discount method on the second object even though this method isn’t part of the YellowMarker class definition. But it is part
of the extended class, and hence, is available to the extending class, YellowMarker. 

Execute anonymous code illustrations : 


Performance benefits of declaring Apex variables as Final 

The discussion around the topic, declaring Apex variables as Final would fetch good performance benefits is something I would like to show with code examples and their execution out put . 

Please carefully examine the code output results, it shows declaring variables as Final would run faster when compared to non final variables . 

These are not benchmark results by salesforce but test done on 3 different orgs and test output exhibit similar output pattern . 

Below code executed in anonymous Apex


Output :



Conclusion : 

The result did not add a ton of processing time in any individual call when run for every thousand calls. But using final here does provide a tangible, measurable benefit, however small.


Comments

Popular posts from this blog

TrackWise Digital - DMS Application - How to Override and make changes to locked EFFECTIVE Document using Flows and Invocable method.

Document management systems (DMS)   is one of the known solutions pharma organization use to manage their internal quality processes and store/ track all documentation related to quality. By using cloud-based product we have advantages over documents getting lost, outdated version rendered to stakeholders, irregular audit tracking like who made the changes and when. These qualities are necessary for good quality software as part of internal adulting process. The document are uploaded into the DMS system by DMS administrators. The document must go through a pre-defined workflow review process before it is made EFFECTIVE and made available for end users for training reference. The workflow lifecycle for DMS workflow approval cycle includes a) Draft, b) In Review, c)Under Approval and d) EFFECTIVE. Once the document is EFFECTIVE, the document is locked for further editing. The document is later used for internal training and can be printed and shared with internal users for performing...

TrackWise : E-Signature doesn't work

If you are on Trackwise Digital [TWD] Platform Summer 23 or highier and come across an error ' E-Signature doesn't work ' for username / password with OAuth connected app . Check the Login History of the user and confirm you see the error ' Username-Password Flow Disabled ' . If this happens then you need to enbale the ' OAuth Username-Password Flows ' which will be disabled by default for Summer 23 platform or highier . Steps to enable : 1. Setup ==> search ==> OAuth and OpenID Connect Settings 2. Enable - Allow OAuth Username-Password Flows Reference https://lnkd.in/giVuzGbs

Salesforce Rebate Management Tool – Streamline Your Rebate Process

Salesforce Rebate Management tool is a powerful tool for businesses to streamline their rebate process. It helps businesses to manage their rebate program more efficiently by automating the entire process. The tool helps businesses to provide rebates to their customers based on their purchases, which ultimately helps businesses to increase their sales and revenue. In this article, we will discuss the benefits of using Salesforce Rebate Management tool and how to set up benefit mapping. Benefits of using Salesforce Rebate Management Tool 1. Automate your rebate process – The tool automates the entire rebate process, from creating rebate offers to calculating payouts, which saves time and eliminates manual errors. 2. Increase customer loyalty – By offering rebates to your customers, you can increase their loyalty and encourage repeat purchases. 3. Improve sales performance – The tool helps businesses to increase their sales performance by incentivizing their sales team to sell more pro...

TrackWise Digital : ' The post install script failed ' Salesforce Error

A Document Management System (DMS) is Sparta TWD managed package application which is used to manage all corporate document with activities such as create, edit, upload, and manage the corporate documents. The application comes with pre-defined workflow process like review and approval process which could be used to make document effective before the document is made available to all the business users. The TrackWise Digital Platform application must be installed for DMS to work, and TWD platform application must be installed prior to installing DMS application. With Salesforce has three major releases per year, Sparta also releases 3 releases in a year with major enhancements and features. With such releases more and more features are made available to their customers without impacting business. With DMS packaging being upgraded thrice a year there could be possibility the administrator could face one of the known errors during DMS package upgrade in salesforce orgs which is:...

Salesforce Introduces ‘Hire Me’ Button to facilitate Hiring in Salesforce ecosystem.

Salesforce’s new Hire Me button will help simplify how Trailblazers hire and get hired in the Salesforce Ecosystem. Individuals will be able to indicate interest in new work opportunities on their Trailblazer.me Profile, while hiring managers will be able to simply click on the Hire Me button to message and connect with potential job candidates. It’s the quick, easy, and straightforward way to discover and connect with Salesforce experts. The introduction of the Hire Me button will empower Trailblazers to identify as job seekers and to connect with organizations that need their expertise, and to make it easier for hiring managers to connect with potential job candidates.” The initiative by Salesforce is to prepare 20,000 new Trailblazers for jobs in the Salesforce ecosystem by 2028. To begin with displaying a badge to get Hired by prospective employers who can see I'm available for work and directly message you about work opportunities , follow below steps . 1. Login to your trail...