Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,640
|
Comments: 51,260
Privacy Policy · Terms
filter by tags archive

A Firefox Secret

time to read 1 min | 73 words

I accidently hit Ctrl+Shift+S on a site ( I inteded to hit Ctrl+Shift+A ) and suddenly the whole site changed, the advertisment that I meant to nuke vanished and everything was so much nicer.

It didn't take a long time to figure out what happened. This key sequence turned off CSS. I'm not sure if ti's Firefox or WebDeveloper that did it, but it's nice anyway.

time to read 3 min | 409 words

I’m writing this hoping that someone could explain me some of the weird things that are going on in there:

  • No way to run just one test without knowing its full name? I’m sitting in front of a test called LoadTest. Every class in the DAL has such a test. I need to type the full name of the class just to get something that in TestDriven I can do by right click and selecting “Run Tests”.
  • What is going on with Expected Exception and AssemblyInitalize? The two posts should explain what is going on there, but basically both of these features are horribly broken.
  • What is going on with sometimes building the application when I run a test and sometimes not building it. Invariably it doesn’t build after I saw a test failing, change my code, and then I’m left scratching my head wondering what the hell happened there.

 

Things that I really like about it:

  • Run all the tests, you get some broken ones. Run the test again, only the previously failed tests are running. This is a pretty good way to get trimming away test case failures without running the full suite over and over again. In many case we are talking about significant speed advantage.
  • Generate good test code that I can later modify. Sure, it generates a method per overload, and that about it. But it makes sure that I don’t forget about anything while I work my way down the list of generated tests. I often turn a dozen tests into a single one. (There really isn’t any need to test simple properties more than once, now is there?)
time to read 4 min | 641 words

I’m writing a fairly complex application right now, using VS 2005 and .Net 2.0

The reason that I mention VS 2005 is that it’s supplementing a lot of the tools that I used to use. NUnit, NAnt, NCover, etc. This is all well and good, but the cost of changing so much at a single time caused me to be slack in writing unit tests. Especially since I’m doing just the DAL right now, and I’m using Castle’s ActiveRecord and NHibernate so it practically writes itself. After writing several thousands lines of code I got really nervous as I contemplated the next step in the chain. I had a piece of code with very few tests, and I was going to use that to build the rest of my application?

So I’m writing stupid unit tests right now, because I’m too stubborn to let it pass as if it didn’t matter.

  • Create the object; verify that all the properties are in the right value.
  • Save the object graph to database, load it again, compare the objects.

Any now and then I get to do something that resembles real unit testing, where I actually get to test logic, and not mechanics. The issue here is that in the middle of all the tests that VS generously provided via code generation, I started to slowly carve my own tests. VS produce very extensive unit tests, most of them I discard, but it’s pretty good in making sure that I wouldn’t miss the important ones.

I found several cases where the unit testing framework in VS didn’t behave as it should, and the whole thing is actually driving me crazy sometimes. No easy way to just point at a test/class/folder and just run the tests there. And I can’t get TestDriven 1.1 to work on VS RC (that is, I can’t get it to handle the VS’ Unit Testing, it handles NUnit just fine).

What I did find was that even writing the tests after the fact, I started to improve the code, just so I could write decent tests. I’m not talking about refactoring; this is way too early for that and the code is pretty clean. It’s actually those little things like checking for null, and valid ranges, and the like, which I skipped during the first rush to get the DAL working (I tested several approaches to write the DAL, and you could probably say that the current code started as a spike.).

What is interesting is that I uncovered a few routine bugs, a misspelled configuration that would cause an immediate failure in runtime. I just fixed those without being impressed, since I would’ve caught them the first time I would’ve started the application anyway.

What really impressed me was during this routine test writing, I managed to stumble on a fairly subtle bug. I wasn’t saving some information to the database, so I was getting the default instance back from another routine. This may have very well escaped my notice, since even after finding it, I was convinced that it couldn’t happen, I thought about this scenario, and I gave a solution to it. When I arrived at the site of the bug, I found out that while I originally thought about it, I later modified the code and introduced the bug. If I’d a unit test then, I would’ve spotted it the minute I did it. I don’t like to think how this would have appeared if it managed to get to the working application stage, where you get a report about something totally unrelated and finally track your way to the root cause.

In short, Unit Testing is Good, but VS 2005 is still driving me nuts.

time to read 6 min | 1057 words

C# has indexers, but it doesn’t support indexed properties. By that I mean the ability to do something like:
config.Options[“Username”] = “Ayende”;
To be exact, the above syntax is possible, but only by cheating. While the CLR support indexed properties, in C# you need to return an object which has an indexer, leading to a common pattern of inner classes in order to allow this syntax.
Here is the last such set of classes that you’ll need, served to you by the power of delegates and generics. One thing to note here is that there is some code duplication here, I can't see how to prevent that withou mutiply inheritance, and it's a very small amount of code, so I allowed it.

public class PropertyIndexer<RetType, IndexType>
{
 private Getter getter;
 private Setter setter;
 public delegate RetType Getter(IndexType index);
 public delegate void Setter(IndexType index, RetType value);
 
 public PropertyIndexer(Getter getter, Setter setter)
 {
  this.getter = getter;
  this.setter = setter;
 }
 
 public RetType this[IndexType index]
 {
  get { return getter(index); }
  set { setter(index, value); }
 }
}
public class PropertyIndexerGetter<RetType, IndexType>
{
 private Getter getter;
 
 public PropertyIndexerGetter(Getter getter)
 {
  this.getter = getter;
 }
 
 public RetType this[IndexType index]
 {
  get { return getter(index); }
 }  
}

public class PropertyIndexerSetter<RetType, IndexType>
{
 private Setter setter;
 public PropertyIndexerSetter(Setter setter)
 {
  this.setter = setter;
 }
 
 public RetType this[IndexType index]
 {
  set { setter(index, value); }
 }  
}

The usage of this class is very simple, here is a sample of using it to allow a Linq like querying over a list of objects.

public class UserContainer
{
 private PropertyIndexerSetter<User, string> usersByName;
 private List<User> users;
 
 public EasyIndexedProperties()
 {
  users = DAL.GetUserList();
  usersByName = new PropertyIndexerSetter<stringint>(
   delegate(string userName) 
   { 
    return users.Find(
      delegate(User user) 
       { return user.Name == userName; }
     )
   });
 }
 public IList<User> Users { get { return users; } }
 public User UserByName { get { return usersByName; } }
}

And the client code looks like this:

UserContainer userContainer = new UserContainer(); 
User user = userContainer.UserByname["Ayende"]; 

The constructor is a little bit threatening, but it’s actually easy to understand what is required of you, certainly better than the 1.1 way of inner class that would get it directly, and then iterating over the list explicitly, etc.

Enjoy

time to read 4 min | 668 words

In this post Nola wonders about the lack of the post/pre increment/decrement operators in Ruby.

I’m by no mean an expert of Ruby (Total lines of code written in Ruby: 2), but I can certainly say that I’ve noticed this situation in other languages and tools that I’m using. Both C# and Java support the ++/-- operators to add/decrement one from a number. The root of this operator is from the days of C, when the compiler wasn’t smart enough to do optimizations.

The whole idea was that i++; would be translated to INC I, a single CPU instruction. The issue wasn’t being developer friendly or anything like it. It was all about getting the correct output from the compiler. Remember, C was built in the days where people wrote whole OS using ASM. It had to allow high level constructs while allowing the optimizations that the programmers already knew and the compiler just couldn’t supply those optimizations.

Fast forward to today, I would like an honest opinion about it, how many of you use these operators outside of a for() loop? Let’s see the possible use cases:

  • I++; è On a line all on its own, incrementing the variable by one. This is just a shorthand to I+=1; or I = I + 1; Many books teaches that no true C-based programmer would the other options when they can use the first.
  • arrayOfSomething[i++]; è Access the array and increment the indexer in one line. A true time saver for those who are debited by lines of codes. (You wrote 100 lines to do that, WTF? J )

 

In the first case, I get a sense of uneasiness when I see a line like this:

I++;

Something in me tells me that it’s not right; it looks like an expression, not a statement. I just get uncomfortable around this code, and I always change it to:

I+=1;

Not without some sense of guilt that now I’ve wasted two whole characters and I’m not concise enough.

Then there is the access the array and increment the indexer in one go:

arrayOfSomething[i++];

Invariably I’ve found that the above can cause problems. Sure, it’s fast to write and it looks elegant, but how often would your eyes just skip it? It’s a side effecty code, and that is never good. Sure, the increment operator is widely known and it’s well understood what is going on their, but I still have hard feeling about this type of code. Invariably I would change it to:

arrayOfSomething[i];

I+=1;

I wasted a couple of characters, but I get a much clearer code this way. The compilers nowadays are more than capable of doing these sorts of optimizations by themselves without me holding their hands. I’m a firm believer in throwing as much as possible on the compiler. If I’ve two code patterns, one efficient and concise[1] and the second less efficient yet elegant, I would almost always go with the second until performance tuning forced me the other way.

Readable code is more important than fast code.



[1] It goes without saying that such a code is also probably not elegant. There is no question if both are elegant and one is more efficient.

time to read 2 min | 400 words

I’m working with a code generation tool right now, and it’s a pretty spiffy one at that. Integration with Vs.Net, wizards, forms and whatever else that strikes your fancy. I’m sure that I can even make it serve coffee and dance a jig, if I found the right buttons. The problem is that while it’s certainly a very smart tool, it’s not letting itself into an invalid state.

Is that a problem? I can hear you say; it’s a Good Thing that it doesn’t allow you to put it in an invalid state. Well, it’s good for the tool, but it’s bad for me. I don’t work like that. I’m not put together like a machine does, and I’m certainly going through stages where my code is invalid. Either it’s not compilable or it doesn’t make sense. Maybe there is someone out there that can get compilable code every time they move a line, but I can’t.

Forcing me to work in the proper way means that I’m going to get very frustrated at the tool. No, I wouldn’t think first about the query’s parameters before I wrote the query, and I wouldn’t like any advice whatsoever about the possibilities of improving my schema because then it would be more normalized. Yes, the tool is probably right, but there are more concerns than the ability to get 100% normalized data. I wrote a couple of tools for developers myself, and I tried to give as much information as possible while not limiting the user to the way I would like it to work with my tools. For instance, if you use NHiberante Query Analyzer, it will show a red icon if the current mapping document is not valid according to the schema, but it will let you continue.

For all other tool makers out there, try to make it as flexible as possible. I don’t think linearly, not nearly (pun intended). Don’t try to force me to do to do so; that way leads to very high levels of user frustration. I’m more than sure that the tool is excellent, and I can certainly appreciate the amount of time that was dedicated to making sure that there was no way to input bad data to the system. But the inability to work my way, and the inability to write code instead of working my way through dozens of GUI wizards are driving me crazy.

Knife Of Dreams

time to read 1 min | 118 words

The Wheel of Time turns, and Ages come and pass, leaving memories that become legend. Legend fades to myth, and even myth is long forgotten when the Age that gave it birth comes again. In one Age, called the Third Age by some, an Age yet to come, an Age long past, a wind rose above the broken mountain named Dragonmount. The wind was not the beginning. There are neither beginnings nor endings to the turning of the Wheel of Time. But it was a beginning."

I’m reading it right now, from the first chapter of the Knife of Dreams. It sends a shiver down my spine as I do so. It has been such a long wait…

A Grave Mistake

time to read 1 min | 194 words

I’m about to commit a really big mistake, and I’m helpless to prevent it.

Product image for ASIN: 0312873077

Knife of Dreams is about to come out in a couple of weeks, and I haven’t read its prologue yet. I’ve been a fan of the Wheel of Time since 1996; the books are one of the main reasons that I’ve any knowledge in English.

I’ve attempted to resist it, but I can’t, I’m about to start reading: Embers Falling on Dry Grass: Prologue to Knife of Dreams

 

I’m sorry; I can’t help it any longer. I gotta read it. It will have to be done in one seating, of course, and afterward I’ll be to ecstatic to sleep, but it’s Robert Jordan that we are talking here. His books are certainly worth it.  

time to read 2 min | 217 words

ObjectDumper is a nice & simple class that uses reflection to output the content of object (including inner objects) to the console.

Andres showed it in the PDC and it’s available in the Linq preview sample in “C:\Program Files\LINQ Preview\Samples\ObjectDumper"

 

Currently it throws everything to the console, but it’s a very simple change to make it output everything to a string and return it.

What is the connection to testing? Well, since it’s capable of output hierarchical data, it’s a convenient way to use compare a complex object, without going through all the properties, and sub properties, and sub-sub-sub-sub properties, etc.

 

Compare:

Assert.AreEqual(

            ObjectDumper.Dump(expected,5),

            ObjectDumper.Dumo(actual,5));

 

To:

 

Assert.AreEqual(expected.Name, actual.Name);

Assert.AreEqual(expected.Description, actual.Description);

… similar code for every property and then going into sub objects, etc …

 

What would you rather write?

 

Naturally, this isn’t 100% fool proof, but it can certainly reduce the amount of time you spend comparing your objects.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. API Design (10):
    29 Jan 2026 - Don't try to guess
  2. Recording (20):
    05 Dec 2025 - Build AI that understands your business
  3. Webinar (8):
    16 Sep 2025 - Building AI Agents in RavenDB
  4. RavenDB 7.1 (7):
    11 Jul 2025 - The Gen AI release
  5. Production postmorterm (2):
    11 Jun 2025 - The rookie server's untimely promotion
View all series

Syndication

Main feed ... ...
Comments feed   ... ...