Assuming you want to create a new function, there are a few ways you could build it.
For now let’s focus on the function’s return type…
Type First
In this case, you know the type of the value which your function will return.
You have decided that you want to return a integer.
The simplest way to do this is using the mi<space> template, which will result in the code below.
All that’s needed is:
- An appropriate name.
- Any required parameters.
- The body of the method.
…and you’re done.
But what if, in that first moment, you don’t know what type you’d like to return from your method?
Type Last
If you’re not entirely sure of the type your method should be returning, don’t worry…. Just don’t specify one …. yet.
Use CodeRush’s m<space> template to generate the method which has no return value. Otherwise known as a void proc.
As before you have to specify:
- An appropriate name
- Any parameters
- The body of the method.
However let’s suppose that you’ve done this and you have your brand new Return42 function.
You’ll notice that at this point our function doesn’t compile. This is, of course, because we are attempting to return an int from a method which specifies a return type of void.
This is clearly unacceptable and needs fixing. Ordinarily we’d visit the signature of the function, highlight the word void and overtype it with int.
This can be rather annoying. However CodeRush has our back.
Simply place your caret on the return keyword and select Convert to Function from the smart tag menu.
CodeRush deduces the type of the expression being returned, and alters the signature of the method to int rather than void.
In our example, the involved type was easy to deduce and relatively simple, but the procedure works with more complex expressions as well.
Summary
People aren’t all the same, and work in a myriad different ways.
I’m one of those people who tend not to think in terms of the specific type I’m returning, until the point of actually returning a value.
CodeRush doesn’t force me to think about this, and is happy to wait until I’m ready.
When I am ready, CodeRush still has my back with tools to help achieve my goals in my way.