Working Effectively With Legacy Code
by scidept
- 2,682 views
Rails Conf 2009 presentation by BJ Clark and Pat Maddox of the science department on Working with Legacy Rails Code
Rails Conf 2009 presentation by BJ Clark and Pat Maddox of the science department on Working with Legacy Rails Code
Accessibility
Categories
Upload Details
Uploaded via SlideShare as Apple Keynote
Usage Rights
© All Rights Reserved
Statistics
- Likes
- 5
- Downloads
- 0
- Comments
- 0
- Embed Views
- Views on SlideShare
- 2,625
- Total Views
- 2,682
You could have well-designed code with no tests... “clean room”...falcon circling the sky then strikes
Bad design
Dependencies (Rails makes you not care. Stuff like const_missing is great but hides pain points, association chains)
Systems become large, it’s important for them to be designed and architected such that you can reason about subsystems.
Eventually, programmers want the Big Rewrite. We’ve both advocated for the Big Rewrite on a project that hadn’t even launched yet.
- Pros: we know them
- Cons: Locked into Ruby (issue when you need to have multithreaded code). Opinionated - when your business expands past those opinions, must pay down debt. Example: AR assumes one database. Need to write libraries/rearchitect to support clustering
*** Choosing mediawiki
- Pros: Easy to get the site up and running
- Cons: Difficult to extend, difficult to scale
- Result: Spent a year+ replacing it piece-by-piece with Rails
*** Using ActiveScaffold
- Pros: Get scaffolding quickly and easily
- Cons: Internal code is a mess, untested, difficult to extend
*** Code you write
- Not refactoring / writing tests
- Poorly tested code is almost as bad as not testing at all.
one database server => multiple database servers (requires community to create new tools. no clear migration path out of the box)
*** We know that doesn't actually work
*** Plus you don't have working software
* Agile approach
** Do simple things to add value right now
** Technical debt is central to Agile development - embrace it
- Testing allows you to refactor
- Refactoring pays down debt
- Virtuous cycle => Testing makes refactoring possible, refactoring makes testing easier
** RATFT
- Antipattern: red/green deploy Just because you have tests for your 70 line controller method, doesn't mean it's good or that you're done.
- red/green/REFACTOR deploy
- Get to green, take the time to make your code nice. You should spend equal or more time refactoring than making your tests green.
AboutUs: 2-3 deployments per day. No staging.
A. Acceptance (originally called functional)
Cuke:
- Uses full rails stack.
- Tests multiple requests in a single test.
- Hits multiple models and controllers, session, external services, etc.
* What's the point?
-1 cucumber test covers the same amount of code as 25 unit tests
- Level of abstraction - reasoning about usage of the system, as opposed to one tiny little piece out of context
- Captures existing system functionality
Characterization tests - let you know how the system behaves currently. May even expose bugs but you don’t fix them just yet! Make a note or a story in tracker and move on.
** Pushback
- Writing tests is too hard. No it's not
- No, really, it is too hard
- Push it behind a webservice. Write simple integration tests. Example: AboutUs uses mediawiki as a parsing engine. Easy to write Rails-app level tests for transformations, then push it off to mediawiki service
- Don’t touch it after you silo it.
- Rails 3: mountable apps
** Write your own -Pivotal's authorization system (can_create, can_update auto-called from controller)
** Extraction is a very valid refactoring technique.
* Got behavior for free
* If they learn this, they’ll start to write code this way
* It “just worked”
We realize that legacy controllers aren’t even this clean.
We never said it would be easy.
Refactor your code, then you can make it sex
This is the goal
You can use a framework
Code is tougher to understand due to indirection
Account and project focused on domain responsibilities
AccountRegistration provides natural point for stuff like sending a verification email (also helps with testing)
AccountRegistration can get sophisticated without muddling model - validates_associated project and video, if you want
You could write a script to clean up AccountRegistration records when they’re no longer needed, depending on domain
** Magic number (california sales tax)
** coupled to TaxCalculator implementation
* Every Java programmer asks “what library to use for DI”
* Ruby programmers say “don’t use it”
* Misses the point. Don’t use a framework. Use Ruby
Understand how has_many works - it’s not magic!
This lets you be very creative and have fun
Working Effectively...gives you concrete strategies for getting a code base under test