Implement configurable programming paradigm checks and suggestions that can guide developers to write code following specific paradigms, with initial focus on functional programming.
Problem Statement
Currently, Sourcery focuses on general code quality improvements but doesn't provide paradigm-specific refactoring suggestions. Developers who want to follow specific programming paradigms (functional, object-oriented, etc.) have to manually identify and refactor code patterns, which can be time-consuming and inconsistent across teams.
Add a configuration option to enable paradigm-specific analysis and suggestions, starting with functional programming. This would include:
Functional Programming Checks:
Detect and suggest replacing loops with map(), filter(), reduce()
Identify iterative loops and suggest tail-recursive alternatives
Flag non-tail recursive functions and suggest tail-recursive refactoring
Identify mutable data structures and suggest immutable alternatives
Flag side effects in functions and suggest pure function alternatives
Recommend function composition over nested function calls
Suggest replacing class-based solutions with function-based ones where appropriate
Identify opportunities for currying and partial application
Loop to Tail Recursion:
# Before - Iterative
def factorial(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
def sum_list(lst):
total = 0
for item in lst:
total += item
return totalSourcery Suggestion:
def factorial(n, acc=1):
return acc if n <= 1 else factorial(n - 1, acc * n)
def sum_list(lst, acc=0):
return acc if not lst else sum_list(lst[1:], acc + lst[0])
Benefits
Consistency: Enforce coding standards across teams
Learning: Help developers learn paradigm-specific patterns including recursion
Performance: Tail recursion can be optimized by interpreters/compilers
Code Quality: Improve maintainability through paradigm adherence
Stack Safety: Promote tail recursion to avoid stack overflow issues
Flexibility: Configurable per project needs
Object-Oriented Programming paradigm checks
Procedural programming guidelines
Custom paradigm definitions
Team-specific paradigm rules
Recursion depth analysis and optimization suggestions
Educational: Teaching functional programming and recursion concepts
Team Standards: Enforcing consistent coding paradigms
Legacy Refactoring: Migrating codebases to different paradigms
Code Reviews: Automated paradigm compliance checking
Performance Optimization: Converting inefficient recursion to tail recursion
Warn about Python's recursion limit (default 1000) when suggesting recursive solutions
Provide options to use functools.lru_cache for memoization when appropriate
Consider suggesting iterative approaches when recursion depth could be problematic
This feature would significantly enhance Sourcery's value for developers and teams who want to maintain consistent programming paradigms while leveraging automated refactoring suggestions, with special emphasis on proper recursive patterns.
Please authenticate to join the conversation.
In Review
Feedback
7 months ago

andres@aemonge.com
Get notified by email when there are changes.
In Review
Feedback
7 months ago

andres@aemonge.com
Get notified by email when there are changes.