Skip to content

Latest commit

 

History

History
77 lines (58 loc) · 2.32 KB

File metadata and controls

77 lines (58 loc) · 2.32 KB

Delete Dead Code

This example demonstrates how to identify and remove dead code from a codebase using Codegen. The script efficiently cleans up unused functions and variables, helping maintain a lean and efficient codebase.

Note

Dead code refers to code that is not being used or referenced anywhere in your codebase. However, some code might appear unused but should not be deleted, such as test files, functions with decorators, public API endpoints, and event handlers.

How the Dead Code Removal Script Works

The script (run.py) performs the dead code removal in several key steps:

  1. Codebase Loading

    codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON)
    • Loads a codebase using the Codebase.from_repo method
    • This example uses the tox-dev/tox repository because it is mostly self-contained
  2. Function Removal

    for function in codebase.functions:
        if "test" in function.file.filepath:
            continue
        if function.decorators:
            continue
        if not function.usages and not function.call_sites:
            print(f"🗑️ Removing unused function: {function.name}")
            function.remove()
    • Skips test files and decorated functions
    • Removes functions with no usages or call sites
  3. Variable Cleanup

    for func in codebase.functions:
        for var_assignments in func.code_block.local_var_assignments:
            if not var_assignments.local_usages:
                print(f"🧹 Removing unused variable: {var_assignments.name}")
                var_assignments.remove()
    • Iterates through local variable assignments
    • Removes variables with no local usages

Running the Script

# Install Codegen
pip install codegen

# Run the script
python run.py

Example Output

� Deleting dead code...

🗑️ Removing unused function: _get_parser_doc
🧹 Removing unused variable: decoded
🧹 Removing unused variable: shebang_line
...
🧹 Removing unused variable: _

🔧 Total functions removed: 2
📦 Total variables removed: 240

Learn More

Contributing

Feel free to submit issues and enhancement requests!