- 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
Absolutely not a WTF.
get() not throwing an exception is a bug, but that's probably a non-issue, since only WTF-code would ever try to call it when size() returns 0.
Admin
Write only memory does serve a function.... It leaks.
Admin
'FINO' Asynchronous Memory
:D
Admin
Astounding!
What we have here is a more profound absence of information than null or undefined.
This changes the way I see the kosmos.
Admin
I'll give it a try:
`(,@())
Admin
No, that's perfectly OK. Even a "regular" list can contain null entries which will be returned when calling get() with the appropriate index. Returning null values is quite normal in Java, e.g. the API hashtable class does it to indicate that there is not entry for that key.
Most people have no problem with that, some think it's horrible and will go to great lengths to eliminate all possibility of a null value ever being passed anywhere in their code - quite futile, if you ask me.
Admin
If you have a lot of pointers you need to fill with empty lists, using the API class (which has no fields) will save a LOT of memory compared with using empty "actual lists" (which contain an array of default size 10 to store the contents). You can use the same object everywhere, but then you may have to create your own infrastructure to access it from different places (i.e. a Singleton), while the API class already has that for your. And if you add something to it accidentally, the added element will pop up everywhere you used the singleton, which is a lot worse than getting an exception.
Admin
This is not much of a WTF IMO. Buggy get but possibly for a reason. Not using Collections.EMPTY_LIST but again there could be a reason
> Then I searched for where it was used and got even more mystified.... they kept creating new instances of EmptyList all over the place, storing it and then converting them back to null before exiting the method it was used in!
Converting them back to null?? No offense but I get the impression of a newcomer to Java getting confused by something that is rare outside Java, C# etc. but not that strange when you are used to it. Obviously creating new EmptyLists is not great but presumably they are passing these lists to some methods.
A similar case is found when generating xml: when you have no attributes for an element you use EmptyAttributes.
And an empty list that silently drops added objects can be very useful.
Admin
No it isn't, and btw emacs is better....
Admin
Yes it is. I'm a Java developer and it's the only part of the Sun coding standards I refuse to follow.
And it's vi for me ;-)
Admin
Admin
Actually, a function that returns void is quite useful -- it's called a procedure.
Admin
'() is the quoted constant (), which is equivalent to NIL (the symbol), which is roughly equivalent to null in Java. Since ()/NIL are self-evaluating, the quote is unnecessary.
The (Common) Lisp that would correspond to that Python snippet would be:
(let (x)
(push 1 x)
(print x))
Admin
You recognized the "WTF" of not returning an exception but rather happily returning NULL if I ask for the list element #1934495 of an "empty" list, but then you say it's not a WTF?
Admin
Creating an "EmptyList" class is like creating a "FourElementList" class. It's incorporating a possible *attribute* of an instance of a flexible, generic class into a new classes name, which makes absolutely no sense whatsoever.
Do you think you it's OK to have an "EmployeeNamedJeff" class or a "IntegerValueOf12" class as well?
Admin
ed(1) is the standard text editor. Diverge from the standard at your peril.
Admin
Haha! Brainf**k can do an empty list with no characters at all! And you can return NULL with a single character code:
.
Why mess with OO when Turing complete is all you need? :)
Admin
Yes, now we're getting the hang of it!
Admin
That's a very good point, te he.
Admin
Maybe it's just an implementation of this:
http://www.google.pl/search?q=null+object+design+pattern
Admin
you guys all missed the point... you have to remember that "truth is how you define it" [:)]
Admin
Different ways of creating an unmodifiable empty List without coding up an entire class:
Collections.EMPTY_LIST;
Collections.emptyList();
Collections.unmodifiableList(new Vector());
And similarly for for Maps and Sets. Clearly there is no reason to ever create a class called EmptyList in java. This example is defenetly a WTF.
One of my pet-peeves is Java programmers that have no idea whats in the collections framework. I had a coworker once brag about how he implemented an awsome sorting algorithm for Sets. I asked him why he didn't just use a SortedSet, and he just stared at me then walked away.
Admin
Wheels get re-invented all the time, sometimes even for a reason, and in such a simple case like that it's arguably faster to implement it than to check the docs if this wheel has already been invented.
Admin
Subclasses with immutable attributes make sense, especially if they are faster and consume less memory than the flexible, generic class.
Admin
Exactly. Write-only memory is also useful.
Imagine that the memory is not read by you, but by the hardware or operating system.
Admin
I'm really quite convinced that the extended for loop does not use the size() method anywhere - as evidenced by my having written classes and wrapers which work properly with the for loop and do not implement the size() method at all.
The only requirement for the for loop is that the class implements am accessable non-static method with this signature:
Y'all wanna wtf? I got yer wtf riight here: a wrapper which allows iterating through the set bits in a BitSet
public static Iterable<Integer> onesIn(final BitSet bs) {
return new Iterable<Integer>() {
public Iterator<Integer> iterator() {
final int first = bs.nextSetBit(0);
return new Iterator<Integer>() {
int i = first;
public boolean hasNext() {
return i >= 0;
}
public Integer next() {
int ret = i;
i = bs.nextSetBit(i + 1);
return ret;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
Looks totally effed until you see how it's used:
import static com.foo.support.Util.onesIn;
...
BitSet bs = new BitSet();
...
for (int i : onesIn(bs)) {
System.out.println("bit " + i + "is set");
}
I call it the "irritator" idiom, as a play on iterator, and the fact that many will likely find it irritating. A bit wasteful (iterators, autoboxing) but pretty in usage, which is sometimes the right tradeoff.
Hmm. Looks like I probably got yer flamewar right here, too.
Admin
My (OT) post immediately above was in response to this (OT) post.
Admin
Wow.
I'm glad that sometimes you have a reason for reinventing the wheel. But being to lazy to check docs (especially ones as well written as the standard java API docs) is hardly a valid reason.
Admin
Actually there is no EmptyList class in the Java API docs, so one must be rather lucky to find EMPTY_LIST. But in first place one has to expect EMPTY_LIST to exist.
And yes, there are sometimes reasons for knowingly reinventing the wheel. Some existings wheels just don't exactly fit, are too weakly specified, too universal or too complicated to use.
Admin
You're right, Collections.EMPTY_LIST is easy enough to miss entirely. And its true that reinventing the wheel is sometimes appropriate. Sorry about the harshness of my previous post.
Admin
Yes, this is a perfectly acceptable use of a class.
The examples you give aren't comparable. They are naive examples showing classes which do exactly the same as an existing generic class. EmptyList is different. It guarantee emptiness, which a generic list can't do. This can be useful in situations where you have generic methods operating on lists and you need to make sure some lists that get passed in remain empty (due to them having met some previous pre-condition).
Admin
True, but how will that iterator keep track of where in the list it is? In some cases, it's possible to do that without using size(), e.g. in a linked list. But the default implementation of iterator() in AbstractList, which the WTF code uses, does indeed call size() in its hasNext() implementation.
Admin
I agree there's a slim chance that the developer wrote this class rather than used one from the collections library for a reason, but in that case (since it's clearly non-obvious to everyone here), it should have been accompanied by a big fat comment, ideally in the class javadoc. I can't help but agree with the original poster - reimplementing existing part of the core Java API just causes confusion, since it's rarely obvious whether it was deliberate or accidental, and bloats the project source unnecessarily.
As for the speed argument, it's also faster to name all variables a, b, c, etc.; but that doesn't mean one should do it. IMHO, far too much code is written these days with speed rather than correctness foremost in the developers mind, and if one isn't very careful, a small amount of speed now is gained at the expense of a whole lot of speed later (when the code is being maintained).
Just my 2p ;)
Admin
Depends on the collection.
Note also that the API doc says that Collection.size() doesn't necessarily tell you the number of elements it contains.
OT, the captcha for anonymous submission is a broken link.
Admin
I will not refer to Java, since I'm not a Java programmer...
A function that returns void, same to say that it is a function that does not return values...
If you refer that in a language that has "Pointers" to access memory. A function that returns void can mean a null pointer which can be usefull, for instances, to define an undefined pointer or object.
Now imagine your empty void function, it does have an memory address, right?... I can see it as usefull for a callback, code injection...
Admin
<grin> so, like a display function that pumps boilerplate to stdout then? or a sleep function?
Admin
Strictly speaking, there is no such thing as a function that returns void. <grin> Users of many languages use the term function to describe what really should be called procedures. A true function always returns a value solely dependent upon the value(s) of the parameters and does not depend upon or modify the state of the system in any way.
</grin>
Admin
True!
<pun intended>
In fact, GCC C has special attributes for functions of this type.
A function declared with __attribute__ ((pure)) must return a result that only depends on parameters passed and non-volatile global variables (and is ensured not to change the value of any globals).
A function declared with __attribute__ ((const)) is what is described by RevMike as a "true function" -- it cannot even read global variables.
Functions like these are far less useful in procedural languages like C.
I should also mention that Pascal (and probably other languages with a Pascal-like syntax) made the distinction between procedures and functions, although the distinction was based purely on whether it returned a value.