如果您将模式定义为一种巧妙的技巧,可以帮助您以优雅且可能更具可读性的方式解决编程问题[1]。使用该continue语句的设计模式是什么(如果要避免深层嵌套的if语句)?
for item in items:
if is_for_sale(item):
cost=compute_cost(item)
if cost<=wallet.money:
buy(item)
for item in items:
if not is_for_sale(item):
continue
cost = compute_cost(item)
if cost > wallet.money:
continue
buy(item)
Run Code Online (Sandbox Code Playgroud) 我只在课堂上看到过它的实现。我有文本阴影,为Y-coordinate每个声明添加 1px。我想重构它,使其增加,而其他值保持不变。这是代码:
text-shadow: 0px 1px #00B760, 0px 2px #00B760, 0px 3px #00B760, 0px 4px #00B760, 0px 5px #00B760, 0px 6px #00B760, 0px 7px #00B760, 0px 8px #00B760, 0px 9px #00B760, 0px 10px #00B760, 0px 11px #00B760, 0px 12px #00B760, 0px 13px #00B760, 0px 14px #00B760, 0px 15px #00B760, 0px 16px #00B760, 0px 17px #00B760, 0px 18px #00B760, 0px 19px #00B760, 0px 20px #00B760, 0px 21px #00B760, 0px 22px #00B760, 0px 23px #00B760, 0px 24px #00B760, 0px 25px #00B760, …Run Code Online (Sandbox Code Playgroud) 我的代码有以下情况.
class A{
public void putLogicHere(){
//method is about 500 lines long.
//Initial Few lines of code will be common
//middle part of code will be customized by its subclasses
//Again here comes few lines of common code
}
}
class B extends A{}
class C extends A{}
Run Code Online (Sandbox Code Playgroud)
在设计层面,我应该采取什么方法来进一步打破这个代码?考虑到方法的中间部分将由层次结构中的其他人自定义.
我想重构以下代码:
public Credito(String numero, String titular, LocalDate fechacaducidad, double credito, int marcainternacional,
String nombreentidad, int ccv) {
mNumero = numero;
mTitular = titular;
mFechaDeCaducidad = fechacaducidad;
mCredito = credito;
mMovimientos = new Vector<Movimiento>();
mMarcaInternacional = marcainternacional;
setmNombreEntidad(nombreentidad);
setmCCV(ccv);
}
public Credito(String numero, String titular, LocalDate fechacaducidad, int tipo, int marcainternacional,
String nombreentidad, int ccv) {
mNumero = numero;
mTitular = titular;
mFechaDeCaducidad = fechacaducidad;
mTipo = tipo;
mCredito = calcularCredito(mTipo);
mMovimientos = new Vector<Movimiento>();
mMarcaInternacional = marcainternacional;
setmNombreEntidad(nombreentidad);
setmCCV(ccv);
} …Run Code Online (Sandbox Code Playgroud) 有一个方法如下,我想简化在使用它时传递参数:
public virtual Segment AddDataElement(string[] dataElement)
{
dataElementList.Add(dataElement);
return this;
}
Run Code Online (Sandbox Code Playgroud)
该方法被多次调用如下:
message.AddSegment("NAD")
.AddDataElement(new string[] { partyFunctionCodeQualifier })
.AddDataElement(new string[] { partyIdentifier, "", "92" });
Run Code Online (Sandbox Code Playgroud)
是否可以修改方法定义,以便可以编写更易读的调用lile?
message.AddSegment("NAD")
.AddDataElement(partyFunctionCodeQualifier)
.AddDataElement(partyIdentifier, "", "92");
Run Code Online (Sandbox Code Playgroud)
后者可以在内部转换为早期的形式吗?
使用babel进行合并时出现ESLint错误:
第28行:预期分配或函数调用,而是看到一个表达式no-unused-expressions
第29行:预期分配或函数调用,而是看到一个表达式no-unused-expressions
任何想法如何摆脱这些同时使我的计时器仍按预期工作?还是您有更好的方法让我做计时器?
class RequestTimer extends Component {
constructor(props) {
super(props);
this.state = {
seconds: 0,
minutes: 0,
hours: 0
}
this.getTime = this.getTime.bind(this);
}
getTime() {
let second = this.state.seconds
let minute = this.state.minutes;
let hour = this.state.hours;
this.state.seconds % 59 === 0 && this.state.seconds !== 0 ? minute += 1:null;
this.state.minutes % 59 === 0 && this.state.seconds % 59 === 0 && this.state.minutes !== 0 ? (hour += 1, minute = 0):null;
this.setState({
seconds: second +=1,
minutes: …Run Code Online (Sandbox Code Playgroud) 我偶然发现两个方法重载完全相同的身体,我想重构它只是一个.
private static bool Compare<T>(T obj1, T obj2, out int test) where T : IComparable<T>
{}
private static bool Compare(IComparable obj1, IComparable obj2, out int test)
{}
Run Code Online (Sandbox Code Playgroud)
签名表示几乎相同的东西,唯一的区别是泛型.有没有办法合并它们?
我有两种方法。它们非常相似。我尝试使用泛型,但不适用于TryParse()
public static int EnterIntengerNumber()
{
while (true)
{
Console.Write("Enter an intenger number: ");
if (int.TryParse(Console.ReadLine(), out int number))
{
return number;
}
else
{
ConsoleError("Incorrect value");
}
}
}
public static double EnterRealNumber()
{
while (true)
{
Console.Write("Enter a number: ");
if (double.TryParse(Console.ReadLine(), out double number))
{
return number;
}
else
{
ConsoleError("Incorrect value");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何合并或重构它们?
我重构旧代码,但不知道对这部分代码该怎么做。我需要此代码是有史以来最快的代码中最快的代码。:D不,我只是想知道这种情况下是否有更好的写作风格。
我有主班。在主类中,我有7个关系(a,b,c,d,e,f,g)的关联类。在这些类中,除了以下两个属性外,还包括:(datetime1,datetime2)。我的目标是检查这些关系中的任何一种属性是否已更改,以及是否更改了更改其他(second_main)类中的当前属性的运行方法。但是,在这些(main和second_main)类上,此属性关联不能相同。(意味着a.datetime1不能与b.datetime1具有相同的关系,因为b.datetime1可以在不对a.datetime1进行任何更新的情况下进行更改。但是,当a.datetime1进行更改时,它必须更改b.datetime1。
也许我写的有点太复杂了。所以请耐心等待。
这个版本可读性不高,我认为可以写得更好。我尝试了lambda表达式,但是列表初始化可能需要很长时间,并且
(!this.a.IsNull("datetime1") && this.a.IsChanged("datetime1")) ||
(!this.b.IsNull("datetime1") && this.b.IsChanged("datetime1")) ||
(!this.c.IsNull("datetime1") && this.c.IsChanged("datetime1")) ||
(!this.d.IsNull("datetime1") && this.d.IsChanged("datetime1")) ||
(!this.e.IsNull("datetime1") && this.e.IsChanged("datetime1")) ||
(!this.f.IsNull("datetime1") && this.f.IsChanged("datetime1")) ||
(!this.g.IsNull("datetime1") && this.g.IsChanged("datetime1")) ||
(!this.a.IsNull("datetime2") && this.a.IsChanged("datetime2")) ||
(!this.b.IsNull("datetime2") && this.b.IsChanged("datetime2")) ||
(!this.c.IsNull("datetime2") && this.c.IsChanged("datetime2")) ||
(!this.d.IsNull("datetime2") && this.d.IsChanged("datetime2")) ||
(!this.e.IsNull("datetime2") && this.e.IsChanged("datetime2")) ||
(!this.f.IsNull("datetime2") && this.f.IsChanged("datetime2")) ||
(!this.g.IsNull("datetime2") && this.g.IsChanged("datetime2"))
{
DejAtributDoCasti("a.datetime1",
"b.datetime1",
"c.datetime1",
"d.datetime1",
"e.datetime1",
"f.datetime1",
"g.datetime1",
"a.datetime2",
"b.datetime2",
"c.datetime2",
"d.datetime2",
"e.datetime2",
"f.datetime2",
"g.datetime2");
}
Run Code Online (Sandbox Code Playgroud) 我有一个大的 switch 语句,它的圈复杂度为 31,它必须重构为至少 25。这是错误:严重性代码描述项目文件行抑制状态抑制状态错误 CA1502 'Worker.StartListening()' has a cyclomatic复杂度为 31。重写或重构方法以将复杂度降低到 25。
谢谢!
这是代码:
public void StartListening()
{
var consumerSettingsSection= this.configurationManager.GetSection<ConsumerSettingsSection>("appZ/consumer");
foreach (var setting in consumerSettingsSection.QueueSettings)
{
var eventType = ConsumedEventType.NotSpecified;
switch (setting.Name)
{
case "A":
eventType = ConsumedEventType.A;
break;
case "B":
eventType = ConsumedEventType.B;
break;
case "C":
eventType = ConsumedEventType.C;
break;
case "D":
eventType = ConsumedEventType.D;
break;
case "E":
eventType = ConsumedEventType.E;
break;
case "F":
eventType = ConsumedEventType.F;
break;
case "G":
eventType = ConsumedEventType.G;
break;
case "H":
eventType = …Run Code Online (Sandbox Code Playgroud) refactoring ×10
c# ×5
.net ×2
java ×2
code-reuse ×1
css ×1
eslint ×1
generics ×1
javascript ×1
loops ×1
python ×1
reactjs ×1
sass ×1