根据反if运动,最好不要在我们的代码中使用ifs.任何人都可以告诉我是否有可能摆脱这段代码中的if?(switch也不是一个选项,重点是删除条件逻辑,而不是用类似的语言结构替换ifs)
if(s == "foo")
{
Writeln("some logic here");
}
else if(s == "bar")
{
Writeln("something else here");
}
else if(s == "raboof")
{
Writeln("of course I need more than just Writeln");
}
Run Code Online (Sandbox Code Playgroud)
(语言:Java或C#)
cle*_*tus 65
这是一种方式...... :)
delegate void DoStuff();
...
IDictionary<string, DoStuff> dict = new Dictionary<string, DoStuff>();
dict["foo"] = delegate { Console.WriteLine("some logic here"); };
dict["bar"] = delegate { Console.WriteLine("something else here"); };
dict["raboof"] = delegate { Console.WriteLine("of course I need more than just Writeln"); };
dict["foo"]();
Run Code Online (Sandbox Code Playgroud)
bma*_*ies 18
建立关联数据结构.Map<String, String>
在Java中,IDictionary<string, string>
在C#中.在开始时初始化它,然后......
Bal*_*usC 17
利用战略模式.
用Java术语:
public interface Strategy {
void execute();
}
public class SomeStrategy implements Strategy {
public void execute() {
System.out.println("Some logic.");
}
}
Run Code Online (Sandbox Code Playgroud)
您使用如下:
Map<String, Strategy> strategies = new HashMap<String, Strategy>();
strategies.put("strategyName1", new SomeStrategy1());
strategies.put("strategyName2", new SomeStrategy2());
strategies.put("strategyName3", new SomeStrategy3());
// ...
strategies.get(s).execute();
Run Code Online (Sandbox Code Playgroud)
ufu*_*gun 12
使用从抽象基类SomeThingWriter派生的虚方法编写类.
那么从基类派生的每个类都应该实现像writeSomething或任何你想要的函数.
abstract class MyBaseClass
{
public abstract void writeSomething();
}
class DerivedClass1 : MyBaseClass
{
public override void writeSomething()
{
Writeln("something else here 1");
}
}
class DerivedClass2 : MyBaseClass
{
public override void writeSomething()
{
Writeln("something else here 2");
}
}
Run Code Online (Sandbox Code Playgroud)
而不仅仅是打电话
MyBaseClass c = new DeriveClass1();
c.writeSomething();
c = new DerivedClass2();
c.writeSomething();
Run Code Online (Sandbox Code Playgroud)
Dra*_*mon 12
看看这个广告系列,它解释得非常糟糕.ifs没有任何问题,但在某些情况下,它们可以表明你没有充分利用OOP.
该活动试图推广的是增加多态性的使用,以便将调用代码与它正在查看的对象类型分离.
你会使用一些更聪明的对象而不是s作为字符串:
interface I {
public String getName();
public void doSomething();
}
class A implements I {
public String getName() { return "one"; }
public void doSomething() { ...; }
}
class B implements I {
public String getName() { return "two"; }
public void doSomething() { ...; }
}
Run Code Online (Sandbox Code Playgroud)
然后你可以用以下内容替换ifs:
I obj = ...get an A or B from somewhere...;
obj.doSomething();
Run Code Online (Sandbox Code Playgroud)
在某些情况下,避免if结构可能是合法的
在其他人,它只是简单的白痴,试图避免如果.
虽然为避免if结构而提供的示例是有效的替代方案,但您应该问自己:
为什么我要使我的代码不必要地复杂以避免简单的if结构?如果唯一的原因是你必须因为反竞选活动那么它的坏理由
Java的
使用实现某种方法的枚举.
enum MyEnum{
foo{
public void mymethod(String param1, String param2){
//dostuff...
}
},
bar{
public void mymethod(String param1, String param2){
//dostuff...
}
};
public abstract void mymethod(String param1, String param2);
}
Run Code Online (Sandbox Code Playgroud)
然后在你的班上:
MyEnum.valueOf(mystring).mymethod(param1, param2);
Run Code Online (Sandbox Code Playgroud)
首先,在阅读这些"反"运动时要非常细心.
C#
switch (myStringVar)
{
case "one": doSomething(); break;
case "two": doSomething(); break;
case "three": doSomething(); break;
default: doSomething(); break;
}
Run Code Online (Sandbox Code Playgroud)
最后,它将此代码减少到if ...所以,只是为了可读性更好,而不是性能.
实际上,如果微软认为交换机(在c#中)最好用if替换 - 好的,我将使用(在您描述的具体情况下)交换机.
顺便说一句,在这个例子中,该活动似乎非常清楚地回应了您的问题