News for hackers. Stuff that matters.
Re:Reflections on 10k Hours of Programming by jbverschoor on Friday August 06, 2021 @04:35PM (#28092438)
Don’t agree. Longer pieces of code are way easier to reason about. Deep function nesting, which seems clean is very confusing mentally, and also difficult to describe in terms of function name.

Explain *why* you’re doing something a certain way. This also includes a little bit of how and what. Other than that, just make sure your business terms are reflected in the code, or at least have a proper definition of things, which you can easily reason about.

Re:Reflections on 10k Hours of Programming by LouisSayers on Friday August 06, 2021 @06:37PM (#28093578)
They may be easier in some cases to reason about - which is great until you start writing tests.

Once you start writing tests you find that the size of test functions will reflect the size of the function you're testing. The test functions start to have huge set-ups in order to test a small bit of logic, and you end up spending a lot of time fixing tests every time you make a change to that function.

This is why it's often better to find ways to make functions smaller.

There is a difference however of people refactoring code into smaller classes / functions vs what I'd call "hiding" code in classes / functions. If all someone has done is broken a large function into a chain of functions then of course that is not good.

When people refer to small functions / classes they refer to breaking up the concepts that a class / function represents into smaller concepts that build upon one another. This also increases the reusability of code.

So in short - I agree, longer pieces of code can be better for reasoning about, but I disagree that that necessarily makes them better.

Re:Reflections on 10k Hours of Programming by taneq on Saturday August 07, 2021 @12:22AM (#28095668)
I worked with a guy who liked writing little <10 line functions, deeply nested, in dozens of files. Each individual function was nice and easy to read but figuring out what the entire call tree was doing required hours of archaeology.

Edit: He also refused to ever delete any code he'd written "because it might be useful later." I once spent three hours tracing the source of some values, only to discover that the output of the entire call tree I was working my way through was being quietly thrown away at the end.

Re:Reflections on 10k Hours of Programming by jcelerier on Friday August 06, 2021 @05:40PM (#28093068)
Eh, to each their mental models. I have much less trouble understanding nested and, some may say, very-class-segregated code than mostly procedural one as the general shape of the code + the pattern names used generally gets me 95% of where I have to be to understand what is the issue at hand. But then I found out that not a lot of people draw diagrams when they are coding ? (Which in my case helps immensely - and after some time you can just draw the diagram in your head)
Re:Reflections on 10k Hours of Programming by bcrosby95 on Friday August 06, 2021 @06:28PM (#28093509)
I've always felt like this is where IDEs could help. You should be able to "inline" the implementation of a function or even whole class without actually changing the code.
Re:Reflections on 10k Hours of Programming by emilprogviz on Monday August 09, 2021 @02:20PM (#28120304)
I've always felt the same, so I recently made an interactive mock-up of such an IDE feature: https://emilprogviz.com/expand-calls/?inline,substitution Not mobile friendly, best seen on desktop. To inline a function call, either click the "+" icon, or go up/down with the keyboard and press spacebar when the caret is on that line.

(There's also a video where I explain the reasoning behind the mock-up, but I guess the motivation is already blatantly obvious to most readers of the above discussion, so feel free to skip the first chapters: https://youtu.be/wdf6S6oumH8 )

Then I learned that the Smalltalk editor Pharo has a similar feature! Seems many people have the same idea. Today's major IDE makers should have copied this feature already!

Re:Reflections on 10k Hours of Programming by cudgy on Friday August 06, 2021 @04:48PM (#28092577)
Agreed. When looking at complex code, it can be helpful to expand the functions inline to be able to view all of it in a single screen flow. Switching between too many files and functions sometimes creates friction, making it more difficult.

However, when writing the code if a section of code is repeated more than 2 times, it should be considered to break it out into a separate function with a useful name.

Re:Reflections on 10k Hours of Programming by fiddlerwoaroof on Friday August 06, 2021 @11:32PM (#28095444)
Unless you’re doing something like performance analysis, I think needing to peek into the definition of a function to understand a piece of code is a smell: it indicates that the function is only abbreviating and not providing an abstraction.
Re:Reflections on 10k Hours of Programming by dragonsngoblins on Sunday August 08, 2021 @08:43PM (#28111589)
Well a lot of the time when you are looking at something you are trying to diagnose some problem or another, which means you want to be 100% sure of what something is doing. Even if the function provides a good abstraction you might still want to make sure that it is doing exactly what it says on the tin because you are desk checking some weird behaviour and you want to make sure each step is acting correctly
Re:Reflections on 10k Hours of Programming by fiddlerwoaroof on Sunday August 08, 2021 @08:58PM (#28111671)
Agreed, but that means you verified that the use site was correct and are now examining whether the abstractions it depends on were implemented correctly.

My point is just that you shouldn’t normally have to keep both sides of a call site in mind at once.