martinohanlon / shortcut

Shortcut is a cross platform command line application and API for creating shortcuts.

Home Page:https://http://shortcut.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove setters getters and insert Attributes docstrings

kiwi0fruit opened this issue · comments

Looks like setters and getters are redundant. It definitely possible to add attributes docstrings so they automatically go to documentation. For example see: source, docs.

Can you be more specific ? What getters and setters? ' desktop_directory, menu_directory? I dont understand why they are redundant.

@martinohanlon Yep, I talk about them. All attributes in Python are public. So we simply can use:

class ShortCutter:
    def __init__(self):
        self.desktop_directory = 'DESKTOP'

sc  = ShortCutter()
# And then change and access desktop_directory attr:
sc.desktop_directory = 'HELLO'

If you want private attributes you simply name them starting with underscore so other people know that they might be changed in future.

Getters and setters are implemented when you want to refactor your code but want to have the same attribute public API.

The other hackish way of using setters and getters is for automatic docstring documentation. But it's worthless if your documentation software understands something like this (and I bet most of them do understand indeed):

    def __init__(self, raise_errors=True, error_log=None):
        """
        Creates ShortCutter.

        :param bool raise_errors:
            Whether to raise exceptions or skip errors and continue.
        :param error_log:
            File object where to write errors when raise_errors=False.
            Default is `None` - do not write errors.
            Can also be `sys.stderr` or `io.StringIO()`.

        Attributes:
        -----------
        raise_errors : bool, default True
            Whether to raise exceptions or skip errors and continue
        error_log : object, default None
            File object where to write errors when raise_errors=False.
            Default is `None` - do not write errors.
            Can also be `sys.stderr` or `io.StringIO()`.
        desktop_folder : str
            Directory used when creating desktop shortcuts
        menu_folder : str
            Directory used when creating menu shortcuts
        bin_folder : str
            `Scripts` or `bin` dir path (the one to where setup.py installs)
        site_packages : str
            Site packages dir path (the one to where setup.py installs)
        """

It is best practice to provide @properties for public attributes. There is no downside to using them and even though there is no functional change happening within the property it future proofs.

@martinohanlon But redundancy can never be a good practice :-) See this.