Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 6,128 | Comments: 45,550

filter by tags archive

Do you trust your compiler? Really trust your compiler?

time to read 1 min | 83 words

There is a discussion in the alt.net mailing list right now about how far you can and should trust your compiler. I thinks that this is interesting, because this piece of code of mine is on its way to production:

public Guid Create<T>(T newEntity)
{
	using (CrmService svc = GetCrmService())
	{
		object cheatCompiler = newEntity;
		Guid guid = svc.Create((BusinessEntity) cheatCompiler);
		return guid;
	}
}

This is part an implementation of an interface in an assembly that cannot reference BusinessEntity.

I am feeling good with this.


Comments

Peter w

I can tell you one thing about the .NET 1.1 compiler: do not generate your code from an excel spreadsheet function.

If a function line has roughly 60,000 lines, it will compile, but will yield a stack overflow in execution

Jimmy Bogard

Shouldn't you constrain that generic method too? Or would that break the calling assembly?

Ayende Rahien

That would break the assembly that define the interface, that is the problem.

Stefan Wenig

you could use some defensiveness (is/throw argumentexception instead of cast), and I don't see the value of using a generic here. do you mean

Guid Create (BusinessEntity newEntity)

in the interface? then what's wrong with this?

Guid Create (object newEntity)

you might want to consider making the whole interface generic, your class could then just implement IWhatever.

Ant&#227;o

I'm sorry but I consider that bad coding. It's not the compilers fault that you accept runtime exceptions...

If you add a where statement, everything will be fine.

public Guid Create(T newEntity)

    where T : BusinessEntity

{

using (CrmService svc = GetCrmService())

{

    object cheatCompiler = newEntity;

    Guid guid = svc.Create((BusinessEntity) cheatCompiler);

    return guid;

}

}

Ayende Rahien

Anato,

Well, this doesn't really work. The interface has to reside in another assembly, and you can't add constraints to such an interface method.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. The worker pattern - about one day from now

There are posts all the way to May 30, 2016

RECENT SERIES

  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats