safe to removeitem from emptytable?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

safe to removeitem from emptytable?

Post by Bugala »

Since manual doesnt mention how RemoveItem() command reacts to empty table, I am checking this to make sure it is safe to do so.

When I do:

Code: Select all

mytable={}

InsertItem(mytable, 2)

item1 = RemoveItem(mytable)
item2 = RemoveItem(mytable)
item3 = RemoveItem(mytable)

DebugPrint(item1)
DebugPrint(item2)
DebugPrint(item3)
It simply returns NIL on item2 and item3 cases, which are RemoveItem() on an empty table, meaning I could keep removing stuff from the table and keep waiting for it to return NIL to see that all the items from it have been removed.

But since this is not commented on RemoveItem() documentation, is this actually a safe way to do it?
User avatar
emeck
Posts: 169
Joined: Fri Apr 03, 2015 3:17 pm

Re: safe to removeitem from emptytable?

Post by emeck »

@Bugala

Why don't you check with IsTableEmpty() before removing?
PowerBook 5.2 MorphOS 3.15
PowerBook 5.8 MorphOS 3.15
Amiga 1200 BPPC/BVision AOS4.1 FE
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: safe to removeitem from emptytable?

Post by Bugala »

@emeck

Thanks from the tip, didnt realise there was specific command for this. This will probably do the trick for didnt want to use TableItems to do the check since I was expecting it could be slow and I was in need of speed. but I suppose checking whether removed item is NIL or IsTableEmpty(), chances are IsTableEmpty() is even faster, since I dont then need to do the RemoveItem() part at all in case it is empty.
Post Reply