News for hackers. Stuff that matters.
Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @12:41AM (#18966231)
It’s a lot easier to read one function that calls 10 functions that are named what they do than to read one long 300 line function.

Inlining functions is not a readability thing. It’s for performance when needed. In C, you can give a compiler hint to online a function and get the best of both worlds.

Re:John Carmack on Inlined Code (2014) by xorcist on Tuesday January 22, 2019 @02:44AM (#18966687)
Hard disagree on this. I read quite a bit of code for a living and one of my single biggest complaints about most codebases are trivial functions (or useless objects).

If your code does n operations in sequence, just list those operations. Don't hide them off somewhere.

Code that reads linearly is by far the easiest to read. At least when you are familiar with the language and idioms. It's like the difference between a novel and a choose-your-adventure book.

Modern IDEs help, but there's only so much they can do.

Re:John Carmack on Inlined Code (2014) by kazagistar on Tuesday January 22, 2019 @03:57AM (#18966983)
My favorite rule is orthogonal to length. Functions should ideally be at one level of abstraction. If you are doing 100 things at a single level of abstraction, and it flows logically, then by all means, keep it togeather. But if in a high level protocol specification you, say, concatenate strings and describe the details of fetching from files etc, then it might be better to isolate those.

Of course, what exactly a level of abstraction depends on how you model, and personal opinion. But I find it a more useful starting point then "line count".

Re:John Carmack on Inlined Code (2014) by xorcist on Tuesday January 22, 2019 @05:58AM (#18967425)
Yes, absolutely! A hundred line function can be trivial, and a oneliner can be complex. It has nothing to do with function length but how much complexity and dependencies they isolate.

What you say about specifications certainly rings true. In those cases the reader's preferred abstraction level is pretty much given.

Re:John Carmack on Inlined Code (2014) by lmm on Tuesday January 22, 2019 @07:06AM (#18967670)
> If your code does n operations in sequence, just list those operations. Don't hide them off somewhere.

In principle I agree. In practice, if your function needs to do more than say five things one after another then something has gone wrong. If you were sending a human a set of instructions on how to do something, that's the point where you'd start grouping them into logical "meta-steps" (e.g. prepare the ingredients, make the broth, prepare the meat, finish cooking the dish).

Chopping a 12-step function into 3 4-step functions without regard for what each step actually does will make it less readable, just like doing the same to a recipe, or putting paragraph breaks and full stops into a passage at random. But a single function that does a long sequence of things is a code smell in the same way as an overly long sentence; it's well worth looking for a way to logically group these steps into a structure that will help the reader understand.

Re:John Carmack on Inlined Code (2014) by chpmrc on Tuesday January 22, 2019 @08:02AM (#18967918)
Here's a counterargument to that: you have to read and understand those "trivial" functions only once and just assume they are correct and do what they do when you keep encountering calls to them.

> Modern IDEs help, but there's only so much they can do.

Exactly. In my experience (YMMV) 100% of the time the complaints about using "too many functions" (or similar) come from people who use poor tools or don't really know how to use them.

Re:John Carmack on Inlined Code (2014) by jfries on Tuesday January 22, 2019 @06:44AM (#18967591)
Speaking of IDEs, a cool feature would be automatic read-only inlining for short functions. Could probably help understanding what's going on in code with lots of calls.
Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @08:25AM (#18968056)
Usually I’m not just reading other people’s code to understand it, I’m stepping through it while running it. In that case, if it is broken up by function, I get to put watches on interesting variables and step over functions and see the data change. If I see something interesting I can step into the function. With one big blob of code, I have to set breakpoints to skip over the non interesting parts.
Re:John Carmack on Inlined Code (2014) by maxxxxx on Tuesday January 22, 2019 @01:06AM (#18966313)
"Inlining functions is not a readability thing."

Sometimes it is for readability. For example we have code that serializes commands sent to a device. It used be built with a command pattern which was hard to debug. We have replaced it with a huge switch statement and now the code is much easier to understand and refactor. Obviously this approach often doesn't work but there are cases where inlining makes code easier to read.

Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @01:37AM (#18966432)
Even with using a switch statement, how would it be less readable to put each part of the statement in a function?
Re:John Carmack on Inlined Code (2014) by iainmerrick on Tuesday January 22, 2019 @04:01AM (#18967008)
Imagine there are two or three key variables, and a bunch of repeated blocks of code that modify some or all of the variables. (That sounds messy but it’s the kind of thing that could easily come up in an embedded controller.)

If you turn all those code blocks into functions, it’s no longer easy to visually inspect the code to see all the places where variable X is modified. It might not be a net win for readibility.

Personally, I wouldn’t define a function purely to contain a repeated block of code; I only do it if the code is related, i.e. all operating on the same data.

In my first example, before trying to wrap things in functions, I’d try to figure out if some of those variables can be tied together into a single object with a nice clear interface.

Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @10:31AM (#18968930)
If you turn all those code blocks into functions, it’s no longer easy to visually inspect the code to see all the places where variable X is modified. It might not be a net win for readibility.

This goes back to not modifying global (or class global) variables and using a functional style. If you need to pass four or five variables put them in your languages equivalent of a pass by value struct.

In the case of my favorite IDE, Extract Method and creating a struct/class from a parameter list is an automated, guaranteed safe refactor.

Re:John Carmack on Inlined Code (2014) by maxxxxx on Tuesday January 22, 2019 @09:05AM (#18968285)
Good explanation. Thanks?
Re:John Carmack on Inlined Code (2014) by mtzet on Tuesday January 22, 2019 @01:28AM (#18966398)
> Inlining functions is not a readability thing.

Inlining for readability is precisely what the linked article is about.

Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @01:39AM (#18966452)
If you are changing global state, and each function has an order dependency, I could see that. But it seems like even then his general takeaway is not to inline functions but not to change global state and use a more functional style of programming.
Re:John Carmack on Inlined Code (2014) by mtzet on Tuesday January 22, 2019 @02:08AM (#18966561)
I think the takeaway is both. Avoid state changes and use a functional style wherever possible. But, when you do need to make state changes, keep it inline rather than hiding it behind function calls. Better to have one long function, sectioned off by comments, than to have state changes with subtle order dependencies in several different functions.
Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @02:15AM (#18966581)
I would go one step further. Don’t section them off with just comments, use nested scoping blocks. At least most modern IDEs then can collapse the sections and you still have the concept of local variables.
Re:John Carmack on Inlined Code (2014) by lmm on Tuesday January 22, 2019 @07:08AM (#18967678)
Or use a state monad to distinguish between steps that mutate state and functions that don't, making your order dependencies visible and easy to work with...
Re:John Carmack on Inlined Code (2014) by scarface74 on Tuesday January 22, 2019 @03:49PM (#18972296)
And this is how we end up with classes like AbstractSingletonProxyFactoryBean
Re:John Carmack on Inlined Code (2014) by lmm on Wednesday January 23, 2019 @05:50AM (#18977223)
No, just the opposite. Languages with inadequate composition facilities forcing overly specialised single-purpose classes lead to AbstractSingletonProxyFactoryBean. Small composable abstractions are how you avoid it.

Zygohistoric prepromorphism has the same kind of meme status as AbstractSingletonProxyFactoryBean, but the difference is that a zygohistoric prepromorphism is a one-liner you can compose together yourself out of the functions that the library in question makes available, whereas AbstractSingletonProxyFactoryBean is a class in its own right that the library in question has to define.

Re:John Carmack on Inlined Code (2014) by m0zg on Tuesday January 22, 2019 @02:58AM (#18966737)
If it calls 10 functions, then the equivalent "inlined" function written under my rule is at most 100 lines long, which means it fits on one page easily, and it's almost certainly easier to read than one where you have to follow 10 different function calls. Function naming usually sucks. As they say it's one of the hardest problems in computer science. If your code is 10 lines or less, it should be self-evident (perhaps with a short comment) as to what it does.

Also, as an aside, inlining "for performance" with the inline keyword doesn't always inline. You have to use a GCC hint to force it.

Re:John Carmack on Inlined Code (2014) by icebraining on Tuesday January 22, 2019 @03:55AM (#18966973)
> If it calls 10 functions, then the equivalent "inlined" function written under my rule is at most 100 lines long

Only if those 10 functions weren't already inlined themselves.

Re:John Carmack on Inlined Code (2014) by m0zg on Tuesday January 22, 2019 @04:13AM (#18967069)
But then they won't be 10 lines or less anymore.
Re:John Carmack on Inlined Code (2014) by icebraining on Tuesday January 22, 2019 @04:41AM (#18967166)
Yeah, but if you have A() calls B() calls C(), and they're all a few lines long, how do you decide which ones to inline? If you just do it as you're writing, don't you end up missing inlining opportunities further ahead just because you've already inlined before, and therefore the resulting function is too big to be further inlined?
Re:John Carmack on Inlined Code (2014) by m0zg on Tuesday January 22, 2019 @05:12AM (#18967268)
>> how do you decide which ones to inline

Use whichever setup maximizes readability. Lift code out into functions if it's a logical, reasonably self contained block used 3 or more times. Use your best judgment, in other words. If you require me to jump around the file (or worse, files, plural) to read your code, make sure there's a good reason for all this cognitive overhead.