I have a question about code duplication and refactoring, I hope that it is not too general. Let's say you have a rather small piece of code (~ 5 lines), which is a sequence of function calls, which is not a very low level. This code is repeated in several places, so it would probably be nice to extract the method here. However, in this particular example, this new function will suffer from low cohesion (which, among other things, manifests itself in the fact that it is difficult to find a good name for the function). The reason for this is probably because this repeating code is only part of a larger algorithm - and it is difficult to separate it into well-named steps.
What would you suggest in such a scenario?
Edit:
I wanted to leave the question at a general level so that more people could find it useful, but obviously it would be better to support it with some code. The example may not be the best of all (it smells in quite a few ways), but I hope it does its job:
class SocketAction { private static class AlwaysCreateSessionLoginHandler extends LoginHandler { @Override protected void onLoginCorrect(SocketAction socketAction) throws IllegalAccessException, IOException { Server.checkAllowedDeviceCount(socketAction._sess.getDeviceID()); socketAction.registerSession(); socketAction._sess.runApplication(); } } private static class AutoConnectAnyDeviceLoginHandler extends LoginHandler { @Override protected void onLoginCorrect(SocketAction socketAction) throws IllegalAccessException, IOException { if (Server.isUserRegistered(socketAction._sess.getUserLogin())) { Log.logSysInfo("Session autoconnect - acquiring list of action threads..."); String[] sa = Server.getSessionList(socketAction._sess.getUserID()); Log.logSysInfo("Session autoconnect - list of action threads acquired."); for (int i = 0; i < sa.length; i += 7) { socketAction.abandonCommThreads(); Server.attachSocketToSession(sa[i + 1], socketAction._commSendThread.getSock()); return; } } Server.checkAllowedDeviceCount(socketAction._sess.getDeviceID()); socketAction.registerSession(); socketAction._sess.runApplication(); } } private static class OnlyNewSessionLoginHandler extends LoginHandler { @Override protected void onLoginCorrect(SocketAction socketAction) throws IllegalAccessException, IOException { socketAction.killOldSessionsForUser(); Server.checkAllowedDeviceCount(socketAction._sess.getDeviceID()); socketAction.registerSession(); socketAction._sess.runApplication(); } } }
language-agnostic refactoring code-duplication
lukem00
source share