3

For example, using C, let's say I define a node like this:

typedef struct nde {
    int val;
    struct nde* next;
}node;

And then I "wrap" it with something like this:

typedef struct lst {
    node* head;
}list;

I do mean this case specifically. I know that using the wrapper could be useful if you want to include other information, but is it considered "bad practice" if it only contains the a pointer to the head? It's a much more intuitive way of doing it for me, mainly because when pushing or popping or using some other function you'll always be dereferencing, and the naming conventions make more sense as well.

josepi
  • 31
  • 1

1 Answers1

8

No, this is not an anti-pattern, it's actually not uncommon. This allows you to have a single handle that refers to the list, which doesn't change when you remove elements. If you don't do this, then removing the first element of the list is is a special case, because you need to update the caller's pointer to point to the new head. And if there are multiple references to the list, it's almost impossible to arrange for all of them to be updated.

If you know that there will only ever be one reference to the list, then there's no need to do this.

Barmar
  • 741,623
  • 53
  • 500
  • 612