Archive for the 'Software Engineering' Category

Fuzzy about Scala

I’ve recently started working on a new project (SwiftKey), and my immediate priority is to get our testing sorted out. To that end, I’m constructing a fuzz test suite. Partly because I think that it’s the right tool for the job, and partly because it’s a good opportunity for me to learn more about the language, I’m writing it in Scala.

A fuzz test interacts with the System Under Test in a random, yet structured manner. The object of the exercise is to construct sequences of API calls (or network traffic, or whatever) that flush out the kind of bugs that are less likely to be discovered by unit tests tests and the like. Fuzz tests are particularly good at finding things like security problems, buffer overruns, memory leaks etc.

Clearly randomness is a large part of a fuzz test, so I’m creating some utilities that make it easier to randomly choose between several different actions. I thought that I’d publish them just in case anyone else finds them useful, and also to solicit feedback to help me improve my use of Scala (bear in mind that I’m still finding my feet, so be gentle!).

So, here’s my first attempt:

val random = new Random
 
def oneAtRandom(actions: () => Unit*)() =
  () => actions(random.nextInt(actions.length))()

oneAtRandom takes a set of functions and returns another function which, each time it’s called, executes one of them at random. For example:

val randomAction = oneAtRandom(
  () => print('a'), () => print('b'), () => print('c'))
 
for(_ <- 1 to 10)
  randomAction()

Which gives the following output:

aabaacbbcc

So far, so good. But what if we want different functions to be executed with different frequencies? We might have some actions that we want to only happen occasionally? Here’s another version of oneAtRandom that takes a weighted set of functions and calls them in proportion to their relative weights:

def oneAtRandom(actions: (Int, () => Unit)*)() = {
  val totalWeight = actions.foldLeft(0) {_ + _._1}
  () => {
    val r = random.nextInt(totalWeight)
    var w = 0
    val (_, action) = actions.find {
        case(weight, _) => w += weight; w > r
      }.get
    action()
  }
}

Here’s how it’s used:

val randomAction = oneAtRandom(
    (1, () => println("rare")),
    (10, () => println("occasional")),
    (50, () => println("frequent"))
  )
 
for(_ <- 1 to 100)
  randomAction()

And to prove that it’s doing what it claims:

$ scala random2.scala | sort | uniq -c
  88 frequent
  11 occasional
   1 rare
 
$ scala random2.scala | sort | uniq -c
  82 frequent
  12 occasional
   6 rare

So I’ve got it working, which I’m happy about. But I’m sure that it could be improved. I’m sure, for example, that there must be a “nice” functional way to implement the loop inside the weighted version (instead of using var w as an accumulator), but each of my attempts to do so have ended up being considerably more complex than the current version.

I’d also love to use the -> notation to pass the arguments, for example:

val randomAction = oneAtRandom(
    1 -> () => println("rare"),
    10 -> () => println("occasional"),
    50 -> () => println("frequent")
  )

But doing so results in a parse error—the only way I’ve found to avoid it is to add additional parentheses:

val randomAction = oneAtRandom(
    1 -> (() => println("rare")),
    10 -> (() => println("occasional")),
    50 -> (() => println("frequent"))
  )

which is starting to feel a bit too much like Lisp for my taste :-)

Finally, I’m really missing some of the handy little utilities provided by Ruby. In Ruby, for example, I could write the loops that call randomAction like this:

10.times { randomAction }

I know that it’s trivial to create this kind of utility, but one of the nice things about Ruby is that they all come as standard out of the box.

In any case, I’d be very grateful for any and all feedback on the above. Thanks in advance!

Debug It! Review Roundup

It’s clear that the author’s years of debugging experience were superbly distilled into this collection of high impact advice.

www.drdobbs.com

A must have book for anyone writing programs.

www.felgall.com

Perhaps it’s the fact that it’s Paul Butcher’s first book that gives Debug It! its remarkable charm and conviction. The fact that very few other books cover the theme on such an intellectual and psychological level undoubtedly helps set it apart from the few books currently out there too. Either way you look at it, Paul’s take on debugging is certainly essential reading for those looking to clean up their act and, more importantly, their code.

www.linuxuser.co.uk

Overall, this book is going to be a huge win, and I think it’s a worthy successor to the Release It! reputation. Development managers and team leads should get a copy for the junior developers on their team as a Christmas gift, but only after the senior developers have read through it as well.

blogs.tedneward.com

Excellent discussion of the strategy of debugging. Buy, it read it and kill bugs.

i-programmer.info

I feel like armored with all this knowledge I am ready to become a one man Anticimex army for software bugs. Now it has been a while since I read the book but I remember solving bugs with a new confidence while reading it and after. Strongly recommended for everyone doing software development in any form.

The C#rypt

Frankly, I wish this book would be read by a lot of developers

www.jasonbock.net

This book is highly recommended as it contains a whole lot of wisdom and experience from the field of software engineering. It’s only 190 pages, so you should be able to get through very quickly. So, you can’t lose on this one.

ElegantCode

I think that this book should be read by all new software developers as well as all junior or mid-level developers. There’s plenty to be learned by the senior developer types too. Even if you’ve been doing these things for a long time, this is a good read to make sure you’re keeping your feet planted firmly on the ground.

lostechies.com

With my many years of experience in supporting and debugging large existing enterprise systems, I have to say that Paul Butcher summarize and structure all the knowledge (and more) that I have, sometimes painfully, accumulated during this activity. This is therefore an excellent book that I will recommend to everybody that is involved in software development in general and maintenance activities specifically.

blog.martinig.ch

I know that this book influenced the way I work now, and there aren’t many books I could say something like this about.

devlicio.us

All in all, there is just a lot of tremendously valuable information in this book. And it’s only about 190 pages so it definitely won’t take you a long time to read it. I’ve frequently been amazed at the inability of developers to efficiently debug issues when they occur. And i’m not just talking about bad developers. I’ve seen plenty of good or even great developers having trouble with debugging efficiently. This book would definitely get them on the right track, with just a little bit effort.

davybrion.com/blog

Understanding is everything: that is at the heart of Paul Butcher’s comprehensive study of the science and psychology of debugging.

softtalkblog.wordpress.com

The book does a great (and truly pragmatic) job of covering all these aspects and addressing a wide range of topics related to debugging software.

hamagudi.com

If you are too busy to read this delightful book in its entirety, then at least read the final chapter whilst mandating that your entire development team read “Debug It!” from cover to cover.

javablog.co.uk

As usual, the publisher has an excellent book on a practical subject that answers oractical qustions on debugging.

matt.eifelle.com

I would highly recommend Debug It! to any junior-level programmer who’s interested in developing a more disciplined approach to debugging. If you’re not a junior-level programmer but still feel like you waste a lot of time debugging, you will probably find this book helpful as well. It’s like having a mentor sitting there with you, teaching you how to take your debugging game to the next level.

lylejohnson.name

It’s really good seeing these ideas in words because it’s quite easy to forget about the best way to approach problems in the heat of the moment and the approaches suggested by Paul certainly aren’t done everywhere in my experience.

www.markhneedham.com

While I was familiar with many of the practices discussed in the book … I learned quite a few new things.

starglider.blogspot.com

It does a great job of setting the scene for debugging and getting you into the right mind set, while also talking about the complications that can arise once the bug is found and squashed. It’s almost worth looking at for the anecdotes alone, to understand the lengths that you sometimes have to go to when trying to understand some truly bizarre defects.

blog.jonmdickinson.com

This is a recommended read for anyone who works closely in the software industry, be it a developer, a tester or even a product manager.

vidhujoshua.blogspot.com

Debug It! in Brazil

Debug It!

Debug It! is making its way around the world, as this review by Luiz Marques in Brazil demonstrates.

Luiz is kind enough to say:

While I was familiar with many of the practices discussed in the book … I learned quite a few new things.

He goes on to say:

Well worth the time, specially as it is not very long.

Many thanks, Luiz.

Forgive and Remember

You’re recovering from a major operation—which nursing unit do you choose? One that reports an error once every 500 patient days, or one that reports an error once every 50 days?

What if I were to tell you that in the first unit, which on the face of things makes 10 times fewer mistakes, nurses don’t report errors because they’re concerned that “heads will roll.” Would that change how you feel? How many errors are being swept under the carpet? Do you think that they’re likely to be learning from their mistakes or repeating them over and over again? (This example comes from Hard Facts, Dangerous Half-Truths & Total Nonsense by Jeffrey Pfeffer and Robert I. Sutton, Harvard Business School Press, ISBN: 1-59139-862-2.)

In software, we have our own name for mistakes—we call them bugs. And every bug is an opportunity to learn.

To read the rest of this article, take a look at Issue 6 of PragPub magazine

Debug It! Sample Code

Debug It!

Sample source for section 10.3 “Resource Leaks and Exception Handling” is online here.

It’s setup to run in Visual C++ 2008, but should be relatively easy to port to other environments.

Any questions, you know where I am!

Ted Neward reviews Debug It!

Debug It!

Ted Neward has just published a review of Debug It! on his blog.

His summary is:

Overall, this book is going to be a huge win, and I think it’s a worthy successor to the Release It! reputation. Development managers and team leads should get a copy for the junior developers on their team as a Christmas gift, but only after the senior developers have read through it as well. (Senior devs, don’t despair—at 190 pages, you can rip through this in a single night, and I can almost guarantee that you’ll learn a few ideas you can put into practice the next morning to boot.)

Debug It! reviewed on devlicio.us

Debug It!

Krzysztof Koźmic has just published a review of Debug It! on devlicio.us:

http://devlicio.us/blogs/krzysztof_kozmic/archive/2009/08/30/book-review-debug-it-find-repair-and-prevent-bugs-in-your-code.aspx

Within the review, he says:

I know that this book influenced the way I work now, and there aren’t many books I could say something like this about.

Which is the best compliment I can imagine as an author. Very many thanks, Krzysztof.

First reviews of Debug It!

Debug It!

The first couple of reviews of Debug It! have recently been published:

Book Review: Debug It!

Debug it! A Book on Software Debugging

Help, this is driving me mad!

The Pragmatic Programmers have just started publishing an online magazine: PragPub. Among other things, it contains a quiz which in this issue tests your knowledge of programming languages.

I consider myself a bit of a languages geek, so I thought that I’d have no trouble with it. But it’s driving me mad!

The quiz consists of little snippets of code in different languages. The first letters of each language then spell out the name of another language.

I can get 5 of the 6, but one of them has me completely stumped. Even though I’m 99% sure what letter it’s name must start with.

Warning: Spoilers follow!

Continue reading ‘Help, this is driving me mad!’

Debug It! in beta

Debug It!

As you may know, I’ve been working on a book. It’s been a long process, but the finishing line is finally in sight – as of today, it’s available as a “Beta Book” from The Pragmatic Bookshelf:

http://pragprog.com/titles/pbdp/debug-it

This means that it’s available for purchase and download as a PDF and ebook, and the paper version can be pre-ordered. :-)

Update

See Debug It! Review Roundup.