Oren Eini

aka Ayende Rahien

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,579
|
Comments: 51,204

Copyright ©️ Ayende Rahien 2004 — 2025

Privacy Policy · Terms
filter by tags archive
stack view grid view
  • architecture (609) rss
  • bugs (450) rss
  • challanges (123) rss
  • community (378) rss
  • databases (481) rss
  • design (895) rss
  • development (640) rss
  • hibernating-practices (71) rss
  • miscellaneous (592) rss
  • performance (397) rss
  • programming (1085) rss
  • raven (1446) rss
  • ravendb.net (530) rss
  • reviews (184) rss
  • 2025
    • June (1)
    • May (10)
    • April (10)
    • March (10)
    • February (7)
    • January (12)
  • 2024
    • December (3)
    • November (2)
    • October (1)
    • September (3)
    • August (5)
    • July (10)
    • June (4)
    • May (6)
    • April (2)
    • March (8)
    • February (2)
    • January (14)
  • 2023
    • December (4)
    • October (4)
    • September (6)
    • August (12)
    • July (5)
    • June (15)
    • May (3)
    • April (11)
    • March (5)
    • February (5)
    • January (8)
  • 2022
    • December (5)
    • November (7)
    • October (7)
    • September (9)
    • August (10)
    • July (15)
    • June (12)
    • May (9)
    • April (14)
    • March (15)
    • February (13)
    • January (16)
  • 2021
    • December (23)
    • November (20)
    • October (16)
    • September (6)
    • August (16)
    • July (11)
    • June (16)
    • May (4)
    • April (10)
    • March (11)
    • February (15)
    • January (14)
  • 2020
    • December (10)
    • November (13)
    • October (15)
    • September (6)
    • August (9)
    • July (9)
    • June (17)
    • May (15)
    • April (14)
    • March (21)
    • February (16)
    • January (13)
  • 2019
    • December (17)
    • November (14)
    • October (16)
    • September (10)
    • August (8)
    • July (16)
    • June (11)
    • May (13)
    • April (18)
    • March (12)
    • February (19)
    • January (23)
  • 2018
    • December (15)
    • November (14)
    • October (19)
    • September (18)
    • August (23)
    • July (20)
    • June (20)
    • May (23)
    • April (15)
    • March (23)
    • February (19)
    • January (23)
  • 2017
    • December (21)
    • November (24)
    • October (22)
    • September (21)
    • August (23)
    • July (21)
    • June (24)
    • May (21)
    • April (21)
    • March (23)
    • February (20)
    • January (23)
  • 2016
    • December (17)
    • November (18)
    • October (22)
    • September (18)
    • August (23)
    • July (22)
    • June (17)
    • May (24)
    • April (16)
    • March (16)
    • February (21)
    • January (21)
  • 2015
    • December (5)
    • November (10)
    • October (9)
    • September (17)
    • August (20)
    • July (17)
    • June (4)
    • May (12)
    • April (9)
    • March (8)
    • February (25)
    • January (17)
  • 2014
    • December (22)
    • November (19)
    • October (21)
    • September (37)
    • August (24)
    • July (23)
    • June (13)
    • May (19)
    • April (24)
    • March (23)
    • February (21)
    • January (24)
  • 2013
    • December (23)
    • November (29)
    • October (27)
    • September (26)
    • August (24)
    • July (24)
    • June (23)
    • May (25)
    • April (26)
    • March (24)
    • February (24)
    • January (21)
  • 2012
    • December (19)
    • November (22)
    • October (27)
    • September (24)
    • August (30)
    • July (23)
    • June (25)
    • May (23)
    • April (25)
    • March (25)
    • February (28)
    • January (24)
  • 2011
    • December (17)
    • November (14)
    • October (24)
    • September (28)
    • August (27)
    • July (30)
    • June (19)
    • May (16)
    • April (30)
    • March (23)
    • February (11)
    • January (26)
  • 2010
    • December (29)
    • November (28)
    • October (35)
    • September (33)
    • August (44)
    • July (17)
    • June (20)
    • May (53)
    • April (29)
    • March (35)
    • February (33)
    • January (36)
  • 2009
    • December (37)
    • November (35)
    • October (53)
    • September (60)
    • August (66)
    • July (29)
    • June (24)
    • May (52)
    • April (63)
    • March (35)
    • February (53)
    • January (50)
  • 2008
    • December (58)
    • November (65)
    • October (46)
    • September (48)
    • August (96)
    • July (87)
    • June (45)
    • May (51)
    • April (52)
    • March (70)
    • February (43)
    • January (49)
  • 2007
    • December (100)
    • November (52)
    • October (109)
    • September (68)
    • August (80)
    • July (56)
    • June (150)
    • May (115)
    • April (73)
    • March (124)
    • February (102)
    • January (68)
  • 2006
    • December (95)
    • November (53)
    • October (120)
    • September (57)
    • August (88)
    • July (54)
    • June (103)
    • May (89)
    • April (84)
    • March (143)
    • February (78)
    • January (64)
  • 2005
    • December (70)
    • November (97)
    • October (91)
    • September (61)
    • August (74)
    • July (92)
    • June (100)
    • May (53)
    • April (42)
    • March (41)
    • February (84)
    • January (31)
  • 2004
    • December (49)
    • November (26)
    • October (26)
    • September (6)
    • April (10)
Deep Dive into RavenDB webinars
  previous post next post  
Mar 05 2010

Sometimes I have code blinders on

time to read 1 min | 82 words

This is a piece of code that I am using in RDB, at some point, it threw a null reference exception:

image

I am ashamed to admit that I started doing some really deep debugging to understand the bug (this happen only under very strange circumstances).

When I figured out what it was, I was deeply ashamed, this is easy.

Tweet Share Share 31 comments
Tags:
  • Rhino DivanDB

  previous post next post  

Comments

Ian Nelson
05 Mar 2010
10:08 AM
Ian Nelson

index variable might not be populated if TryGetValue fails, which would caused log.DebugFormat to throw a NullReferenceException ?

Ian Nelson
05 Mar 2010
10:10 AM
Ian Nelson

Oops, I'm blind too, just realised that "value" is the out parameter :-$

neonp
05 Mar 2010
10:11 AM
neonp

value maybe null if trygetvalue fails. So you have a NullReferenceException when call value.IndexDocuments(...)

Benny Thomas
05 Mar 2010
10:11 AM
Benny Thomas

Been there, done that! :-)

FallenGameR
05 Mar 2010
10:12 AM
FallenGameR

Ayende, why have you stoped using HTML highlighting?

Not it's only images for code.

Ian Nelson
05 Mar 2010
10:12 AM
Ian Nelson

OK, try again Ian:

index parameter is null in rare cases, TryGetValue returns false in this case, and log.DebugFormat throws NullReferenceException ?

James McKay
05 Mar 2010
10:13 AM
James McKay

Um...so, the intention is that when you try to index on a non-existent index it should fail silently? That being the case I'd call the method TryIndex rather than Index and have it return a Boolean to indicate failure.

If a method can't do what its name says it does, it should always throw.

anon
05 Mar 2010
10:16 AM
anon

index can be null.

indexes can be null.

value can be null.

Simple.

Tommy Carlier
05 Mar 2010
10:22 AM
Tommy Carlier

Even if TryGetValue fails (and value is null), the last statement is still executed. Missing an else.

Lodewijk
05 Mar 2010
10:32 AM
Lodewijk

String.Format fails because 'index' is null.

Dirk
05 Mar 2010
10:57 AM
Dirk

I think this post was not about "find the bug"! It's about "We all are humans..." ;-)

Brad
05 Mar 2010
11:08 AM
Brad

log is null?

daniel
05 Mar 2010
11:59 AM
daniel

nothing like another pair of eyes at times like that.

efdee
05 Mar 2010
12:28 PM
efdee

What Tommy Carlier said. You forgot 'return' when the TryGetValue fails :-)

Matt
05 Mar 2010
13:10 PM
Matt

I'd be more ashamed of the if (condition == false) bit..

Markus Zywitza
05 Mar 2010
13:11 PM
Markus Zywitza

This is serious. It means that even you cannot code 20 hours a day for more than four weeks... ;-)

Grimace
05 Mar 2010
13:35 PM
Grimace

@Matt

I actually picked up that code style from Ayende. Shorter does not always mean more readable and an exclamation mark is easily overlooked.

I used to apply the exclamation mark, but after trying the "== false" for a while, I'm sold.

Demis Bellot
05 Mar 2010
13:42 PM
Demis Bellot

Yeah we've all been there, usually a good time for a break.

Dmitry
05 Mar 2010
13:45 PM
Dmitry

There should be a "return" statement after log.Debug().

Patrik Hägne
05 Mar 2010
13:57 PM
Patrik Hägne

I'm totally with Matt on this one, the (condition == false) is BAD practice. It's SOOOO much easier to overlook than the exclamation mark. The exclamation mark adds the negation beforehand so that you know that you are negating the statement when you read it.

I feel you on the bug though, been there, done that and in the end it's always like DUH!

Tommy Carlier
05 Mar 2010
14:27 PM
Tommy Carlier

@Patrick I also use the exclamation mark, but I also think it's not always very obvious (only 1 character that looks like a letter). I'd actually prefer a "not"-keyword here, like in VB.

Maybe the "condition == false" wouldn't be as easy to overlook if you turned it around (like they do in C++ for a different reason):

if (false == condition) { ... }

Tommy Carlier
05 Mar 2010
14:31 PM
Tommy Carlier

Actually, now that I think about it: I sometimes create 2 versions of a method: the regular one and the "not"-one. Some extension methods I wrote:

  • ICollection <t.HasItems(): same as Count > 0

  • ICollection <t.IsEmpty(): same as Count == 0

  • string.IsEmpty(): same as string.IsNullOrEmpty(s)

  • string.IsNotEmpty(): same as !string.IsNullOrEmpty(s)

Especially the HasItems/IsEmpty on the collection show the intention of the code.

Jason Meckley
05 Mar 2010
14:34 PM
Jason Meckley

It's not that apparent to me either. I think it's that index will be null if TryGetValue returns false. in which case you either need to return after logging or put value.IndexDocuments in an else block.

Mr_Simple
05 Mar 2010
15:15 PM
Mr_Simple

@Grimace

false == is even better. Saves time running after stupid bugs.

alwin
05 Mar 2010
18:29 PM
alwin

@Tommy Carlier,

Why not string.HasValue() ? instead of IsNotEmpty()

Paul Batum
06 Mar 2010
00:40 AM
Paul Batum

@Mr_Simple

Sure, its better if you're using a language like C, because if you accidentally use '=' instead of '==' then you'll get a compile error. But C# doesn't have this problem, so I'm not sure what "stupid bugs" you're thinking it prevents.

Try It
06 Mar 2010
00:50 AM
Try It

@Paul Batum

Do it, then come back and let us know what your experience is. I think you'll find it errors out quite nicely thank you.

Try It
06 Mar 2010
01:39 AM
Try It

@Paul Batum

I need to man up Paul.

I owe you a great big apology, once I wipe the egg from my face. You're quite right. I was thinking how assigning with constants on the left upsets the compiler.

I'll crawl back under my rock now with your permission.

firefly
06 Mar 2010
05:01 AM
firefly

I think Oren just need another vacation :)

Tommy Carlier
06 Mar 2010
13:42 PM
Tommy Carlier

@alwin I think that "empty" and "not empty" is clearly defined in relation to strings, while "value" is not as clearly defined: what is considered a value?

alwin
06 Mar 2010
14:33 PM
alwin

@Tommy Carlier,

Well of course it's all just a matter of personal taste. For me a string has a value if it has something in it, and yes, spaces are a value too.

I have this:

public static bool IsNullOrEmpty(this string self)

{

return string.IsNullOrEmpty(self);

}

public static bool HasValue(this string self)

{

return !IsNullOrEmpty(self);

}

With HasValue I have one less negation wrt IsNotEmpty, or !IsNullOrEmpty.

Comment preview

Comments have been closed on this topic.

Markdown formatting

ESC to close

Markdown turns plain text formatting into fancy HTML formatting.

Phrase Emphasis

*italic*   **bold**
_italic_   __bold__

Links

Inline:

An [example](http://url.com/ "Title")

Reference-style labels (titles are optional):

An [example][id]. Then, anywhere
else in the doc, define the link:
  [id]: http://example.com/  "Title"

Images

Inline (titles are optional):

![alt text](/path/img.jpg "Title")

Reference-style:

![alt text][id]
[id]: /url/to/img.jpg "Title"

Headers

Setext-style:

Header 1
========
Header 2
--------

atx-style (closing #'s are optional):

# Header 1 #
## Header 2 ##
###### Header 6

Lists

Ordered, without paragraphs:

1.  Foo
2.  Bar

Unordered, with paragraphs:

*   A list item.
    With multiple paragraphs.
*   Bar

You can nest them:

*   Abacus
    * answer
*   Bubbles
    1.  bunk
    2.  bupkis
        * BELITTLER
    3. burper
*   Cunning

Blockquotes

> Email-style angle brackets
> are used for blockquotes.
> > And, they can be nested.
> #### Headers in blockquotes
> 
> * You can quote a list.
> * Etc.

Horizontal Rules

Three or more dashes or asterisks:

---
* * *
- - - - 

Manual Line Breaks

End a line with two or more spaces:

Roses are red,   
Violets are blue.

Fenced Code Blocks

Code blocks delimited by 3 or more backticks or tildas:

```
This is a preformatted
code block
```

Header IDs

Set the id of headings with {#<id>} at end of heading line:

## My Heading {#myheading}

Tables

Fruit    |Color
---------|----------
Apples   |Red
Pears	 |Green
Bananas  |Yellow

Definition Lists

Term 1
: Definition 1
Term 2
: Definition 2

Footnotes

Body text with a footnote [^1]
[^1]: Footnote text here

Abbreviations

MDD <- will have title
*[MDD]: MarkdownDeep

 

FUTURE POSTS

  1. RavenDB GenAI Deep Dive - about one day from now

There are posts all the way to Jun 04, 2025

RECENT SERIES

  1. Recording (16):
    29 May 2025 - RavenDB's Upcoming Optimizations Deep Dive
  2. Webinar (6):
    27 May 2025 - RavenDB's Upcoming Optimizations Deep Dive
  3. RavenDB News (2):
    02 May 2025 - May 2025
  4. Production Postmortem (52):
    07 Apr 2025 - The race condition in the interlock
  5. RavenDB (13):
    02 Apr 2025 - .NET Aspire integration
View all series

RECENT COMMENTS

  • Scooletz, Page faults when working with data that is greater than RAM is not an uncommon issue for us. One of the reasons ...
    By Oren Eini on Recording: RavenDB's Upcoming Optimizations Deep Dive
  • What a massive presentation! As a person who spent some time with a db written in .NET I can strongly relate to some points. ...
    By Scooletz on Recording: RavenDB's Upcoming Optimizations Deep Dive
  • I’d love to learn your thoughts on SPANN https://arxiv.org/abs/2111.08566 that with centroids and keeping the posting lists s...
    By Scooletz on Comparing DiskANN in SQL Server & HNSW in RavenDB
  • Joel, The DiskANN paper talks about it being viable for more than a billion vectors datasets.  In such a scenario, it would ...
    By Oren Eini on Comparing DiskANN in SQL Server & HNSW in RavenDB
  • Do you know why they chose DiskANN? These things are usually about tradeoffs but it seems DiskANN is just worse in every way.
    By Joel on Comparing DiskANN in SQL Server & HNSW in RavenDB

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats
}