Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created November 14, 2016 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmfear/c6a152ee5cab168f573c037ad1756497 to your computer and use it in GitHub Desktop.
Save brianmfear/c6a152ee5cab168f573c037ad1756497 to your computer and use it in GitHub Desktop.
Ternary Lazy Evaluation Demo (salesforce.com Apex Code)
@isTest class TernaryLazyEvalTest {
static Integer counter = 0;
static Boolean flag { get; set { counter++; flag = value; } }
static Boolean setTrue() {
return flag = true;
}
static Boolean setFalse() {
return flag = false;
}
@isTest static void unitTest() {
Boolean result = Math.random()<0.5?setTrue():setFalse();
// If counter is two, that means both sides of ternary were evaluated
System.assertEquals(1, counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment