ConstructiCat Logo
CodeBust.
Browse section ▾

Long Method.

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

##Signs and Symptoms

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

##Reasons for the Problem

Like the Hotel California, something is always being added to a method but nothing is ever taken out. Since it’s easier to write code than to read it, this “smell” remains unnoticed until the method turns into an ugly, oversized beast.

Mentally, it’s often harder to create a new method than to add to an existing one: “But it’s just two lines, there’s no use in creating a whole method just for that...” Which means that another line is added and then yet another, giving birth to a tangle of spaghetti code.

##Payoff

  • Among all types of object-oriented code, classes with short methods live longest. The longer a method or function is, the harder it becomes to understand and maintain it.

  • In addition, long methods offer the perfect hiding place for unwanted duplicate code.

##Performance

Does an increase in the number of methods hurt performance, as many people claim? In almost all cases the impact is so negligible that it’s not even worth worrying about.

Plus, now that you have clear and understandable code, you’re more likely to find truly effective methods for restructuring code and getting real performance gains if the need ever arises.