This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maximize your code reusability

 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
can any one give some tips on how to maximize your code reusability?
Thanks,
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer has since the 1970s been high cohesion, low coupling. Easier said than done. How do you create goodness? How do you measure success?
Looking for those answers, I recently stumbled across a Great Little Paper by Robert Martin that explains Open for extensibility, Closed for modification as well as I've ever seen, and adds a graph of abstractness vs stability. Things built on the "main line" contribute to reusability; others are potential problems. See if "Uncle Bob's" ideas help.
BTW: Back when I was a "seeker" (knows there is a problem, has no clue what the solution is) my own measure of success for reusability was: How complex are the instructions? The ideal is "Call this, it will give you an answer." The reality was more like "set up five files, call three programs in sequence, wave a dead chicken over the keyboard, call five methods in sequence ..." I guess I still use this measure - if I look at the docs for something and don't get it in a couple minutes, I move on.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to OO and Patterns because Junilu is MAKING me
< topic swiper!! >
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Liu:
can any one give some tips on how to maximize your code reusability?

Here are some thoughts:
- write unit tests for your code, preferably first. By writing unit tests for your classes, they already are used by at least two clients - the tests and the production code. Writing tests for your code will therefore help you to make your code easier to use in different places.
- write small methods/classes. The more focused your units are, the more likely they can be reused in a different context.
- don't tolerate duplication. If you have two snippets of code which are similar in structure and/or behaviour, you have missed an opportunity to reuse. By refactoring the commonality in its own place, you often get more general pieces of functionality which later can get reused in other places.
Did that help?
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! Ilja and Stan. It really helps.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Looking for those answers, I recently stumbled across a Great Little Paper by Robert Martin that explains Open for extensibility, Closed for modification as well as I've ever seen, and adds a graph of abstractness vs stability. Things built on the "main line" contribute to reusability; others are potential problems. See if "Uncle Bob's" ideas help.

And if it does help, you might want to consider the book "Agile Software Develoment - Principles, Patterns and Practices" by the same author.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic