View Single Post
  #5  
Old 08-07-2018, 11:25 AM
loramin loramin is offline
Planar Protector

loramin's Avatar

Join Date: Jul 2013
Posts: 10,230
Default

Well again, not a C guy, so I can't comment on the performance specifics of try/catch vs. if/else in C. I'm almost certain that there is some sort of cost to try/catch, but I'm also a very strong believer in one of Donald Knuth's famous quotes:

Quote:
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
However, even if you forget about performance, what I can say is that in any language there is real value in "doing things the way everyone else does them", both because A) if everyone else does it that way, there's probably a good reason, and B) even if there isn't, code readability is critically important. In other words, if people are used to reading if/elses, then using try/catches (even if they are equal performance-wise) is a bad idea because it makes it harder for others to read and understand your code.

(As an aside, a very common misconception among programming learners is that your computer/compiler is the primary "reader" of your source code. This misconception isn't actually one at first, when you're just working on your own code and learning. However, in a professional environment your code gets read by one or more peer reviewers when you first check it in, then again by you or someone else in the future everytime a bug gets fixed or a new feature gets added that relates to that code. If you piss off the compiler with uncompilable code you can fix it in a few seconds, but if you piss off the humans with unreadable you can waste hours if not days of people's time.)

As for:

Quote:
Originally Posted by maskedmelon [You must be logged in to view images. Log in or Register.]
if I didn't utilize the try-catch block, then I'd have to include two copies of that "if" line that i have commented out in addition to their else clauses and possible more conditionals if i added parameters to the method.
On the face of it (and again, I'm not a C guy, but in most languages ...) that means you should refactor that logic out into a separate function. Then you can use as many if/else or try/catch blocks as you want and just call that function however many times you want, while still keeping your code DRY (ie. following the best practice of "Don't Repeat Yourself").
__________________

Loramin Frostseer, Oracle of the Tribunal <Anonymous> and Fan of the "Where To Go For XP/For Treasure?" Guides
Anyone can improve the wiki! If you are new to the Blue or Green servers, you can improve the wiki to earn a "welcome package" of platinum and/or gear! Send me a forum message for details.
Last edited by loramin; 08-07-2018 at 11:35 AM..