tags:

views:

122

answers:

4

Each of our programs has something in common,

which is a waste of time and mind.

It's fine when the common part is simple,but not always the case.

Has anyone ever thought of this kind of question?

The goal is:

When we do coding,we only do the new,say,the part not similar with any part done before.

If there is a good solution,it'll be a great relief for many programmers!

+5  A: 

This is the point of any library, programming language, or design pattern: to abstract away solved problems so they don't have to be solved again.

Of course, there will always be new, more complex problems to solve. And people disagree about the right way to solve them. So there's a lot more to be done.

Matthew Flaschen
+3  A: 

There are plenty of solutions, ranging from gotos, to functions, to classes, to modules or components, to libraries. All of these allow you to reuse code.

Even the simplest hello world app makes heavy use of it. You don't have to write all the output functionality yourself, you can make use of the language's standard library, and the OS routines for printing text to the screen.

Even a primitive language such as C offers a printf function for printing text to the screen, for example, so that you don't have to write it yourself.

jalf
Excellent answer. Good one remembering the always hated GOTO, the only way of code reuse I had available back on my SINCLAIR ZX81
tekBlues
+4  A: 

Yes, I thought and think about that every single day - the solution is not simple but it is attainable: always try to factor out what is common among your code into reusable artifacts. That is the solution, but it doesn't happen by accident - it is a daily effort.

Otávio Décio
A: 

Code reuse is the ideal, of course, but there are many practical obstacles that get in the way. For example (thinking primarily of library reuse here):

  1. You might not know about the existing functionality, or know if it meets your needs. Sometimes the time needed to find out about reusable functionality is greater than the time needed to implement it yourself.

  2. The existing code may differ slightly from what you need. Sometimes the differences may not be apparent until you are well into trying to reuse the code.

  3. The existing code may have bugs that only become apparent when used in your application (a special case of #2). Debugging into other people's code is often a real challenge, particularly if you don't have modifiable source available.

  4. The existing code may come with license restrictions that are inappropriate for your project as a whole.

  5. The existing code may come with a lot of dependencies on other libraries and code, that bloat your executable, make it more fragile, or restrict your ability to deploy it to some environments.

  6. The existing code may conflict with other libraries you also want to link with your application.

dewtell