News for hackers. Stuff that matters.
Re:Special Cases Are a Code Smell by arethuza on Friday January 25, 2019 @04:42AM (#18996752)
That first example in the article was interesting because it made me realise that I have a strong aversion to pushing the complexity into the data rather than into the code. Modifying the list seems to me that it could be a cause of other bugs (what if the list is re-used and something depends on it?).
Re:Special Cases Are a Code Smell by DarkWiiPlayer on Friday January 25, 2019 @05:20AM (#18996885)
Never mind bugs, imagine that array has 5 million elements. Good luck re-allocating and copying that to add an element at the start and another at the end.
Re:Special Cases Are a Code Smell by dpark on Friday January 25, 2019 @02:57PM (#19001872)
I don't like the approach of pointlessly reallocating the array this way, but 5 million elements is not a problem on any machine built in the last 20 years. That's just 20 megs for 4-byte integers. With modern GC, this is cheap. It's not something you'd want to do in a tight loop (the copy time will start to matter), but for something like the example, the cost (in machine terms) is negligible.
Re:Special Cases Are a Code Smell by pure-awesome on Friday January 25, 2019 @06:30AM (#18997146)
I also experienced an instinctual aversion to padding the array that I can't really motivate (honestly) except to say "I don't like it."

The version with special cases feels cleaner to me.

Re:Special Cases Are a Code Smell by scrabble on Friday January 25, 2019 @10:01AM (#18998467)
Padding the array is a bit of a code smell. You can't just pad an array without allocating a new block of memory. After allocation you need to copy the array into the new space. This is a significant amount of overhead to a fairly straightforward and efficient algorithm.
Re:Special Cases Are a Code Smell by arethuza on Friday January 25, 2019 @10:39AM (#18998843)
Well, that's easy to fix - copy your array into a linked list so you an insert elements at the head efficiently. ;-)
Re:Special Cases Are a Code Smell by klyrs on Friday January 25, 2019 @11:53AM (#18999786)
Yes, the real problem with this implementation is the data structure they use. But is `next == null` a special case? ogod what do we do?!
Re:Special Cases Are a Code Smell by banku_brougham on Saturday January 26, 2019 @07:35PM (#19009560)
This is interesting. Is it possible to implement a linked list while eliminating the end of the list ‘edge case’, in the fashion of the parent article?
Re:Special Cases Are a Code Smell by pure-awesome on Monday January 28, 2019 @06:55AM (#19016774)
Create a "sentinel" node which represents null and also points to itself, perhaps?