How do programmers practice code reuse

前端 未结 14 1275
天涯浪人
天涯浪人 2021-02-13 14:14

I\'ve been a bad programmer because I am doing a copy and paste. An example is that everytime i connect to a database and retrieve a recordset, I will copy the previous code and

相关标签:
14条回答
  • 2021-02-13 14:24

    Easy: whenever you catch yourself copy-pasting code, take it out immediately (i.e., don't do it after you've already CP'd code several times) into a new function.

    0 讨论(0)
  • 2021-02-13 14:25

    The first thing to note is that by using copy-and-paste, you are reusing code - albeit not in the most efficient way. You have recognised a situation where you have come up with a solution previously.

    There are two main scopes that you need to be aware of when thinking about code reuse. Firstly, code reuse within a project and, secondly, code reuse between projects.

    The fact that you have a piece of code that you can copy and paste within a project should be a cue that the piece of code that you're looking at is useful elsewhere. That is the time to make it into a function, and make it available within the project. Ideally you should replace all occurrances of that code with your new function, so that it (a) reduces redundant code and (b) ensures that any bugs in that chunk of code only need to be fixed in one function instead of many.

    The second scope, code reuse across projects, requires some more organisation to get the maximum benefit. This issue has been addressed in a couple of other SO questions eg. here and here.

    A good start is to organise code that is likely to be reused across projects into source files that are as self-contained as possible. Minimise the amount of supporting, project specific, code that is required as this will make it easier to reuse entire files in a new project. This means minimising the use of project specific data-types, minimising the use project specific global variables, etc.

    This may mean creating utility files that contain functions that you know are going to be useful in your environment. eg. Common database functions if you often develop projects that depend on databases.

    0 讨论(0)
  • 2021-02-13 14:25

    Try and get into the habit of using other people's functions and libraries.

    You'll usually find that your particular problem has a well-tested, elegant solution.

    Even if the solutions you find aren't a perfect fit, you'll probably gain a lot of insight into the problem by seeing how other people have tackled it.

    0 讨论(0)
  • 2021-02-13 14:29

    At first, create a library with reusable functions. They can be linked with different applications. It saves a lot of time and encourages reuse.

    Also be sure the library is unit tested and documented. So it is very easy to find the right class/function/variable/constant.

    0 讨论(0)
  • 2021-02-13 14:30

    Put the code into a routine and call the routine whenever you want that code to be executed.

    0 讨论(0)
  • It depends somewhat on what programming language you're using. In most languages you can

    1. Write a function, parameterize it to allow variations
    2. Write a function object, with members to hold the varying data
    3. Develop a hierarchy of (function object?) classes that implement even more complicated variations
    4. In C++ you could also develop templates to generate the various functions or classes at compile time
    0 讨论(0)
提交回复
热议问题