Showing posts with label pex. Show all posts
Showing posts with label pex. Show all posts

Thursday, March 17, 2011

Be Assertive: Change your (programming) life using power of defensive thinking.

We use defensive programming extensively.
In the context of Radical Programming it boils down to three practices
  1. Make no assumptions about the input to your code. Don't trust it to be well behaving. Build a firewall of validation code around your public interfaces.
  2. Use assertions in code to check assumptions.
  3. Test the code thoroughly. Try every way to break your code.

Murphy's Law

Murphy's law states that if something can go wrong then eventually it will.
We can not predict all the ways in which our code will be called. Our assumptions may turn out to be wrong. Code branches you "know" will never be taken are executed. The conditions that must "obviously" be true are far from. Eventually in some case our code will fail miserably. Assertions are our friends.
Always test your code with debug build and lots of assertions.


Fail Often, Fail Fast, Fail Cheap.

Aim of the defensive programming is to discover as many bugs during development as possible. Ideally we should write as many assertions as possible. Initially there will be lots of failures and it will be difficult to get the code to pass unit tests or even the basic scenario running. However once all those issues are fixed they tend to not come back and what remains is a robust code.
Assertions are basically alternative way of comprehending your code. When your assertions fail in most cases they mean that your implementation is wrong, but sometimes your assumptions/assertions are wrong. Either way you have found a problem and end up in a better place.
Once you encounter the invalid data, you should immediately stop processing it to limit unexpected side effects. Failure in one place may not be visible until it creates mayhem in other component and results in data corruption. Best place to catch failures is before it starts cascades of failure. A stitch in time saves nine.

Three types of Checks

There are generally three types of checks.
1. Checks that help you during development and debugging
This is the bulk of assertions that go into debug builds. These are often preconditions and post conditions, loop invariant etc. The important 
2. Checks that must be part of release build
Parameter validation is one example. In case of public interfaces you must throw exceptions for invalid parameters so that caller knows what went wrong. If you use just debug asserts the assertion will not fail in release build and continue causing mayhem downstream. There are other genuine conditions where your code can not continue.
You have to make sure that asserts you use stops execution at the point of failure in release code.
3. Checks that validate the consistency of  state and data at various points.
It is important to check internal consistency periodically. For example if your internal data is a tree data structure then it is important to check that there are no cycles. This code is useful as assertions, in test code and even in released code as "feature".

Self document code with assertions.

Like unit tests the assertions are part of the active documentation. Every place in your code where you write a comment, think if there can  be a assertion instead.

Use of oracles

Oracles are alternative implementations that give the same results.
If using assertion is perfection then using oracles is divine.

Fulfill assertion's destiny by subjecting code to testing and static analysis.

Writing the assertions is only half the story. You must also exhaustively test the code.
Monkeying around
Unit tests and scenario tests are no doubt helpful, but it also helps to monkey around your code by actively giving it input that will break it.
Active Debugging
It always helps to instruct your debugger to break on assertion failure so you can actually watch your program fail.

 

Do not write clever code

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?
-Brian Kerninghan
Often times the clever code fails because the requirements change and the underlying assumptions are no more valid. Cleverness is based on taking short cuts and to be frank it does "cleverly" use the context to achieve that. But when you add new functionality and refactor that context is gone. And you are left with the broken code.
The KISS principle viz. Keep It Simple & Stupid is still the best advice.

Ideas for coming up with great Assertions.

Books 

Toward Zero Defect Programming Writing Solid Code (Microsoft Programming Series)

Tools: 
PEX is a great tool from Microsoft Research

More Info


Wednesday, January 26, 2011

How Programming Language Shapes Thought: Overview of Series

In my last blog I went down the memory lane to see some of my own hand written notes from 1996-1998. Actually reams and reams of papers filled with many interesting ideas ranging from chip design to programming languages from Natural language processing to object oriented operating system design. (I also found bunch of science fictions I wrote that I am going to cleanup and publish.
I am planning a series of blogs on the subject. Here is an overview.  

I am opinionated because I'm Literate.
Donald Knuth coined the term "Literate Programming 
He says:  Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do.
Planned Blog : Don't be illiterate!!

Syntax sugar is good.
 In my opinion the number one job of any programming language is to make programmers productive. I hate people who say "it's just syntax sugar". Yes, you are right- you idiot! Assembler is a syntax sugar, C is a syntax sugar, C++ is syntax sugar ..so on and so forth. I want 10th generation language right now. As long as it makes me productive and makes my intent clear sugar is good!! 
Planned Blog : Sugar is good!!
If someone is insisting on writing the 'raw' code then they are wasting their time and money.

Code is Design  
Software is different, the code we write is itself design.
Planned Blog : Be Extreme! Refactor your thinking: How to neutralize the guys who ask for design specs... 

Quality vs. Correctness: Be Assertive. 
Now, as software engineers we spend a huge amount of time finding fixing and debugging the bugs. So being productive means language features that force me to think about quality and make it easy to write the code. One of the books that has influenced me the most is Writing Solid Code. Two things that have stuck with me for lifetime are (A) I use asserts like crazy (B) I use debugger a lot. 
I like to be super paranoid so lots of asserts. And I write a lots of tests which tend to be exhaustive.
Here is a catch though : Absence of bugs is not quality and presence of asserts is not correctness.  When I misunderstand something or simply did not think about something. I write wrong code , wrong asserts and wrong tests ! All unit tests pass and I have a bug filed against me !!!  
planned blog: Be Assertive
planned blog: Quality Strategy
planned blog: Being Probably Correct .. 
planned blog: Automate my way to disaster. 
planned blog: Fragile Development

Ineffective Design!
One of my favorite books was  The Design and Evolution of C++ . The three takeaways ..
  1. Language must be must be a natural progression of what programmers are comfortable with right now.
  2. It must be statically compiled. With no runtime overhead.It must be able to link with existing code.
  3. Pay the price for only for the features that you use. Also,
With multi paradigm language like C++ one problem is that with increased flexibility comes increased complexity and increased risk that you'll shoot in your foot. You are always free to write the ineffective, sloppy or very clever "(!?#@)" code.  With increased flexibility comes increased responsibility. Thankfully there are books like these
Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)
More Effective C++: 35 New Ways to Improve Your Programs and Designs
But problem is that all that good stuff is option.
Be Effective ! Analyze that! 
Use static analysis tools
Other blogs
  • Design by contract
  • Unit Testing
  • Pex an interesting Idea
and many more ...
    Testing Object-Oriented Systems: Models, Patterns, and Tools (ARP/AOD) 2 Vol. SetTest Driven Development: By ExampleToward Zero Defect Programming