c# - refactoring if else -
i refactoring legacy code. there big if else statement. pattern can use. have used dictionary in past if-else has lot of (&&) don't know how refactor this.
for eg code is
public void changecontrolvisibiltiy() { if (name != "i") { //do } if ((name != "a") && (name != "b") && (name != "c") && (name != "d") && (name != "f") && (name != "g") && (name != "h") && (name != "i") && (name != "j") && (name != "h") && (name != "i") && (name != "j")) { //do } if ((name != "a") && (name != "b") && (name != "d") && (name != "f") && (name != "g") && (name != "h") && (name != "i") && (name != "j") && (name != "h") && (name != "i") && (name != "j")) { //do } if ((name != "a")) { //do } if ((name != "a") && (name != "k") && (name != "l") ) { //do } if ((name != "a") && (name != "k") && (name != "l") && (name != "b") && (name != "d") && (name != "f") && (name != "g") && (name != "h") && (name != "i") && (name != "j") && (name != "h") && (name != "i")) { //do } if ((name != "a") && (name != "l")) { //do } if (name == "m") { //do } else if (name == "c") { //do } else { //do } }
well looks bit awkward remove 2. , 3. if statement. if have single parameter check can use switch statements.
nevertheless should use .equals() instead of == , please give real working code example. in general if have same code on , on again pattern extract new method e.g. if have smth name.equals("a") || name.equals("b")
make new method oredequals(string name, string... matches)
.
Comments
Post a Comment