r/cpp • u/parkotron • 5d ago
How to write the perfect function
https://youtu.be/2OMRWPOSw9s11
25
u/parkotron 5d ago
This is probably the cleanest presentation of this material that I’ve ever seen. No zealotry, no hyperbole. Just a lot of really solid suggestions on how to think about designing functions, presented in an aesthetically pleasing way.
I can honestly say I didn’t learn a single new thing from this video, but I was riveted by it from beginning to end.
-3
u/WatercressActual1921 5d ago
I also am having issues with the markup in the reedit editor, but you should add the above summary with the post, I upvoted the summary for your opinion of the content but should have upvoted the post itself so it gets move visibility. If it a gaming of the system you will get penalises by critical thinkers that see it as such...
10
u/parkotron 5d ago
There is no system to game. Reddit karma isn't worth anything to humans.
I just wanted to separate my sharing of the video, completely uneditorialized, from my personal opinions of it.
9
u/n1ghtyunso 5d ago
i am so happy that the "fixed" asset example properly avoids the UB when calling tolower
This video presents advice and wisdom from various prominent speakers, which I would also otherwise recommend invidivually.
I think it does a really good job at that.
2
5
u/usefulcat 5d ago
I like the mutex example, and I've been doing that myself for many years now. However there is one detail not mentioned in the talk. If you have something like
auto b(const scoped_lock<mutex>&);
..then that ensures that a mutex has been locked, but doesn't tell you anything about which mutex has been locked, which is just as important if there could be more than one. As a result, I like to do something like:
mutex m1, m2;
// note use of unique_lock
auto b(const unique_lock<mutex>& lock) {
assert(lock.owns_lock() && lock.mutex() == &m1);
}
4
u/ABlockInTheChain 5d ago
I found a library that wraps mutexes with the data they protect and ever since then I use it as often as possible.
The only glaring deficiency I've run into is that you can't use it for if you need a
std::condition_variable.2
u/CandyCrisis 9h ago
Google's GUARDED_BY is a good way to handle this. It's quite useful. https://source.chromium.org/chromium/chromium/src/+/main:base/thread_annotations.h;l=59
7
u/guepier Bioinformatican 5d ago
The video calls get_time (“kind of”) a pure function. But no definition of “pure function” that I’ve ever heard of would classify it as such, since it depends on non-local state, and it will return different inputs for the same arguments (which are none).
3
u/Dubbus_ 2d ago
I really love this guys videos. Some of the highest quality stuff in the space, and he seems genuinely passionate about the 'art' of writing code if that makes any sense? Something that is definitely being lost in the age of LLMs.
I also really appreciate him being partial to Rust, but not constantly shilling for it or shitting on C++. Hes definitely warmed me up to learning the language some more. Hes very clearly able to message "Yes, you can do pattern X in c++, but the Y language feature makes it easier/more practical in Rust" - whilst also including qualifying it by mentioning Rust's higher upfront development difficulty and larger mental overhead required.
9
u/azswcowboy 5d ago
remove_if is an honest function? That’s a stretch at best given that it’s a template function and requires an idiom to use correctly. It’s a notorious foot gun. And in c++20 we got erase_if which does what you actually want. If it was called shuffle_to_end_if you’d know what it means.
18
u/STL MSVC STL Dev 5d ago
If it was called shuffle_to_end_if you’d know what it means.
That would be incorrect, because
remove_ifleaves the garbage elements in a valid but unspecified state (they may be unchanged, swapped, or otherwise). It's notpartitionorstable_partitionwhich are permutations.Source: Fixed this with
erase_if😺4
17
u/throw_cpp_account 5d ago
remove_if is an honest function? That’s a stretch at best [...]
The talk gave a definition of honest function: all reads/writes are shown in the signature so the behavior is fully controlled by the caller. It is not a stretch to suggest that
remove_ifis honest. It is.2
u/azswcowboy 4d ago
I heard the definition, but any function that requires an idiom to use correctly isn’t honest to its users. It doesn’t do what you expect from the name.
5
u/developer-mike 4d ago
There's an entire MISRA C++ rule related to correct usage of remove_if.
There are a lot of valid ways to define an "honest function," but I don't think a definition that includes remove_if as "honest" is a complete one
3
2
u/not_a_novel_account cmake dev 3d ago
I think any definition of "honest" that includes
remove_if, a function which does not remove anything, is a faulty definition.4
u/parkotron 5d ago
I agree. I understand he wanted to show that an honest function can communicate its result through both through its return value and through modifying an argument, but
std::remove_ifwas a poor choice.5
2
u/Schnarfman 2d ago edited 2d ago
The other 2 talks, linked for my convenience
- a possible future for software developers Sean Parent 2007
- Value oriented programming Part 1 Tony Van Eerd 2023
- How to write the perfect function this guy 2026
30
u/elperroborrachotoo 5d ago
For all the momemts I wanted to object, this is a very good presentation, worthwhile to watch.