Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions examples/delete_dead_code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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**
```python
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**
```python
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**
```python
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

```bash
# 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

- [Deleting Dead Code](https://docs.codegen.com/tutorials/deleting-dead-code)
- [Codegen Documentation](https://docs.codegen.com)

## Contributing

Feel free to submit issues and enhancement requests!
44 changes: 44 additions & 0 deletions examples/delete_dead_code/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import codegen
from codegen import Codebase
from codegen.sdk.enums import ProgrammingLanguage


@codegen.function("delete-dead-code")
def run(codebase: Codebase):
removed_functions_count = 0
removed_variables_count = 0

for function in codebase.functions:
# Skip test files
if "test" in function.file.filepath:
continue

# Skip decorated functions
if function.decorators:
continue

# Check if the function has no usages and no call sites
if not function.usages and not function.call_sites:
print(f"🗑️ Removing unused function: {function.name}")
function.remove()
removed_functions_count += 1

# Clean up unused variables
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()
removed_variables_count += 1

print("\n")
print(f"🔧 Total functions removed: {removed_functions_count}")
print(f"📦 Total variables removed: {removed_variables_count}")


if __name__ == "__main__":
print("🔍 Analyzing codebase...")
codebase = Codebase.from_repo("tox-dev/tox", programming_language=ProgrammingLanguage.PYTHON, commit="b588b696e0940c1813014b31b68d7660d8a1914f")

print("🚮 Deleting dead code...")
run(codebase)