0

I am quite new to metaprogramming especially in C#, and so I am looking for good pointers on how I can achieve the following. We start with these methods:

public JObject x(int a)
public JObject y(int b)

Now, I have a requirement to add an optional parameter to all methods in this file, while keeping backward compatibility. So in order to satisfy the last requirement, I need to create new methods for each of those like this:

public JObject x(int a)
public JObject y(int b)
public JObject x(int a, int o = defaultValue)
public JObject y(int b, int o = defaultValue)

Now, the problem is that I have 50+ of these methods, and I want to avoid copy pasting things around if possible. I don't have much experience with metaprogramming so my question is:

  • Any ways the tooling around Visual Studio / ReSharper can help me achieve that?
  • Should I write some script that parses my file and does this for me? (any recommended resources there?)
  • Or should I just have fun copy pasting :( ?

Thanks in advance!

Guy Daher
  • 5,314
  • 5
  • 36
  • 64
  • Are you adding these to an existing class - i.e. inheriting down to add these? – Enigmativity Sep 11 '17 at 09:39
  • @Enigmativity Yep, not inheriting, just adding in the same class – Guy Daher Sep 11 '17 at 09:40
  • 3
    If you have method `x(int a)`, then adding `x(int a, int o = defaultValue)` is a [bad idea](https://stackoverflow.com/q/2674417/1997232) (it's confusing and may cost someones time when debugging). Either **change** existing method to support optional parameter or add new method **without** default value: `x(int a, int o)`. – Sinatr Sep 11 '17 at 09:49
  • 3
    Why do you need to keep old signature if you're adding new parameters as optional? – Aleksey L. Sep 11 '17 at 09:52
  • @GuyDaher - Then you should be generating a `partial class` and it sounds like you need to post a [mcve] for us to give you better advice. – Enigmativity Sep 11 '17 at 09:52
  • There is no single best way because none of your options are good, your best bet is probably to just copy/paste and adjust. – Lasse V. Karlsen Sep 11 '17 at 09:58
  • @Sinatr: Now that you mention it, you are totally right. I should remove the optional param, thank you! – Guy Daher Sep 11 '17 at 10:03
  • 1
    @AlekseyL. Backward compatibility requirement. Check [here](https://stackoverflow.com/questions/1456785/a-definitive-guide-to-api-breaking-changes-in-net/23517490#23517490). – Guy Daher Sep 11 '17 at 10:04

2 Answers2

0

Script is probably the best option I can think of off the top of my head.

Though the question itself brings out a code smell to me. Optional arguments themselves I find irritating as they tend to result in big complicated methods full of if elses. Also using optional arguments would mean you could maintain backwards compatability adding them to the existing method call.

I think there's a bigger design issue that should be looked at around the need to add 50+ overloads with optional arguments.

Twisted Mentat
  • 414
  • 4
  • 13
  • I think this answer is correct, but highlights that the question is simply too broad to give a specific answer. – Enigmativity Sep 11 '17 at 09:54
  • Agreed. I am modifying to remove the optional arguments and just add overloaded methods. Thanks for pointing that out. But for the backward compatibility point that you made, checkout [this](https://stackoverflow.com/questions/1456785/a-definitive-guide-to-api-breaking-changes-in-net/23517490#23517490). – Guy Daher Sep 11 '17 at 10:06
-1

Visual Studio search and replace supports RegEx searches, and you can use the matches in your replacement, so that's probably the way to go.

James L
  • 14,843
  • 10
  • 44
  • 67