Example of expression bias?

From: Keith H Duggar <duggar_at_alum.mit.edu>
Date: 18 Jun 2006 21:58:26 -0700
Message-ID: <1150693106.694486.93550_at_y41g2000cwy.googlegroups.com>



Over in c.l.c++.m I ran across this recent example

http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/aa6bf172a1895b4a/

of what I think might be expression bias? What say you?

For those that don't want to decipher the C++ here are the basics (I'm going to rename things some and fill in more fields for ease of discussion). The guy has a type with some fields one of which is name

struct Person {

  string name     ;
  string division ;
  int    salary   ;

  // ... other stuff
} ;

Then he hard-codes an ordering into the type (by overloading the less operator)

struct Person {

  string name     ;
  string division ;
  int    salary   ;

  bool operator< ( Person & that )
  {
    return this.name < that.name ;
  }
  // ... other stuff
} ;

He did this to allow convenient use the standard ordered containers, sorting algorithms, etc without much fuss. Lo and behold, when he wants a simple query "Find a Person with name = X" and he wants to do it quickly when the container is already sorted by name, turns out it's a pain in the ass to do this with the STL algorithms. Why? Well for lots of reasons but one is that C++ has no convenient way to "talk about" fields by there name. It only has the simple access syntax Person.name, etc but no convenient way to indicate subsets of fields to algorithms etc. You have to code up some helper classes, or pass around member pointers, or ...

Also, suppose later you want to search or compute other algorithms based on attributes other than name. Uh oh, you have hard-coded the ordering by name. That asymmetry means implementing algorithms on the other fields is going to look quite different especially if you try to use the library routines. This is why C++ coders so often retreat back to open loop coding ie for(beg;end;step){...}. Because in that ... you can use the regular field name access x.y to get your work done without lots of obscure wrapping.

Anyhow, these are the kinds of C++/OO problems that get really annoying after a while and that it seems would not arise in a relational world. I'm somewhat surprised that more OOers don't see such things as fundamental problems with an OO world view.

  • Keith --
Received on Mon Jun 19 2006 - 06:58:26 CEST

Original text of this message