jump to navigation

Extend Your Objects January 25, 2008

Posted by gordonwatts in computers.
trackback

Sometimes, when coding up a particle selection, you end up with code that looks like the following:

if (abs(jet.Eta()) < 2.0 && jet.Pt() > 10.0 && jet.EMF()<0.90) {
  good_jets.push_back(jet);
}

That is pseudo code, of course. The jet object is some collaboration defined object — not something you can really alter. But that selection can get pretty messy and long (especially for things like electrons) and you want to share it with others, so you write a small subroutine so you can write

if (ismykindofjet(jet)) {
  good_jets.push_back(jet);
}

Promotes reuse, factorization — all sorts of good things. I would like to see C++ add a bit of extra syntactic sugar (which is so _not_ C++ like):

if (jet.ismykindofjet()) {
  good_jets.push_back(jet);
}

It just looks so much cleaner. In other languages these are called extension methods. They don’t give you access to the private variables of the class – but they do make the code look cleaner. At least, they would make my code look a lot cleaner!

I suppose it is too late to get something like this into the next round of C++.๐Ÿ™‚

Comments»

1. anonymous - January 26, 2008

Why not subclass jet or make a wrapper class that wraps jet? Both solutions could provide the ismykindofjet() method.

2. Anonymous - January 26, 2008

#define jet_ismykindofjet ismykindofjet(jet)

Be my jet
Be my jet,
Are you gonna be my jet?
Yeah!

I think this may be apropos:
http://www.lyrics007.com/Jet%20Lyrics/Will%20you%20be%20my%20girl%3F%20Lyrics.html

ohhhh YEAH baby (pirouettes, strikes a foolish pose)

3. hrivnac - January 26, 2008

If you are allowed to use Java, you can use Groovy (which is bytecode-compatible with Java). Then you can do something like this:
// Define “extension” method within a class
class IntegerCategory {
static boolean isEven(Integer value) {
return (value&1) == 0
}
}

// Use it
use (IntegerCategory.class) {
println 2.isEven()
println 3.isEven()
}

4. carlbrannen - January 26, 2008

I use java instead of c or c++ because it is so tightly type cast that by the time I’ve gotten something to compile it’s close to running correctly. And java does encourage this sort of thing.

5. Gordon Watts - January 27, 2008

Subclassing almost gets me there. The only thing it misses is when both I and someone else add code – now when we try to share we have to modify our code and some how coordinate in a way we wouldn’t if you could just “add” a new method. The “#define” doesn’t work because if I have “jet1” and “jet2”, etc. Like the song!!๐Ÿ™‚

I wasn’t aware Java had things like that — but great! I’m a little suprised by your comment about staticly typed language — I thought java was exactly that. Languages like python, ruby, and visual basic are dynamically type/late binding and so are very different. But java and C++, at least on that scale, fall into the same bucket.

6. gordonwatts - January 27, 2008

hrivanc – sorry your comment didn’t appear earlier – wordpress flagged it as spam and I didn’t see right away that it was waiting for me to look at.

Yes — that is exactly what I want. (C# has something just like that, which is what triggered this post). Unfortunately, no one, as far as I can tell, uses java or anything else for end-user analysis. So that is an awful lot of infrastructure that has to be ported first.๐Ÿ˜ฆ

7. hrivnac - January 28, 2008

I don’t agree that nobody is using Java & comp. for data analyses. There are many users of JAS and jHepWork (certainly not in Atlas). And The Atlas event display – Atlantis – is written in Java.

The example I mentioned above was not directly in Java, but in Groovy. Groovy is a kind of Java-extension – dynamically typed language. It is completely compatible with Java (you can freely mix them in one program) and it has many unsafe features which some people would like to have in Java (like operator overloading, multiple inheritance,…). And it has excellent support for XML and SQL. It is, for example, very easy to create a histogram from SQL table: http://hrivnac.free.fr/wordpress/?p=72

Java itself can support features you have asked for via “Dynamic Proxy”:
http://www.ibm.com/developerworks/java/library/j-jtp08305.html It is very powerful system, but a bit complex.

[It is strange that all my cotributions are marked as spams – maybe because of included URLs ?]

8. Weblog of Julius Hrivnac » Blog Archive » Object Extensions - January 28, 2008

[…] The complete thread is here. […]

9. Extend Your Language « Life as a Physicist - January 31, 2008

[…] January 31, 2008 Posted by gordonwatts in computers. trackback A few days ago I had a post about extending objects – the desire to add little utility methods to objects that others had defined (and control). A few […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: