The Worst Ruby Codes I've Seen in My Life
In this presentation by Fernando Hamasaki de Amorim at RubyKaigi 2015, the focus is on identifying and analyzing poor Ruby code practices, particularly those stemming from WOP (Workaround-Oriented Programming). While Ruby is generally celebrated for its clarity and elegance, Hamasaki points out that the misuse of workarounds can lead to significant code issues and complexity.
Key Points Discussed:
Definition of WOP: WOP refers to the use of workarounds and patches in coding, often arising from advanced problems in software development.
Experienced Programmers at Fault: The examples presented are drawn from the work of seasoned developers, illustrating that even those with experience can create problematic code.
Example of Poor Code - Credit Card Masking: A method to mask credit card numbers is criticized for its complex implementation, as it modifies the original variable instead of simply returning the masked version. This can lead to unintended consequences in other parts of the application.
Rails Controller Flow: A Rails controller's action for domain creation is highlighted for its convoluted logic. The presentation suggests simplifying the flow to prioritize readability by discarding complex error handling paths.
Confusing Class Design: A poorly constructed Ruby class is discussed, particularly its constructor which requires certain keys in a hash. Hamasaki recommends using the fetch method for clearer error handling and improved design, advocating for clearer definitions of class methods to avoid confusion.
Naming Conventions: Misleading method names are critiqued, exemplifying how expectations may not align with actual functionality, resulting in potential misuse of methods.
Conclusions and Takeaways:
Hamasaki emphasizes the importance of clarity in Ruby programming. Good practices should include avoiding WOP, utilizing clear naming conventions, and establishing design structures that promote straightforward logic flows. By learning from examples of bad code, developers can improve their craft and create cleaner, more maintainable applications.