代码可重用性:值得吗?

时间:2021-12-08 11:40:53

We all write reusable classes and code.

我们都编写可重用的类和代码。

We factor in configurability to allow us to reuse this fantastic new class again and again.

我们考虑到可配置性,允许我们一次又一次地重用这个梦幻般的新类。

We tell our bosses that spending this extra time now will save us time and money later.

我们告诉我们的老板,现在花这个额外的时间会节省我们的时间和金钱。

But in reality, for those of us who don't write third party libraries, and spend our time working on a application as a whole, how many times does that one class you spent extra time writing to be reused actually get reused in another project?

但实际上,对于我们这些不编写第三方库的人,并将我的时间花在整个应用程序上,你花费额外时间编写重用的那一课实际上会在另一个项目中重复使用多少次?

How many bespoke classes do you have in your library which will get used in more than one project?

您在图书馆中有多少个定制课程,可以在多个项目中使用?

18 个解决方案

#1


28  

My common rule of thumb is:

我的常见经验法则是:

  1. If you repeat it once, copy it.
  2. 如果您重复一次,请复制它。

  3. If you repeat it twice, refactor it.
  4. 如果你重复两次,重构它。

#2


18  

Great question!
I think "designing for re-use" is the wrong way around. I find that the code that I write to be working, clean and pretty is reusable. Actual design for re-use only happens the first time that code actually gets re-used!

好问题!我认为“重复使用设计”是错误的方法。我发现我写的代码工作,干净和漂亮是可重用的。重复使用的实际设计仅在代码第一次实际重新使用时发生!

Spending time beforehand trying to make something reusable tends to be a waste of time, because you never know what will need to be re-used.

花时间预先尝试使某些东西可以重复使用往往是浪费时间,因为你永远不知道需要重新使用什么。

That being said, at my job we have a collection of libraries (large, 500MB or more) that gets re-used for almost every project - mostly domain-specific stuff.

话虽这么说,在我的工作中,我们有一系列库(大型,500MB或更多)可以重新用于几乎每个项目 - 主要是特定于域的东西。

#3


11  

leppie wrote:

My common rule of thumb is:

我的常见经验法则是:

  1. If you repeat it once, copy it.
  2. 如果您重复一次,请复制它。

  3. If you repeat it twice, refactor it
  4. 如果你重复两次,重构它

And I would add, make sure that a comment is added to both parts of the code to indicate the duplication in case there's a bug. You don't want to fix it in one part and not the other (BTDTGTT).

我想补充一点,确保在代码的两个部分都添加了注释,以指示在出现错误时重复。您不希望将其固定在一个部分而不是另一个部分(BTDTGTT)。

Rob

#4


3  

I'm no expert in XP methodology (or any methodology), but I think the YAGNI principle could be applied here.

我不是XP方法(或任何方法)的专家,但我认为YAGNI原则可以在这里应用。

Only modify the code for reuse when you have to reuse it.

只有在必须重用时才修改代码以便重用。

#5


2  

There is a difference between configurability and reusability- the former can be extremely useful in a lot of different situations, when the environment changes or whatever else- making things configurable in the way I understand it, is mostly a case of separating code and data- that's just good practice really.

可配置性和可重用性之间存在差异 - 前者在许多不同情况下非常有用,当环境发生变化或其他任何事情时 - 以我理解的方式配置事物,主要是分离代码和数据的情况 - 这真的是一种很好的做法。

Designing for reusability is only really useful if you're creating something you are planning to use as a library for multiple projects. As the years go by I have become increasingly aware of the YAGNI principle and these days I simply aim to write clean and robust code for the task at hand. My experience has been that if something is going to be reused it is very unlikely that you will predict exactly how it will need to be reused, so it's much better to only add the code that you need for now. That way if you need to reuse it in future you can add new stuff that does exactly what you need without having to break any existing functionality that you wrote in the past trying to anticipate how you might need it now.

如果您正在创建计划用作多个项目的库的内容,那么设计可重用性才非常有用。随着岁月的流逝,我越来越意识到YAGNI的原理,现在我只是想为手头的任务编写干净而强大的代码。我的经验是,如果要重用某些东西,你不太可能准确预测它将如何被重用,所以最好只添加你现在需要的代码。这样,如果您将来需要重复使用它,您可以添加新功能,完全满足您的需求,而不必破坏您过去编写的任何现有功能,试图预测您现在可能需要它。

You may find that after doing this a couple of times you have a library that is stable and robust and doesn't need you to change it because it actually does all the things you need- to me it's much easier to allow that to happen in that evolutionary way than to waste too much time guessing about the future.

您可能会发现,在执行此操作后,您有一个稳定且强大的库,并且不需要您更改它,因为它实际上完成了您需要的所有事情 - 对我来说,更容易实现这一点。这种进化方式比浪费太多时间猜测未来。

#6


2  

I think that the best approach is to try and design code with good interfaces and separation of responsibilities between classes without worrying too much about reuse. But at least if you design this way you're leaving open the possibility of reuse. A good rule of thumb is to ask yourself "if I come back to this code after 3 months, will I understand it and could I extend it if I had to?"

我认为最好的方法是尝试设计具有良好接口的代码并在类之间分离责任,而不必过多担心重用。但至少如果你这样设计,你就可以重新开放。一个好的经验法则是问自己“如果我在3个月后回到这个代码,我会理解它吗?如果必须的话我可以扩展它吗?”

IMO one of the worst practices in the industry is when a team are given the go-ahead to go off and write their own "reusable" framework....

IMO是业内最糟糕的做法之一,当团队获得批准并开始编写自己的“可重用”框架时....

#7


2  

I like the LISP model where you are continually extending the language. Eventually you wind up with a domain-specific language for your problem domain. Not that I actually write any lisp, but in the languages I use most often---Lua and C right now---I usually pull something out into a module and reuse it rather than clone and modify.

我喜欢LISP模型,你不断扩展语言。最终,您最终会使用针对您的问题域的特定于域的语言。并不是说我实际上写的是任何口齿不清,但在我最常使用的语言中 - 现在是Lua和C--我通常会将某些东西拉出来并重新使用它而不是克隆和修改。

For a C programmer, the quintessential example of this approach is Dave Hanson's bookC Interfaces and Implementations. Dave took every reusable idea he'd had in writing three or four compilers and put them all in a book---and the software is free. Fabulous stuff. Now if I write C code and I want to use it a second time, I make an interface in Hanson style. Some things I did this term: 2-dimensional arrays, 2-dimensional arrays with blocking, 2-dimensional bitmaps, readers and writers for pbmplus files, and so on. With this infrastructure in place it was easy to write a program I'd been wanting for years, which is to remove black edges from scans of photocopies of book pages.

对于C程序员来说,这种方法的典型例子是Dave Hanson的书籍C接口和实现。戴夫在编写三四个编译器时考虑了他所有的可重用的想法,并将它们全部放在一本书中 - 而且该软件是免费的。很棒的东西。现在如果我编写C代码并且我想再次使用它,我会用Hanson风格制作一个界面。我做过这个术语的一些事情:二维数组,带阻塞的二维数组,二维位图,pbmplus文件的读者和编写器等等。有了这个基础设施,很容易编写我多年来一直想要的程序,即删除书页复印件扫描的黑边。

So I agree with whomever said when you want to reuse it, pull it out---but not before.

所以我同意任何人在你想要重复使用时所说的,把它拉出来 - 但不是之前。

#8


2  

IMHO, certain code is likely to be reused often, and it makes sense to prepare it for frequent reuse. Other code isn't, and probably doesn't need to be developed beyond solving the immediate problem.

恕我直言,某些代码很可能经常被重用,并且为经常重用做好准备是有意义的。其他代码不是,并且可能不需要开发除了解决当前问题。

Of course, do be aware that telling the difference is NP-hard. :)

当然,要注意区分是NP难的。 :)

#9


1  

If you're sure you're not going to need it again, don't bother. Not even if you think it might come in handy. Refactor it when you really need it again...

如果您确定不再需要它,请不要打扰。即使你认为它可能会派上用场。当你真的需要它时重构它...

However, not making it reuseable is no excuse for not making it transparent. Whenever I write code as transparently as possible, it always turns out to be 99% reusable already...

然而,不能使其可重复使用并不是不使其透明化的借口。每当我尽可能透明地编写代码时,它总是可以重复使用99%......

#10


1  

Once you get above the level of technical utilities I have seen very little actual reuse in the real world.

一旦达到技术实用程序的水平,我在现实世界中看到的实际重用率很少。

If you think about it the reasons are clear. Say the widget_bodger application contained 90% of the functionality you require than you would just add the missing functionality to the application.

如果你仔细想想,原因很清楚。假设widget_bodger应用程序包含您需要的90%的功能,而不是将缺少的功能添加到应用程序中。

Or say the business admired a really cool "beep" function in the widget_bodger and wanted it incorporated into the gernerate_executive_expenses application. Ah resuse you might think, but then you dig into the code and find that GEE application is one of the oldest apps in the company, is written in C, must run on highly available hardware and the only thing that is resuable is the basic algorithm.

或者说,该业务在widget_bodger中羡慕一个非常酷的“嘟嘟”功能,并希望将其整合到gernerate_executive_expenses应用程序中。你可能会想到重新使用啊,但是你深入研究代码并发现GEE应用程序是公司中最古老的应用程序之一,用C语言编写,必须在高可用性硬件上运行,唯一可以恢复的是基本算法。

#11


1  

There are very different opinions on what makes code reusable. I'd say your time well spent making the code clear and well-factored (i.e. separation between responsibilities).

关于什么使代码可重用,有不同的意见。我说你的时间花在了清晰和明确的代码上(即职责分离)。

A side benefit from this is better reusability. The main benefit is making the code easier to understand, change and debug.

这样做的另一个好处是更好的可重用性。主要好处是使代码更易于理解,更改和调试。

To wrap it up. Don't do complex configuration schemes, extension points and events just to make it reusable. Try to find the right moving parts so that the code can be composed in response to new needs.

把它包起来。不要做复杂的配置方案,扩展点和事件,只是为了使它可重用。尝试找到正确的移动部件,以便可以根据新的需求组合代码。

#12


1  

Often "reusable" code is code that is abstracted and modularized, and, to me, the main benefit is not reusability but rather increased testability. Because when you isolate and modularize code it usually becomes more testable. Reuse is a convenient but often unused side effect.

通常“可重用”代码是抽象和模块化的代码,对我而言,主要的好处不是可重用性,而是提高了可测试性。因为当你隔离和模块化代码时,它通常变得更容易测试。重复使用是一种方便但通常未使用的副作用。

And on another note, Juval Lowry advocates interface-based programming since he maintains that interfaces are the sole component of reusability. Anything else has implied functionality (hard to re-use) while interfaces define a contract only which is implicitly reusable.

另一方面,Juval Lowry提倡基于接口的编程,因为他认为接口是可重用性的唯一组成部分。其他任何东西都隐含了功能(难以重复使用),而接口只定义了一个隐式可重用的契约。

#13


0  

I agree with you insofar as there is no point in coding in a way that makes the class easy to use outside of the current application. Most if us don't do this and in business environments there is no need to. If another application needs this functionality at a later date then extracting and commonalising the code can be done as part of that second project, and management are likely to agree with this viewpoint.

我同意你的意见,因为编码没有任何意义,使得课程在当前应用程序之外易于使用。大多数如果我们不这样做,并且在商业环境中没有必要。如果另一个应用程序在以后需要此功能,则可以作为第二个项目的一部分来提取和共享代码,管理层可能会同意这一观点。

However it is good practice to make your code reusable within the constraints of the current application. Refactor your code so as to avoid duplication within your current scope of work. This way you make it easier to work with at a later date. Don't so much code for reuse as for change, change is far more common.

但是,最好在当前应用程序的约束下使代码可重用。重构您的代码,以避免在您当前的工作范围内重复。这样您就可以在以后更轻松地使用它。没有那么多的代码可以重用和变更,变化更为常见。

#14


0  

One of the reasons why SOA has been failing, or hasn't lifted of yet, is that it's difficult to reuse a service: either it's too specific and it can't be used elsewhere, or too generic (and usually very complex) and doesn't meet the needs of different clients.

SOA失败或尚未解除的原因之一是,重用服务很困难:要么太具体而且不能在其他地方使用,要么过于通用(通常非常复杂),不符合不同客户的需求。

This ain't "code reuse", it's "service reuse", but there are some common concepts.

这不是“代码重用”,它是“服务重用”,但有一些共同的概念。

#15


0  

My 2c is that the ethic of code-reuse needs to be a company thing, not just a project thing. This means that when you start a new project, the primary concern is "which other projects can I steal code from to get this job done as well and as quickly as possible?".

我的2c是代码重用的伦理需要是公司的事情,而不仅仅是项目的事情。这意味着当你开始一个新项目时,主要关注的是“我可以从哪个其他项目中窃取代码以尽快完成这项工作?”。

This WILL conflict with the question of "what's the best - or trendiest - language/tool for the job?"

这将与“什么是最好的 - 或最潮流的 - 工作的语言/工具”这个问题相冲突?

Companies that have this approach end up with a pool of engineers that can easily switch from project to project, because language, framework and internal code base are all consistent.

采用这种方法的公司最终会拥有一个工程师库,可以轻松地从一个项目切换到另一个项目,因为语言,框架和内部代码库都是一致的。

The down-side is that switching to a "new" language or framework is much more politically difficult, even though it WILL need to happen at some point.

缺点是转换为“新”语言或框架在政治上更加困难,即使它需要在某个时刻发生。

#16


0  

Another benefit with reuse is that you can easiliy track where things happens in the oode base. if you got a million lines of code it can take hours to find all places where the application behaves in certain way. With a modern IDE you can just click "find references" and you'll find, within a couple seconds, all places where your component/method is used. This can be useful when you want add new features, fix bugs or just want to learn how the system works.

重用的另一个好处是,您可以轻松跟踪oode基础中发生的事情。如果您有一百万行代码,则可能需要数小时才能找到应用程序以某种方式运行的所有位置。使用现代IDE,您只需单击“查找参考”,您就可以在几秒钟内找到使用组件/方法的所有位置。当您想要添加新功能,修复错误或只是想了解系统的工作方式时,这非常有用。

#17


0  

We have a few, but what we have many more of are projects with the functionality you want but done in their own way, so more often than not you end up cannibalising features from old projects and reimplementing them in your new one. I'd argue that this still counts - you're still getting the benefit of having written the code before, and you still save the time.

我们有一些,但我们有更多的是具有您想要的功能但以自己的方式完成的项目,所以通常最终会从旧项目中蚕食功能并在新项目中重新实现它们。我认为这仍然很重要 - 您仍然可以获得以前编写代码的好处,而且您仍然可以节省时间。

Besides, I've found that it's only the second time that you try and use the functionality that it becomes obvious where the reusability is, and if you try and generalise something speculatively you almost always get it wrong and have to change it next time.

此外,我发现这是你第二次尝试使用它在可重用性方面显而易见的功能,如果你试图通过推测性地推广一些东西,你几乎总是弄错了,下次必须改变它。

#18


0  

Like other posters have said if you are going to copy the code then re-use, you will be glad you did this once you start debugging your copied code.

像其他海报一样,如果您要复制代码然后重复使用,一旦开始调试复制的代码,您会很高兴这样做。

#1


28  

My common rule of thumb is:

我的常见经验法则是:

  1. If you repeat it once, copy it.
  2. 如果您重复一次,请复制它。

  3. If you repeat it twice, refactor it.
  4. 如果你重复两次,重构它。

#2


18  

Great question!
I think "designing for re-use" is the wrong way around. I find that the code that I write to be working, clean and pretty is reusable. Actual design for re-use only happens the first time that code actually gets re-used!

好问题!我认为“重复使用设计”是错误的方法。我发现我写的代码工作,干净和漂亮是可重用的。重复使用的实际设计仅在代码第一次实际重新使用时发生!

Spending time beforehand trying to make something reusable tends to be a waste of time, because you never know what will need to be re-used.

花时间预先尝试使某些东西可以重复使用往往是浪费时间,因为你永远不知道需要重新使用什么。

That being said, at my job we have a collection of libraries (large, 500MB or more) that gets re-used for almost every project - mostly domain-specific stuff.

话虽这么说,在我的工作中,我们有一系列库(大型,500MB或更多)可以重新用于几乎每个项目 - 主要是特定于域的东西。

#3


11  

leppie wrote:

My common rule of thumb is:

我的常见经验法则是:

  1. If you repeat it once, copy it.
  2. 如果您重复一次,请复制它。

  3. If you repeat it twice, refactor it
  4. 如果你重复两次,重构它

And I would add, make sure that a comment is added to both parts of the code to indicate the duplication in case there's a bug. You don't want to fix it in one part and not the other (BTDTGTT).

我想补充一点,确保在代码的两个部分都添加了注释,以指示在出现错误时重复。您不希望将其固定在一个部分而不是另一个部分(BTDTGTT)。

Rob

#4


3  

I'm no expert in XP methodology (or any methodology), but I think the YAGNI principle could be applied here.

我不是XP方法(或任何方法)的专家,但我认为YAGNI原则可以在这里应用。

Only modify the code for reuse when you have to reuse it.

只有在必须重用时才修改代码以便重用。

#5


2  

There is a difference between configurability and reusability- the former can be extremely useful in a lot of different situations, when the environment changes or whatever else- making things configurable in the way I understand it, is mostly a case of separating code and data- that's just good practice really.

可配置性和可重用性之间存在差异 - 前者在许多不同情况下非常有用,当环境发生变化或其他任何事情时 - 以我理解的方式配置事物,主要是分离代码和数据的情况 - 这真的是一种很好的做法。

Designing for reusability is only really useful if you're creating something you are planning to use as a library for multiple projects. As the years go by I have become increasingly aware of the YAGNI principle and these days I simply aim to write clean and robust code for the task at hand. My experience has been that if something is going to be reused it is very unlikely that you will predict exactly how it will need to be reused, so it's much better to only add the code that you need for now. That way if you need to reuse it in future you can add new stuff that does exactly what you need without having to break any existing functionality that you wrote in the past trying to anticipate how you might need it now.

如果您正在创建计划用作多个项目的库的内容,那么设计可重用性才非常有用。随着岁月的流逝,我越来越意识到YAGNI的原理,现在我只是想为手头的任务编写干净而强大的代码。我的经验是,如果要重用某些东西,你不太可能准确预测它将如何被重用,所以最好只添加你现在需要的代码。这样,如果您将来需要重复使用它,您可以添加新功能,完全满足您的需求,而不必破坏您过去编写的任何现有功能,试图预测您现在可能需要它。

You may find that after doing this a couple of times you have a library that is stable and robust and doesn't need you to change it because it actually does all the things you need- to me it's much easier to allow that to happen in that evolutionary way than to waste too much time guessing about the future.

您可能会发现,在执行此操作后,您有一个稳定且强大的库,并且不需要您更改它,因为它实际上完成了您需要的所有事情 - 对我来说,更容易实现这一点。这种进化方式比浪费太多时间猜测未来。

#6


2  

I think that the best approach is to try and design code with good interfaces and separation of responsibilities between classes without worrying too much about reuse. But at least if you design this way you're leaving open the possibility of reuse. A good rule of thumb is to ask yourself "if I come back to this code after 3 months, will I understand it and could I extend it if I had to?"

我认为最好的方法是尝试设计具有良好接口的代码并在类之间分离责任,而不必过多担心重用。但至少如果你这样设计,你就可以重新开放。一个好的经验法则是问自己“如果我在3个月后回到这个代码,我会理解它吗?如果必须的话我可以扩展它吗?”

IMO one of the worst practices in the industry is when a team are given the go-ahead to go off and write their own "reusable" framework....

IMO是业内最糟糕的做法之一,当团队获得批准并开始编写自己的“可重用”框架时....

#7


2  

I like the LISP model where you are continually extending the language. Eventually you wind up with a domain-specific language for your problem domain. Not that I actually write any lisp, but in the languages I use most often---Lua and C right now---I usually pull something out into a module and reuse it rather than clone and modify.

我喜欢LISP模型,你不断扩展语言。最终,您最终会使用针对您的问题域的特定于域的语言。并不是说我实际上写的是任何口齿不清,但在我最常使用的语言中 - 现在是Lua和C--我通常会将某些东西拉出来并重新使用它而不是克隆和修改。

For a C programmer, the quintessential example of this approach is Dave Hanson's bookC Interfaces and Implementations. Dave took every reusable idea he'd had in writing three or four compilers and put them all in a book---and the software is free. Fabulous stuff. Now if I write C code and I want to use it a second time, I make an interface in Hanson style. Some things I did this term: 2-dimensional arrays, 2-dimensional arrays with blocking, 2-dimensional bitmaps, readers and writers for pbmplus files, and so on. With this infrastructure in place it was easy to write a program I'd been wanting for years, which is to remove black edges from scans of photocopies of book pages.

对于C程序员来说,这种方法的典型例子是Dave Hanson的书籍C接口和实现。戴夫在编写三四个编译器时考虑了他所有的可重用的想法,并将它们全部放在一本书中 - 而且该软件是免费的。很棒的东西。现在如果我编写C代码并且我想再次使用它,我会用Hanson风格制作一个界面。我做过这个术语的一些事情:二维数组,带阻塞的二维数组,二维位图,pbmplus文件的读者和编写器等等。有了这个基础设施,很容易编写我多年来一直想要的程序,即删除书页复印件扫描的黑边。

So I agree with whomever said when you want to reuse it, pull it out---but not before.

所以我同意任何人在你想要重复使用时所说的,把它拉出来 - 但不是之前。

#8


2  

IMHO, certain code is likely to be reused often, and it makes sense to prepare it for frequent reuse. Other code isn't, and probably doesn't need to be developed beyond solving the immediate problem.

恕我直言,某些代码很可能经常被重用,并且为经常重用做好准备是有意义的。其他代码不是,并且可能不需要开发除了解决当前问题。

Of course, do be aware that telling the difference is NP-hard. :)

当然,要注意区分是NP难的。 :)

#9


1  

If you're sure you're not going to need it again, don't bother. Not even if you think it might come in handy. Refactor it when you really need it again...

如果您确定不再需要它,请不要打扰。即使你认为它可能会派上用场。当你真的需要它时重构它...

However, not making it reuseable is no excuse for not making it transparent. Whenever I write code as transparently as possible, it always turns out to be 99% reusable already...

然而,不能使其可重复使用并不是不使其透明化的借口。每当我尽可能透明地编写代码时,它总是可以重复使用99%......

#10


1  

Once you get above the level of technical utilities I have seen very little actual reuse in the real world.

一旦达到技术实用程序的水平,我在现实世界中看到的实际重用率很少。

If you think about it the reasons are clear. Say the widget_bodger application contained 90% of the functionality you require than you would just add the missing functionality to the application.

如果你仔细想想,原因很清楚。假设widget_bodger应用程序包含您需要的90%的功能,而不是将缺少的功能添加到应用程序中。

Or say the business admired a really cool "beep" function in the widget_bodger and wanted it incorporated into the gernerate_executive_expenses application. Ah resuse you might think, but then you dig into the code and find that GEE application is one of the oldest apps in the company, is written in C, must run on highly available hardware and the only thing that is resuable is the basic algorithm.

或者说,该业务在widget_bodger中羡慕一个非常酷的“嘟嘟”功能,并希望将其整合到gernerate_executive_expenses应用程序中。你可能会想到重新使用啊,但是你深入研究代码并发现GEE应用程序是公司中最古老的应用程序之一,用C语言编写,必须在高可用性硬件上运行,唯一可以恢复的是基本算法。

#11


1  

There are very different opinions on what makes code reusable. I'd say your time well spent making the code clear and well-factored (i.e. separation between responsibilities).

关于什么使代码可重用,有不同的意见。我说你的时间花在了清晰和明确的代码上(即职责分离)。

A side benefit from this is better reusability. The main benefit is making the code easier to understand, change and debug.

这样做的另一个好处是更好的可重用性。主要好处是使代码更易于理解,更改和调试。

To wrap it up. Don't do complex configuration schemes, extension points and events just to make it reusable. Try to find the right moving parts so that the code can be composed in response to new needs.

把它包起来。不要做复杂的配置方案,扩展点和事件,只是为了使它可重用。尝试找到正确的移动部件,以便可以根据新的需求组合代码。

#12


1  

Often "reusable" code is code that is abstracted and modularized, and, to me, the main benefit is not reusability but rather increased testability. Because when you isolate and modularize code it usually becomes more testable. Reuse is a convenient but often unused side effect.

通常“可重用”代码是抽象和模块化的代码,对我而言,主要的好处不是可重用性,而是提高了可测试性。因为当你隔离和模块化代码时,它通常变得更容易测试。重复使用是一种方便但通常未使用的副作用。

And on another note, Juval Lowry advocates interface-based programming since he maintains that interfaces are the sole component of reusability. Anything else has implied functionality (hard to re-use) while interfaces define a contract only which is implicitly reusable.

另一方面,Juval Lowry提倡基于接口的编程,因为他认为接口是可重用性的唯一组成部分。其他任何东西都隐含了功能(难以重复使用),而接口只定义了一个隐式可重用的契约。

#13


0  

I agree with you insofar as there is no point in coding in a way that makes the class easy to use outside of the current application. Most if us don't do this and in business environments there is no need to. If another application needs this functionality at a later date then extracting and commonalising the code can be done as part of that second project, and management are likely to agree with this viewpoint.

我同意你的意见,因为编码没有任何意义,使得课程在当前应用程序之外易于使用。大多数如果我们不这样做,并且在商业环境中没有必要。如果另一个应用程序在以后需要此功能,则可以作为第二个项目的一部分来提取和共享代码,管理层可能会同意这一观点。

However it is good practice to make your code reusable within the constraints of the current application. Refactor your code so as to avoid duplication within your current scope of work. This way you make it easier to work with at a later date. Don't so much code for reuse as for change, change is far more common.

但是,最好在当前应用程序的约束下使代码可重用。重构您的代码,以避免在您当前的工作范围内重复。这样您就可以在以后更轻松地使用它。没有那么多的代码可以重用和变更,变化更为常见。

#14


0  

One of the reasons why SOA has been failing, or hasn't lifted of yet, is that it's difficult to reuse a service: either it's too specific and it can't be used elsewhere, or too generic (and usually very complex) and doesn't meet the needs of different clients.

SOA失败或尚未解除的原因之一是,重用服务很困难:要么太具体而且不能在其他地方使用,要么过于通用(通常非常复杂),不符合不同客户的需求。

This ain't "code reuse", it's "service reuse", but there are some common concepts.

这不是“代码重用”,它是“服务重用”,但有一些共同的概念。

#15


0  

My 2c is that the ethic of code-reuse needs to be a company thing, not just a project thing. This means that when you start a new project, the primary concern is "which other projects can I steal code from to get this job done as well and as quickly as possible?".

我的2c是代码重用的伦理需要是公司的事情,而不仅仅是项目的事情。这意味着当你开始一个新项目时,主要关注的是“我可以从哪个其他项目中窃取代码以尽快完成这项工作?”。

This WILL conflict with the question of "what's the best - or trendiest - language/tool for the job?"

这将与“什么是最好的 - 或最潮流的 - 工作的语言/工具”这个问题相冲突?

Companies that have this approach end up with a pool of engineers that can easily switch from project to project, because language, framework and internal code base are all consistent.

采用这种方法的公司最终会拥有一个工程师库,可以轻松地从一个项目切换到另一个项目,因为语言,框架和内部代码库都是一致的。

The down-side is that switching to a "new" language or framework is much more politically difficult, even though it WILL need to happen at some point.

缺点是转换为“新”语言或框架在政治上更加困难,即使它需要在某个时刻发生。

#16


0  

Another benefit with reuse is that you can easiliy track where things happens in the oode base. if you got a million lines of code it can take hours to find all places where the application behaves in certain way. With a modern IDE you can just click "find references" and you'll find, within a couple seconds, all places where your component/method is used. This can be useful when you want add new features, fix bugs or just want to learn how the system works.

重用的另一个好处是,您可以轻松跟踪oode基础中发生的事情。如果您有一百万行代码,则可能需要数小时才能找到应用程序以某种方式运行的所有位置。使用现代IDE,您只需单击“查找参考”,您就可以在几秒钟内找到使用组件/方法的所有位置。当您想要添加新功能,修复错误或只是想了解系统的工作方式时,这非常有用。

#17


0  

We have a few, but what we have many more of are projects with the functionality you want but done in their own way, so more often than not you end up cannibalising features from old projects and reimplementing them in your new one. I'd argue that this still counts - you're still getting the benefit of having written the code before, and you still save the time.

我们有一些,但我们有更多的是具有您想要的功能但以自己的方式完成的项目,所以通常最终会从旧项目中蚕食功能并在新项目中重新实现它们。我认为这仍然很重要 - 您仍然可以获得以前编写代码的好处,而且您仍然可以节省时间。

Besides, I've found that it's only the second time that you try and use the functionality that it becomes obvious where the reusability is, and if you try and generalise something speculatively you almost always get it wrong and have to change it next time.

此外,我发现这是你第二次尝试使用它在可重用性方面显而易见的功能,如果你试图通过推测性地推广一些东西,你几乎总是弄错了,下次必须改变它。

#18


0  

Like other posters have said if you are going to copy the code then re-use, you will be glad you did this once you start debugging your copied code.

像其他海报一样,如果您要复制代码然后重复使用,一旦开始调试复制的代码,您会很高兴这样做。