9 Replies - 4900 Views - Last Post: 02 May 2012 - 04:48 AMRate Topic:

Microjerks are at it again

Posted 30 April 2012 - 06:32 PM

Ah my old favourites Microsoft who can't seem to follow a single accepted convention have struck again.

This time I'm writing an expression in VS BI Reporting, and I need to sort records based on actual and potential ratings, which can be NA, 0, 1, 2, 3, 4 or 5. There are four ratings, Safety, Environment, Plant and Outrage. So I figure "Yeah I'll just make a bit string to sort with", like so:

There a of Casting both Explicit and Implicit casting Boolean -> Integer -> Integer -> String and lot of noise. Making it hard and error-prone (since you're hoping the ordering of the operators in what you intended).

To make it much easier to read and understand,create some method.
I create a couple of dedicated Extension Methods.

And when you want a create a Bit String Representation of a collection of Boolean.

Public Function ToBitString(Booleans As IEnumerable(Of Boolean)) As String
Dim BitRepresentation As New Text.StringBuilder(Booleans.Count + 1)
For Each b In Booleans
BitRepresentation.Append(b.To0_1.ToString)
Next
Return BitRepresentation.ToString
End Function
End Module

Re: Microjerks are at it again

Posted 01 May 2012 - 04:41 AM

Hmm... looks like Access. Ick.

Why convert when you can just be explicit?

IFF(Fields!SafetyActCode.Value = "5","1","0")

I agree with AdamSpeight2008. I have actually seen -1 for a boolean false. But, really, all you need is an agreement of what is false ( or true ) and what isn't for boolean logic to be happy. You can do the 0/1 shuffle in some languages, like C, when that is explicit behavior. However, such behavior can never really be assumed.

Re: Microjerks are at it again

Posted 01 May 2012 - 05:49 AM

AdamSpeight2008, on 01 May 2012 - 06:52 AM, said:

baavgai The function based approach is better to maintain, less error prone.

I agree. I don't know enough of the guts of the system in question to offer that style of code. Just, without knowing much, I would have gone with some kind of case to get the explicit value I wanted rather than assume implicit correctness.