too-many-public-methods / R0904#

Message emitted:

Too many public methods (%s/%s)

Description:

Used when class has too many public methods, try to reduce this to get a simpler (and so easier to use) class.

Problematic code:

class SpaceInvaders:  # [too-many-public-methods]
    def __init__(self):
        pass

    def fire_laser_beam(self):
        pass

    def deploy_shield(self):
        pass

    def launch_missile(self):
        pass

    def activate_super_laser(self):
        pass

    def summon_mothership(self):
        pass

    def destroy_planet(self):
        pass

    def teleport(self):
        pass

    def invoke_aliens(self):
        pass

    def invade_earth(self):
        pass

    def takeover_galaxy(self):
        pass

Correct code:

class LaserBeam:
    def __init__(self):
        pass

    def fire(self):
        pass

    def activate_super(self):
        pass

    def destroy_planet(self):
        pass


class Shield:
    def deploy(self):
        pass


class Missile:
    def launch(self):
        pass


class SpaceInvaders:
    def __init__(self):
        self.laser = LaserBeam()
        self.shield = Shield()
        self.missile = Missile()

    def summon_mothership(self):
        pass

    def destroy_planet(self):
        pass

    def teleport(self):
        pass

    def invoke_aliens(self):
        pass

    def invade_earth(self):
        pass

    def takeover_galaxy(self):
        pass

Configuration file:

[main]
max-public-methods=7

Additional details:

Having too many public methods is an indication that you might not be respecting the Single-responsibility principle (S of SOLID).

The class should have only one reason to change, but in the example the spaceship has at least 4 persons that could ask for change to it (laser manager, shield manager, missile manager, teleportation officer...).

Related links:

Created by the design checker.