Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reusing code?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I�m hoping that some of you clever chaps could offer me some advice on code reuse.

You see, whenever I make applications, I typically only find very limited scope for code reuse. I also only find limited use for inheritance. For example, the various types of users that my system might have to deal with.

I�m wondering if anyone could give me some tips on how to identify areas of my design that could be reusable, where I can use inheritance and where to use interfaces. I�ve read all the books that talk about why these things are important but very few of them show you how to do it in the real world.

If anyone could point me to some online resources on this then that would be great. I could also do with a couple of books on how to spot reusable aspects of my designs and so on.

Any help would be great

Thanks all

Simon
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the OO, Patterns, UML, and Refactoring forum, where they love to discuss topics such as this...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find some very general principles for OO design, including the use of inheritance and interfaces, at http://www.objectmentor.com/resources/listArticles?key=topic&topic=Design%20Principles

Robert C. Martin's "Agile Software Development - Principles, Patterns and Practices" is also very good.

Regarding reuse, in my experience the best way to get code reuse is to find logical duplication in the code, refactor it so that it becomes physical duplication and then remove that duplication by extracting the duplicated code to a place from where it can be reused. Martin Fowler's "Refactoring" book is a classic in that area.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you ever in the situation where you're tempted to copy and paste some code, perhaps changing around some variable names? Almost all of those situations are opportunities for code reuse. Here's what I do whenever I'm tempted to copy and paste:

1. Look in the appropriate place for an existing function or class that will do the job, or at least part of the job.

2. If there's no such function or class, extract the copied code into such a function or class, rather than pasting it. Put it in the appropriate place identified in step (1).

I always find that the majority of my code soon migrates to locations from which it can easily be reused.

In my experience, small to medium projects - say less than about 100,000 lines - don't offer a huge number of opportunities for reuse by inheritance, especially in a single inheritance language like Java.
 
Simon Harvey
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Thanks for that guys

Simon
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two of the papers on this page http://www.surfscranton.com/architecture/JavaPages.htm (Law of Demeter and Knight's Principles) both drive you toward designs that will improve reusability. Many of the patterns and principles we all argue about, um, discuss cordially, are focused on reducing dependencies, which is key to enabling reuse.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic