views:

169

answers:

5

Hi guys

I am trying to create my first fluent interface and I was just wondering what other poeple thought was more fluent and which one they would prefer to use?

Check.Field().Named("est").WithValueOf("sdsd").IsNotNull()

Check.Field("est").WithValueOf("sdsd").IsNotNull()

Check.Field("est").WithValue("sdsd").IsNotNull()

Cheers Anthony

+4  A: 

Last one definitely:

Check.Field("est").WithValue("sdsd").IsNotNull()
OscarRyz
+2  A: 

I concur:

Check.Field("est").WithValue("sdsd").IsNotNull()

As short as possible, while still making sense.

Avoid noise words like .as. .of. .and. .in. unless they add contextual meaning. I've seen fluent interfaces that do this, and it adds nothing useful except more typing and more hoops for the application to jump through when it executes.

Robert Harvey
A: 

Or, to mimic some of the collections, use of Item property:

Check["est"].WithValue("sdsd").IsNotNull()

Some might also say you should just use .IsNull() and negate the whole expression, like this, but that's a "six -vs- half a dozen" thing:

!Check["est"].WithValue("sdsd").IsNull()
lavinio
I prefer IsNotNull method because you can literally read what the code is doing.
SolutionYogi
You can't even be sure IsNotNull() returns a boolean. That is even less likely in a fluent interface, where you'd return `this` - it's possible IsNotNull() returns a collection that can be further filtered (jQuery style), ...IsNotNull().IsVisible()... . Also, in vb this is even less readable - `not Check.filters...IsNull()`
Kobi
Hence my careful wording of "some might say" :). (1) The method name starts with "Is" which is a convention for returning a Boolean, even in a fluent interface - not everything can return "this". (2) The original question was tagged as C# not VB, so I ignored "Not" and just mentioned "!".
lavinio
A: 

Another option might be:

Check.Field("est").IsNotNull("sdsd")

Something like LINQ's FirstOrDefault called with a predicate instead of filtering and then calling FirstOrDefault.

Daniel Straight
I thought of this but i didn't think it was as clear if someone know nothing about the code and literally saw "Check.Field("est").IsNotNull("sdsd")" vs "Check.Field("est").WithValue("sdsd").IsNotNull()" I would have thought the latter would be more clear.
vdh_ant
Well the thing that confuses me, without knowing the code, is how a field with a value of "sdsd" could possibly be null (which I read to mean "have a null value").
Daniel Straight
A: 

What is Check? I suspect it's not needed at all. I understand what you're trying to do, but remember that the expression you're trying to build will still likely end up in an if statement. With that in mind, think about how "if check X" reads. Not too well, imho. Additionally, what does the 'Field' function have to do to resolve that name? Do you have that somewhere else that you could present in a nicer way (perhaps an extension method on some base type?)

Joel Coehoorn
Actually I have the following cases: Check and Validate. Check will throw an exception where as validate will return a bool at the end. So I was thinking the way it would read would be as follows: public void Test(object value) { Check.Argument("value").WithValue(value).IsNotNull(); .... } and my validate case would be like: public void Test(object value) { if (Validate.That("value").WithValue(value).IsNotNull()) { ... } }
vdh_ant
vdh_ant