Which is the better method to use "if else" statements

+42
Reply to
D Yuniskis
Loading thread data ...

xctly!ndthydntndvwls!

Reply to
D Yuniskis

The ability to spot an exit point is a different issue than having one vs. multiple exit points. Yes, I will agree that having multiple exits can make the code harder to read, but the key word here is "can". In the "real world", coding is done by humans and humans can determine when to follow guidelines and when to break them. The code being discussed is a perfect example of when multiple returns can be perfectly clear.

--
The problem with the code being discussed is that it is highly contrived and 
not a good representation of real-world situations. I suspect it is from a 
class exercise. If so it could easily mislead the student into thinking that 
multiple returns are better than an if-then-else ladder in the general case.
Reply to
Chris Burrows

It is a good idea to check all those parameters immediately at entry that can be checked at that stage and return immediately if test failed. Things that can be checked at this stage are for instance checks against null pointers, values used as indexes etc.

When writing kernel mode services, it is extremely important to check that the parameters could actually have been accessed from the calling (usually user mode) address space. Failing to do these tests properly caused a lot of crashes for instance on early NT4 versions, when part of the graphical user interface was moved from user mode to kernel mode to avoid the overhead of frequent user/kernel/user mode changes in NT3.51.

Getting rid of those pathological cases immediately in the beginning will simplify the rest of the code, when there is no need to test for pointer validity or index validity for these time each time a variable is accessed and avoids multiple error handling code within the actual function body.

Reply to
Paul Keinanen

.....

Very good point and saves often quite a bit of processing time.

The one thing on readability and conditionals I rarely see pointed out or used, is the case where inverting the conditional WILL make the code more readable and LESS likely to miss return statements.

Compare -

void foo(int *p, int q) { if (p); ... for 50+ lines do something with *p else ... couple of lines set some default values or return

... for 50+ lines do something else with q and earlier results }

With void foo(int *p, int q) { if (!p); ... couple of lines set some default values or return else ... for 50+ lines do something with *p

... for 50+ lines do something else with q and earlier results }

The first I often see and the various loops and sub-blocks in that section can make it difficult to see which block or conditional the return is associated with.

--
Paul Carpenter          | paul@pcserviceselectronics.co.uk
    PC Services
 Timing Diagram Font
  GNU H8 - compiler & Renesas H8/H8S/H8 Tiny
 For those web sites you hate
Reply to
Paul Carpenter

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.