New-age Transactional Systems - Not Your Grandpa's OLTP
John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.
The content has been bookmarked!
There was an error bookmarking this content! Please retry.
Posted by Jonathan Allen on Jun 01, 2009
C# does not work well with boxed numerical values. Unlike Visual Basic, the basic numeric comparison operators such as == do not work with boxed types even when both values are the same type.
| Variable | Type | Value |
|---|---|---|
| a | int | 0 |
| b | decimal | 0.0 |
| c | decimal | 0.0 |
| boxA | boxed int | 0 |
| boxB | boxed decimal | 0.0 |
| boxC | boxed decimal | 0.0 |
| dynA | dynamic holding an int | 0 |
| dynB | dynamic holding a decimal | 0.0 |
| dynC | dynamic holding a decimal | 0.0 |
| Comparison | C# | VB |
|---|---|---|
| a==b | true | true |
| b==a | true | true |
| b==c | true | true |
| a.Equals(b) | false | <-- |
| b.Equals(c) | true | <-- |
| boxA == boxB | false | true |
| boxB == boxA | false | true |
| boxB == boxC | false | true |
| boxA.Equals(boxB) | false | <-- |
| boxB.Equals(boxC) | true | <-- |
| dynA == dynB | true | n/a |
| dynB == dynA | true | n/a |
| dynB == dynC | true | n/a |
As you can see, using C# 3 and earlier even two boxed decimals with the same value will evaluate as being unequal. This occurs even when the Equals method on the Decimal class would otherwise return true.
Fortunately with C# 4 you can avoid these problems. By first casting the boxed values as dynamic, you do get the correct results even when comparing different types.
Console.WriteLine((dynamic)boxA == (dynamic)boxB);
Using Drools? See what you're missing! Get the Power of Drools with the Assurance of Red Hat
Getting Started with Stratos - an Open Source Cloud Platform
Why NoSQL? A primer on Managing the Transition from RDBMS to NoSQL
Agile Maturity Model Applied to Building and Releasing Software
I like the phrase "does not work well". C# has well-defined rules that avoid guesswork. Not just for the compiler, but also for the human reader. Trying to be too smart would make other (more likely cases) harder to read. VB is better in this respect only if you do trial-and-error coding.
To compare boxed values of the same type, use the static method Object.Equals (a, b). This is much more explicit than using "dynamic".
Comparing boxed integers to boxed decimals smells bad in any case. If you really need it, being a bit more explicit about it won't hurt.
I agree. This was all designed purposely. It was the design we wanted. C# was a very very "static" language, where (most) everything had to be explicit. If for a particular project, I didn't want these behaviors, I would use something else (like VB!).
Now I feel we're losing an option instead of gaining some.
What are we losing? "dynamic" is opt-in, and it consistently behaves as you would expect a dynamic language to. Don't want it, use "object".
John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.
Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.
Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.
Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).
Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.
Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.
One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.
InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.
3 comments
Watch Thread Reply