Skip to main content

Code reviews rule: Method with global visibility

Written by David Martin
Updated this week

Method with global visibility

Why is this an issue?

The global access modifier exposes methods to any code, including code in other namespaces and managed packages. Once a method is declared global, it becomes part of your package's public API and cannot be removed or have its signature changed without breaking subscribers' code.

This creates long-term maintenance obligations:

  • You must maintain backwards compatibility indefinitely

  • Bug fixes may be constrained by the existing API contract

  • Subscribers may depend on undocumented behavior

Examples

Example of incorrect code when public would suffice:

global class AccountService {
global static void processAccount(Account acc) {
// Only used within this namespace
}
}

Example of correct code:

public class AccountService {
public static void processAccount(Account acc) {
// Accessible within the namespace, not locked into global API
}
}

How can I fix violations?

  1. Use public instead: For methods only called within your namespace, use public visibility.

  2. Review necessity: Only use global when the method must be accessible from other namespaces.

When should I disable this rule?

You may dismiss specific violations when global is used intentionally. If global is required, design the API with long-term maintainability in mind.

Resources

Did this answer your question?