Skip to content

Prefer Explicit Declarations? #3

@brianmfear

Description

@brianmfear

I have lots of problems with this section, but I'll try to sum them up in this one issue.

First, I'd suggest that private should never appear in code. This is because (a) it's the default model, and (b) it's 8 characters counted against you each time you use it. A developer is expected to know the the class access model for any language they use. It's also "close enough" to the default Java type, minus the fact that we don't have packages.

Second, I'd suggest that "with sharing"/"without sharing" be used as sparingly as possible. This sounds counter-intuitive, doesn't it? After all, sharing/security is important, isn't it? Of course it is, and it also has an associated cost! Remember, the default model is "keep whatever mode you were in." Staying where you are has no cost. Entering or leaving "system mode" has a cost, even if you don't actually change modes. It's like shifting from fifth gear back into fifth gear over and over again, just to make sure you're in fifth gear. I was actually able to measure this cost, and it is small, but some function-heavy loops could have severe performance problems (16% or more CPU time).

So, "with sharing"/"without sharing" has two costs: character counts against you and CPU time to switch modes. This section should be expanded to exactly specify when "with(out) sharing" should be used. All controllers, extensions, rest service, and web services classes should use "with sharing" explicitly unless they are designed to do otherwise. All trigger classes and utility classes should use "default" sharing unless they are designed to do otherwise. No class should ever use "without sharing" unless you're explicitly trying to do something you couldn't normally do. This should be rare to non-existent.

Third, using this. I'd prefer that this was never, ever used, although the truth is, we do sometimes to because of scoping effects. Using this is the same usually the same thing as defining members to be private. It should be obvious, and if it's not, your functions are too big. They should usually fit on a screen. The only net effect of using this. is the waste of five characters per usage, which could easily become 15% of your total code base's character usage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions