Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, November 26, 2025

Read the effing Docs

Yeah, not really news, but just fell into one of those traps, where you not only forget/fail to fully read the documentation, but also fall prey to premature optimization.

It probably dates back to my 8086-Assembler-4.77MHz programming times, that I want to avoid an API call whenever possible - so if their result appears invariant, I happily store if to avoid a second call.

40 years later, when working with Android RecylcerViews, this led to a terrible bug that cost me 2 days to identify and fix.

As the docs say for onBindViewHolder (for the RecyclerView.Adpater)

Note that unlike android.widget.ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use getBindingAdapterPosition which will have the updated adapter position.

In order to pass the adapter position down to an onClick lambda, I even — specifically — made it final.

No wonder, that the code didn't work correctly in some — albeit very rare — circumstances, e.g. when an item further up in the list got removed from the view / adapter.

Again, reading the above would make it clear that this was not a time for premature optimization.

I think I should avoid those optimizations at all, the compiler and jvm is quite good at it anyway — especially identifying invariant function calls. Moreover, Android phones no longer run on 4.77MHz CPUs ;)

Lesson learned.

Sunday, April 16, 2023

NULL values again

Today I learned that you essentially cannot concatenate (concat, ||) nullable columns properly in DB2/SQL.

To be honest, I should have know this of course.

I have a table that records money spent, with a date, text, account and a physical location, e.g. a town, district, ... indicating where the money was spent.

It is nullable because not all transactions need to have a place recorded.

For some statistical analysis I wanted to create one column with text and account and the location simply concatenated into one, like account||' '||text||' '||location.

Turns out, then the location is NULL the whole string will turn NULL

Which reminded me - painfully, after about 30min - that NULL is not a value. You cannot do anything with NULL.

So you have to cast it away to a default value, e.g. with coalesce(location,'dummylocation').


Sigh.

Friday, November 30, 2012

I Hate Apps That...

... are only a badly written container for HTML, without any proper error handling...


And btw: this is not Android specific...

Friday, December 10, 2010

Singleton

Oh, yes, faux singletons... haven't we all had them.

via Geek&Poke

Monday, January 25, 2010

Regex Pitfall

I guess this was the first time ever that I had to search/match/replace with regular expressions across line boundaries.
Not the usual multi-line ("/m") operation; quite the contrary, I wanted the source string to be treated as one single line regardless of newline-characters in it.

In other words: the dot (".") should also match a newline.

Not that easy, it turns out.
Quoting from the Regex Tutorial:
The dot matches a single character, without caring what that character is. The only exception are newline characters. In all regex flavors discussed in this tutorial, the dot will not match a newline character by default. So by default, the dot is short for the negated character class [^\n] (UNIX regex flavors) or [^\r\n] (Windows regex flavors).
Changing this behavior is actually language dependent.
Since I needed it within JavaScript (XUL/Thunderbird) I had to revert to a [\s\S] instead of the .
JavaScript [does] not have an option to make the dot match line break characters. In
those languages, you can use a character
class
such as [\s\S] to match any character.
This character matches a character that is either a whitespace
character (including line break characters), or a character that is not a
whitespace character. Since all characters are either whitespace or
non-whitespace, this character class matches any character.
Weird. Unreadable. Requires a comment.
But it works.

Saturday, March 08, 2008

Thursday, September 20, 2007

How to kill a dragon with various programming languages

There's a beautiful princess, prisoner in the highest tower of a castle, guarded by a mighty dragon, and a fearless knight must rescue her…

This is how each language would manage to rescue the princess from the hands of the dragon

  • Java - Gets there, finds the dragon, develops a framework for dragon anihilation with multiple layers, writes several articles about the framework… But doesn't kill the dragon.
  • .NET - Gets there, sees the idea of the Java developer and copies it. Tries to kill the dragon, but the monster eats him.
  • C - Arrives, looks down at the dragon, pulls out his sword, beheads the dragon, finds the princess… And ignores her to see the last checkins of linux kernel cvs.
  • C++ - Creates a basic needle, and gathers funcionality until he has a complex sword that he can barely understand… He kills the dragon, but gets stuck crossing the bridge because of memory leaks.
  • COBOL - Arrives, sees the dragon and thinks that he is too old to kill a monster that big and rescuing the princess, so he leaves.
  • Pascal - He prepares for 10 years to create a dragon anihilation system… When the moment comes, he discovers the program can only take lizards as an entry.
  • VB - Builds a dragon destruction weapon based on several components, jumps to the back of the dragon and in the most critical time he discovers that the sword works only on rainy nights…
  • PL/SQL - Gets data from other dragon slayers, creates tables with n ternary complexity relations, tridimensional data, OLAP, takes 15 years to process the information… And by then, the princess became a lesbian.
  • Ruby - Arrives with massive fame, saying he is the best at anything and when he faces the dragon, he shows a lame motion picture of himself killing a dragon… The dragon eats him out of boredom.
  • Smalltalk - Arrives, analyzes the dragon and princess, turns around and leaves, they are way too inferior.
  • shell - Creates a very powerful dragon slaying weapon… But in the moment of truth, he can't remember how to use it.
  • shell(2)- The guy approaches the dragon with a two line script that kills, cuts, disembowels, impales, chops to pieces and packs the beast, but when he runs it the script grows, it fattens, irritates and puts alcohol in the fire of the dragon…
  • Assembler - He thinks he's doing the right and most efficient things… But he writes an A instead of a D and kills the princess to end up f***ing the dragon.
  • Fortran - Arrives and develops a 45-thousand-code-line-solution, kills the dragon, meets the princess… But she calls him a weakling and runs after the Java programmer who was elegant, and also rich.
  • FOX PRO - Develops a dragon killing system. It's gorgeous and works on the outside, but it's really patched inside, so when he runs the dragon anihilator, he realizes he forgot to index the DBFs.
  • PROCESS ANALYST - Approaches the dragon with two tons of documentation, develops the unified dragon-killing process, he develops a DFD to free the princess and marry her, convinces the dragon that it's the best for him and it won't hurt. When he executes the process, he estimates the effort and the damage he will cause with a plan signed by the Pope, Buddha and Michael Jackson. Then he buys a couple of nukes, 45 cannons, an aircraft carrier and hires 300 heavily armed men… When all he needed was the sword he was holding in his hand in the beginning…
  • CLIPPER: Sets up a routine that loads a codeblock array to insult the dragon, serenade the princess, load the sword in memory, beat the crap out of the dragon, clean the mess, prepare a raspberry milkshake for the princess, make love to her, take a bath, start the car, put it some gas and come back home. When he runs it, he gets a "Bound Error: Array Access" and the dragon eats him with fries.
  • Lisp, where the famous knight-errant, after speaking with numerous experts in dragon-killing, and modeling the knowledge they posess, he programs the system, and when he runs it he realizes he forgot a bracket (bender the offender).
  • HTML: Mounts a web on famous swords used to kill dragons, but he ignores the W3C standards. When he meets the dragon, he finds out the code isn't compatible with his browser, so he's left swordless. The dragon eats him as an appetizer.
  • Prolog: Thinks he needs a weapon to kill the dragon. Searches in a catalog for 182014 weapons. By the time the princess dies of her age, he's achieved to know how to make every weapon starting with A: Atomic Bombs, Anti-Air Weapons, Arches, Ammunition, Axes...
  • PHP: Creates a web page that when he executes it would eliminate the $dragon selecting from a weapons databese in MySQL over an Apache server. Nevertheless he forgot the WHERE in the DELETE query and kills the princess, the dragon, the peasants, the witch, the sorceror and the programmer himself.
  • JavaScript: The programmer tries to kill the great green dragon that spits fire throug his mouth. He creates a script that will delete the dragon when he loads a webpage, to create seconds after, some damsels to throw him flowers and make clapping sounds. Unfortunately he didn't take into account the DOM structure of the lizard, also known as Mozilla, and the only thing he gets is to fill his console of errors and that the Book of Mozilla tells how he was devoured.
  • ActiveX: The programmers create a tunnel to enter the dragon's lair from the castle and run a program that will kil the dragon from a safe and prudential distance. The dragon discovers the tunnel, eats the workers who dug, the dragon slayers, and enslaves every servant in the castle. The castle becomes a dragon-breeding place, full of little dragons that the dragon sends in pop-ups to other castles. The untasty remains of the knights are put in cans of Spam and sent to other castles as well as a warning. (aquelquesiente)
  • Basic. He creates a weapon able to kill paper dragons, but no matter how they improve it, they discover it's not good enough to kill any dragon bigger than a baby poodle.
  • Matlab: They create a loop that calculates the trajectories to shoot a giant arrow at the dragon. The program works flawlessly. What they need now are the voluntaries caoable to launch tha arrow with the necessary strength and accuracy.
  • Videogame Programmer : Spends two years programming a state-of-the-art sword with shaders and all. When the time comes to kill the dragon, he finds that half the knights aren't strong enough to raise the sword. Then someone programs a patch that reveals the sex scenes with the princess and Hillary Clinton makes it a scandal.

Monday, September 10, 2007

From one to many

Last weekend I (again) discovered what every programmer / architect knows anyway:
The step from doing something on ONE item to doing it on MANY items is the hardest.
And there's an add-on:
Moving from ONE to TWO (in the above sense) is more complex than from TWO to MANY (unlimited).
The reason for the latter is that (to me) "2" is still not as generic as "n", or as I like to put it:
2 is just a special case of 1
quite often if you (in code) have to do something specifically twice, you don't use any loop or iteration construct, you just do it twice.
E.g.
foo.doSomething();
bar.doSomething();

only if you have to push beyond 2 people start to use collections and iterations.
for (Object o: somecollection)
{
o.doSomething();
}


just for 2 objects, no-one bothers to create a collection, they just instantiate two objects.

At least thats what I usually do - and I know others as well.

But - as I said - I experienced the "from 1 to n" problem last weekend - let me elaborate:

In our neighborhood we have a great service that delivers a box of vegetables right to our home.
There's also a website, where you can check this weeks contents of the box.

Unfortunately, though, the site does not offer an RSS feed.
No problem, I just create(d) my own, with a simple job, that runs daily:
  1. connect to the site
  2. scrape the contents of the site
  3. re-format them
  4. and put them into a (static) RSS file, hosted on my homepage.
Put that RSS URL into iGoogle and have the contents right there in my personal google portal.

Easy.

Just 2 weeks ago, we decided to have 2 boxes (one with vegetables only, one with vegetables and fruit) on alternating weeks. So I had to re-write my little thing to handle two different "boxes" instead of just one.
(Frankly, I just went for the n-solution, not the 2)

I more or less had to totally re-design the whole hack... About the only thing that remained quite stable was the core RSS handling (through Rome) and the scraping/parsing of the HTML (using HTMLparser).
What I had earlier looked more like a classic C (not C++) program, with almost everything done in main()... I knew it was a bad design... but I did not anticipate the initially.

So, this was a really good lesson for me again, to "design for n" early.

Thursday, August 23, 2007

Paul Graham: Holding a Program in One's Head

The latest essay buy the wonderful, always insightful Paul Graham:
A good programmer working intensively on his own code can hold it in his mind the way a mathematician holds a problem he's working on. Mathematicians don't answer questions by working them out on paper the way schoolchildren are taught to. They do more in their heads: they try to understand a problem space well enough that they can walk around it the way you can walk around the memory of the house you grew up in. At its best programming is the same. You hold the whole program in your head, and you can manipulate it at will.
Read the whole essay Holding a Program in One's Head.