[SalesForce] Invalid Class/Method identifier, is this a bug

I was writing some unit tests just now, and was lazily creating more test methods when I ran across an error on trying to save my unit test.

> Compiling testOLIUpdateManualRollups.cls
> Compile Failed
> testOLIUpdateManualRollups.cls: Invalid identifier: testRollupNew_MRR_Total__c (Line: 364)

To make it very clear, I was attempting to create a new method called testRollupNew_MRR_Total__c

There's more to the test class that I'm writing, but the relevant Apex is simply

static testMethod void testRollupNew_MRR_Total__c(){
    // test as of yet to be written
}

Now, I know that the keywords/reserved words can't be used alone as identifiers for classes, methods, or variables ('test_Integer_result' and the like are valid identifiers), but why isn't an identifier ending in __c valid for methods?

I can't find anything on this restriction in the documentation for method declaration, and a cursory googling doesn't turn up anything either.

Upon further testing, it appears that __c cannot appear in any portion of the method name. Underscores at the start of an identifier also appear to be verboten, though this is legal in Java, and I can't find any documentation on that restriction either.

This strikes me as a either an oversight in the formal grammar for the Apex language or a bug in Salesforce's compiler (perhaps transpiler might be more accurate?). I can't see any situation where the compiler should expect a custom field identifier anywhere in a method declaration, so I'm at a bit of a loss here.

I'll be working around this by removing the __c from the method name. Just wondering if anybody else can confirm this or can offer an explanation.

Best Answer

Two consecutive underscore characters in Apex are used to separate namespace of a managed package from the inner name inside of a managed package.

This is the reason why they are forbidden in Apex Class names, methods, variables and properties.

Related Topic