Use of IIF
A question for my venerated colleagues in these newsgroups.
One of the threads that came up today led me to thinking about the IIF function. For example, this simple test If iCol 26 Then col = Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64) Else col = Chr(iCol + 64) End If can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Do you guys use it? What are your views on this topic? Where does it come from, is it likely to be supported evermore? Do you care? Look forward to hearing from you. -- HTH Bob Phillips |
Use of IIF
On Tue, 19 Aug 2003 22:52:57 +0100, "Bob Phillips"
wrote: A question for my venerated colleagues in these newsgroups. Venerated? Wow, I've never been venerated before. Makes me feel warm & fuzzy... (Oh, you meant Tom, Dave, Chip and Myrna, not me. Right, I'm with you now...) 8^ One of the threads that came up today led me to thinking about the IIF function. For example, this simple test [Snip IF / ENDIF block] can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Not really; I think "to each his own". I couldn't comment on the relative performance of each syntax, but let's face it, in real world situations on 2+ GHz machines is it ever likely to make much difference? Do you guys use it? My personal preference is for readability (self documentation, easy maintainability, etc). Like you, I often avoid single line If statements just because I find appropriateley indented If / End If blocks clearer when one is glancing through the code. As for Iif, I generally limit its use (in Excel VBA at least) to picking options based on arguments; say for constructing dialog text. But I can't say that I'd NEVER use it for anything else. What are your views on this topic? Where does it come from, Well, there was this guy named Bill Gates and this other guy named Paul Allen... 8^ is it likely to be supported evermore? If by "it" you mean Iif, then I would think so; certainly it's in Vb.Net. Also it can be quite useful in constructing queries in Access. I can't see it being dumped. Do you care? I wouldn't say that there's an OVER abundance of choices for If / Then syntaxes; the number that we have works for me. Look forward to hearing from you. --------------------------------------------------------- Hank Scorpio scorpionet who hates spam is at iprimus.com.au (You know what to do.) * Please keep all replies in this Newsgroup. Thanks! * |
Use of IIF
Hi Bob,
I tend to prefer If...Then statements for readability and consistency (you can't use IIF if either condition requires multiple lines of code). Of course maybe I've just been using If...Then longer and I don't like new constructs. <g One drawback of IIF is that it's about an order of magnitude slower than the equivalent If...Then statement, but this isn't likely to be a real issue unless you're executing it tens of thousands of times. -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "Bob Phillips" wrote in message ... A question for my venerated colleagues in these newsgroups. One of the threads that came up today led me to thinking about the IIF function. For example, this simple test If iCol 26 Then col = Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64) Else col = Chr(iCol + 64) End If can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Do you guys use it? What are your views on this topic? Where does it come from, is it likely to be supported evermore? Do you care? Look forward to hearing from you. -- HTH Bob Phillips |
Use of IIF
I don't typically like IIF or single-line IF statements because of
breakpoints. You can't set a breakpoint inside of a single-line or IIF statement. So, if I had code like this: Do until something If condition1 Then action1 Else action2 End If Loop I could set a breakpoint at the action1 line and the code wouldn't stop until that condition was met. However, with this: Do until something IIF(condition1, action1, action2) Loop I can't stop the code anywhere inside the IIF statement. Now, if that's not true and somebody knows how, please let me know. Other than that, I can't think of any real problems with it. Mark --- Mark Bigelow mjbigelow at hotmail dot com http://hm.imperialoiltx.com *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it! |
Use of IIF
On Tue, 19 Aug 2003 16:06:11 -0700, Mark Bigelow
wrote: You could try adding a Watch (Debug menu - Add Watch...) item to any variable or expression in the Iif statement, and have the code break when that expression either changed or became true. I don't typically like IIF or single-line IF statements because of breakpoints. You can't set a breakpoint inside of a single-line or IIF statement. So, if I had code like this: Do until something If condition1 Then action1 Else action2 End If Loop I could set a breakpoint at the action1 line and the code wouldn't stop until that condition was met. However, with this: Do until something IIF(condition1, action1, action2) Loop I can't stop the code anywhere inside the IIF statement. Now, if that's not true and somebody knows how, please let me know. Other than that, I can't think of any real problems with it. --------------------------------------------------------- Hank Scorpio scorpionet who hates spam is at iprimus.com.au (You know what to do.) * Please keep all replies in this Newsgroup. Thanks! * |
Use of IIF
Another slight problem with IIF is that both True / False are evaluated
either way. x = 0 ans = IIf(x < 10, 5, 1 / x) Here, you should not get an error because the return value should be 5. However, IIF evaluates both sides,and generates a "Division by 0" error. This can have unpredictable consequences when it's more complicated. ans = IIf(True, 4, a * x ^ 2 + b * x + c) Here, the return value should always be 4, but IIF evaluates the more complicated expression anyway. This contributes to its slowness. I personally like IIF for simple things that do not involve right-hand side evaluations, and where there is little chance for an error. x = 5 ans = IIf(x < 10, 3, 4) Just some thoughts... -- Dana DeLouis Using Windows XP & Office XP = = = = = = = = = = = = = = = = = "Bob Phillips" wrote in message ... A question for my venerated colleagues in these newsgroups. One of the threads that came up today led me to thinking about the IIF function. For example, this simple test If iCol 26 Then col = Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64) Else col = Chr(iCol + 64) End If can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Do you guys use it? What are your views on this topic? Where does it come from, is it likely to be supported evermore? Do you care? Look forward to hearing from you. -- HTH Bob Phillips |
Use of IIF
My philosophy is that a programming language should support my natural
thought process. To the extent that language constructs do, I use them. So, if I think of "if icol26 then do this else do that", then I'll write it as such. If, on the other hand, I think of "ans = well, if x <0 then y/x else 0" then I'll write it as such using an IIF. Of course, because VB insists on evaluating everything in an expression before it does anything, it severly restricts the elegance of conditionals in the language. But, it gives you a flavor of how my code relates to my thinking. Similar logic (of how my thoughts map onto code) dictates how I use other constructs as well. I often use ':' to construct 'a single thought.' A few examples: To initialize variables, I almost always use i = 0: y = 1: z = 2. For simple properties, I'm very likely to use: Dim lProp As Long Property Get Prop() As Long: Prop = lProp: End Property Property Let Prop(uProp As Long): lProp = uProp: End Property To swap two variables, the code will always be temp = a: a = b: b = temp. [If there is reason to swap variables in two places, it will be a parameterized subroutine which contains the single line above.] If the swap is conditional, the code might look like: If i j Then temp = a: a = b: b = temp or If i j Then _ temp = a: a = b: b = temp Finally, while I cluster my variables by intent, I rarely 'label' by type. [IMO, if one doesn't know what the type of a variable ought to be, one doesn't understand the application the code reflects, and should keep their paws off it.] So, my declarations will look like: Dim _ InFirstName As String, InLastName As String, _ InMidInitial As String, _ OutName As String, _ i As Long, j As Long, sTemp As String [Yes, I do label the type of a variable when it really could be anything!] Among the many things the folks at MS could do to improve VB would be to evaluate only the minimum necessary to execute a statement and implement defines, and more importantly, parameterized defines. -- Regards, Tushar Mehta, MS MVP -- Excel www.tushar-mehta.com Excel, PowerPoint, and VBA add-ins, tutorials Custom MS Office productivity solutions In article , says... A question for my venerated colleagues in these newsgroups. One of the threads that came up today led me to thinking about the IIF function. For example, this simple test If iCol 26 Then col = Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64) Else col = Chr(iCol + 64) End If can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Do you guys use it? What are your views on this topic? Where does it come from, is it likely to be supported evermore? Do you care? Look forward to hearing from you. -- HTH Bob Phillips |
Use of IIF
On Wed, 20 Aug 2003 10:06:04 -0400, Tushar Mehta
wrote: temp = a: a = b: b = temp. For me the beauty in this is enhancing vertical context. When you can only see 25-30 lines of code on the screen at one time, it's often nice to be able to see [more] indent levels and other flow and scope information - _especially_ when you start nesting code (where you have the additional problem of horizontal overreach!). But like Bob, and no doubt you, I make a judgement call as to whether I'll "fool myself" - which generally translates to: "If it's short and my mind can process it instantly and accurately, cram it on one line." As to iif, same deal - it's a judgement call. If I don't sense that it will "fool myself" or hamper instant recognition of its functionality, I usually jump on it for the benefit of vertical context improvement. Ergo my favorite statement in VBA is "End If" - that's where I have room to comment the ensuing logic (LOL and ducking) WB <--- hairy overnesting coder <g |
Use of IIF
Thanks all for the responses. If you will bear with me, I will précis what I
have got from these responses. Hank Scorpio, apart from not realising that he also is venerated within these NGs, would not use them by choice, preferring self-documenting, easily maintainable code, and feels that breaking If ... Else ... Endif over several lines and indenting supports that approach.. Rob Bovey made a similar point, but typical of Rob, he actually knew that IIF is considerably slower, something the rest of us may have suspected, but maybe know! He also made the point that it is limited to single statement clauses. Mark Bigelow also made the point that debugging is easier if the code is split across multiple lines. Another significant drawback of IIF was made by Dana DeLouis, namely that both expressions are evaluated in an IIF, which could lead to problems. VBA does not support short-circuiting., although Dana does use this construct. Tushar Mehta chipped in with his view that if it supports the natural thought process, he would use it. But more interestingly, IMO, was Tushar's clearly laid out continuation of this thought, where he showed other examples of his premise.This also prompted support from Wild Bill in that it allows you to see more code at a time. Bob's thought: Thanks everyone for these thoughts, I found them useful I tend to align to Hank's views, the maintainability is more important than speed in most cases in these mega-mega processing paradigms we meet today, and this is better served with multi-line, separate If statements. Rob's point re performance, his point about single statement clauses, Mark's observation on debugging, and Dana's point regarding the evaluation of IIF expressions, all of these added to Hank's initial point, and it doesn't leave much going for IIF in my opinion. I have never been much of a user of this construct, but I think that from now on I will relegate it to the memory, and not use it again. I will however, pick up on some of Tushar's points, they made a lot of sense to me. Thanks again everyone for these contributions. Bob "Bob Phillips" wrote in message ... A question for my venerated colleagues in these newsgroups. One of the threads that came up today led me to thinking about the IIF function. For example, this simple test If iCol 26 Then col = Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64) Else col = Chr(iCol + 64) End If can also be written using IIF as col = IIf(iCol 26, Chr(iCol \ 26 + 64) & Chr(iCol Mod 26 + 64), Chr(iCol + 64)) My view is that whilst the second is shorter, and often a more obvious statement (as in this case?) of what is happening, it can often be quite the opposite, and be very confusing. As I tend to like to keep to a certain style, for instance I always use If ... End If even for single statements, I tend not to use it. But is this wasteful prejudice? Do you guys use it? What are your views on this topic? Where does it come from, is it likely to be supported evermore? Do you care? Look forward to hearing from you. -- HTH Bob Phillips |
All times are GMT +1. The time now is 03:38 PM. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com