I am familiar with two general approaches to creating unreadable and unmanageable software.
The first one is to take logically unrelated operations and combine them into one giant blob via shared/global variables and god objects/methods. This is what commonly known as spaghetti code.
The second one it to take every piece of functionality and shatter it into tiny pieces, so no unit of code represents anything anymore. This can be done with endless callbacks or layers of abstraction, and it can be made worse by complex frameworks with a lot of "magic" taking place or a lot of boilerplate being required. I don't know any common name for this, but let's call it mush coding.
It looks like the post describes how to convert moderately readable spaghetti code into unreadable mush code. (I would claim that a person not familiar with JQuery could easily understand the initial code, while a person not keenly familiar with Backbone would be completely lost in the final version.)
If most of your methods are longer than 100 lines, something is probably wrong. If most of your methods are shorter than 3 lines, something is probably wrong as well.
While in principle I agree with you, the article references an extremely simple application for the sake of example. In reality, you would never build an application this simple in this manner. An article showing how to do this with a real webapp from beginning to end would nearly be book-sized.
When building an actual webapp, the Backbone method is going to be much easier to extend, test, and understand versus jQuery spaghetti.
Precisely the point I was going to make. This is a condensed example of translating jQuery-like thinking to backbone-like thinking. To quote kjbekkelund:
> [The backbone] code is more maintainable, easier to reuse and extend, and easier to test.
Great observation, in particular the "mush coding" has gotten popular over the years, instead of getting shit done people build abstraction over abstraction and high-level frameworks detached from reality and full of crappy magic. Backbone is a very good example for this (shattering code it into tiny pieces).
Method length should be 1 to 3 lines. Longer and you are incurring technical debt. The function name should be descriptive and it's description shouldn't be, 'adds_one_to_a_number_unless_override_is_on_then_it_subtracts_four_except_when_global_is_set_then_it_jumps_into_a_switch'
No, no, no, no. Did I mention no? I inherited a Javascript application that was maybe 1500 lines of code. Every function was 3-5 lines. Most were like this:
And foo.some_method (in another file) was similar. What it meant was it was impossible to get a view of what was going on in the system. It took months to accomplish an understanding that should have taken weeks.
I really think the original authors heard your advice and decided comprehension didn't matter, only function length.
Function should accomplish a task. Sometimes (very rarely) a task may be two hundred lines; usually, it is much shorter. Sometimes three lines, but often 15-20.
I used to believe that, but after a while I realized that it's a heuristic derived from code that's already split up into clear responsibilities.
Large methods are only a symptom that this component of your system does too much and probably has too many dependencies. Small methods do nothing for you except add indirection if you've not solved the dependency problem, and adding indirection to an entangled system is even worse than having large methods.
In other words, somehow an aspect of the end result of clear code became common advice, probably because it's so obvious and replicable. "Write decoupled code with focused responsibilities" on the other hand is nebulous.
It's sort of like when dating advice is simplified into "be mean to girls".
I've read that claim. The only time I really had to work with code like that was horrible.
It was like a book where the sentences were in random order, with the next sentence number appended, so you would have to browse to another page every few words. :-(
The context switches didn't make me productive... Sometimes, I had to look 3-4 levels up or down in the hierarchy to find a parameter type (this was in a scripting language). If anyone talked to me, it took a looong time to rebuild the model in my head.
The first one is to take logically unrelated operations and combine them into one giant blob via shared/global variables and god objects/methods. This is what commonly known as spaghetti code.
The second one it to take every piece of functionality and shatter it into tiny pieces, so no unit of code represents anything anymore. This can be done with endless callbacks or layers of abstraction, and it can be made worse by complex frameworks with a lot of "magic" taking place or a lot of boilerplate being required. I don't know any common name for this, but let's call it mush coding.
It looks like the post describes how to convert moderately readable spaghetti code into unreadable mush code. (I would claim that a person not familiar with JQuery could easily understand the initial code, while a person not keenly familiar with Backbone would be completely lost in the final version.)
If most of your methods are longer than 100 lines, something is probably wrong. If most of your methods are shorter than 3 lines, something is probably wrong as well.