This rule ensures that any Apex class that use SOQL or SOSL declares its sharing mode using one of the keywords: with sharing, without sharing, or inherited sharing. Specifying the sharing level is crucial because it controls whether the class respects the current user’s record-level access permissions when accessing Salesforce data. Without an explicit sharing declaration, the default behavior can lead to unintended data exposure or security issues.
Note that exception classes and classes without any SOQL queries are exempt from this rule, as they do not directly access Salesforce records. Following this best practice ensures that your Apex code respects organizational security policies and prevents accidental data leaks.