Is code duplication enough reason to retrieve a method ...?

... when the extracted method will suffer from low cohesion (without forming a good abstraction and having a bad name)?

For example, what name would you indicate for the following method?

private void foobar() { Server.checkAllowedDeviceCount(socketAction._sess.getDeviceID(); socketAction.registerSession(); socketAction._sess.runApplication(); } 

This is a possible duplicate of my other question: To dry or not to dry? To avoid code duplication and cohesion - or my desperate way to get some suggestions from more experienced programmers (I hope you forgive me). Check out the link above - it contains the code that the sample that I present here is based on.

+4
source share
4 answers

I am creating code detectors for code . Often I see several sets of ABCPQ code found as clones where ABC are conceptually coherent and PQ are conceptually coherent, but ABC and PQ are not related. A clone detector (or an uneducated reader of your code) will see the same sequence as the clones. Yes, you could try to make a bad FOOBAR abstraction from ABCPQ, but from a principled point of view of the reader, you better make only ABC inyo abstraction, and then consider what to do with P Q clones.

I do not know if this is applicable in your situation, since all your calls are sockets (ABC?), And I am not familiar with your interface.

+1
source

If duplication was “bad,” then yes, I would think about extracting it, but you will always want to balance this with other issues.

In addition, according to the replication refactoring method (C #) , there are four methods for extracting a method, and duplication is one of them.

Others (as indicated in a related article):

  • Encourages best coding techniques by emphasizing discrete reusable techniques.
  • Encourages self-documenting code through good organization. When descriptive names are used, high-level methods may look more like a series of comments.
  • Encourages the creation of more subtle methods to simplify overriding.
0
source

After you looked at your question "To DRY ...", I think that this function needs to be extracted - duplication of smell enough to justify it.

Trying to infer the purpose of the SocketAction class from the inner classes that you pointed out in this question, it looks like StartSession is a reasonable name for this function.

0
source

Here is the key, if you needed to change one of the duplicates (to fix the error or add a function), will you also have to change the rest? If the answer is yes, then combine them into one function.

I also notice that all three lines of your function are associated with socketAction, and this / self is not referenced at all. This suggests that this method should be part of the socketAction class.

0
source

All Articles