How do programmers practice code reuse

前端 未结 14 1543
天涯浪人
天涯浪人 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:30

    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

提交回复
热议问题