Break down large, complex methods into smaller, focused methods with clear names that describe their purpose. This improves code readability, maintainability, and testability.
Break down large, complex methods into smaller, focused methods with clear names that describe their purpose. This improves code readability, maintainability, and testability.
When methods grow too long or handle multiple concerns:
For example, instead of:
def wrapper_script
# 50+ lines of complex code with slight variations
if new_resource.use_inline_powershell
# inline powershell version
else
# regular shell out version
end
end
Refactor to:
def wrapper_script
if new_resource.use_inline_powershell
inline_powershell_wrapper_script
else
shell_out_wrapper_script
end
end
def inline_powershell_wrapper_script
# inline powershell implementation
end
def shell_out_wrapper_script
# regular shell out implementation
end
Or when logic is repeated:
def resolved_package(pkg)
new_resource.anchor_package_regex ? "^#{pkg}$" : pkg
end
This approach reduces cognitive load, improves reusability, and makes code easier to understand and modify.
Enter the URL of a public GitHub repository