Original Post
So, let's say I'm writing a function for a texture manager class that checks if a certain requested texture has been loaded already, if it has it returns a pointer to that texture, if not it loads it into memory. This is something I'm sure we've all done very frequently as it's a necessity for just about every application or game that uses art assets (models, textures, sounds and so on) - but it's also a perfect example of a situation in which I find myself frequently as a programmer and that I'd love to shed some light on.
It's about error checking/handling. At which level do I want to do this (in what way)? When writing a function that loads a texture as described above, I have two choices. Either I do the error checking/handling right there, in the function itself or I delay doing so. In the former case I would have to link my error checking/handling header in the texture manager code file and then, in the function that loads the texture, check if there was an error, throw up an error report and so on. In the latter case my texture manager wouldn't know about the error handling related code at all and I'd just return a NULL instead of a pointer to texture data. This delays error handling until another 'level' of code is reached because then, when I use the texture manager in, say, my main code file, I have to do the checking there, see if my call to the function actually returned data or NULL - in which case I'd throw an error.
From my point of view, the former method means clearer code but requires links to the error handling header in lots of code files (wherever an art asset has to get loaded) while the latter approach means some ugly error checking in parts of the code where it would be more comfortable to just call the functions and assume everything worked (because the function in itself would already have thrown an error at runtime if there were any problems) but therefore is sleeker with the links between headers and code files as all the function that are loading art assets for example need no link to the error related functions, they can just return NULL.
Looking at this, I kinda prefer the former approach but I'd love to get some additional input from you all! How do you do it? Are there other alternatives, golden rules, pros or cons I haven't covered in my short evaluation there?
Thanks ahead of time!
It's about error checking/handling. At which level do I want to do this (in what way)? When writing a function that loads a texture as described above, I have two choices. Either I do the error checking/handling right there, in the function itself or I delay doing so. In the former case I would have to link my error checking/handling header in the texture manager code file and then, in the function that loads the texture, check if there was an error, throw up an error report and so on. In the latter case my texture manager wouldn't know about the error handling related code at all and I'd just return a NULL instead of a pointer to texture data. This delays error handling until another 'level' of code is reached because then, when I use the texture manager in, say, my main code file, I have to do the checking there, see if my call to the function actually returned data or NULL - in which case I'd throw an error.
From my point of view, the former method means clearer code but requires links to the error handling header in lots of code files (wherever an art asset has to get loaded) while the latter approach means some ugly error checking in parts of the code where it would be more comfortable to just call the functions and assume everything worked (because the function in itself would already have thrown an error at runtime if there were any problems) but therefore is sleeker with the links between headers and code files as all the function that are loading art assets for example need no link to the error related functions, they can just return NULL.
Looking at this, I kinda prefer the former approach but I'd love to get some additional input from you all! How do you do it? Are there other alternatives, golden rules, pros or cons I haven't covered in my short evaluation there?
Thanks ahead of time!