Use fully qualified names for System Types in Apex

Illia Leshchuk
Sep 18, 2023

Whenever you’re dealing with System types, use fully qualified names to avoid accidental or intentional name shadowing.

public void finish(Database.BatchableContext context)
{
if (!complete && !Test.isRunningTest())
{
System.Database.executeBatch(this);//chaing batch execution
}
}

The code above is good and working, but it can easily be broken if someone introduces a new class with the name Test (yes, it’s legit). And you can consider yourself lucky if the code will just start throwing “Method does not exist or incorrect signature: void isRunningTest() from the type Test” But imagine someone accidentally or intentionally implemented System.Test “interface” in the custom class — this could lead to elusive and unpredicted behaviour. All of this could’ve been avoided just by using fully qualified name System.Test.isRunningTest() instead.

PS: those are legit class names in Apex from the top of my head:

  • Test
  • JSON
  • Database

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Illia Leshchuk
Illia Leshchuk

Written by Illia Leshchuk

A salesforce developer/architect

No responses yet

Write a response