Thursday, November 16, 2006

Nots and Ands

Don’t you just love programming!

If Not (AppSettings("ProxyServer").Equals("")) And (AppSettings("ProxyPort").Equals("")) Then
req.Proxy = GetProxy()
End If


The not doesn't make any difference if both values are empty strings! It should be:


If Not (AppSettings("ProxyServer").Equals("") And AppSettings("ProxyPort").Equals("")) Then
req.Proxy = GetProxy()
End If


Spot the difference!


I reckon there ought to be an easier way to express this concept and I'm sure that 'not/and' 'not/or' structures like this cause lots of bugs. Perhaps new operators, something like this would help:

all({list of values}) {single value to compare against}
any({list of values}) {single value to compare against}


(both returning a boolean)

So to back to the example, to only set proxy settings if both port and server settings exist, we'd have

If all(AppSettings("ProxyServer").length, AppSettings("ProxyPort").length) > 0 Then
req.Proxy = GetProxy()
End If


I'll see if can code, all and any in c#...

No comments: