r/programming • • Feb 09 '19

Small Functions considered Harmful

https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29
9 Upvotes

84 comments sorted by

View all comments

Show parent comments

3

u/oridb Feb 10 '19

Hmm, so when you want to understand a function you compile it and read the assembly?

Not always, but I definitely do it. It depends how deeply I need to understand the assembly. For example, recently I needed to optimize some image manipulation, since it was a bottleneck on an embedded device. I needed to see what decisions the compiler made, so I was reading the assembly output, as well as GCC vectorization reports, so that I could understand what I needed to do to convince the compiler to produce better assembly. In the end, all it needed were a few __attribute__((aligned(16)) annotations, but I definitely did need to read the assembly and compiler reports to get enough of an understanding of why it was missing blindingly obvious vectorization opportunities.

It's extremely rare that I can understand code well enough to modify it with confidence, though, without at least skimming the function bodies a couple of layers deep around where I'm making a change.

1

u/gas_them Feb 10 '19

Depends how deeply I need to understand it, yes.

Wow, you are so close to getting the point. There is no need to read a function implementation if you don't have to deeply understand it. Just like there is no need to always read through the assembly.

3

u/oridb Feb 10 '19

Wow, you are so close to getting the point. There is no need to read a function implementation if you don't have to deeply understand it.

Ah, so do you regularly change code without understanding it well?

0

u/gas_them Feb 11 '19

I know what a function does, there is no need to know how it is implemented. That's all I have to "understand" to use the function. You have to understand that to manage large codebases. There is just too much to read if you do things your way.

Do you know how std::sqrt is implemented? Are you telling me you are reading through its assembly each time you use it?

This is getting ridiculous at this point.

2

u/oridb Feb 11 '19 edited Feb 11 '19

I know what a function does

So, without looking at the documentation (Edit: or rather, with looking at about as much documentation as your colleagues typically write), can you tell me what exceptions ArrayList.toArray() throws? If your coworker wrote their own RegionsOfInterest.getActiveRegions(), but didn't write heavy comments about pre/post conditions and caveats, would you be able to tell me what ways it can fail? Are there any restrictions on the argument ranges? What kind of thread safety does it provide? Is it expensive or cheap to call repeatedly? I keep needing to answer these kinds of questions, and I have no idea how you'd figure these questions out without reading the code.

How about something less trivial -- ResourceBundle ResourceBundle.getBundle​(String baseName, ResourceBundle.Control control)? Can you explain to me what exactly the edge cases here are, and what failures can be caused by passing incorrect values? What parts of the environment does it depend on to make the decision on which actual bundle to return?

Do you know how std::sqrt is implemented?

Usually, it's a few Newton-Rhapson iterations of a builtin SSE2 approximate square root instruction. For pure software implementations, starting values can be chosen using the technique here: https://doi.org/10.1016/j.tcs.2005.09.056.

0

u/gas_them Feb 11 '19

without looking at the documentation

Why? Why not look at the documentation?!

Are you telling me you read the source code of toArray? What the fuck it must take you forever to get anything done.

4

u/oridb Feb 11 '19 edited Feb 11 '19

I've never seen companies reliably write out detailed documentation for their functions. If your company does write detailed and clear docs that cover all of the edge cases, you've found a unicorn. So the reason for 'no documentation' is that this is the default state of most code a programmer will interact with.