The journey to deleting badly written lambda code
Key point
Through pair programming, lambda expression logic that hurt readability was refactored into simple if statements and clear method names.
Details
This covers the process of discovering that excessive use of lambda expressions and Optional in legacy code was hurting readability, and how it was improved. The existing code placed a burden on colleagues when making changes due to its complex pipeline flow and unclear variable names.
Refactoring Process
Through pair programming with a colleague, the logic was analyzed, and the following changes were made to improve readability.
- Method renaming: Removed the time-dependent 'old' and changed
isUsedOldSubscriptiontoisValidSubscription. - Method separation: Split the complex logic inside the lambda into 4 simple private methods such as
hasValidPeriodandisUsable. - Simplified validation flow: Applied the Early Return pattern to reduce the burden of remembering conditions, and organized all conditions into a single return statement connected with AND operations.
Improvement Effects
Although the number of lines increased, the simple if-statement structure reduced cognitive burden. Even new developers without domain knowledge can now easily understand the code, and this highlights reflection on habitually writing lambda expressions and the need to write code from a colleague's perspective.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.