Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding spaghetti code while writing small functions

My understanding of "Spaghetti Code" is a code base that jumps from one block of code to another without an logical and legible purpose. The most common offender seems to be the GOTO statement.

I'm currently reading/referencing the function chapter of Clean Code: A Handbook of Agile Software Craftsmanship. The author, while self admittedly, is extremely strict on the size of functions. I understand the idea of keeping functions small, however, he suggests they should be around 5 lines. While Classes certainly become more legible, I'm afraid of creating spaghetti code by writing smaller functions. Smaller functions also seem to inadvertently create much higher abstractions as well.

At what point does code become spaghetti code? How abstract is too abstract? Any answers would be greatly helpful.

As an aside, I'm a long time follower of Stack Overflow although this is my first time posting a question, so any suggestions regarding my post are welcome as well.

Thanks a lot!

like image 913
Chris Avatar asked Aug 21 '13 18:08

Chris


People also ask

Why is a spaghetti code not recommended when writing a program?

Spaghetti code is when the code you work with is unstructured by nature, tightly coupled, and contains an unnecessary amount of mental translation between reality and its representations. The issue with spaghetti code is that the lines composed for the software are not easy to mentally digest.

When the code is very small the function should be?

A function should be as short as possible. A function may grow if it needs to enforce sequential coupling rules, e.g. if there are several steps that need to happen in a particular order. On the other hand, the individual steps could be farmed out to separate functions if the function gets too big.

What causes spaghetti code?

Spaghetti code is a pejorative piece of information technology jargon that is caused by factors like unclear project scope of work, lack of experience and planning, an inability to conform a project to programming style rules, and a number of other seemingly small errors that build up and cause your code to be less ...

What is the opposite of spaghetti code?

Lasagna code is often used to describe overly-abstracted layer-based systems, although its usually put in a favorable light compared to the randomness of spaghetti code. The positive attributes of lasagna code look like a direct opposite of spaghetti code.


3 Answers

As already said in the comments, there is no absolute rule. At the end, you should aim for a good readability of your code. But that is not all about the length of your methods. Robert Martin suggests ordering the methods according to the degree of abstraction. Abststract methods should be at the top of your class, and the more a method is, the deeper it should be located.

Another importand aspect is the method name. It should be chosen well in order to make clear what the method does! If you choose your method names wisely, then comments should be hardly necessary. For example, consider an if-statement:

if(isValidAge(value)) {
   ...
}

is much more readable than

if(value > 10 && value < 99) {
   ...
}

because the intention of the statement becomes much clearer. Of cause you could add a comment in the second example. But comments often become outdated (there is an extra chapter in Robert Martin's book about that). I think, this style of programming leads to many short methods.

It is hard to choose the right level of abstraction. According to my expecience, it is easier to start with a low level of abstraction. So I can first concentrate on solving the problem well. When I need more abstraction later, I still can refactor the code. TDD helps a lot!

Hope, this helps ...

like image 118
Marc Hauptmann Avatar answered Oct 11 '22 20:10

Marc Hauptmann


I agree with comments and answers here. From practical point of view the thinks which Robert Martin writes in his books are every time very good orientations and I try to get as much close as possible to this "rules" and indeed 5-lines-methodes are mostly not to bad.

In my eyes best way to avoid spaghetti code is to write (small) classes with a high Cohesion. The disadvantage is that you become a whole bunch of classes, which makes it sometimes a little bit more hard for new employees to come in the project.

like image 23
Micha Avatar answered Oct 11 '22 19:10

Micha


I understand the idea of keeping functions small, however, he suggests they should be around 5 lines.

That sounds ideal :)

While Classes certainly become more legible, I'm afraid of creating spaghetti code by writing smaller functions.

Spaghetti code is caused by code jumping from place to place with (having different levels of abstraction in the same function - low-level IO code and high level application logic). If you extract small functions, your result is getting further away from spaghetti code, not closer).

At what point does code become spaghetti code?

When the code forces you (the programmer) to make mental jumps (switch contexts) from line to line, the code is spaghetti code. This is true whether you use GOTOs or not (but GOTOs can make the problem much worse).

like image 38
utnapistim Avatar answered Oct 11 '22 20:10

utnapistim