- Home /
Refactoring Code
Im going mad with C#. Today is my 3rd day trying to refactor a SIMPLE snippet of code.
private RebindableKey FindKeyByInputName(string inputName) {
RebindableKey foundKey = null;
RebindableData data = GamePicker.getRebindableData();
foreach(RebindableKey key in data.GetCurrentKeys()) {
if(key.inputName == inputName) {
foundKey = key;
}
}
return foundKey;
}
private RebindableAxis FindAxisByInputName(string inputName) {
RebindableAxis foundAxis = null;
RebindableData data = GamePicker.getRebindableData();
foreach(RebindableAxis key in data.GetCurrentAxes()) {
if(key.axisName == inputName) {
foundAxis = key;
}
}
return foundAxis;
}
Please, i would really appreciate if someone could give me a light, those typing rules are driving me mad! I would love if Unity could support a non-typed language.
if you were checking identical fields/etc. for different classes then it's straightforward, and maybe worthwhile, but that doesn't appear to be the case.
there's no easy refactor because you not only have different classes (the easy part), but you're also checking different properties/members/etc. the data.
data.GetCurrent$$anonymous$$eys()
data.GetCurrentAxes()
key.inputName
key.axisName
you could try to map the data onto a common structure using an interface or even inherited class, but that's a lot of effort to save 10 lines of code. without knowing those other class (`Rebindable$$anonymous$$ey, RebindableAxis, GamePicker`) it's difficult to know.
far me it from me to say how you might spend your time, but it might be better spent doing something else. you can't get those 3 days back...
There is no error to solve, the code is working, i just trying to refactor the whole thing.
I totally agree with @Owen and @gjf, those classes requires tiny improvements which could solves the whole question at once. The point is that those classes where not made by me, its a script package that i downloaded from asset store in the attempt to speed up my time in the simple task of $$anonymous$$apping custom $$anonymous$$eyBindings ingame.
It should be a simple task, but now i found myself stucked on those little details. About the 10 lines question, im started to became very a concerned about the number of times these problems are happening on my codebase. $$anonymous$$ost of them related to code based on Unity packages which ends up shortening the time between development and completion, but at the same time, add dirty and noises to my codebase.
I tried to refactor the code using generics, but as @gjf said, they arent simple different classes, they are different classes with different typed methods.
Nevertheless, it should not be a problem hard to solve. I think i just dont have the enough expertise in C# to do it in the right way.
Thats why i asking help.
Answer by Owen-Reynolds · Sep 02, 2014 at 03:38 PM
love [...] a non-typed language.
There's your problem. I'm not saying there might not be a fabulous solution to this in Lisp. But doing fancy stuff with classes in C# pretty much requires you to think in strong types.
My first thought would have been that there's going to be a big list of intermingled keys & axisis, at least displayed to the user. And no way am I building that with string and tape -- there needs to be a common base class RebindableThing. Not because it's cool, but because it will save time.
But, it takes a while to be even decent at this. Examples in books range from terrible to pointless. I've found the only way to learn is to waste a few months -- make a design, use it, realize its junk; make a better one, realize that's only slightly better junk... . Then, even the best design can become useless when you have to add this new, unexpected game feature.
Luckily, there's a long tradition in games (and real programs) of just getting it to work. So, you have to duplicate every single function dealing with keyBinds and axisBinds. Game coders do much uglier things every day.
Answer by kacyesp · Sep 02, 2014 at 03:46 PM
What you have is fine. Yes, it looks like a bit of repetitive code, but it's neat, concise, and not worth spending 3 days over.
You should that think about the cost-benefit.
If you're going to spend days refactoring, spend it refactoring the overall design.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162102im_/https://answers.unity.com/themes/thub/images/avi.jpg)