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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Aug 05, 2001
Thanks a lot! Ilja and Stan. It really helps.
Joined: Jul 11, 2001
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.