Published December 5, 2024 | Version v2
Software Open

DPy: Code Smells Detection Tool for Python

  • 1. ROR icon Dalhousie University

Description

DPy is a static analysis tool designed to detect code smells in Python codebases. It identifies code smells belonging to implementation and design granularities.

Features

  • A comprehensive set of supported code smells: The tool detects many implementation and design smells.
  • Configurable thresholds: User may enable/disable the detection of a smell. Also, the used thresholds are customizable.
  • Detailed reporting: Provides detailed reports including the source code entity (such as package, module, class, and function), location, and detected smell type.
  • Export formats: Supports exporting results in JSON and CSV formats for easy integration with other tools and workflows.

Folder Descriptions

dit_analysis/

This folder includes the analysis and results of the Depth of Inheritance (DIT) for the top-10 Python projects analyzed with DPy.

DPy/

Contains executables for both Windows and Unix-like systems (e.g., Linux and Mac).

human_evaluation/

Datasets collected through manual evaluation efforts.

projects/

Contains information and results of projects used for human evaluation.

verbosity_conversion/

Includes the analysis and datasets for verbosity conversion between Python and Java.

Installation

This guide will help you add the DPy executable to your system's PATH so you can run it from any terminal or command prompt.

Windows Installation

  1. Move the Executable to a Directory:
    1. Copy the DPy/DPy-windows-v0.0.7.exe/DPy.exe file to a directory of your choice, such as C:<your_path>\DPy.

  2. Add the Directory to PATH:

    1. Press Win + R, type sysdm.cpl, and press Enter.
    2. Go to the Advanced tab and click on Environment Variables.
    3. Under System Variables, find the variable Path and select it, then click Edit.
    4. Click New and add the path to the directory where DPy.exe is located (e.g., C:<your_path>\DPy).
    5. Click OK to save and close all dialogs.

  3. Verify the Installation:

    1. Open a new Command Prompt (Win + R, type cmd, and press Enter).
    2. Type DPy and press Enter. If installed correctly, the program will execute.

Linux and macOS Installation

  1. Move the Executable to a Directory:
    1. Copy the DPy/DPy-ubuntu-v0.0.7/DPy file to a directory, such as /usr/local/bin or a custom directory like ~/DPy:

      bash cp DPy /usr/local/bin

  2. Make the File Executable (if not already):

    1. Run the following command:

      bash chmod +x /usr/local/bin/DPy

  3. Add the Directory to PATH (if using a custom directory):

    1. Open your shell configuration file (e.g., ~/.bashrc~/.zshrc, or ~/.profile) in a text editor:

      bash nano ~/.bashrc

    2. Add the following line to include the directory in the PATH:

      bash export PATH="$PATH:~/DPy"

    3. Save the file and reload the shell:

      bash source ~/.bashrc

  4. Verify the Installation:

    1. Open a new terminal and type DPy --version. If installed correctly, the program will execute.   

     Notes for macOS Users:

  • macOS often uses zsh as the default shell (since macOS Catalina). Make sure you update the correct shell configuration file (~/.zshrc).
  • If /usr/local/bin is not writable, you can use /opt/homebrew/bin if you have Homebrew installed, or create a custom directory in your home directory (e.g., ~/DPy).

Uninstallation

To remove DPy :

  • Windows:
    • Delete the executable from the directory where it was stored.
    • Remove the directory from PATH in the Environment Variables settings.

  • Linux and macOS:

    • Delete the executable from the directory (e.g., /usr/local/bin or your custom directory).
    • Remove the PATH entry from your shell configuration file if applicable.

Usage

Once installed, you may invoke the tool using analyze command. Use help option to see all the available options.

DPy analyze --help

The above command should result in an output something similar to the following.

│ *  --input    -i      TEXT  The path to the input Python file or directory for analysis. [default: None] [required]                                                                                                             │
│ *  --output   -o      TEXT  The path to the directory where results will be saved. [default: None] [required]                                                                                                                   │
│    --format   -f      TEXT  The output format for results, e.g., 'json' or 'csv'. [default: json]                                                                                                                               │
│    --config   -c      TEXT  The path to a custom configuration file. If not provided, defaults to 'default_config.json' in the script directory. [default: None]                                                                │
│    --log_dir  -l      TEXT  The directory to store log files. Defaults to the output directory if not specified. [default: None]                                                                                                │
│    --help                   Show this message and exit.   

Run the following command to analyze a Python project

DPy analyze -i /path/to/your/python/project -o /path/to/output/directory

Customization

You may customize the detection thresholds for smells and other settings by providing a JSON configuration file. An example partial json file is presented here.

{
  "Smells": {
    "BrokenHierarchy": {
      "enable": true
    },
    "BrokenModularization": {
      "enable": true,
      "min_fields": 5
    },
    "ComplexConditional": {
      "enable": true,
      "max_conditions": 2
    }
  }
}

Supported code quality metrics

Class/Module-level metrics

Along with the package name, module name, and class name, the tool provides the following code quality metrics for each class/module:

  • LOC: Lines of code
  • NOM: Number of methods
  • NOPM: Number of public methods
  • WMC: Weighted methods for class
  • NOF: Number of fields
  • LCOM: Lack of cohesion among methods
  • Fan-in: Incoming (class) dependencies
  • Fan-out: Outgoing (class) dependencies
  • DIT: Depth of inheritance

Method/Function-level metrics

Along with the package name, module name, class name, and function (or method) name, the tool provides the following code quality metrics for each method/function:

  • LOC: Lines of code
  • CC: Cyclomatic complexity
  • PC: Parameter count

Supported code smells

Design smells

The tool supports detection of the following design smells:

  • Multifaceted abstraction: This smell arises when an abstraction has more than one responsibility assigned to it.
  • Insufficient modularization: This smell arises when an abstraction exists that has not been completely decomposed, and a further decomposition could reduce its size, implementation complexity, or both.
  • Broken modularization: This smell arises when members of an abstraction are broken and spread across multiple abstractions.
  • Hub-like modularization: This smell arises when a class-level abstraction has dependencies with a large number of other class-level abstractions (high incoming as well as outgoing dependencies).
  • Broken hierarchy: This smell arises when a supertype and its subtype conceptually do not share an “IS-A” relationship, resulting in broken substitutability.
  • Wide hierarchy: This smell arises when an inheritance hierarchy is “too” wide and shallow, indicating that intermediate types may be missing.
  • Deep hierarchy: This smell arises when an inheritance hierarchy is excessively deep.
  • Rebellious hierarchy: This smell arises when a subtype rejects the methods provided by its supertype(s).

Some of the above smells, specifically multifaceted abstractioninsufficient modularizationhub-like modularization, and broken modularization, are also identified at the module level.

Implementation smells

The tool supports detection of the following implementation smells:

  • Complex conditional: A complex conditional statement.
  • Complex method: A method with high cyclomatic complexity.
  • Empty catch block: A catch block of an exception is empty.
  • Long identifier: An identifier with excessive length.
  • Long method: A method is excessively long.
  • Long parameter list: A method has a long parameter list.
  • Long statement: An excessively long statement.
  • Magic number: An unexplained number is used in an expression.
  • Missing default: A switch statement does not contain a default case.
  • Long lambda function: A long lambda function.
  • Long message chain: A large chain-like access to attributes of an object.

Files

rep_package_DPy.zip

Files (108.6 MB)

Name Size Download all
md5:43188413dfd37a54d149e51eea915124
108.6 MB Preview Download