usable in any place a human can be used

Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

20091007

just because its better doesn't mean its good

I recently wrote a blog post called why no love for scripting languages lamenting the lack of open source scripting environments in Windows 7. I got some interesting feedback, most of which went along the lines of "POWERSHELL!!!1!!one!"

Being an interested fellow I took it upon myself to look up PowerShell, and it looks like a nice language, really good for administrative tasks. So I read some of the manual and it looked ok, and I started looking for some scripts that would compare old school .bat to the new PowerShell. I found what I was looking for here PowerShell Examples. We will be looking at an example that displays the current date.

@ECHO OFF
IF NOT "%1"=="" GOTO Syntax

:: Use BATCHMAN to retrieve day
BATCHMAN DAY
:: Errorlevel 0 means BATCHMAN was not found
IF NOT ERRORLEVEL 1 GOTO NotFound
FOR %%A IN   (1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL  %%A SET DD=0%%A
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 1%%A SET DD=1%%A
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 2%%A SET DD=2%%A
FOR %%A IN (0 1)                 DO IF ERRORLEVEL 3%%A SET DD=3%%A

:: Use BATCHMAN to retrieve month
BATCHMAN MONTH
FOR %%A IN (1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL  %%A SET MM=0%%A
FOR %%A IN (0 1 2)             DO IF ERRORLEVEL 1%%A SET MM=1%%A

:: Use BATCHMAN to retrieve year
BATCHMAN YEAR
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL  %%A SET YYYY=198%%A
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 1%%A SET YYYY=199%%A
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 2%%A SET YYYY=200%%A
FOR %%A IN (0 1 2 3 4 5 6 7 8 9) DO IF ERRORLEVEL 3%%A SET YYYY=201%%A

:: Store in variable and clean up temporary variables
SET SortDate=%YYYY%%MM%%DD%
SET YYYY=
SET MM=
SET DD=

:: Display the result
ECHO.
ECHO SortDate = %SortDate%
GOTO End

:Syntax
ECHO.
ECHO SortDate.bat,  Version 1.00 for MS-DOS
ECHO Display the current date in YYYYMMDD format
ECHO.
ECHO Usage:  SORTDATE
ECHO.
ECHO This batch file uses BATCHMAN, a utility by Michael Mefford
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:End

Rough, if you've ever had to write anything non-trivial in batch you will start to feel that pain at the back of your eyes right now, this is your brain trying to eat your memories. Batch is hideous, and difficult to write, and gross, and everyone hates it. Don't believe me, well you don't have to, Microsoft agreed with me and began development on Monad in 2003, this project would become PowerShell. Now let's look at the PowerShell equivalent of this code

""
"Date / Format   YYYYMMDD        DD-MM-YYYY        MM/DD/YYYY"
"============================================================"
"Yesterday       " + (get-date (get-date).AddDays(-1) -uformat %Y%m%d) + "        " + (get-date (get-date).AddDays(-1) -uformat %d-%m-%Y) + "        " + (get-date (get-date).AddDays(-1) -uformat %m/%d/%Y)
"Today           " + (get-date -uformat %Y%m%d)                        + "        " + (get-date (get-date)             -uformat %d-%m-%Y) + "        " + (get-date (get-date)             -uformat %m/%d/%Y)
"Tomorrow        " + (get-date (get-date).AddDays(1)  -uformat %Y%m%d) + "        " + (get-date (get-date).AddDays(1)  -uformat %d-%m-%Y) + "        " + (get-date (get-date).AddDays(1)  -uformat %m/%d/%Y)

That is much better. I see some stuff that looks like objects in there (I'm definitely seeing the dot operator). I'm a n00b to PowerShell and yet this code is easy to grok and all and all very pretty. Nicely done Microsoft, you get a cookie.

Well this would be a pretty boring post if I just went around patting Microsoft on the head for doing a good job. The interesting thing about PowerShell Examples is that they go on to provide the same example in other languages as well. Here is the same thing in perl.

#! perl

# SortDate.pl,  Version 1.00
# Display "sorted" date (YYYYMMDD)
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Parse time string
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

# Add "base year"
$year = $year + 1900;

# Add 1, since moth seems to be zero based
$mon  = $mon  + 1;

# Add leading zeroes if necessary
if ($mon < 10) {
 $mon = "0".$mon
}
if ($mday < 10) {
 $mday = "0".$mday
}

# Concatenate substrings
$sortdate = $year.$mon.$mday;

# Display result
print "\nSortDate = $sortdate\n";

Certainly could use a text formatter of some kind to get rid of the icky manual padding, but for someone who has never written a line of perl, this looks quite nice and readable (although I am quite aware that that is not always the case for perl). So what's got a bee in my bonnet? Well I almost didn't write this until my friend Jeremiah tweeted the following

Writing powershell. This is happily like writing perl. 7:31am Oct 6th

And a little bit later

ZOMG This is seriously just like writing perl... I <3 you #PowerShell 10:41am Oct 6th

Side note: Jeremiah is about the smartest man in the world when it comes to SQL follow him here and view his blog facility9.

Why go about reinventing the wheel, if you are going to make a language similar to perl, just host perl. What's the harm? Its hard to call it harm, but its inefficient and tastes a little bit too much like embrace, extend, extinguish to me. PowerShell becomes the defacto scripting language in Windows enticing open source programmers because it is similar to their language of choice. So why fret, PowerShell, perl, batch, bashscript, who cares? Well there are legion reasons why software developers should care.

  1. CPAN, PEAR, GEMS, etc. - These are huge repositories of tested code that can be leveraged quickly and easily by open source developers. CPAN (Comprehensive Perl Archive Network) contains 16,600 modules. PEAR (PHP Extension and Application Repository) contains 536 different packages with 1,255,213 lines of code. RubyForge hosts 8406 different projects. These established languages have a gigantic ecosystem of usable code.
  2. Learning curve - Do you know the intricacies of your chosen language? Did you spend two hours debugging a wily error and because of it have forever learned some dark corner of your language? Can you tell me in your sleep the difference between $foo and $$foo in php? Well none of that will help you in the new PowerShell.
  3. Brain drain - Come up with a really clever way to solve a problem in PowerShell, great, keep it to yourself. Where is the community? There is powershellcommunity.org. But there is yet to be an established authority for the community

Now these problems I outline are true of any emerging language. They are normally offset by some inherent positives in the language. I haven't examined PowerShell in-depth enough to find out its intrinsic value. On first blush and with my limited exposure, it seems to be a competent enough pseudo object oriented scripting language well suited for administrative tasks. Nothing ground breaking, nothing that knocked my socks off, in the words of Homer Simpson:

I saw you desperately trying to cram one more salty treat into America's already bloated snack hole. So I did what I could. I did what any loving husband would do! I reached out to some violent mobsters.

PowerShell is just one more salty treat that Microsoft is cramming into America's already bloated snack hole. It would be fine if they had a level playing field and allowed other scripting languages to be first class citizens, but they don't. After the mind numbing pain of batch scripting, PowerShell seems great. It really starts to lose its shine when you view it against the cornucopia of free mature open source scripting languages.

I want to end with a real world example (anonymized). There was a system that managed blerns. Blerns could be collected in collections called Bars, Bars could be collected in collections called Foos. These things were numbered and a system was built to manage them in COBOL. Because COBOL likes to make pictures, the original architects thought to themselves, we will pack the information into an 8 digit number like so FFBBbbbb, so that 01010001 would be the first blern, in the first bar, in the first foo, 01010002 would be the next and so on.

This worked great, the company was selling blerns left and right, and pretty soon foo:01 had 98 bars. Then things started to break down, 3 more bars, and the whole house of cards would come crashing down. What to do, what to do?! The engineers were assembled and solutions were offered.

We only have 8 foos, just assign the overflow into foo:10.

Seemed reasonable enough, but that would have resulted in weird code springing up all over the place like this

if foo == 1 or foo == 10
  return "foo the first"
else
  ...
end

Surely there was a better solution, what could it be? Well, suffice it to say I wasn't present for these meeting and I only saw the terror of the last solution being halfway implemented and this new solution coming in. The solution was a technical sounding concept called field widening. It amounted to storing the identifiers like so FFFBBBbbbb, 10 digits now capable of holding up to 1000 Foos, 1000 Bars per Foo, and 10000 Blerns per Bar. Surely this was better, but was it any good. I guess it might be, as long as we don't live in an exponential world.

Oh...



20091005

why no love for scripting languages

Windows 7 is about to drop 20091022. And the internet is buzzing with comments like this:

Definitely the best OS of all time. - WindowsFTW
Snow Leopard for me please! - Sam
It's sad that people are excited by this level of mediocrity only because XP is archaic and Vista is a disaster. Talk about lowered expectations.

Get your anti-virus, and registry cleaners, and disk defragmenters, and other assorted utilities ready. You'll need them. Modern OS? Not hardly. - Terry

Well putting religion aside, Windows 7 looks to me what Vista should have been. Microsoft is doing a great job with the ad campaign, they have awesome buzz going, and the computing world will probably be better off thanks to Windows 7.

To be fair I use Vista Business at work and Vista Home Premium at home with an Ubuntu partition that I dual boot into. I don't hate Windows or Microsoft, but I do feel that both deliver underpowered products at overblown prices, their greatest strength is that with such a high market share other manufacturers make their products to easily inter-operate. That is really the thing that keeps me from dropping Vista at home, after 4 hours of trying I still couldn't get my PS3 to play nice with my Ubuntu box for media streaming, it worked in 2 minutes with Vista.

So here is my question for Windows 7, why no love for scripting languages?

Apple's new Mac OS X Snow Leopard has built in support for Ruby, PHP, Perl, and Python.

Ubuntu can install almost any hosting environment with the click of a button or by issuing a simple sudo apt-get ruby

Windows is supposed to be the OS for everyone, easy to use, so where as the Ubuntu nerds are more than happy with firing up a terminal and apt-getting some fun, why doesn't Windows have these scripting environments ready to rock out of the box?

Could it be that Windows is philosophically opposed to hosting scripting environments? I don't think so as they build in a J-Script interpreter, try double clicking a .js file.

Could it be that Windows foot print would be gigantic if they included these languages? Ruby has a 23.7MB installer, Python 2.6.3 is 14.2MB, Perl 6's Rakudo source is 314KB, and PHP 5.3.0 has a 23MB installer. So in total we are talking about 60MB of disk space.

So why no love? I'm guessing that it is three fold.

  1. Not invented here syndrome. These aren't Microsoft technologies, so why bundle them in with a Microsoft product. This is somewhat valid, if these environments expose a security hole or need lots of updates, this is overhead for Microsoft that doesn't add any money to their bottom line but costs them in maintenance.
  2. Conflicts with the .Net platform. If these environments are installed by default, then people could make HotCocoa like bindings and create Windows programs outside of the .Net platform. Microsoft wants people to use IronRuby, IronPython, CLR based scripting languages. Really they want you to use C# and Visual Basic.Net so that you will buy the Visual Studio licenses, they are after all a software business.
  3. Unsuitable hosting environment. This one is more conjecture as I'm not an OS Engineer, but it is informed conjecture. Windows lacks the fundamental security model that is at the heart of Unix systems. Windows 7 will more than likely continue with the bolted on solution of User Account Controls, although hopefully less annoying in this latest release. This makes the idea of sandboxing a scripting environment much more difficult in the Microsoft world than in a Unix based OS.

But Windows 7 definitely should include these scripting languages, and others (lua is the first to come to mind), because it will ultimately benefit them. Steve Ballmer once famously shouted on stage "Developers, developers, developers, developers!"



There is a world of Ruby, Python, PHP, Perl, Lua, Ocaml, Erlang, Haskell, etc. developers that have a huge hurdle to jump in getting their programs onto the Windows platform. They can either bundle the interpreter into their program a-la py2exe, they can try to get their program to run on a CLR based implementation of their language (if one exists), or they can try to get the user to install the necessary platform.

If Microsoft made it easier to release to these platforms, a world of new and innovative software could flourish on the PC platform. Rails development wouldn't have to suggest that if you have the misfortune of owning a windows-box to do all your development inside of a virtual linux-box.

If you’re working on Windows, you may find it easier to install Instant Rails. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.

From what I understand it is not currently baked in to Windows 7, but maybe we could throw developers a bone in SP1. If they could standardize the convention of scripting languages installation on the Windows platform, they could become a friendly release environment to a large segment of the open source movement. If Microsoft wants to engage the Open Source community in a meaningful way, this would be a great first step. Anything is better than the sham CodePlex Foundation Microsoft recently tried to pass off as an Open Source haven, but that really warrants its own post.