Skip to main content

CodeSOD: An Exert Operation

3 weeks 5 days ago

The Standard Template Library for C++ is… interesting. A generic set of data structures and algorithms was a pretty potent idea. In practice, early implementations left a lot to be desired. Because the STL is a core part of C++ at this point, and widely used, it also means that it's slow to change, and each change needs to go through a long approval process.

Which is why the STL didn't have a std::map::containsfunction until the C++20 standard. There were other options. For example, one could usestd::map::count, to count how many times a key appear. Or you could use std::map::findto search for a key. One argument against adding astd::map::containsfunction is thatstd::map::count basically does the same job and has the same performance.

None of this stopped people from adding their own. Which brings us to Gaetan's submission. Absent a std::map::contains method, someone wrote a whole slew of fieldExists methods, where field is one of many possible keys they might expect in the map.

bool DataManager::thingyExists (string name) { THINGY* l_pTHINGY = (*m_pTHINGY)[name]; if(l_pTHINGY == NULL) { m_pTHINGY->erase(name); return false; } else { return true; } return false; }

I've head of upsert operations- an update and insert as the same operation, but this is the first exert- an existence check and an insert in the same operation.

"thingy" here is anonymization. The DataManager contained several of these methods, which did the same thing, but checked a different member variable. Other classes, similar to DataManager had their own implementations. In truth, the original developer did a lot of "it's a class, but everything inside of it is stored in a map, that's more flexible!"

In any case, this code starts by using the [] accessor on a member variable m_pTHINGY. This operator returns a reference to what's stored at that key, or if the key doesn't exist inserts a default-constructed instance of whatever the map contains.

What the map contains, in this case, is a pointer to a THINGY, so the default construction of a pointer would be null- and that's what they check. If the value is null, then we erase the key we just inserted and return false. Otherwise, we return true. Otherotherwise, we return false.

As a fun bonus, if someone intentionally stored a null in the map, this will think the key doesn't exist and as a side effect, remove it.

Gaetan writes:

What bugs me most is the final, useless return.

I'll be honest, what bugs me most is the Hungarian notation on local variables. But I'm long established as a Hungarian notation hater.

This code at least works, which compared to some bad C++, puts it on a pretty high level of quality. And it even has some upshots, according to Gaetan:

On the bright side: I have obtained easy performance boosts by performing that kind of cleanup lately in that particular codebase.

[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.
Remy Porter

George Lucas Makes First Comic-Con Appearance to Discuss His Upcoming 'Museum of Narrative Art'

3 weeks 5 days ago
Star Wars creator George Lucas made his first Comic-Con appearance ever on Sunday. The Hollywood Reporter describes the scene: Thousands waited hours just to get inside, chanted "Lu-cas, Lu-cas!" while they waited, and then gave a wild standing ovation as the filmmaker took to the stage, introduced by rapper-actress Queen Latifah, and sat down next to filmmaker Guillermo del Toro and Star Wars production designer Doug Chiang. If the 6,500-strong crowd was disappointed he didn't talk a whiff about Star Wars or Indiana Jones, it wasn't shown, as cries of "I love you, George!" and waving lightsabers punctuated the air several times. Lucas even received a standing ovation when he left the presentation, which was devoted entirely to the Lucas Museum of Narrative Art. He, along with museum board member and fellow art collector del Toro and Chiang, were there to not only give a first look at the museum but also make a case for the importance and validity of narrative art, which includes comic book art, as a vital form of expression... A video presentation showed interior looks at the museum — there are no right angles anywhere, Latifah underscored — as well as images that will be in the collection. A cover of DC comic Mystery in Space, featuring the first appearance of Adam Strange; the first ever Flash Gordon comic strip; a cover of 1950s EC comic Tales from the Crypt; strips of Peanuts and Garfield; art ranging from Brian Bolland and Hellboy creator Mike Mignola to underground cartoonist Robert Crumb, Windsor McKay and Moebius; art of Astro Boy and Scrooge McDuck. But there were also images of art by Norman Rockwell, N.C. Wyeth and Frieda Kahlo. Also in the museum will be concept and storyboard art from Star Wars and Raiders of the Lost Ark by Ralph McQuarrie and Jim Steranko, as well as the props of starships and speeders from various Star Wars movies. Chiang explained that comic art in particular had long been discounted. "It's not taken seriously," he said, and when he was younger was told, "You will outgrow it one day.... I'm so glad I didn't," he said, before driving home the point that one of the strengths of narrative art is that it's driven by story. "Story comes first. Art comes second...." The museum, which has had its opening pushed back several times, is slated to open in 2026. More Comic-Con highlights: Pennywise, the scary clown, returns in the upcoming HBO series "IT: Welcome to Derry" Comic-Con also saw a trailer for the new rock mockumentary Spinal Tap II: The End Continues. (Bassist Derek Smalls is now apparently into cryptocurrency...) Breaking Bad creator Vince Gilligan has a new series called Pluribus coming to AppleTV+, a nine-episode sci-fi drama starring Rhea Seehorn from Better Call Saul. (Watch its bizarre trailer here.)

Read more of this story at Slashdot.

EditorDavid