I Don’t Believe It!

I Don’t Believe It!

By Frances Buontempo

Overload, 32(180):2-3, April 2024


Sometimes we are surprised by unexpected outcomes or how long things take. Frances Buontempo confesses to how she’s lost hours recently, but learnt from the experiences.

I recently spoke at CppOnline [CppOnline], a new online-only conference. It was loads of fun, though it always feels odd talking to your monitor and hoping someone is listening. We were advised to close unnecessary applications and browser tabs down to ensure smooth performance of our machines while we spoke. You may find this hard to believe, but I spent about four hours closing browser tabs, taking up time I could have otherwise spent on an editorial. I currently have 62 open; a grand improvement on the 99 or more before the conference. No editorial though, sorry.

If you’re not a tab hoarder you might find spending so much time closing tabs very strange, but I know I am not the only person who does this. I could just bookmark pages, but I gave up on bookmarks years ago, because links went stale and I had so many I couldn’t find anything. If I have a tab open, it’s usually something I do want to read or listen to at some point, and then maybe make notes or buy music or similar. One tab I closed was for a new turntable, because our old one seemed to have stopped working. I bit the bullet and bought the new turntable. It’s excellent and in the process of setting it up, I discovered why the old turntable didn’t work. The pre-amp was unplugged. The new bit of kit does have a USB port though, so I can record all my old records one day. Closing that tab was expensive, informative and has probably caused another time consuming job.

Another tab was The Return of -1/12 by Numberphile on YouTube [Numberphile]. They discussed infinite series. As many of you know, 1 + ½ + ¼ + … equals 2. We can prove this, since writing

means

which tells us when we subtract both we get 2S - S = S = 2. QED. That doesn’t seem unreasonable. However, if we were now to try writing 1 + 10 + 100 + … we get into trouble. Writing

S=1+10+100+…

would mean we could have

10S=10+100+1000+…

so we would then be claiming 10S - S= 9S = -1. I’m not sure about you, but this suggests the sum, S, is -1/9, which seems very unlikely. Of course, there is a restriction on the terms of the infinite sum. The terms need to decrease by enough so that we can actually write the equals sign, otherwise the sum doesn’t converge on a number and we end up with unbelievable nonsense. Maybe you already know about infinite series and analytic continuations [Wikipedia-1], which allow us to extend the domain of functions. They are not to be confused with algebraic continuations which allow us to continue execution using futures and similar, and might mean I end up with more tabs open again were I to try to explain in detail. The take away message is that reasoning is often caveated with prerequisites; for example, a radius of convergence for a series. Applying similar logic in different circumstances may lead to surprises or mistakes. If something seems unbelievable, like adding positive numbers and getting a negative answer, an assumption you are making might be wrong.

A relevant computing example concerns benchmarking. A long time ago, Roger Orr wrote an article entitled ‘Order notation in practice’, based on his talk at an ACCU conference [Orr14]. He demonstrated various factors which also influence the performance of an algorithm besides its complexity measure. He discussed strlen, and discovered many compilers had optimised away the call, so the theory didn’t match the practice. Trying to build up an intuition about possible outcomes, so you spot when something is amiss, is an important skill, so well spotted Roger. Kevin Carpenter talked about building intuition at MeetingCpp [Carpenter23], and discussed making educated guesses, which may or may not be true. I couldn’t attend his talk, because it clashed with mine, so I had a tab open to listen at some point. Fortunately, I managed to catch his re-working of the talk live at CppOnline and even ask a question. So, I closed another tab.

Our intuition can be wrong, but we need to start somewhere. Lots of interesting mathematics falls out of proving a first guess is incorrect, or finding circumstances under which the ordinary does not happen, leaving us with something extraordinary. And wondering what-if can be fruitful. Whether that’s imagining a square root of -1, or exploring what is possible at compile time, new disciplines emerge. However, sometimes wondering why we have 5 test cases for a function with 7 if/else branches leads us to deduce we can delete the extra branches. The tests may still pass, however there’s a chance someone forgot to add more tests when they added more code. Mutation testing might well pick this kind of thing up. If you’re not familiar with this, at a high level it randomly mutates the code, dropping branches, changing + to – and similar, and reports back if any tests still pass. Filip von Laenen wrote an article about mutation testing for us back in 2012 [vonLaenen12] if you want to know more. He did say at the time he wasn’t a C++ programmer so could only give details on other languages and mention a couple of frameworks in C++ he was aware of. Perhaps the time has come for someone to write a new article telling us about current tools?

Tests for branches in code came to mind because Jez Higgins recently tooted [Higgins24a] about some flappy code he refactored, which had more branches than tests. Of course, a code coverage tool should pick that up, though mutation testing may find other problems. Jez spotted this by eye from simply looking at the code and wrote about this in a blog [Higgins24b]. Thankfully, he has followed it up with the refactorings to make the code better, and allowed us to include the write up in this issue. The code he considers in his blog is unbelievable, but untidy and confusing code does emerge over time, and you need to find time to tidy up once in a while, otherwise the weeds grow and take over. As a side note, we caught up with Jez at the Norfolk Developers Conference [NorDev], which a handful of ACCU people based in the UK go to. Jez didn’t have a ticket for the speakers’ dinner, so found an EMF gig in town that evening instead. Unbelievable. (Possibly a niche joke if you don’t know the band EMF, but here’s a famous song by them [EMF]: You’re unbelievable. Apologies).

I picked the title ‘I don’t believe it’ based on an oft-repeated phrase by a TV character, Victor Meldrew [IMDB]. A variety of slightly unlikely things happen to him, and he usually responds with a variation of the phrase “I don’t believe it.” I caught myself saying this a few times recently, and treating that as a warning because the character is a slightly sulky old man. Not something to aspire to. Now, not all unbelievable things are negative. For example, finding a gig at the last minute is a nice surprise. Fighting some code for a couple of hours and finding it compiles is always a surprise too, but often leaves you wondering if it really works. Life is so much calmer if you can take tiny baby steps to refactor something. I hope Jez does write up his refactoring steps – maybe we can see this as an article in Overload. Refactoring is an important skill, and I suspect many of us still have lots to learn.

As languages change, we need to keep learning. It’s never easy, and I don’t know about you, but I am often surprised when I come across things I hadn’t noticed before. One of the many tabs I closed was from CppReference, telling me all about std::piecewise_construct [CppRef-1]. (Aside: you know I am reopening these tabs to double check what they say as I write: place bets on my tab count when I’m done.) The std::piecewise_construct_t is an empty class tag type and is used to differentiate between functions taking a tuple of two elements and those taking two arguments directly. In contrast, the next tab told me about std::forward_as_tuple [CppRef-2]. This allows me to construct a tuple of references to forward as an argument to a function. CppReference gives an example using a map:

  std::map<int, std::string> m;

We can then add a value like this:

  m.emplace(std::piecewise_construct,
            std::forward_as_tuple(10),
            std::forward_as_tuple(20, 'a'));

How we ended up needing this, I can only imagine. Perhaps someone will write in and tell me? Seriously, if you do fall across something in C++, or any language, you hadn’t spotted before, write a page for us and send it my way. Let’s help each other learn. There will be motivating examples and reasons behind the piecewise construct and forward as tuple. I just haven’t followed this up, because my tab count has now hit 68. I could wander over to the bookcase and look it up in a book instead, but then I definitely wouldn’t get an editorial written.

Talking of obscure parts of C++, I have been reviewing a manuscript for a potential book, and noticed a sidebar claiming C++23 added the new keyword really. My first instinct was, oh no, yet another thing I didn’t notice. The writer had not explained what it did or why it was introduced, so like a sucker I opened yet another tab or three, and went hunting. I did find a blog post [D’Angelo22] which has the subtitle ‘A blog for April Fool Day’, which explains a function taking an int, say f(int x), can be called with a double, so the new keyword would allow us to say f(really int x). As for the manuscript I am reviewing, I am tempted to add a link to the xkcd Wikipedian Protestor holding a banner saying “[Citation needed]” [xkcd]. Writers do get things wrong, but hopefully our Overload review team spot any such inexactitudes. Do let us know if we missed anything though.

Forming an intuition takes time and sometimes helps us to form correct instincts, though we all get things wrong from time to time. Again, the counterintuitive results in mathematics, or any discipline, often lead to novel approaches and concepts. This is a good thing. Furthermore, if you get to a point where you think you are so good at something you could do it with your eyes shut, you often get a wake-up call. Again, this is a good thing, because it should encourage you to up your game and keep learning. Hopefully you won’t turn into Victor Meldrew, moaning and complaining, while muttering “I don’t believe it” instead. The unfamiliar is an opportunity. I recall a discussion about Duff’s device [Wikipedia-2] when I had been programming for a living for a year or so and thought I knew it all. This stopped me in my tracks. I still have to concentrate on how the loop unrolling works and what is going on. It’s weird, confusing and kinda beautiful all at once. I suspect most programmers enjoy slightly surprising edge cases and unusual ways to do things, because we enjoy thinking and learning.

What have we learnt? Citations are a good thing, because at least they may stop you falling for an April Fools’ joke. Some things are unbelievable because they are incorrect and based on false assumptions. Other things are unbelievable because we just discovered a whole new approach. Let’s check our results from time to time, and try to avoid resting on our laurels. Surprises can be annoying, but they can be wonderful too. And, 64 tabs, in case you wondered.

References

[Carpenter23] Kevin Carpenter, ‘Tooling Intuition’, presented at Meeting C++ 2023, available at https://www.youtube.com/watch?v=mmdoDfw9tIk

[CppOnline] https://cpponline.uk/

[CppRef-1] CppReference: std::piecewise_construct, https://en.cppreference.com/w/cpp/utility/piecewise_construct

[CppRef-2] CppReference: std::forward_as_tuple https://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple

[D’Angelo22] Guiseppe D’Angelo, ‘C++23 will be really awesome’, available at https://www.kdab.com/cpp23-will-be-really-awesome/

[EMF] ‘You’re Unbelievable’ performed by EMF: https://www.youtube.com/watch?v=g4gU74gMbp0

[Higgins24a] Jez Higgins, March 2024, https://mastodon.me.uk/@jezhiggins/112039275413895974

[Higgins24b] Jez Higgins, ‘To see a world in a grain of sand’, blog post published 24 February 2024 at https://www.jezuk.co.uk/blog/2024/02/to-see-a-world-in-a-grain-of-sand.html

[IMDB] Victor Meldrew, character from One Foot in the Grave:https://www.imdb.com/title/tt0098882/characters/nm0934014

[NorDev] Norfolk Developers Conference: https://nordevcon.com/

[Numberphile] Tony Feng ‘The Return of -1/12’, uploaded February 2024, available athttps://www.youtube.com/watch?v=FmLIGN8ZGdw

[Orr14] Roger Orr, ‘Order Notation in Practice’ in Overload 124, December 2014, https://accu.org/journals/overload/22/124/orr_2043/

[vanLaenen12] Filip van Laenen ‘Mutation Testing’ in Overload 108, April 2012, https://accu.org/journals/overload/20/108/overload108.pdf#page=17

[Wikipedia-1] Analytic continuation: https://en.wikipedia.org/wiki/Analytic_continuation

[Wikipedia-2] Duff’s device: https://en.wikipedia.org/wiki/Duff%27s_device

[xkcd] https://xkcd.com/285/

Frances Buontempo has a BA in Maths + Philosophy, an MSc in Pure Maths and a PhD using AI and data mining. She's written a book about machine learning: Genetic Algorithms and Machine Learning for Programmers. She has been a programmer since the 90s, and learnt to program by reading the manual for her Dad’s BBC model B machine.






Your Privacy

By clicking "Accept Non-Essential Cookies" you agree ACCU can store non-essential cookies on your device and disclose information in accordance with our Privacy Policy and Cookie Policy.

Current Setting: Non-Essential Cookies REJECTED


By clicking "Include Third Party Content" you agree ACCU can forward your IP address to third-party sites (such as YouTube) to enhance the information presented on this site, and that third-party sites may store cookies on your device.

Current Setting: Third Party Content EXCLUDED



Settings can be changed at any time from the Cookie Policy page.