NAM108: a method that repeats its class name¶
Junior¶
At the call site, the object already says what it is. config.read_config() says "config" twice, and config.read() says the same thing.
class Config:
def read_config(self): # NAM108: repeats "config"
...
def read(self): # the call reads config.read()
...
To fix it, drop the class name from the method name.
Senior¶
The repeat makes every call site longer and hides the part of the name that varies. In a class with user_name, user_email and user_id, the words that matter come last.
The whole class name has to appear in the method name as a run of words. CodeContext.update_code_context matches, ConfigLoader.config_path doesn't.
It stays quiet on:
- test modules and
Test*classes. - private methods.
- a method named exactly like its class, such as
Filter.filter. - a class where three or more methods repeat its name. That is a namespace by design, like tkinter's
Wm.wm_titleandWm.wm_frame.
This is the check that most needs the allowlist. A user's name and a user name are different things, and Style.background_style returns a different style from its own. Only the author knows which repeat is a slip:
Expert¶
Class and method names are split into lowercase words on underscores and on case changes, so HTTPClient gives http, client. The method matches when the class's word list appears as a contiguous run in the method's. The three-method threshold (_PREFIX_CONVENTION) reads the sibling names from Context.siblings, which the visitor fills per class.
Its corpus rate is moderate (0.35 per thousand in CPython, 4.64 in click), but its precision is the lowest of the eight. Many findings are compound nouns where the repeat is meaningful. The allowlist entry is Class.method, so it never silences the same name in another class. It is the check most likely to be cut if calibration doesn't improve.