- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Frist feelings: not filling it.
Admin
Technically notFilled is a good name for the method, but they should've still used isNullOrEmpty
Admin
Technically, "notFilled" is a very bad name, because it does the exact opposite of what one would expect.
Admin
I like how the word filled is used here. It's as if a string object is this empty container (vessel) like a bottle which needs to be filled with a liquid or something. Does filled mean fully filled? Or partially filled?
Admin
no, notFilled is a bad name, always go with positives in this case Filled, it gets rid of ambiguity in interpretation and prevents double negatives. notFilled is just double plus un-good.
Admin
That's why the naming of variables and methods should be positive. We have operator ! to negate.
Admin
"This code, as written, would fail with an ArgumentException if you did supply an email, password, or user key."
So how is it not impossible that they didn't fail to not notice this during any and every single test? Did they never enter nothing into none of those fields? Or didn't they release this wrapper library without testing nothing at all?
Admin
Don't extension methods seem like a /really/ bad idea? In terms of breaking various abstractions I mean. It's as though MS is appealing to programmers' worst instincts, just to attract developers to their language (and IDE) and force other languages to choose between following their lead and doing the Right Thing.
Python monkey patching doesn't count because Python doesn't count as a proper language.
Admin
All tests returned notFailed so they went ahead and released the wrapper library.
Admin
| Don't extension methods seem like a /really/ bad idea?
No, they're a great idea if applied properly - for example porting a VBA application with extensive string manipulation. Very readable even without tracking down the extension method.
(as long as they're not the opposite behavior of the function name as in this example)
Admin
Extension methods don't break encapsulation. They don't have access to the private/protected/internal members of the class. An extension method is just syntactic sugar on top of a static method.
The only difference between
and
is that the first can be called like
string s = "foo"; bool b = s.notEmpty();
, where the second needsstring s = "foo"; bool b = MyExtensions.notEmpty(s);
Admin
All tests or BOTH tests? I think a little more testing may have been required.
Admin
Can you call extension method on null values in C#? I'm not a C#er, but would have expected a NullReferenceException...
IsNullOrEmpty is defined to take a string argument, seemingly for this reason.
Admin
As Homer said, "I'm not not licking toads."
Admin
Yes, you can call an extension method on a null object, which would be confusing.
Admin
afair from technical high school you totally can. This way, you can even "avoid" such exceptions/errors by checking nulls in the extension method and drive people mad! :D
Reason it is allowed is that it is still allowed to call it like a normal static method, even if it has the "this" modifier on the first argument.
That's just memory for the final exams though, it may be wrong.
Admin
"So how is it not impossible that they didn't fail to not notice this during any and every single test? Did they never enter nothing into none of those fields? Or didn't they release this wrapper library without testing nothing at all?"
I would understand it as that someone had written the wrapper, but never actually used it, let alone tested it.
Admin
The check fails if (and only if) email, password, AND userkey are filled. It succeeds with email/password, email/userkey, or... Nothing. which I guess nobody bothered to test for.
Admin
...or not. Double negations do things to your brain. I need coffee.
Admin
TRWTF is people that think IsNullOrEmpty is enough. What do you do when some clown just sends spaces?
Admin
It really should have returned FileNotFound.
Admin
You can call an extension method on a null argument. However, it's considered bad practice and most extension methods will throw an ArgumentNullException if you do.
Admin
I think Eric Ray has the final word on this.
An extension method is just syntactic sugar around a static method that takes advantage of the public (and maybe internal, but not protected and not private) members of the argument supplied, conventionally, as "this." So, obviously, you can pass a null reference, so long as "this" is a reference type. It's no more confusing than calling the equivalent static method with a null reference.
Now, calling an extension method on "object" would be a tad questionable, I agree. I'd expect the C# compiler to complain if it found a clash between an extension method and a bog-standard class method ... but it's such a horrible idea, I've never tried it, so I don't know.
Similarly, defining generic extension methods is something probably best left to library writers who are very, very, careful about what they are doing.
Not that any of this really applies to today's WTF, which is entirely horrible in its own simple and unextended right. Or wrong. Or something.
Admin
The main problem with extension methods is potential name conflicts, if two libraries both try to define string.notFilled.
Admin
You use IsNullOrWhiteSpace.
Admin
Instance methods take priority and there will be no warning, let alone error.
Did you happen to notice today's article?
Admin
Then the first namespace import gets the invocation, again see above no warning or error.
Admin
TRWTF: A comment with a link to thedailywtf[dot]com is flagged for moderation.
Admin
Use IsNullOrWhiteSpace instead. That checks for Null, Empty, and all white space.
Admin
TRWTF is assuming that library writers are very, very careful about what they are doing.
Admin
Oh, I have had comments flagged for moderation without any links at all.
I suspect that the moderation system is just taking a random sampling and using statistical analysis to determine whether all comments are valid or not. Within the standard +/- 5% error margins.
Admin
It does the opposite of what you would expect; their use case is to fill the string variable with data. Although, I would agree with other commenters that "not" !should be a name prefix.
Admin
Seems quite dangerous to me. So you use an extension method, and some later version of the class adds a method of the same name that behaves slightly differently -- not necessarily as egregious as today's example, doing the exact opposite, but maybe handling edge cases differently. Then your code starts behaving differently, maybe so slightly that you don't realize it until it fails spectacularly. Well, I'd want at least a warning there!
Admin
You should always use isNullOrBlank if blank strings are not desirable. This would return true if the string was null, empty, or blank. isNullOrEmpty should only be used if blank strings are OK. You don't need to define your own extension method that does exactly the same thing as the built-in function, obviously.
Admin
Clearly, I need to read more carefully before commenting.
Admin
Admin
Yes, if there is one piece of good advice that a previous lead dev pushed was to not use not in method names (or a negative) for that reason
Do not use not. ;-)
Admin
I didn't not see what you didn't not do there.
Admin
Most extension methods?? Actually I'd be willing to be most throw a NullReferenceException because they access files/methods of 'this' without checking. But "most" extension methods are written by who-knows-who and does who-knows-what. I will say though being able to use null propagation syntax when calling extension methods is one of the main reasons I use them. "optionalObject?.IsReady() == true" is a good deal nicer than having your code littered with null checks (hello Java).
Admin
That's why the naming of variables and methods should be negative. We have operator ! to validate.
Admin
"Do not use not" has broader application: in signage and documentation, people have a tendency to remember only the more salient terms, but handle "not"/"no" more as stopwords. "Do not stack untested items on front shelves" becomes "blah stack blah untested blah front shelves". Next thing, untested items are unwittingly being stacked on the front shelves because the rule isn't being triggered. When this is health-and-safety stuff, negative wording is potentially hazardous.
The requirements are easier to remember and in application need less conscious deliberation when they are phrased in positive language. If you have a "dos" and "donts" list, replace the "donts" with the "dos" that should be done instead.
Admin
"No smoking!"
Yes indeed, I'm gasping. (spark, puff)
Admin
This is NOT an extension method in c#: bool notFilled(string str)
The proper extension method needs to have the "this" keyword, and also needs to be static (and usually public, though that's not mandatory): public static bool notFilled(this string str)
If it's compiling, then there must be a proper extension method somewhere which is being called instead of the posted one (which is buggy as described). Anyway, trying to call an extension method on a null string will still result in an exception. That's why IsNullOrEmpty is not an extension method but a static class method: if (string.IsNullOrEmpty(str)) { ... }
Admin
[repost with better formatting]
This is NOT an extension method in c#:
bool notFilled(string str)
The proper extension method needs to have the "this" keyword, and also needs to be static (and usually public, though that's not mandatory):
public static bool notFilled(this string str)
If it's compiling, then there must be a proper extension method somewhere which is being called instead of the posted one (which is buggy as described). Anyway, trying to call an extension method on a null string will still result in an exception. That's why IsNullOrEmpty is not an extension method but a static class method:
if (string.IsNullOrEmpty(str)) { ... }
Admin
Perhaps there's an in-house style which says all variable names must be camelCase, so "filled" wouldn't be allowed but "notFilled" would.
Admin
false
Admin
Nobody mentioned another problem with this code sample: the name "instantiate" for the first method. The method doesn't instantiate anything except an ArgumentException and it only does that if one or more of the parameters are not empty or null. If the name were "checkInstantiation" it would a little better in that it would indicate what the method was apparently trying to do.
Admin
Interesting, I thought it would throw a null exception. Anyway, the other point stands: the "this" keyword is missing, so that was not the method being called.
Admin
"Perhaps there's an in-house style which says all variable names must be camelCase, so "filled" wouldn't be allowed but "notFilled" would."
So call it "isFilled", which also correctly describes what the function does (2nd best). Or swap "return true" and "return false" so it's not doing the opposite of what the name says and probably generating a bug everytime the function is used (3rd best, because we're all more likely to misuse a negative-name). Or read the string library documentation and use one of the existing, thoroughly validated functions rather than re-inventing it (best).
Admin
As indicated by the ellipsis before the method end, this is not the full content of the "instantiate" method - just part of the argument validation preamble.