Request : Change argument signature - pointer to reference & arguments to struct

Request 1 - Refactor pointer to reference/reference to pointer

It would be really nice if there were a simple refactoring option that would allow you to just change a pointer argument to a reference (or vice versa) and would update the usages to reflect this e.g.

class Base
{
public:
virtual void MyFunction(TYPE* obj)
{
obj->Invoke(true);
}
};

class Derived : public Base
{
public:
virtual void MyFunction(TYPE* obj)
{
obj->Invoke(false);
}
};

...

TYPE* obj1 = new TYPE;
TYPE obj2();
Base a;
a.MyFunction(obj1);
Derived b;
b.MyFunction(&obj2);

If you refactored the argument in the Base class MyFunction signature to const& then I'd expect (or very much like) to get this :

class Base
{
public:
virtual void MyFunction(const TYPE& obj)
{
obj.Invoke(true);
}
};

class Derived : public Base
{
public:
virtual void MyFunction(const TYPE& obj)
{
obj.Invoke(false);
}
};

...

TYPE* obj1 = new TYPE;
TYPE obj2();
Base a;
a.MyFunction(*obj1);
Derived b;
b.MyFunction(obj2);

And the inverse too.  It's just a fairly common thing I'm encountering with some huge old projects and it would be so very much nicer if it could be automatically handled for me.  Maybe there's a way to do this already, but it doesn't look like the change signature function is capable of this sort of change (based on my tests).

A good portion of the time when I'm changing a signature it's because of these sorts of things related to code style and maintainability rather than adding or removing arguments.

Request 2 - Arguments to Struct

Although on that side of things it would be really nice to have a refactor to structure. i.e.

void MyFunction(int has, int far, int too, int many, int arguments, bool which, bool is, bool silly)
{
if (which)
return has + far;
... etc
}
...
MyFunction(1, 2, 3, 4, 5, true, false, true);

Could have a refactor option that creates a structure complete with a constructor and adjusts the invocations too i.e.

struct MyFunctionStruct
{
MyFunctionStruct(int has, int far, int too, int many, int arguments, bool which, bool is, bool silly):_has(has),_far(far),_too(too),_many(many),_arguments(arguments),_which(which),_is(is),_silly(silly) {}

int _has = 0;
int _far = 0;
int _too = 0;
int _many = 0;
int _arguments = 0;
bool _which = false;
bool _is = false;
bool _silly = false;
};

void MyFunction(MyFunctionStruct myFunctionStruct)
{
if (myFunctionStruct._which)
return myFunctionStruct._has + myFunctionStruct._far;
... etc
}
...
MyFunction(MyFunctionStruct(1, 2, 3, 4, 5, true, false, true));

Or even better if you could select a partial set of arguments to be turned into a struct.  Obviously there would be limitations (although some of that could be helped by a move constructor being generated too).

Unless in the invocation code a line could be added creating the struct before the invocation then it would have to be a non-reference, and you'd have to leave that up to the user to refactor a second time to a reference, but if it could add the line then this would allow it and would help :

MyFunctionStruct myFunctionStruct(1, 2, 3, 4, 5, true, false, true);
MyFunction(myFunctionStruct);

And setting it to const (the ideal) would require checking that nothing in the function itself is assigning to the passed values, but perhaps if the original arguments were all const then it could do that automatically.

This would be useful as of course a large number of arguments is harder to read and maintain as well as being slower than passing around a single reference or pointer to a structure.

 

Anyhow, please do give these some consideration, automated refactoring that could do this would make my life a lot easier.

2 comments

Hello Per-Anders,

Thanks for your suggestions, I've filed corresponding feature requests in our issue tracker - https://youtrack.jetbrains.com/issue/RSCPP-21024 and https://youtrack.jetbrains.com/issue/RSCPP-21025. Can't promise that we'll get to them anytime soon, but the first refactoring certainly looks useful (it's different from Change Signature though as Change Signature does not modify the body of the function).

0

Please sign in to leave a comment.