2

OK, so I have a variety of methods that require a serial number and part number. My problem is that some methods have the order swapped:

void Foo(string serialNumber, string partNumber);
void Bar(string partNumber, string serialNumber);

This is an absolute pain because I have to find all references to these methods and manually check all the variable names and values passed in as arguments. Carefully reading is much harder than compiler errors. I could do something like the following:

public class SNString
{
    public SNString(string serialNumber)
    {
        SerialNumber = serialNumber;
    }

    public string SerialNumber { get; set; }

    public override string ToString()
    {
        return SerialNumber;
    }
}

and reset the methods

void Foo(SNString serialNumber, PNString partNumber);
void Bar(PNString partNumber, SNString  serialNumber);

This allows for compler errors, but it's really ugly to call:

Foo(new SNString("123"), new PNString("456");

I would also have to go through the long slog of fixing all the compiler errors generated by this change, and it's probably more work than manually validating all my method calls. Is there a better way to do this?

Maybe even a coding AI that would look for all variations of variables named "sn", "serialn", etc, and flag potential problems?

stevep
  • 167
  • 1
  • 7
  • 1
    I don't understand. You have two methods with the same parameters but the order of the parameters is different. Are all the references to both methods currently being called correctly (i.e., correct argument is passed to the corresponding parameter) and you just want to unify the order of the parameters across both methods? If so, what does this have to do with the names of the variables? Or do you _currently_ have wrong values passed to the methods? – 41686d6564 stands w. Palestine Jun 04 '22 at 21:51
  • I've found instances where the variables are passed in the wrong order. So something expecting sn, pn gets pn, sn from the caller. I get no compiler error. I just have a strange runtime error that can be hard to debug. It's a problem of different developers writing methods that expect parameters in different orders. It causes confusion and mistakes for the callers. I want someway to validate it or generate a compiler error. – stevep Jun 04 '22 at 21:54
  • You can refactor the methods in visual studio, just change the orders of the parameters and the callers should be refactored also. Are you tracking down a bug? – Raymond Holmboe Jun 04 '22 at 21:58
  • In that case, it's a bad idea to try and automate this, IMO. The safest way is to review each reference manually. After you fix the existing problems, you can worry about unifying the order of the parameters (is that what you want to do moving forward?) which _can_ be automated. – 41686d6564 stands w. Palestine Jun 04 '22 at 21:58
  • @RaymondHolmboe Can you share a link to the kind of refactoring you're talking about? I have a feeling this is just a painful manual process.. – stevep Jun 04 '22 at 22:02
  • https://learn.microsoft.com/en-us/visualstudio/ide/reference/change-method-signature?view=vs-2022 – Raymond Holmboe Jun 04 '22 at 22:06
  • Also found this Roslyn analyzer that will emit errors if parameters are not named: https://stackoverflow.com/a/51891862/1172996 (because c# can't enforce parameter naming) – Raymond Holmboe Jun 04 '22 at 22:07
  • 1
    @stevep In VS: `Edit > Refactor > Reorder Parameter`. However, that won't help you when some of the callers are using the wrong order of arguments. As I said above, the safest option is to go through each reference manually and make sure the methods are being called correctly before worrying about any refactoring. – 41686d6564 stands w. Palestine Jun 04 '22 at 22:08
  • You could always create something like a `PartsData` class which has a serial and model number and pass that instead. The class could be built such that it could not be created without both bits of data – Ňɏssa Pøngjǣrdenlarp Jun 04 '22 at 22:14

1 Answers1

1

As Raymond touched on, calling methods with named arguments could be a solution. It assures the correct arguments to be handed over regardless of the order of arguments in method signatures.

You can call Foo and Bar methods with the same order of arguments.

Foo(serialNumber: "Serial Number", partNumber: "Part Number");
Bar(serialNumber: "Serial Number", partNumber: "Part Number");
emoacht
  • 2,764
  • 1
  • 13
  • 24