Skip to content

ChuckJonas/wtfapex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 

Repository files navigation

What the f*ck, Apex?

A list of funny and tricky Apex examples

Inspired by wtfjs

When a boolean is not a boolean

Boolean b;
if(b!=true) system.debug('b is not true');
if(b!=false) system.debug('b is not false');

See Advanced Apex Programming in Salesforce for explanation.

String compare is case-insensitive (except when it's not)

String x = 'Abc';
String y = 'abc';
System.assert(x == y); // passes
System.assertEquals(x, y); // fails

See explanation on StackExchange

Shadowing System (global) classes

Nothing prevents you from recreating a class with the same name of one that exists in the System (default) namespace.

public class Database {
    public static List<sObject> query(String qry){
        System.debug(qry);
        return null;
    }
}

Running Database.query('foo') will call our new class (essentially overriding the Database methods)!?

The same principle also applies to standard SObjects:

public class Account { }

Account acc = new Account();
acc.AccountNumber = '123'; // Variable does not exist: AccountNumber

Source: Daniel Ballinger

"Phantom" Inner Class Type Equivalency

public class IExist{}
System.assertEquals(IExist.class, IExist.IDont.Class); //passes

Source: Kevin Jones

Exceptions are "exceptional"

In their naming conventions:

public class OhGodBees extends Exception{}
//fails: OhGodBees: Classes extending Exception must have a name ending in 'Exception'

and their Constructors:

public class BeesException extends Exception{
    public BeesException(){}
}
//fails: System exception constructor already defined: <Constructor>()

For explanation and further interesting observations, see Chris Peterson's blog post.

System can have ambiguous return types

Database.query is one of many cases where the Salesforce System namespace doesn't play by its own rules. It can return either a List<SObject> or a single SObject. No casting required.

Foo__c foo = Database.Query('SELECT Id FROM Foo__c');
List<Foo__c> foos = Database.Query('SELECT Id FROM Foo__c');

Try writing your own method to do this and you'll get an error:

Method already defined: query SObject Database.query(String) from the type Database (7:27)

You can overload arguments, but not return type.

Local Scope Leak

If you write an If/Else without braces, symbols scoped in the "if" seem to leak into the "else":

if(false)
   String a = 'Never going to happen.';
else
   a = 'I should not compile';

Worth noting that Java won't even allow you to declare a block scoped variable inside a "braceless IF" as it can never be referenced elsewhere.

Source: Kevin Jones

Broken type inference for Set<>

Let's take a look at the standard Set class...

It can be iterated in a foreach loop:

Set<String> mySet = new Set<String>{'a', 'b'};
for(String s : mySet){}

But, according to Salesforce (compiler & runtime), it does not actually implement the Iterable interface:

String.join(mySet, ','); // Doesn't compile! "Method does not exist or incorrect signature: void join(Set<String>, String)..."

// Just to make sure, lets check at runtime..
System.assert(mySet instanceof Iterable<String>);  // Yup, this fails! I guess Set really isn't an Iterable...

Except... It actually does:

String.join((Iterable<String>) mySet, ','); // this works!?

Vote to fix this

Fun with Hashcodes

Enums in batch

Objects in hashcodes

JSON Serialization

  1. There's no way to control automatic serialization of object properties (like [JsonProperty(PropertyName = "FooBar")] in C#)
  2. There are reserved keywords that you can't use as property names.

Meaning the following cannot be parsed or generated using JSON.deserialize or JSON.serialize:

{
   "msg": "hello dingus",
   "from": "Dr. Dingus"
}

Work-around

Generics (parameterized interfaces) exist, but you can't use them

Once upon a time, generics were actually part of Apex. However, they have since been removed (with the exception of system classes (List<>, Batchable<>, etc).

Why would you want generics when your OS has perfectly good Copy & Paste functionality built right into it?

Vote for Generics

Initializing Abstract Classes

public abstract class ImAbstract {
    public String foo;
}

ImAbstract orAmI = (ImAbstract) JSON.deserialize('{"foo":"bar"}', ImAbstract.class);
System.debug(orAmI.foo);

See Stack Exchange Post

Polymorphic Primatives

Object x = 42;
System.debug(x instanceOf Integer); // true
System.debug(x instanceOf Long);  // true
System.debug(x instanceOf Double);  // true
System.debug(x instanceOf Decimal); // true

Source: Daniel Ballinger

Since Fixed

Thankfully, these WTFs have since been fixed by Salesforce. We'll keep them documented for historical purposes (and entertainment).

Mutating Datetimes

https://twitter.com/FishOfPrey/status/869381316105588736

More hashcode fun

https://twitter.com/FishOfPrey/status/1016821563675459585

https://salesforce.stackexchange.com/questions/224490/bug-in-list-contains-for-id-data-type

About

A list of funny and tricky Apex examples

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •