Welcome Guest, Not a member yet? Register   Sign In
Avoiding nesting everything inside IF statements
#5

[eluser]kurucu[/eluser]
[quote author="jedd" date="1254256006"]I've read that you should have only one exit point for a given function. I guess they mean grouping of exit points, since you want to return different things based on different inputs. And I can see the argument there, though it's probably something that is easier as you get more proficient (I have exit points scattered everywhere, so I've a long way to go, mind). I think the two approaches you describe, though, have the same number of exit points, and comparable (just inverted) logic that wraps them up...[/quote]
I imagine this depends on the programming language, as many require (especially C derivatives) require the return type to be declared. But for all languages, having a single return point means you know where the code exits the function, you can trace every case, and won't end up with lost cases (particularly within nested IF conditionals). Certainly, you could have no implied 'else' cases with a single return point.
But, everyone who has commented so far has given examples where their function is traceably exiting if the conditional is true, or otherwise continuing - no ambiguity or lost cases.

[quote author="BrianDHall" date="1254256494"]Actually I just stuck that variable there for simplicity, as what I actually am doing in that particular function is that if a person isn't presently working on an incomplete listing then they have no business trying to 'preview' it (which I determine by checking the existence of an ORM-powered class variable).

But yeah, for user login roles I was thinking of exactly that - sometimes it doesn't matter so long as they are logged in, and sometimes it does Smile[/quote]Ah yes, I noted that it was probably an example as I hit 'post' Smile Still similar approaches though. I'm not yet taken by the ORM thing, but it might come to me. Something on a similar subject is having long conditionals in the original IF statement. I wrote, but couldn't stand, conditionals over several lines checking to see if a user was an administrator, or could edit all posts in a forum, or could edit their own posts and this was one of theirs...

I realised that a bit of prep before getting to the "if(..." went a long way. And not a long way to anything in computer terms, but readability -

[quote author="BrianDHall" date="1254256494"]Its really all the human element, you hit the nail on the head there. The problems I find with it are A) lots of nesting isn't pretty and distracts the eye and gives one more thing to have to fix occasionally if your editor gets confused, B) increased chance of mis-matching brackets, putting code in the wrong bracket, or worst of all putting code that assumes a certain security check in the wrong set of brackets, and C) being inside a loop increases human memory usage and requires concentration (you have to constantly remember you are in a loop and what conditions triggered your arrival there) even if it requires no computer over-head in terms of memory or CPU strain.

As ADDey (thats now a word, I just 'precedented' it) as I tend to be, I can't afford any extra mental overhead Wink[/quote]

You're absolutely right. I asked questions about model structure, and ORMS and methods; and then realised that the end result was pretty much the same in terms of functionality. The end result rarely depended on performance (or, as Jedd points out, performance is a minor factor). The end result pretty much depends on "can I, and other developers, follow this code, understand it and modify it without destroying it"? You put it more elegantly - no nested sentences or nuffin.

[quote author="BrianDHall" date="1254256494"]Thanks kurucu, I'm really glad to hear it works as I thought it would as it seems like such a nice option to have in some functions and controllers. Gives me new appreciation for the whole point of pages being 'functions' to begin with - easy to end execution of them without breaking the page or other functions (like exit might do).[/quote]This is why we use the forum!


Messages In This Thread
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 08:46 AM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 09:06 AM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 09:26 AM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 09:34 AM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 01:00 PM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 04:26 PM
Avoiding nesting everything inside IF statements - by El Forum - 09-29-2009, 06:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB