• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Reusable code

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,
Our teacher always told us to write code that could be reused in another application. 99% of the code I write has absolutely nothing to do with eachother. Meaning I can't reuse any code at all. Do you experienced programmers reuse alot of code? Or should I give up on writing reusable code? It takes a bit more time to make it reusable but is it worth it?
 
Jerry Olsson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually came to think of one thing I made reusable that I might actually use. I wrote a Database Handler to connect to a MSSQL database. I've only used it once though.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always do b/c there will be that time where it really needs to be used by something else and of course that will be the one piece that isn't. I also use alot of patterns that make it easy to reuse code
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's great for teachers to talk about reusable code but, in the classroom, you seldom reuse much of anything. One of the reasons for that is that each assignment focuses on something entirely different. It's hard to reuse code that is so vastly different from what you want to do.

In the "real world," however, that isn't quite the case. If you work for a logistics company, you're going to be writing business logic that deals with logistics. In fact, a lot of people taht work with you will be writing similar logic. As everyone is focused on the same sorts of tasks, it's only natural that you'll find code that can be reused. In cases such as that, the benefits can be tremendous.

Just because you won't be reusing the code you have now doesn't mean you shouldn't get in the habit of writing code that can be reused. If nothing else, it often teaches you to decouple systems properly.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Component-level reuse never really took off as it was supposed to. I've read many papers about reuse stating that the only type of reuse that's common in the software industry is low-level reuse, i.e. individual classes and that sort of granularity in general.
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's still worth trying to write reusable code. Where I work, we try to make all of our back end components reusable (by giving them a clear API), but we don't actually package them for reuse until someone actually wants to resuse them. This minimizes some of the extra work associated with making code reusable.

And of course the low level reuse that Lasse mentioned. But that's easier to put in place.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd be happy to see more reuse within a single project. I see too many people cut & paste a 10-line method or retype it with subtle name changes over and over rather than find a good home for it in a utility class. Ouch!

The things that you have to do to make something reusable are pretty solid ideas all the time: make classes with a single responsibility and minimal dependencies.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen code reused so many times in the real world. For example, Look at all the libraries available from apache commons. In our applications we extend a number of classes thereby reusing tried and tested code. Reducing chances of bugs inherently present in untested code. I do not support the notion that reuse is non existent. Hope this helped.
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, remember 2 things.

1. Do not think about reuse till you start doing your first cut & paste. The moment you see yourself typing something that you did earlier (or copying from the earlier work) implies that you need to separate that piece of work and package it separately.

To take this further, I suggest that you read Reuse Release Equivalence Principle by Robert Martin as reference. Basically, it suggests that whenever a piece of code is being used by 2 programs (applications/classes), then the code should exist in its own package. That way, it can be managed without any issues. (Don't worry if you don't get it at first. But remember it. You will realize the impact when you use it).

2. Do not think about reuse until reuse is actually needed. This is a variation of the first, but applies more to framework creation and refactoring. Long time back, I read something called 'Three Example Rule'. What it said was,
a. For your first application, don't think about common packages/libraries/frameworks.
b. For your second application that has something in common with the first, do some refactoring and try to use some classes, but don't still create frameworks/libraries.
c. For your third application that has something in common with the first two, start creating frameworks by extracting common code.

This was reiterated by Kent Beck in his TDD book also.

Hope these points help.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic