liveBook · Manning

Feature envy

As explained in Practices of the Python Pro

Feature envy is a code smell in which a component relies heavily on the features of another component instead of encapsulating the relevant behavior itself.

Feature envy is a code smell: one component performs several tasks mainly by using another component’s features. It behaves as though it wants to be that other component. section 10.2.1

Recognize and reduce feature envy by:

  • Noticing code that directly calls several operations from another module or class. section 10.2.1 section 10.3
  • Moving the related behavior to a single entry point in the component that owns it. section 10.2.1
  • Trusting the abstraction’s interface afterward and using tests to maintain confidence in hidden behavior. section 10.2.1

Query cleaning and bookmark commands

Query-cleaning logic originally required the consumer to call several search functions. Moving it into search.clean_query lets callers request a cleaned query without knowing the internal steps. In Bark, direct calls such as db.add('bookmarks', data) show feature envy alongside tight coupling. section 10.2.1 section 10.3

Figure 10.4. Feature envy from one class to another

Use the figure to inspect feature envy between classes. section 10.2.1

How Practices of the Python Pro covers it

How deep it goes
Defines the smell, shows its query-cleaning form, and prescribes a single-entry-point refactoring.
Examples
Uses query cleaning, DatabaseManager calls, AddBookmarkCommand, and a class-to-class relationship figure.
What it assumes
Assumes code organized into modules or classes and introduces the smell through query-cleaning and command examples.
What you can do after
Diagnose feature envy and refactor behavior behind an entry point owned by the relevant component.
How it connects
Connects feature envy with loose coupling, encapsulation, abstraction, tight coupling, and leaky abstraction.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.