I made an extension method for Action and I'd like to replace all occurrences of action() with action.SafeInvoke() in the code. But the problem is that action is just an example var name, and in reality they are all different. Searching by () will obviously produce terrible results. Searching by Action and looking up every individual var's references in code and finding direct invocation among that is kind of slow.
Is it possible to somehow find all occurrences of Action invocation by () operator when the variable names are unknown?
As I understand it, you have instances of type Action like:
Action foo = ...
Action bar = ...
And then you have in the code:
foo();
bar();
You want something that will change foo() to foo.SafeInvoke() and bar() to bar.SafeInvoke().
There's nothing in Visual Studio that will do this for you automatically, and writing something to do it would be non-trivial. You'd spend a whole lot more time on that extension than you would by making the changes manually.
You can use Visual Studio's "Find usages" functionality to find every place that Action is used. That will at least help you locate where you need to make the changes.
I'd be interested to know, what your SafeInvoke does. If all it does is a null check, you might want to consider if it's really necessary.
Related
I have some code which depends on some include files which are partly defined at the start of source files (which is usual) and others which are used within functions.
I typical example for that are the OpenFOAM solver sources.
Because the scheme of this code is highly procedural, but I want to put all this into a class which provides init(), run() and maybe release(), I plan to put some of the variables into the classes as private making them members.
I don't want to modify the included files because they belong to a library.
The reason for using a class is that other routines classes run together with this code.
Here is the thing. init() must prepare some variable and there situations that theses variables (being type of other clases) not explicit constructors and special arguments. It is called once. run() is called several times. The procedural code has a loop only and the contents of that loop are put into the run() method.
So the best solution was to put these variables into std::unique_ptr and init can construct whatever it needs to. Obviously with that trick the variable signature changed, so I created a second declaration of a reference like this:
std::unique_ptr<volScalarField> mp_p;
volScalarField &p = *mp_p;
Now this is a bit tedious so I created a macro
FOAMPTR(volVectorField, p)
which does all the work for me:
#define FOAMPTR(TYPE,NAME) std::unique_ptr<TYPE> mp_##NAME; TYPE &NAME=*mp_##NAME
It works pretty well, but I'm not fan of macros in general, especially if you need to debug code.
Now my question is: Is there a better way to tackle this and use something else like a template definition which might do all the magic?
Edit: With 'works pretty well' I mean, that the compiler can translate that. The reference though still is invalid.
Edit: Okay, I solved the invalid pointer problem using two Macros:
#define FOAMPTR(TYPE,NAME) std::unique_ptr<TYPE> mp_##NAME
#define FETCHFOAMREF(NAME) auto &NAME=*mp_##NAME
Now I put FOAMPTR(TYPE,NAME) to the member and I get my unique ptrs. In the run() method the second macro FETCHFOAMREF(NAME) is used. Of course init() must be sure to correctly initialize the object or else the program is going to crash.
I still leave the question open because I'm not satisfied with that solution.
Is there a way to perform the opposite of the "Extract Method" refactor in Visual Studio?
I have a legacy codebase that has ~50 very short, private functions that are only used once each, and I have been tasked with inlining them.
If an automatic inline refactoring isn't possible, is it possible to reduce the amount of time it takes to inline these function calls? My current workflow is:
Copy the code in the function.
Find where it is called.
Replace the function call with the copied code.
Replace local variable names from the function.
Delete the function.
The refactoring you are looking for is called "Inline Method".
While Visual Studio does not provide this refactoring out of the box, you can get access to it (and many other useful refactorings) by installing the Jetbrains ReSharper extension for Visual Studio.
With the extension installed, all you need to do is click on the method declaration or method call, and invoke the 'Inline Method' refactoring. This will automatically inline all occurrences of the method and delete it.
You might consider collecting the functions into a header file and decorating them with inline. I realize this is not exactly an answer to what you asked, but may be a better solution to your problem because this way the compiler would perform the inlining (if it sees fit).
It depends on the situation, but keeping the function definitions might lead to clearer code, thus might be of value even if called only once.
This is also quicker and less error prone than manual "inline method" refactorings.
In Xcode4, I'm finding that I can't get code completion to readily narrow for multi-parameter method names where the first part is common. For example, consider the following methods from UITableViewDelegate:
- tableView:viewForHeaderInSection:
- tableView:heightForHeaderInSection:
- tableView:accessoryTypeForRowWithIndexPath:
...
The list goes on; there are quite a few methods that start tableView. If I type "tableView", the list is long and not quickly navigated. If I type "tableView:", code completion is exited as soon as I type the colon (:). If I type a run-on such as "tableViewview", completion also exits, presumably as it no longer literally matches the first method name part.
Is there some way to complete via the keyboard on such a list without resorting to either mousing around or clattering out a random number of arrow key hits to navigate the long list? I'm having a hard time believing that code completion for multi-parameter Objective-C methods is so limited, but neither experimentation nor documentation have proved illuminating thus far.
Update for clarification:
Above I'm referring to completion of a method declaration (e.g. in a .m file), not of a method call. For method calls, Xcode 4.x does completion for each method parameter part independently; this works great. The problem above only applys to declarations, generally when starting to write a new method implementation in a .m file.
It does seems that the behavior you expect is (unfortunately) not implemented in Xcode. However, I've found a partial solution: code completion works when you specify the complete first part of the multi-parameter method, i.e. if you type this (with space at the end)
- tableView:(UITableView *)tableView
and press ESCAPE key then the code completion dialog will appear and you will be able to keep typing the rest of the method name.
You are probably wondering:
"Oh boy, do I really have to type in this long preamble just to get
my code completion?"
Code snippets to the rescue! You just have to type the preamble once and convert it into snippet. I found it's best to configure it the following way, don't forget to add space at the end of code part, it will make things easier.
How should you use the thing you've just created? In your .m file start typing tabl and Xcode will let you autocomplete the snippet for you. Once the preamble is placed press ESC to reveal the auto-completion list.
This solution is not very robust, since you have to create a snippet for every multi-part method you want to use, but it does save some time and there aren't that many multi-part method names that share the same prefix.
I'm annoyed by this problem for quite a long time. Now I can verify that there is no way to do even in the newest Xcode 5, I submitted a bug report:
Multi-parameter method declaration completion in Xcode
Please dup it if you also want it.
Oh sorry, it does not work that way I mentioned with protocol stubs like you wanted in your update.
I use Accessorizer for that, an app in the mac app store to fully implement the declaration of your delegate/protocol methods you want to implement. Hope that help a little bit.
Old post:
I use Tab ⇥ for this.
Example I often have:
You want one of many init methods in the code completion.
Then you don't want to scroll down or use to many keystrokes to get to the right method.
Hit i > hit Tab ⇥ > init is completed.
Hit Escape ⎋ and then w and then again Tab ⇥ for all these initWith methods.
You can even use Tab ⇥ / Escape ⎋ after the first parameter, when there is more than the method with only one parameter.
In your case hit t > Tab ⇥ > Escape ⎋ > a and Return ↩, there you got your third method.
You can also jump to the previous code completion argument with Shift ⇧ + Tab ⇥.
What I like to do is remove all functions which has specific word for example, if the word is 'apple':
void eatapple()
{
// blah
// blah
}
I'd like to delete all code from 'void' to '}'.
What I tried is:
^void.*apple(.|\n)*}
But it took very long time I think something is wrong here.
I'm using Visual Studio. Thank you.
To clarify jeong's eventual solution, which I think is pretty clever: it works, but it depends on the code being formatted in a very particular way. But that's OK, because most IDE's can enforce a particular formatting standard anyway. If I may give a related example - the problem that brought me here - I was looking to find expressions of the form (in Java)
if (DEBUG) {
// possibly arbitrary statements or blocks
}
which, yes, isn't technically regular, but I ran the Eclispe code formatter on the files to make sure they all necessarily looked like this (our company's usual preferred code style):
if (DEBUG) {
statement();
while (whatever) {
blahblahblah(etc);
}
// ...
}
and then looking for this (this is Java regex syntax, of course)
^(\s*)if \(DEBUG.*(?:\n\1 .*)*\n\1\}
did the trick.
Finally did it.
^void.*(a|A)pple\(\)\n\{\n((\t.*\n)|(^$\n))*^\}
Function blocks aren't regular, so using a regular expression in this situation is a bad idea.
If you really have a huge number of functions that you need to delete (more than you can delete by hand (suggesting there's something wrong with your codebase — but I digress)) then you should write a quick brace-counting parser instead of trying to use regular expressions in this situation.
It should be pretty easy, especially if you can assume the braces are already balanced. Read in tokens, find one that matches "apple", then keep going until you reach the brace that matches with the one immediately after the "apple" token. Delete everything between.
In theory, regular language is not able to express a sentence described by context free grammar. If it is a one time job, why don't you just do it manually.
Switch to VI. Then you can select the opening brace and press d% to delete the section.
This is a coding style questions:-
Here is the case
Dim obj1 as new ClassA
' Some lines of code which does not uses obj1
Something.Pass(obj1) ' Only line we are using obj1
Or should we directly initiaize the object when passing it as an argument?
Something.new(new ClassA())
If you're only using the object in that method call, it's probably better to just pass in "new ClassA()" directly into the call. This way, you won't have an extra variable lying around, that someone might mistakenly try to use in the future.
However, for readability, and debugging it's often useful to create the temporary object and pass it in. This way, you can inspect the variable in the debugger before it gets passed into the method.
Your question asks "should we create objects"; both your examples create an object.
There is not logically any difference at all between the two examples. Giving a name to an object allows it to be referred to in more than one place. If you aren't doing that, it's much clearer to not give it a name, so someone maintaining the code can instantly see that the object is only passed to one other method.
Generally speaking, I would say no, there's nothing wrong with what you're doing, but it does sound like there may be some blurring of responsibilities between your calling function, the called function, and the temporary object. Perhaps a bit of refactoring is in order.
I personally prefer things to be consistent, and to make my life easier (what I consider making my life easier may not be what you consider making your life easier... so do with this advice what you will).
If you have something like this:
o = new Foo();
i = 7
bar(o, i, new Car());
then you have an inconsistency (two parameters are variables, the other is created on the fly). To be consistent you would either:
always pass things as variables
always pass things created on the fly
only one of those will work (the first one!).
There are also practical aspects to it as well: making a variable makes debugging easier.
Here are some examples:
while(there are still lines in the file)
{
foo(nextLine());
}
If you want to display the next line for debugging you now need to change it to:
while(there are still lines in the file)
{
line = nextLine();
display(line);
foo(line);
}
It would be easier (and safer) to have made the variable up front. It is safer because you are less likely to accidentally call nextLine() twice (by forgetting to take it out of the foo call).
You can also view the value of "line" in a debugger without having to go into the "foo" method.
Another one that can happen is this:
foo(b.c.d()); // in Java you get a NullPointerException on this line...
was "b" or "c" the thing that was null? No idea.
Bar b;
Car c;
int d;
b = ...;
c = b.c; // NullPointException here - you know b was null
d = c.d(); // NullPointException here - you know c was null
foo(d); // can view d in the debugger without having to go into foo.
Some debuggers will let you highlight "d()" and see what it outputs, but that is dangerous if "d()" has side effects as the debugger will wind up calling "d()" each time you get the value via the debugger).
The way I code for this does make it more verbose (like this answer :-) but it also makes my life easier if things are not working as expected - I spend far less time wondering what went wrong and I am also able to fix bugs much faster than before I adopted this way of doing things.
To me the most important thing when programming is to be consistent. If you are consistent then the code is much easier to get through because you are not constantly having to figure out what is going on, and your eyes get drawn to any "oddities" in the code.