views:

198

answers:

3

I've always been taught that the more pointers you need in a piece of code, the less elegant it is. Also, I know that the only data structure that you 'need' is a singly linked tree. As a result, I've always tried my best to avoid such atrocities as doubly linked lists and doubly linked trees.

Is this true? Is code really inelegant if you use pointers and doubly linked structures where fewer pointers or a singly linked structure is sufficient?

EDIT: Turns out there IS a clever way to avoid the doubly linked tree, by the way.

+5  A: 

Your question has a semantic flaw: excessive X is too much. Too much is always a bad thing.

Any operation which can be done with pointers can also be done with arrays. That doesn't mean it should be. Lists, trees, queues: all have elegance when applied to suitable problems. Just because pointers were the last and most advanced topic in a CS class doesn't mean they should be used in all programs.

While pointers are a frequent source of programming errors, so are integer assignments. That doesn't mean we should avoid using either of them. Just write code which uses them in a straightforward and understandable manner.

wallyk
The last and most advanced topic? What CS course did you take? Where I went to college we were introduced to pointers a few weeks into the first semester, since they're such a fundamental and important part of programming.
Mason Wheeler
We learned pointers second week of Java (cs 101). I always thought they were quite fundamental, too.
piggles
Maybe this explains why C never took off....
duffymo
But Java doesn't have "pointers" in the C-sense. You can't do pointer arithmetic with them. References are subtly different. Surely the OP can't object to references? If so, better give up programming now.
duffymo
You can't do pointer arithmetic in Java, this is true, and I do tend to use the terms reference and pointer a little interchangeably due to the fact that I learned java first. However, references in Java are quite powerful too.
piggles
Usage of pointers—as in linked lists, etc.— were the last topics of C and Pascal programming as organized by the syllabus and textbooks of the era.
wallyk
Ah fair enough.
piggles
+2  A: 

The elegance is not not from the layout of the code, but also from the underlying functionality in the code.

Tree structures(e.g. a lot of famous binary search trees, disjoint sets) are actually very elegant and efficient.

Yin Zhu
+3  A: 

I'd say that your code is inelegant if it's got pointers sprinkled throughout.

However, a well written data structure will abstract the fact that it's using pointers away from you, the client. As long as YOUR code isn't littered with pointers, and the data structure's API is minimal yet complete, I would have no qualms about using them.

As a matter of fact, I'd criticize anybody who eschewed data structures for a reason such as "too many pointers". I don't know who taught you that, but I'd unlearn it quickly. Use the right data structure for the task at hand and don't worry about pointers.

duffymo