diff --git a/examples/delete_dead_code/README.md b/examples/delete_dead_code/README.md new file mode 100644 index 0000000..6a05064 --- /dev/null +++ b/examples/delete_dead_code/README.md @@ -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! diff --git a/examples/delete_dead_code/run.py b/examples/delete_dead_code/run.py new file mode 100644 index 0000000..888cc40 --- /dev/null +++ b/examples/delete_dead_code/run.py @@ -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)