News for hackers. Stuff that matters.
Re:The Shapes of Code by luckycharms810 on Monday January 27, 2020 @10:47PM (#22166268)
While the patterns themselves are interesting, the idea that all code must be rearranged in to smaller functions to be “refactored” seems a little juvenile.
Re:The Shapes of Code by hinkley on Tuesday January 28, 2020 @03:52AM (#22167638)
There’s an exercise for creative writing that my lit major friend told me about. You take everything you’ve written and cut it up into sentences. You just push the sentences around until something clicks and you figure out how to write your way out of the “stuckness”.

Refactoring has rediscovered this trick, among others.

Re:The Shapes of Code by 0x445442 on Tuesday January 28, 2020 @09:06AM (#22169446)
Another similarity to creative writing is this; you're not done when there's nothing left to add, you're done when there's nothing left to remove.
Re:The Shapes of Code by ipnon on Tuesday January 28, 2020 @08:56AM (#22169351)
The code that is easiest to revisit is decomposed into separate functions that are each trivial to understand. They are used to compose similarly trivial functions, and so on. Ideally every point of your program from input to output can be understood clearly and immediately.
Re:The Shapes of Code by 0x445442 on Tuesday January 28, 2020 @09:05AM (#22169428)
Makes the test a hell of a lot easier and more robust as well.
Re:The Shapes of Code by rgoulter on Tuesday January 28, 2020 @04:41AM (#22167848)
I can agree "code should be in small functions" by itself is a bad guideline. I think it's suggested due to other principles around making code easier to read / use.

It'd be better to ask if the code is more complex than it needs to be.

I liked the terms John Ousterhout uses. For some module of a program (e.g. a method, class, package, etc.), it has an interface and an implementation. The interface is "what you need to know to use the module", the implementation is "how it's done".

Complexity comes from dependencies (e.g. more interfaces you need to know about), and from obscurity (e.g. things you need to know about, which aren't obvious from the interfaces given).

In some cases, many small methods would be an increase in complexity overall. In some cases, one big method may be an increase in complexity.

Re:The Shapes of Code by hinkley on Tuesday January 28, 2020 @11:04AM (#22170585)
In most cases one big method is an increase of complexity (and others will add to it over time). Large methods are usually a failure of imagination, like bad and rambling prose.

Code is a set of instructions for the computer, and for the next person who has to maintain it (“ Programs are meant to be read by humans and only incidentally for computers to execute.”)

Cooking instructions are split into steps. Assembly instructions are split into steps. LEGO instructions are split into steps. When drawing instructions aren’t split into steps the Internet turns them into a cultural phenomenon (Step 2: draw the rest of the fucking owl).

Only programmers think they are immune to this ridicule, smash all the steps together, and then get salty when others complain, ignoring a series of luminaries who beg in every format of media available, and for decades, to do otherwise.

Seriously, organize your code, separate the steps. You’re killing us.

Re:The Shapes of Code by mason55 on Tuesday January 28, 2020 @11:28AM (#22170870)
No one disagrees that code needs to work in steps. The conversation is around how much and the answer is going to depend on what you're doing.

Why have a function that prints a whole line? Isn't that just smashing together a bunch of small steps to print one character at a time?

Re:The Shapes of Code by CoolGuySteve on Tuesday January 28, 2020 @01:30AM (#22166959)
It seems antithetical to the intent of "Goto considered harmful":

> My second remark is that our intellectual powers are rather geared to master static relations and that our powers to visualize processes evolving in time are relatively poorly developed. For that reason we should do (as wise programmers aware of our limitations) our utmost to shorten the conceptual gap between the static program and the dynamic process, to make the correspondence between the program (spread out in text space) and the process (spread out in time) as trivial as possible.

While Dijkstra later talks about using procedures to index into the call stack, needlessly adding to the depth of the callstack for single use functions seems excessive.

With this logic, I actually think "The paragraphs with headers" is the way to go since as TFA states: "You know that the algorithm operates in steps, and you know where the steps are located in code."

http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...

Re:The Shapes of Code by makapuf on Tuesday January 28, 2020 @02:20AM (#22167161)
Functions can be inlined by the compiler if it thinks it's an issue. I think it's rather a question of readability : sometimes if a process involves a long series of step I like to be able to read them linearly rather than jumping back and forth to sub steps. I can read just the headers if they're cleanly organized by cleanly separated paragraphs (with braces if needed to indicate separate scopes and no leaking variables between paragraphs).
Re:The Shapes of Code by taspeotis on Tuesday January 28, 2020 @05:47AM (#22168145)
Re:The Shapes of Code by DavidVoid on Tuesday January 28, 2020 @05:56AM (#22168198)
Since that blog seems to be down at the moment here's a cached version of the article/email.

https://webcache.googleusercontent.com/search?q=cache:http:/...

Re:The Shapes of Code by reuben_scratton on Tuesday January 28, 2020 @11:39AM (#22171011)
Absolutely this.

More functions => More complexity => Bad.

Also: decreased legibility, less opportunity for compiler optimisation.