Posts
You guys took too long so I went ahead and got the Violin myself. I'm still curious what it would take to get something out of this community though. How about this, a project of some sort: using whatever technologies you want be it JavaScript, Silverlight, WPF or just plain ol' web programming. We don't necessarily have to have an exchange of money persay, but it would be nice to create something together. We can take a vote or something...Here's another picture for clarity.
Note: Be creative, if that makes any sense.
I'll update as soon as my Violin comes in. I'm pysched.
As multi-core CPUs become more and more common, the ability to write quality software that takes advantage of them will become more important. The tools currently available are not adequate to this task. We are seeing a few things emerge to help solve the problem. One good example is the shift back to web applications hosted on a server. Web applications automatically run in parallel; each request can get it's own thread. However, there's still a shortage of simple, effective techiques for building parallel software on the desktop.
I had an idea of one place that where it would be very easy to build a parallel software approach into the programming language, so that developers can take advantage of a multi-core cpu in certain situations without having to do extra work. This idea is obvious enough that it probably isn't new to me, but I don't think I've read about it anywhere else and so I wanted to write it out here.
What I want to do is updated the standard "foreach" loop that's included in most modern programming languages. .Net, java, PHP, Python, and more all have this simple loop. In a for each loop, you specify an operation to be performed on every item in a collection of items. Current implementations of this loop always run in serial. Process one item, and when that one is finished get the next and process it. However, much of the time this could be done in parallel. My idea is that you could bake this concept into a programming language by making a simple change to your loop declaration. For example, take this simple C# loop declaration. Instead of this:
foreach (object Item in MyCollection)
Do this:
forevery (object Item in MyCollection)
Change just one key word and the compiler knows it can use create separate threads for the contents of the loop.
Of course, there are situations where these loops should not be run in parallel. Maybe you need to break early. Or maybe you're building a count as you go. But this should be relatively easy for the programmer to determine, and they can always use the old behavior when needed.
Are there any languages out there that already implement this concept?
I've talked about this before, and I don't want to go over the same issues a second time. However, I recently had a project where I finally spent a few weeks in C#, with no VB work at all. At last, I had a chance to develop a deeper familiarity with C#. Maybe I would learn something new.
The result? C# moves closer to VB.Net in my estimation, but doesn't quite pass it. All of the short-comings when compared to VB.Net still exist in my mind, but now I've had the chance to get a feel for what C# does about them. I can't put my finger on it, but there are little things here and there in C# that make up for a lot of what I complained about before. For example, I still prefer seeing "End If", "End Sub", "End Class", etc to the more ambiguous "}". But now that I've used C# more it's not as big a deal. I like the VB way, but I'm not as handicapped by C# as I was. The gap is still there, but it's not as wide.
The experience did bring one new shorting coming in C# that I didn't write about before. VB.Net supports partial namespaces; C# does not. Let me explain. Imagine you want to read from a file. In .Net, that means using the System.IO namespace. With Visual Basic, System is imported by default and child namespaces are automatically resolved. So, for example, to see if a file exists I can just say something like this:
That won't work in C#. You have to either type out System.IO.File or add a using directive for System.IO at the top of the file. Now the IO namespace this isn't a big deal. You're probably going to use the classes from the namespace a dozen times if you use them once, or it's not a lot to type otherwise. It does pollute your intellisense namespace though, and it starts to become annoying when you also need StringBuilder and have to import System.Text but aren't using anything else from that namespace. Or maybe you need a single Dataset, but nothing else from System.Data. You can quickly accumulate several using directives that only exist to support one class declaration. The point is that the class library heirarchy in .Net is rather flat, and there are a lot of little things you might want that in C# that all require a using directive where VB.Net does not.If IO.File.Exists("foo.bar") Then
So C# doesn't let you use a partial namespace in a declaration. Big deal. Well, this example only illustrates the least of my complaints. The worst things about what I've shown so far are that it leads to a polluted intellisense prompt and that it breaks your flow to have to jump to the top of the page to add the declaration and then jumb back versus simply typing a shorter name. If this was all there was to it I would just keep my mouth shut and deal with it. But there are other, more important manifestations. I'll give two examples.
Say you're working on a project that involves a lot of XML. You'll probably import the System.Xml namespace. In C# you may also need to import one or more of System.Xml.Schema, System.Xml.XPath, System.Xml.Serialization, or System.Xml.Xsl. And now you're using a whole bunch of different classes with no reference in the code for which specific namespace each class came from. In VB.Net you can just preface the class names of classes not directly in the Xml namespace with only the missing child namespace. For example, if I choose not to import System.Xml.Schema but already have System.Xml I can still just say "Schema.XmlSchema" instead of "System.Xml.Schema.XmlSchema".
That sounds a lot like the same complaint I had earlier, and it usually ends up requiring a little more typing than importing all the namespaces once. What's new, though, is that in my opinion this has the potential tomake the code easier to understand. The specific example of "Schema.XmlSchema" is pretty redundant, but there are plenty of cases where having one level of the namespace with the class would add clarity to the declaration. This is especially true for junior developers who may not be totally familiar with the framework. Used correctly, it can provide just a little bit of important context for each of your declarations.
Now for the next example. After all, the Xml namespace is pretty well understood. Also, my XML example wasn't very good; what do we need the extra context for? Fair enough. Let's look at something even more relevant. What if you're working on a project where you want to use a third party library? In this case, being able to see a small amount of context for each class may have a little more value. And what if you've never used this library before?
The SharpZipLib comes to mind as a reasonable example. Imagine your project involved using the library to untar some files. In C# you would import ICSharpCode.SharpZipLib.Tar and then type class names as usuall. However, there is no help from the IDE in finding out what those classes are without re-typing the entire namespace every time. This shows one final reason why partial namespaces are useful. In VB.Net you just import ICSharpCode.SharpZipLib. Now, if you only type "Tar." you get an intellisense list of members of that namespace as soon as you hit the period key. For this reason, I find VB.Net is much easier to work with when learning the ins and outs of a new library.
In summary, I really like VB.Net's ability to use partial namespaces in declarations. There's probably a more official name for the feature, but I don't know it. It might even be something you can just turn on as an option for a project in C#, in which case I hope someone will tell me where to find that option. I think the feature promotes code that is more readable, doesn't break the flow of the programmer as often, keeps your intellisense namespace clearer, and aids in learning new libraries.
I don't know how many times I've seen code similar to the following on programming help forums:
Function MyMethod(ByVal InputParameter() As String) As String()
'Do Stuff here that returns a different string array
End Function
That's okay. We can make that work just fine. I mean, it could be worse; they could have used an ArrayList. But we can do better, too. Now look at this code:
Function MyNewMethod(ByVal InputParameter As IEnumerable(Of String)) As StringCollection
'Do stuff here that returns the string collection
End Function
This code is a drop in replacement for the code above. By that I mean that anywhere you call the first method, you could replace it with the 2nd method and your code will still work. You don't have to change anything else except the type of the variable that accepts the result. So if it works the same, why change? I mean, it takes a little more thought to read it and therefore you could argue it takes more to maintain. What do you gain? The answer is that you've just made the function more useful and flexible.
Let's start with the InputParameter. Perhaps right now you have always have a string array when you call it. But what if later you start working with something like an ArrayList or the generic List(Of String)? Those will both work with that method right now, with no other changes. You can even put Xml or datatables through there with a little work. IEnumerable(Of String) will accept anything that can give you a string in a For Each loop. So just by changing the type I've instantly made the code more powerful.
Now for the return type. I could have used IEnumerable(Of String) here as well. However, in this case that would actually limit the capabilities of the function. You'd lose the ability to look at values by index. What I want to do is expand the capabilities of the function. String() already implies IEnumerable(Of String). However, by moving from the array up to a StringCollection I not only keep that ability but gain the ability to easily add or remove items from the collection. I also get some bonuses like the nice .Contains() and .IndexOf() methods. So again, I've expanded what the function can do, and therefore made it more valuable.
For example, you might now be able to use it somewhere that before would have required a separate (but very similar) function. Or the switch to the new function might save you having to write a for each loop on the return value because of additional capabilities in the collection like the .Contains() method. It will enable you to get the same work done in less code.
One other point is that the new version should perform about the same as the old one. There might be a very small loss, but any difference is likely to be minor relative to other considerations in your code. Certainly falls under the heading of "premature optimization."
In general, we can think of IEnumerable(Of String) as a wider type, and StringCollection as a more powerful type. A good rule of thumb is to accept a wider type for input and return a more powerful type for output. In this way you will make your code more useful, and in the long run that's probably a good thing.
ASP.Net is often mistaken for a simple update to Classic ASP. Just take ASP, throw in some .Net classes, and you're done. You get a better IDE and you might get a small performance boost from using pre-compiled code, but not much else. Of course, you could use ASP.Net like Classic ASP. If that's as far as you go then I suppose everything above would be true. In fact, one of the nice things about ASP.Net is it's ability to work with you as your skills grow, and give at least small benefits right away. But if you stop here you're missing out on the best parts of the language.
ASP.Net allows you to be more declarative in how you lay out your page. If an SQL SELECT query is the classic example of declarative code, than you can think of each server control as a little SELECT query. It's a way to declare what you want to the framework and let it worry about how to actually do it rather than listing out every little step. The advantages of this approach are numerous: your code base is smaller, busy-work is reduced, you see pages from a higher level, you get better separation of concerns for designers and developers... I could go on. You might be surprised to hear that there's a huge potential performance benefit as well.
If that thought does surprise you, it shouldn't. After all, it's what makes SQL fast. Take an SQL cursor, for example. A cursor is usually the slowest way to accomplish any task in a database. Why? Because it's procedural. If you can re-write the cursor to use declarative statements you will nearly always see a significant performance improvement. This is because the database can now use it's cache and indexes, and even execute the operation in parallel. The same concept applies to a web page, and for the same reasons. I don't know to what extent, if any, ASP.Net applies these concepts. But it should be theoretically possible.
First we'll look at caching/indexing. With declarative code the server can get a rough picture of what every instance of a page will look like. It can use this picture to create and cache a pre-loaded version of the page, where all the declared controls and HTML are loaded and put in the intial state defined by the aspx code. This is a huge improvement, because the amount of work left to do for each request is greatly reduced. Classic ASP would have to start from the beginning for every single request and work it's way through all of the page code to be sure of getting the correct result, like any other procedural code. The ASP.Net environment can take a few shortcuts.
Now let's move on to parallelism. Server controls in ASP.Net ultimately boil down to plain old XML, and every XML document is a tree structure of tags. The nature of XML means you have a certain amount of independence between siblings at any given level of the tree; the contents of one sibling aren't really relevant to the contents of another sibling. That means that each sibling can be processed in parallel. In practice ASP.Net controls can have code that modifies other parts of the page, so this ability is not absolute. However, it should be possible for the compiler to analyze the code and build a dependency tree for a page, and in this way get some advantage. The important thing here is that less procedural code means a more straightforward dependency tree and a greater the potential for parallelism.
As CPUs with more cores become more and more common a built-in mechanism to render a page in parallel will become more and more significant. This is true even though a web server may already utilize multiple cores by processing separate requests in parallel. For example, while one node on a page waits for a request to a database to complete, rather than blocking the entire page other nodes can continue to process. In this way individual requests can still be served faster.
I need to repeat that I have no idea if these concepts are currently implemented. I suspect at moment they are not, and it's a shame if that's so. But the possibility and potential here is certainly interesting.
One of my friend reached out to me last week for help. The photographer she hired for her wedding had gone bankrupted. Since she has not gotten the album now she can't even get her wedding photos in either prints or digital form. All she has access to is the photographer's web site where the people can order prints from. She was hoping that I can somehow retrieve all the photos (~1220) from the site so that at least she would have digital copies of her wedding photos.
I ran across a request in a forum today to create an Excel column name from an index. It sounds simple, but it's harder than it looks.
The obvious solution here is to think about a column name as a base 26 number, with A-Z for digits. Unfortunately, it doesn't quite work like that. The '0' digit is broken. For example, counting the column names from A you wrap around to AA after reaching Z. If this were base 10 it would be like counting from 1 to 9 and then getting 11 instead of 10, or counting from 0 to 9 and then getting 00 instead of 10, depending on whether you treat A as 0 or 1. So it's tricky.
I thought I could get around that but that it would take more work than it's worth, so I decided to look around online. Surely there would be something already out there. What I found was a bunch of over-complicated implementations that all break somewhere on one of the boundaries I described. Even the Microsft support example doesn't work well. What a disappointment.So I ended up writing a new version after all. This one will scale, and it's not even that complicated. I just had to get a little recursive:
Function ColumnName(ByVal index As Integer) As String
Static chars() As Char = {"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, "G"c, "H"c, "I"c, "J"c, "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, "W"c, "X"c, "Y"c, "Z"c}index -= 1 'adjust so it matches 0-indexed array rather than 1-indexed column
Dim quotient As Integer = index \ 26 'normal / operator rounds. \ does integer division, which truncates
If quotient > 0 Then
ColumnName = ColumnName(quotient) & chars(index Mod 26)
Else
ColumnName = chars(index Mod 26)
End If
End Function
That still needs some basic bounds and error checking, but it works well for a quick sample. It's only 11 lines of code as is appears in my IDE (curse the vox formatter!) so it's pretty easy to follow. It should perform well too, since it would be very odd have more than one or two recursive calls. Now hopefully Google can index this page better than all those bad implementations I saw out there, but I'm not holding my breath.
You can probably see me standing in the back, as I got there late!
Story
Back in my early teen years I learned about this programming thing called BASIC, and from there borrowed/bought a bunch of books to learn how to make a video game (amongst VB 6.0 apps). The game is reminiscent of the Zelda (NES) dungeons or levels. I'm not sure exactly how I came up with the name Meepers, but it had something to do with my first character graphic looking like the Muppet's Beaker character, who I believe I thought was called Meeper (due to the character making meeping noises). I guess they don't look that much alike, but whatever. Anyway if you'd like to check out more info please visit my site (I'm working on it, no frills right now) http://www.tranbonium.us/projects/meepers/.
Videos
- Meepers DOS v142 Gameplay - Video I captured of the newest version of the game as it was 10 years ago.
- Meepers XNA Initial Preview - Video of the new version I'm starting to create using C# and the XNA Game Studio from Microsoft (maybe I'll be able to host it up on XBox Live Arcade - you never know ;-P)
This post assumes you have installed the necessary pre-requisites for Silverlight 2 beta 1 and are using Visual Studio 2008.
Setup your solution
Open Visual Studio 2008
Click File > New Project
Select the Silverlight 2 project type
Select the Silverlight Application template
Provide an application name and click OK
You will be prompted to select whether to host your Silverlight application in a Web Site or Web Application Project. We’ll select Web Application Project
Add a WCF Service to the Web Application project
See my blog post on How to create and connect to a WCF Service in VS08 for instructions on setting up a WCF Service. Ignore the project setup …when you get to the section titled “Create the service proxy”, make sure to add the service reference to the Silverlight Application project.
You now have a Web Application, setup to host your Silverlight 2 application, which contains a basic WCF Service, and an empty Silverlight 2 application that is setup to connect to your WCF Service.
Now we’ll create a basic interface for your Silverlight Application.
Create the Silverlight interface
Use the following xaml markup for your interface:
<Grid x:Name="GrdService">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<WatermarkedTextBox x:Name="TxtName" Grid.Column="0"
Watermark="Enter name here..."
HorizontalAlignment="Center"
Width="190"
VerticalAlignment="Center"
Height="30"
Margin="5" />
<Button x:Name="BtnCall" Grid.Column="1"
Content="WFC Call"
HorizontalAlignment="Left" />
<Border Grid.Column="2"
BorderThickness="1"
BorderBrush="Black"
Margin="5"
HorizontalAlignment="Center"
Width="190">
<TextBlock x:Name="TxtResponse"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>
</Grid>
Add the Width and Height properties to the UserControl tag of the Page.xaml file. Set the Width to 500 and Height to 40
Add a Click event handler to the Button control by selecting the Click property. As you fill in the property you will be prompted with an option to select , doing this will automatically create a handler in the Page.xaml.cs code behind.
Connect to the service
Open the Page.xaml.cs file
Locate the button event handler you just created end enter the code below:
private void BtnCall_Click(object sender, RoutedEventArgs e){
// substitute the path to your service here
string wcfUrl =
"http://localhost:[port]/[WebProjectName]/[ServiceName].svc";
BasicHttpBinding bind = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(wcfUrl);
HelloService.HelloClient wcfClient =
new HelloService.HelloClient(bind, endpoint);
// creates a delegate, called when the asynchronous web method call
// completes
wcfClient.SayHelloCompleted +=
new EventHandler<HelloService.SayHelloCompletedEventArgs>(
wcfClient_SayHelloCompleted);
// call the web method asynchronously
wcfClient.SayHelloAsync(this.TxtName.Text);
}
If you didn’t automatically create the SayHelloCompleted delegate create as follows:
void wcfClient_SayHelloCompleted(object sender, HelloService.SayHelloCompletedEventArgs e)
{
if (e.Error == null)
this.TxtResponse.Text = e.Result;
}
There is one more thing to change before this will work. Open the web.config in the Web Application project and locate the tag with the name of your service. In the tag modify the binding property to use “basicHttpBinding”. This is required for Silverlight connectivity.
Ex. binding="basicHttpBinding"
You have successfully connected to a WCF Service from a Silverlight 2 beta 1 application. One thing to note, if you want to connect to a WCF Service that is hosted in a different domain than your Silverlight application you will need to follow the instructions located here to setup a client access policy.