Wednesday, 1 January 2014

Code Optimization : C#

Code Optimization by Amit Rajput





Favor Polymorphism over Enums for Behavior



Three classes called polymorphically to avoid switching logic


Be declarative, use linq to objects




Table Driven Methods





Fail Fast




Watch out for Flag Arguments




Fail Slow




Try Catch Body StandAlone




Primitive Obsession




Redundant Comments



Intent Comments




Favor Polymorphism over Enums for Behavior



Three classes called polymorphically to avoid switching logic



Be declarative, use linq to objects





Table Driven Methods






Fail Fast





Watch out for Flag Arguments





Fail Slow





Try Catch Body StandAlone





Primitive Obsession





Redundant Comments




Intent Comments





Refactor: Register user method has 153 lines of code. Code smell.




Refactor: Remove commented out code and unnecessary comments




Refactor: Move variables as close as possible to line of first use





Refactor: Rename variables to convey meaning and intent





Refactor: validation logic, guard clauses in separate method






Refactor: Complex boolean logic in new method and return early






Refactor: Foreach in several methods SRP






Refactor: Out logic in table and grab values from database


Refactor: Remove try catch, let application fail and exception bubble up if not saved in database




Refactor: RegisterUser Method down to 16 lines


Refactor: Factor out Validation logic in new method




Refactor: RegisterUser now consists of 7 lines from the initial 153




Refactor: The Validation Method




Refactor: Data validation and checking




Refactor: Preference validation




Refactor: The outline reads like a cohesive story


Conclusion

  • Code is improved and easier to read and faster to understand
  • Room for improvement
  • Relies on throwing exceptions, status codes could be used
  • Some methods do more than one thing
  • There are some hard coded values
  • Opportunities for further extraction exist


Bug Hunting in this code base

  • Old structure 1 monolithic method, new structure 7 small cohesive and focused methods
  • Find what you are looking for faster since well named method names and intermediate variables
  • Able to quickly jump into the right spot to fix the bug and methods are terse, expressive and do one thing
  • Find the right piece of code to change in a minimum time
  • Outline rule allows to speed read through the class and easily maintain and fix bugs


Code Metrics RegisterUser Method enhanced




Conclusions

  • Don't overdo it, every method can be transformed in a 1 liner
  • The signal to noise ratio has been improved
  • Removed comments and whitespace, renamed the methods and variables
  • Refactored to well named methods that are focused and do one thing
  • Created self documented code
  • Understand the intent and what the code is doing just by reading it
  • Automated tests for regression testing not to forget to implement


The Path Forward

  • If it isn't broke don't fix it. Don't go wild refactoring all and every code using these techniques
  • When to refactor code
    1. Code currently working with
    2. If code is hard to read but has been chugging along in a system for years, don't risk changing it merely in a desire for cleanliness
    3. Fix the bug and move on, excessive refactoring of old code is of questionable value if size of change is small
    4. Size of refactoring should be inline with size of code change making
    5. Before making changes ensure test coverage so no regressions introduced
  • Accept No Broken Windows
  • Toxic mindset, once people see a building has been disrespected, much more likely to treat it in the same manner
  • Take pride in your code.
  • Take pride in code base and not let mistakes of the past open the door to take on ever more technical debt.


Code Reviews and Pair Programming

  • Review work to ensure maintaining high level quality and following policies
  • Promote proactive cleanliness
  • Set clear guidelines
  • Work in pairs is a real time code review, increases code quality


Always leave the code you are working on a little bit better than you found it. Boy Scouts Rule

  • If we continually leave the code base better than we found it, code bases will not need big, scary refactorings
  • Code bases will stay clean
  • Share your knowledge and write your code with pride and love
  • Knowledge is like love, the more you give the more you get
  • In this sense, happy coding, happy clean coding!

No comments:

Post a Comment