Is this a good idea or lazy?
BlitzMax Forums/BlitzMax Beginners Area/Is this a good idea or lazy?
| ||
In this section of code, the object will have two links to lists which could exist or be null. If it's null, that's no biggie - it's just not in that list so no need to remove it. I've thought of a lazy way of doing this by putting it in a Try block so I don't need to test for nullness, but I've a feeling there's a catch (no pun intended). So is this a fast and easy way to do it, or just plain wrong?If active_link Then active_link.Remove() If inactive_link Then inactive_link.Remove() Try active_link.Remove() inactive_link.Remove() End Try |
| ||
Its faster to do a quick test for null since try/catch blocks are not free |
| ||
hmmm I never got exactly how try catch blocks worked... can someone explain how the example above works? |
| ||
If you get an error, it doesn't bring the whole program to a stop, it simply leaves the Try block and carries on like normal. In the above code, if active_link is Null, instead of giving a Null object error and stopping the program, it will be silently ignored. The above code wouldn't work because if inactive_link was real and active_link was Null, it would reach the first and then leave the Try block without executing inactive_link.Remove() If you want to be smart, you are meant to handle the error that comes from Try in some way rather than just ignore it :) |
| ||
ok I get it now, thanks czar flavius, and it seems like try blocks would only be for those too lazy to do error checking themselves? lol |
| ||
It's useful if you are using somebody else's code or a third party module and you can't be sure it will work correctly. |
| ||
oh that makes sense |