Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do very long methods always need refactoring?

Tags:

I face a situation where we have many very long methods, 1000 lines or more.

To give you some more detail, we have a list of incoming high level commands, and each generates results in a longer (sometime huge) list of lower level commands. There's a factory creating an instance of a class for each incoming command. Each class has a process method, where all the lower level commands are generated added in sequence. As I said, these sequences of commands and their parameters cause quite often the process methods to reach thousands of lines.

There are a lot of repetitions. Many command patterns are shared between different commands, but the code is repeated over and over. That leads me to think refactoring would be a very good idea.

On the contrary, the specs we have come exactly in the same form as the current code. Very long list of commands for each incoming one. When I've tried some refactoring, I've started to feel uncomfortable with the specs. I miss the obvious analogy between the specs and code, and lose time digging into newly created common classes.

Then here the question: in general, do you think such very long methods would always need refactoring, or in a similar case it would be acceptable? (unfortunately refactoring the specs is not an option)


edit: I have removed every reference to "generate" cause it was actually confusing. It's not auto generated code.

class InCmd001 {    OutMsg process ( InMsg& inMsg ) {       OutMsg outMsg = OutMsg::Create();       OutCmd001 outCmd001 = OutCmd001::Create();      outCmd001.SetA( param.getA() );      outCmd001.SetB( inMsg.getB() );       outMsg.addCmd( outCmd001 );       OutCmd016 outCmd016 = OutCmd016::Create();      outCmd016.SetF( param.getF() );       outMsg.addCmd( outCmd016 );       OutCmd007 outCmd007 = OutCmd007::Create();      outCmd007.SetR( inMsg.getR() );       outMsg.addCmd( outCmd007 );       // ......       return outMsg;   } } 

here the example of one incoming command class (manually written in pseudo c++)

like image 570
Gianluca Avatar asked Aug 04 '10 12:08

Gianluca


People also ask

When should refactoring not be done?

General logic based on this: If points 1-5 are all true, don't refactor. If any of points 2, 3, or 5 are false for multiple reasons (for example, multiple bugs would be fixed or multiple features would be easier to implement), count them as false once for each reason they are false.

How long of a method is too long?

A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions.

When should we do refactoring?

The best time to consider refactoring is before adding any updates or new features to existing code. Going back and cleaning up the current code before adding in new programming will not only improve the quality of the product itself, it will make it easier for future developers to build on the original code.

Which refactoring method addresses issues related to excessively long methods?

Composing Method During the development phase of an application a lot of times we write long methods in our program. These long methods make your code extremely hard to understand and hard to change. The composing method is mostly used in these cases.


1 Answers

Code never needs refactoring. The code either works, or it doesn't. And if it works, the code doesn't need anything.

The need for refactoring comes from you, the programmer. The person reading, writing, maintaining and extending the code.

If you have trouble understanding the code, it needs to be refactored. If you would be more productive by cleaning up and refactoring the code, it needs to be refactored.

In general, I'd say it's a good idea for your own sake to refactor 1000+ line functions. But you're not doing it because the code needs it. You're doing it because that makes it easier for you to understand the code, test its correctness, and add new functionality.

On the other hand, if the code is automatically generated by another tool, you'll never need to read it or edit it. So what'd be the point in refactoring it?

like image 197
jalf Avatar answered Oct 02 '22 15:10

jalf