Feature Request: Select block of code/method, hit key, shows example execution

Here's a feature that I think could offer a major advantage for IntelliJ - as important as code navigation and refactoring.

Imagine I have some code:

public String doSomething(String s) {
  return s + " hello " + s.trim();
}


And some other code:

// bunch of stuff
doSomething(someString)


Now, currently, with the cursor on doSomething, I can hit ctrl-shift-i to show the underlying code.

What would be awesome is if I could hit another keystroke (or maybe the same) and it would popup a table showing some example inputs and results.

Eg

Input Results
null NPE
"" " hello "
"example" "example hello example"
" example " "  example  hello example"
"Exa MPLE" "Exa MPLE hello Exa MPLE"



Potential issues:
- coming up with list of inputs - really not that hard - for Strings, the list above would be enough to cater for a lot of cases - alternatively, it could be interactive, have an editable textbox for each input, run the code and update the results on the fly as you type, like the Regex plugin
- complex parameters - ie compound objects - that's ok, just don't support that use case initially, just handling Strings and primitives would be great initially

If not a core part of the product, maybe someone keen could hack up a plugin to demonstrate the concept.

7 comments
Comment actions Permalink

Third potential issue: While I could imagine this to work for static methods (or methods that don't access instance state), I don't think it could be done for non-static methods in a way that makes things clearer than RTFSC.

Ideally, explanations like that should be written in Javadoc - which IDEA can display since ages (even though I admit that writing the HTML for a table displaying the examples in Javadoc can be quite painful - even with the aid of an IDE. Maybe that would be an idea for a plugin?)

Regards,
Jens

0
Comment actions Permalink

There are a lot of complexities but that's what JetBrains are good at dealing with.
The same kind of objections were probably voiced before the Extract Method refactoring became common. There are all sorts of cases where a code selection means that extract method is not possible - don't worry about those, work on the cases where it can work.

ideally of course code should have Javadoc but that's unrealistic. I'm a contractor. As such, I often come in to a role at Megacorp X and have to get up to speed with a codebase which is often poorly documented. The code navigation in Intellij helps heaps with this but there's often convoluted methods that are hard to decipher. Having a few examples instantly pop up would be wonderful for understanding.

0
Comment actions Permalink

a while ago, i stumbled across a junit-generator which did exactly what you are proposing here. however, it only works in simple cases, and these are easily understood by looking at the code. generating input for complex logic - for example, imagine a method that takes a graph as a single parameter - is nearly impossible.

0
Comment actions Permalink

Neville,

Have you ever looked at Agitar ?

http://www.agitar.com/solutions/products/agitarone.html

Click on the demo link for a 5 minute demo of their product.

Basically, it automatically generates JUnit tests that verify a method handles expected and unexpected inputs correctly.  It will flag cases where it throws a NPE or other unexpected exception, and it will flag cases where it doesn't throw a declared exception, e.g. IllegalArgumentException.   It does handle more complicated Object parameters which require their own constructor.

It is interesting esp. if you are developing APIs, although it doesn't work with classes that talk to databases or external services.

I would recommend Jetbrains look into this if they haven't.   The value proposition is very strong, esp. for large enterprises which have millions of lines of legacy code.
You can read their business value proposition here http://www.agitar.com/solutions/business/  
My company has licensed it, although it is very expensive since they license it based on the number of classes.

-Alex

0
Comment actions Permalink

I remember hearing about Agitar on a Java Posse episode.
That's why I know it's doable.
Again, doesn't have to cater for all cases - just simple stuff initially.

0
Comment actions Permalink

Once upon a time, there was a plugin that would generate a sequence diagram for a method or cod. That was very useful for dealing with quickly groking a codebase. Maybe that would be a useful feature/plugin.

0
Comment actions Permalink

This!
This is almost exactly what I'm talking about.
He enters some example values as inputs to the function, then it shows the execution output of the method *as he types*!

http://vimeo.com/36579366

Relevant part starts at 16:45

0

Please sign in to leave a comment.