Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>"<strong>TL;DR</strong> girl" to the rescue! made the code less ugly! learned about LINQ in VB, and how do to (and not do) Lambdas. removed need for reverse dictionary search, removed a lot of repeating yourself, and just made things generally pleasant to work with. :) ♡</p> <hr> <p>Okay, I wanted to take a stab at this, even though Visual Basic isn't my usual thing. However, there were some things that I thought I could improve, so I decided to take a stab. First of all, I created a solution and implemented some basic functionality because I am not fluent in VB and figured it would be easier to do that way. If you're interested, the entire solution can be found here: <a href="http://www.mediafire.com/file/41rp9le7b45ak4p/UglyCode-7128139.zip" rel="noreferrer">UglyCode-7128139.zip</a></p> <p>I created a little sample form and tried to include everything I could glean from the code. Here's a screenshot:</p> <p><img src="https://i.stack.imgur.com/d4SV9.png" alt="Form Screenshot"></p> <p>This little app was really all I used to test the code; but even though I used this as target for the code I wrote, I think I came up with some good ways of dealing with things that can easily be made more generic. All of it is currently implemented in the main form code file, but there's nothing preventing it being pulled out into some more generic helper classes.</p> <p>First, I tackled the lookups. One thing I wasn't sure of was the number of the third Version that could be selected, since it's covered by the <code>Else</code> part of the first big <code>If</code> statement in the question. This wasn't a problem for the lookup I created, since there was only the one unknown value. I chose <code>0</code> but it should probably be updated to the real value if there is one. If it's really just a default, then <code>0</code> should be fine for our purposes.</p> <p>I guess first, let's look at the lookup for number of bits, and the lookup for encoding:</p> <h2>Lookups</h2> <p>You'll note that my lookups and a helper function are declared as <code>Public Shared</code>. I did this because: * neither the lookups nor the helper function requires knowing anything about a specific instance of the class it belongs to. * it allows you to create the item once for the entire application, so you avoid having to create it anew each time. * multiple creations isn't an issue for this application, but I just did it on principle: for large lookups and/or applications which created many instances of a class containing a lookup, the memory and processing requirements can become burdensome.</p> <h3><code>BitsLookup:</code></h3> <pre><code>' Maps from a Version and Mode to a Number of Bits Public Shared BitsLookup _ As New Dictionary(Of Tuple(Of Integer, String), Integer) From { {VersionAndMode(10, "0001"), 10}, {VersionAndMode(10, "0010"), 9}, {VersionAndMode(10, "0100"), 8}, {VersionAndMode(10, "1000"), 8}, {VersionAndMode(27, "0001"), 12}, {VersionAndMode(27, "0010"), 11}, {VersionAndMode(27, "0100"), 16}, {VersionAndMode(27, "1000"), 10}, {VersionAndMode(0, "0001"), 14}, {VersionAndMode(0, "0010"), 13}, {VersionAndMode(0, "0100"), 16}, {VersionAndMode(0, "1000"), 12} } </code></pre> <p>The idea here is very simple: instead of representing the lookup in a procedural manner, via the big <code>If</code> statement or via <code>Select Case</code>, I tried to see what the code was really doing. And from what I can tell this is it. Representing it in this kind of structure seems to me to fit better with the actual meaning of the data, and it allows us to express our ideas declaratively (what to do) rather that imperatively (how to do it). VB's syntax is a little bulky here but lets go through the parts. </p> <p><code>Tuple(Of Integer, String)</code></p> <p>A <code>Tuple</code> is just a convenient way to group data items. Until I saw it in .NET I had only heard of it in the context of a relational database. In a relational database a Tuple is approximately equivalent to a row in a table. There are a few differences, but I'll avoid going off-track here. Just be sure you know that a Tuple is not always used in the same sense as it is here. </p> <p>But, in this case, it seemed to me that the data was organized as a lookup of the number of bits, based upon both the version and mode. Which comes first is not really relevant here, since either here (or in any procedural lookup), we could just as easily reverse the order of the items without it making a difference.</p> <p>So, there is a unique "thing" that determines the number of bits, and that "thing" is the combination of both. And, a perfect collection type to use when you have a unique thing (Key) that lets you look up something else (Value) is of course the Dictionary. Also note that, a Dictionary represents something very much like a database table with three columns, <code>Version</code>, <code>BinaryMode</code>, and <code>NumberOfBits</code> or similar. In a database you would set a key, in this case a primary key and/or index, and your key would be the combination of both <code>Version</code> and <code>BinaryMode</code>. This tells the database that you may only ever have one row with the same values for those fields, and it therefore allows you to know when you run a query, you will never get two rows from one set of values for each.</p> <p><code>As New Dictionary(Of Tuple(Of Integer, String), Integer) From</code></p> <p>In VB this is the way to create a Dictionary using an initializer: create a <code>New Dictionary(Of T1, T2)</code> and then use the <code>From</code> keyword to tell it that an initializer list is coming. The entire initializer is wrapped in curly braces, and then each item gives a comma separated <code>.Key</code> and <code>.Value</code> for the item.</p> <p><code>{VersionAndMode(10, "0001"), 10},</code></p> <p>Now, the first item in our Dictionary is a Tuple(Of Integer, String). You can create a tuple either with something like <code>New Tuple(Of T1, T2)(Item1Val, Item2Val)</code> or something like <code>Tuple.Create(Of T1, T2)(Item1Val, Item2Val)</code>. In practice, you will usually use the <code>.Create</code> method, because it has one very nice feature: it uses type inference to determine which type you actually create. In other words, you can also call <code>Tuple.Create(Item1Val, Item2Val)</code>, and the compiler will infer <code>T1</code> and <code>T2</code> for you. But, there is one main reason that I created this helper function instead:</p> <pre><code>Public Shared Function _ VersionAndMode(Version As Integer, Mode As String) _ As Tuple(Of Integer, String) Return Tuple.Create(Of Integer, String)(Version, Mode) End Function </code></pre> <p>And that's because <strong><em><code>Tuple</code> doesn't tell you anything about the data you are containing.</em></strong> I might even be tempted in a production application to even create a <code>VersionAndMode</code> class that simply <code>Inherits Tuple&lt;Of Integer, String)</code> just because it's a lot more descriptive.</p> <p>That pretty much covers the lookup initialization. But what about the actual lookup? Well, let's ignore for a moment where the values are coming from, but so, now the lookup is trivial. The complexity of the <code>If</code> statement in the original is now contained in what I believe is a much more descriptive fashion, it's a declarative way of stating the same information in that procedure. And with that out of the way, we can just focus on what we're doing instead of how we're doing it: <code>Dim NumberOfBits = BitsLookup.Item(Version, Mode)</code>. Well, I do declare. :)</p> <p>There's another lookup, the <code>EncodingModes</code> lookup, and there's more to be said about that, so I'll cover it in the next section.</p> <h2>The Methods</h2> <p>Once we have the lookup in place, we can take a look at the other methods. Here are the ones I implemented. </p> <h3>Number Of Bits Lookup</h3> <p>So, here's what's left of the big <code>If</code> conglomeration:</p> <pre><code>Public ReadOnly Property NumberOfBits As Integer Get Return BitsLookup.Item( VersionAndMode(Version, BinaryMode) ) End Get End Property </code></pre> <p>There's not really much left to say about this one.</p> <h3>Form Initialization Method</h3> <p>When you have a nice designer, it's tempting to try to do everything there so there's no code to write. However, in our case all the data we need is <em>already contained</em> in the lookups we created. If we were to simply enter the items into the listbox as strings, we'd end up not only repeating ourselves, violating one of the general principles of development (DRY, Don't Repeat Yourself), but we're also losing the nice connections we already have set up with our data. </p> <p>So, let's take a look at that last lookup:</p> <pre><code>Public Shared EncodingModes As New Dictionary(Of String, String) From { {"0000", "&lt;Auto Select&gt;"}, {"0001", "Numeric (max. 7089 chars)"}, {"0010", "Alphanumeric (max. 4296 chars)"}, {"0100", "Binary [8 bits] (max. 2953 chars)"}, {"1000", "Kanji/Kana (max. 1817 chars)"} } </code></pre> <p>Here, again, we just have a declarative way of saying the same thing that was said in the method that created the data imperatively, and again, it's advantageous because we only need to create it one time, and after that look data up based on the key. Here our Key is the "Encoding Mode" and the Value holds the text to displayed for any particular Key. But, so, what happens if we just enter the text into our <code>ListBox</code> in the forms designer?</p> <p>Well, two things. First, we have entered it twice now. Second, is now we either would have to create a different lookup to go back, or, we have to go against the grain in the way a Dictionary is used. It's not impossible, but as you can see in the original <code>get_binary_mode</code> function, it's not very clean either. Plus, we've lost the advantages of the declarative nature of the Dictionary. </p> <p>So, how do we use the existing lookup to create our <code>ListBox</code> items without repeating ourselves? Well, one thought would be to just grab the Values and put them in a list, and then putting that in the <code>.Items</code> field on the ListBox. But, see, we didn't solve the other problem; still we have to go backward, from Value to Key (which in a Dictionary isn't even guaranteed to be unique). </p> <p>Fortunately, there's a solution: using <code>ListBox.DataSource</code>. This allows us to take many different data structures, and feed them to the listbox (nom nom), rather than being limited to <code>List&lt;T&gt;</code> and things that implement IList. But this doesn't necessarily select the proper items for display, so what do we do if it displays the wrong property? Well, the final missing piece is <code>ListBox.DisplayMember</code> where we set the name of the property to be used for display.</p> <p>So, here's the code we can use to set up our listboxes:</p> <pre><code>Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load listboxVersion.DataSource = BitsLookup.Keys.Select( Function(VAM As Tuple(Of Integer, String)) _ VAM.Item1() ). Distinct().ToList() If listboxVersion.Items.Count &gt; 0 _ Then listboxVersion.SelectedIndex = 0 listboxMode.DisplayMember = "Item1" listboxMode.DataSource = EncodingModes.AsQueryable().Select( Function(KVP As KeyValuePair(Of String, String)) _ Tuple.Create(KVP.Key, KVP.Value) ). ToList() If listboxMode.Items.Count &gt; 0 _ Then listboxMode.SelectedIndex = 0 End Sub </code></pre> <p>So, I'm using functionality from LINQ here to get my data in whatever form makes sense from the lookups, setting that as the <code>.DataSource</code>, and then telling the <code>ListBox</code> which member to display. I love it when I get to tell things what to do. :) Now, I can't possibly do justice to Lambda Epxressions here, but let me take a quick stab. So, the first listbox is set up like so:</p> <pre><code> listboxVersion.DataSource = BitsLookup.Keys.Select( Function(VAM As Tuple(Of Integer, String)) _ VAM.Item1() ). Distinct().ToList() </code></pre> <p>Each individual part is fairly understandable, and if you've worked with SQL or other types of queries, I'm sure this doesn't seem too unfamiliar. But, so, the problem with the way we have our data stored right now is that we only have the versions numbers that are in the <code>Tuple</code>s in the <code>BitLookup</code>. Worse yet, there are several keys in the lookup that have each value contained in them. [Note that this is likely a sign that we should have that information's primary store somewhere else; it's ok for it to be part of something else, but we really shouldn't usually have data stored such that the primary store of the data contains duplicated information.] </p> <p>As a reminder of what one of the rows looks like:</p> <pre><code> {VersionAndMode(10, "0001"), 10}, </code></pre> <p>So, there are two things we have to accomplish here. Since the UI representation is the same as the actual number here, we don't have to worry about making something other than a list to hold the data. First, we need to figure out how to extract the values from the Keys of that lookup, and second, we need to figure a way to make sure that we don't have multiple copies of the data in our list. </p> <p>Let's think about how we would do this if we were doing it imperatively. We'd say, ok, computer, we need to look through all the keys in the lookup. (<code>ForEach</code>). Then, we'd look at each one in turn, and take Item1's value (that's the property storing the version number), then probably check to see if it already existed in the list, and finally, if it was not already there (<code>.IndexOf(item) &lt; 0</code>) we would add it. <strong><em>And this would be okay!</em></strong> The most important thing is that this gives the right behavior, and it is quite understandable. </p> <p>However, it does take up space, and it's still very much concerned with <em>how</em> it's getting done. This would eliminate, for instance, improving performance without mucking about with the procedure itself. Ideally, we would want to be able to just <em>tell</em> the computer what to do, and have it hand it to us on a jewel-encrusted gold platter. (That's better than a silver one any day, right?) And this is where LINQ and Lambda expressions come in.</p> <p>So, let's look at that code again:</p> <pre><code> listboxVersion.DataSource = BitsLookup.Keys.Select( Function(VAM As Tuple(Of Integer, String)) _ VAM.Item1() ). Distinct().ToList() </code></pre> <p>We're using one of the LINQ extension methods <code>.Select</code> on the Key collection of the lookup, which does about what it sounds like: it selects something based on each item in the Key collection, and puts it all together into a nice collection for us. We're also using the <code>.Distinct()</code> extension on the result, which ensures that there's no more than one of each item in the list, and finally, we're using the <code>ToList()</code> method which puts everything into a list. </p> <p>Inside the select is where the Lambda Expression comes in:</p> <pre><code> Function(VAM As Tuple(Of Integer, String)) _ VAM.Item1() </code></pre> <p>Caveat: VB only supports Lambda Expressions for things like this, not Lambda Statements. The difference is that a Lambda Expression does not specify a return type, and does not have an <code>End Function</code>. You'll notice I used a space, underscore pattern at the end of the first line, this is because Lambda Expressions must all be on one line, and the <code>" _"</code> tells the compiler to consider the next line to be continued as if it were one line. For full details on the restrictions, see <a href="http://msdn.microsoft.com/en-us/library/bb531253.aspx" rel="noreferrer">Lambda Expressions (Visual Basic)</a>. </p> <p>The parentheses on <code>VAM.Item1()</code> were inserted there for me by VB, but they are not required. But this function is what tells the <code>.Select</code> method which item to put into the new collection for each item in the source collection, and it also tells it what type should be collected (in this case an Integer). The default collection type for most of the common LINQ functions, including Select in this case is <code>IEnumerable(T1)</code>, and in this case, since we are returning an <code>Integer</code>, the compiler can infer the type of the resulting collection, an IEnumerable(Integer). <code>Distinct()</code> remove duplicates and also returns <code>IEnumerable(Integer)</code>, and <code>ToList()</code> returns a <code>List(Integer)</code> from an <code>IEnumerable(Integer)</code>. </p> <p>And that's type we need to set for our <code>ListBox</code>, so we're done with that!</p> <p>And, also, there's the listbox with the Encoding Mode: </p> <pre><code> listboxMode.DataSource = EncodingModes.AsQueryable().Select( Function(KVP As KeyValuePair(Of String, String)) _ Tuple.Create(KVP.Key, KVP.Value) ). ToList() </code></pre> <p>This code works the very same way: we take the <code>EncodingModes</code> lookup <code>Dictionary</code> with items like <code>{"0000", "&lt;Auto Select&gt;"},</code>, we perform a Select to get an IEnumerable returned to us, the function takes a single line (<code>KeyValuePair</code>) from the dictionary, but then it does something a little different. It returns a Tuple with the Key and Value both! Why becomes apparent in the final section, but the important thing is that we're returning something that has both the pieces of data in it, and this is in fact the solution to the problem with figuring out how to get the data we need from the listbox.</p> <p>So, we're in the home stretch. Here are the last couple of items we use to set the textbox with the number of bits: </p> <pre><code>Private ReadOnly Property Version As Integer Get Dim SelectedVersion As Integer = _ listboxVersion.SelectedItem Return SelectedVersion End Get End Property </code></pre> <p>This property just returns the current value from the ListBox, which contains the values we pulled out of the lookup in the setup.</p> <pre><code>Private ReadOnly Property BinaryMode As String Get Dim EncodingMode As Tuple(Of String, String) = _ listboxMode.SelectedItem Dim RetVal As String = "0001" If EncodingMode.Item1 &lt;&gt; "0000" _ Then RetVal = EncodingMode.Item1 Return RetVal End Get End Property </code></pre> <p>And this property pulls the BinaryMode, but notice: there's no need to use the Dictionary in reverse: since we used a DataSource, we can simply pull out the selected item, cast it to the data type we put in, and then we can get out the associated bit of data without ever having to go back to the Dictionary. </p> <p>Just by the fact that the user selected a particular item, we know what the corresponding binary key is, and return that. And, the other cool thing about that is that even if there were duplicate Values in the Dictionary, there would be no ambiguity about which was the proper value. (Now, the user wouldn't know, and that's a problem, but can't solve everything at once. :D) </p> <p>The one little hitch in that property was what to do if the EncodingMode turned out to be '0000'. (That had a value of <code>"&lt;Auto Select&gt;"</code> in the Values, and is not accounted for by the lookup.) So, I auto selected it to be "0001"! I'm sure a more intelligent manner would be chosen for a real application, but that's good enough for me, for now.</p> <h3>Pulling (Putting?) It All Together</h3> <p>Well, the very last piece of the puzzle, the thing that actually gets the number of bits and sets it to the <code>TextBox</code> on the form:</p> <pre><code>Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) _ Handles btnSelect.Click txtNumberOfBits.Text = NumberOfBits.ToString() End Sub </code></pre> <p>So, all we had to do is take the <code>NumberOfBits</code> field which returns the number of bits based on the items the user has selected for Version and EncodingMode. Kinda anti-climactic, huh? </p> <p>Well, sorry for the length, I hope this has been helpful, I know I learned a few things. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload