2 posts tagged “code”
Today's DailyWTF presented a programming challenge: write a function to multiply two numbers "Russian Peasant"-style. Reading that article, I immediately recognized two things. The first is that the algorithm essentially used a stack to hold completed portions of the calculation and then sums up that stack at the end. The 2nd is that stacks are fundamentally related to recursion. If I could write a stack-based solution I could also write a recursive one as well, and the code for the recursive solution would likely be fairly simple.
Of course, by the time I finished reading the article there were also already 22 comments, most of them solutions, one of them in VB.Net (not very good, imo) and one recursive one in Java, but I want to make it clear that I did not read any of those until after I completed my own implementation. It took less than 5 minutes to write and test the stack-based solution. In the end I discarded the stack as unnecessary and just kept a running total instead. It then required less then 5 additional minutes to adapt this to use recursion. I now had a working function for a grand total of 4 lines of code, and I felt it safe to take a peek at what others had done.
The only thing I added to my code based on the comments was to use bit shifts to multiply and divide by two. In one sense you could say it's cheating to write a multiplication function that uses the multiplication operator in any way, even if you're just multiplying or dividing by two. In this case I'd argue that the actual "Russian Peasents" suffered from the same chicken and egg problem, and so you've still met the spirit of the problem. But clearly the bit shifting is "better" from a standpoint of keeping your function "pure", and so I incorporated that into my code, which you can now see here:
Function multiply(ByVal x As Integer, ByVal y As Integer) As Integer
multiply = IIf(x Mod 2 <> 0, y, 0)
If x > 1 Then multiply += multiply(x >> 1, y << 1)
End Function
Let's talk about web sites and validation. I'll start with the basics:
Whenever you have a web form, you must validate server-side. This is because you can never trust anything sent from the user over the web. The user could simply disable javascript, or a more malicious user might send a specially crafted HTTP request. Either way, your server-side code needs to be prepared.
Additionally, you should validate client-side. This is so you can provide faster, more dynamic feedback to user, and also to reduce the load on your server. If you have some javascript that 99% of users run that catches an invalid form before submission, that's a good thing.
Unfortunately, this creates a burden for web developers. There are now two places that need code to accomplish the same task. That may seem petty to a non-developer who's only thinking about the initial creation work, but it really has a huge maintenance cost over the life of the app. The problem is that it becomes real easy to make a change in one place and not the other, which results in a broken web site.
One of the nice things about ASP.Net is that much of the time you don't have to duplicate client-side and server-side validation. It includes a set of generic validation controls that will automatically do the client-side and server-side work with one set of code. Unfortunately, these controls have several limitations.
One limitation is that you can only validate one control at a time. For example, let's say you have several fields on your form and want to require the user to enter data in at least one of them. To do that right now you would need to use a CustomValidator control, which usually also means writing separate javascript or not validating client-side. Another limitation is that they won't validate checkbox controls (you can get around this now by attaching a custom validator to a random textbox, but that's an ugly hack).
So what is a developer to do? Well, in my case, I derived a new generic validation control: the AtLeastOneOfValidator. Since one of my main problems with Vox right now is that it's not easy to post code, I created a separate write-up for the control and posted it over at codeproject. Anyone can read the full details there now, and if you create a (free, non-intrusive) login you can download the code and binary. Here are some of the highlights:
- In addition to the normal controls supported by validators, it will accept CheckBox, CheckBoxList, RadioButton, RadioButtonList, and Calendar. You could use it to require a checkbox (for example, "check this to accept terms") by including the checkbox as the only control with the validator.
- It uses simple dropdown lists to select supported controls, just like any other validator control (I'm a little proud of myself for getting that working, even though it turned out to be pretty easy), up to ten controls. Beyond 10 controls there is a string property you can use to enter a comma-delimited list of control names.
- Client-side validation works for everything but the calendar control. If you use the calendar control, it will post back to the server to finish validation. Since the stock calendar control pretty much sucks and posts back all the time anyway, this shouldn't really surprise anyone. I debated with myself whether to support this control at all.
The control has been tested to work well with IE7 and the latest FireFox 3 (beta 2 at the time of writing). I'm looking for people to test in FireFox 2 and older, IE6, the IE8 beta, Opera, Safari, Nautilus, and Camino. If you see this and can put the control through it's paces in any of those platforms, please post a comment to let me know how it goes.
Once again, you can get the details here.