PowerShell for Developers

PowerShell was written first and foremost for Windows system administrators, and the benefits to this community are clear. It’s not as clear what developers should make of PowerShell.

Administrators can learn PowerShell as a shell first, and gradually transition from interactive use to scripting. They may learn PowerShell as their first programming language and not even give too much thought to the language per se. But a developer has to ask why and when to use PowerShell rather than another language, such as C#.

Doug Finke’s new book Windows PowerShell for Developers (ISBN 1449322700) is “for developers” in a couple ways. First, the style of the book is geared toward developers. The book is small, less than 200 pages, because the author assumes the readers are experienced Windows developers who want to focus on what PowerShell adds to what they already know. Second, the book focuses on tasks a developer might want to do. Rather than show you how to create a new Active Directory user, as many PowerShell books would, this book covers topics such as

  • code generation
  • static analysis
  • interfacing with C#
  • embedding PowerShell in your application
  • working with XML and JSON
  • interfacing with Excel
  • creating DSLs.

So why should software developers use PowerShell? And what tasks should they do in PowerShell? One answer to the first question, implicit in the book’s examples, is that PowerShell makes it possible to carry out common tasks with little code. Another answer explicitly given in the book is integration.

Given PowerShell’s growing integration with the rest of the Windows platform, as PowerShell grows, so does your application.”

The book is full of examples of what tasks a developer might want to do in PowerShell. The examples I found most interesting were embedding PowerShell to provide a scripting language for your application and creating DSLs in PowerShell.

One pattern in the examples is text munging, whether that text is source code or common data file formats. Another pattern is integration, especially integrating Microsoft technologies. PowerShell is designed to make it possible to solve these kinds of problems with a minimum of ceremony.

I’ll close with a couple reasons why might a developer not want to use PowerShell in my opinion. The first is frequency of use. Although PowerShell can solve many problems with significantly less code than C# would require, you have to learn PowerShell first, and you have to use it frequently enough to remember it. You have to use PowerShell enough to repay the time invested in learning and practicing it.

The second reason is size. The C# language and the Visual Studio IDE were designed for large projects, but scale down fairly well for smaller projects. PowerShell was designed for the command line, but scales fairly well for large scripts. If you use PowerShell and C#, you’ll have to decide at what size you want to switch from one language to the other.

Related posts

Sociable numbers

A number is called perfect if it is the sum of its proper divisors, i.e. all divisors less than itself. For example, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28.

Amicable numbers are a sort of generalization of perfect numbers. Two numbers a and b are said to be amicable if a is the sum of b‘s proper divisors and vice versa.

The ancient Greeks knew of only one pair of amicable numbers: 220 and 284. Medieval mathematician Thâbit ibn Kurrah discovered two more pairs: (17296, 18416) and (9363584, 9437056). Leonard Euler (1707–1783) found 58 more pairs. Now over 12 million amicable number pairs have been found.

To generalize things further, start with a number n and compute the sum of its proper divisors, then the sum of the divisors of that number, etc. This sequence of numbers is called the aliquot sequence of n. If this sequence is periodic, n is called a sociable number.

If the aliquot sequence has period 1, n is a perfect number. If the sequence has period 2, n is part of an amicable number pair.

Are there numbers whose aliquot sequence has period 3? Not that we know of. Currently the only aliquot sequence periods that have been demonstrated are 4, 5, 6, 8, 9, and 28.

Related posts

Design for outcomes

Designing a device to save lives is not enough. People may not use it, or may not use it correctly. Or be unable to maintain it. Or …

I’ve seen analogous problems with statistical methods. People will not necessarily adopt a new statistical method just because it is better. And if they do use it, they may use it wrongly, just like medical devices.

(“Better” in the previous paragraph is a loaded term. Statistical methods are evaluated by many criteria: power, robustness, bias, etc. When someone says his new method is better, he means better by the criteria he cares most about. But even when there is agreement on statistical criteria, a superior statistical method may be rejected for non-statistical reasons.)

Related posts

Understanding recursion II

People often joke that in order to understand recursion, you must first understand recursion. This is funny as far as it goes, but it illustrates a common misunderstanding. (I’m not saying the people who make the joke don’t understand what they’re doing. They probably do, and are having a little fun with the people who do not.)

Novices often think that recursion means solving a problem in terms of itself, which sounds like an infinite regress. And it might be. But an important qualifier is missing in this description. Recursion is about solving a problem in terms of smaller versions of itself. The process ends because the problems get smaller until the process reaches some problem small enough to be solved directly.

It would be more accurate, but less humorous, to say that in order to have a good understanding of recursion, you must first have a rough understanding of recursion.

Related post: The secret to understanding recursion

Unprincipled analysis

The other day I started to call someone’s data analysis “unprincipled” until I realized how harsh that sounds. I wanted to convey that an analysis seemed ad hoc, not based on general principles. Then I realized that “unprincipled” implies someone is lacking moral principles rather than statistical principles so I changed my wording.

Related post: Works well versus well understood

Updating distribution diagram with JavaScript

A couple years ago I put up a web version of a chart of probability distribution relationships based on a chart by Lawrence Leemis. It lets you click on a distribution name or an arrow representing a relationship and get details. Here’s a thumbnail image of the diagram:

distribution relationship diagram

Ivan Yanikov had an idea for improving this, adding some JavaScript to the page so you can get additional information by hovering your mouse over the image rather than clicking. The advantage of this approach is that the page doesn’t scroll, so you don’t lose your place in the diagram. You can see his code here. If you’d like to play with it, save the distribution diagram page and paste Ivan’s code at the end just before the closing </html> tag.

Here’s a screen shot of what you’d see when you mouse over the geometric distribution.

mouseover screenshot

Ivan emphasized that this was a quick proof-of-concept rather than something he carefully polished. What would you do to improve it?

Related post: Six analysis and probability diagrams

Second-generation public-key cryptography

This review of a book on elliptic curves summarizes what has happened with public-key cryptography. In a nutshell, methods like RSA were the first generation, and elliptic curve methods are the second generation.

Second-generation methods provide more security per bit. For example, an RSA key with 3072 bits is no more secure than an elliptic curve key with 256 bits.

Late to the party

Learn You a Haskell for Great Good (ISBN 1593272839) is a hard book to judge by its cover. It’s about the Haskell programming language, but what is it like? The title and the art work are playful, and that gives the impression the book is light-weight. On the other hand, the table of contents lists two chapters on monads, so maybe it isn’t so light-weight after all.

Is this book funny or serious? It’s both. It reminds me of a couple of my favorite lines from G. K. Chesterton’s Heretics (ISBN 1613822707):

Mr. McCabe thinks that I am not serious but only funny, because Mr. McCabe thinks funny is the opposite of serious. Funny is the opposite of not funny, and of nothing else.

I’m not going to write a detailed review here because a lot of other people have reviewed it and I have only started reading it. Like I said in the title, I’m late to this party. But I’ve read enough that I think I understand why people recommend it. The book is written with a sense of humor and a casual pace, and yet it covers quite a bit. If you’ve looked at other Haskell books and found them too dry to read, as I have, you might want to try this one.

Related posts

Henri Poincaré: A Scientific Biography

The first clue that Henri Poincaré: A Scientific Biography (ISBN 0691152713) is not going to be a typical biography is in the table of contents. It lists one appendix on elliptic and Abelian functions and another on Maxwell’s equations. This is a biography of a mathematician that doesn’t shy away from math.

The subtitle is “a scientific biography” because the book is primarily about the work of Poincaré  rather than his personal life. It has more to say about the three-body problem and algebraic topology, for example, than about Poincaré’s parents.

I haven’t seen a book like this before. I’ve seen books that are essentially collections of scholarly papers with biographical footnotes. And at the other extreme I’ve seen biographies practically devoid of scientific details. But I don’t remember seeing a biography that unapologetically includes substantial scientific content in the course of telling the story of a scientist’s life.