当代码流是这样的:
if(check())
{
...
...
if(check())
{
...
...
if(check())
{
...
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
我一般都看到这个工作,以避免上面的凌乱的代码流:
do {
if(!check()) break;
...
...
if(!check()) break;
...
...
if(!check()) break;
...
...
} while(0);
Run Code Online (Sandbox Code Playgroud)
有哪些更好的方法可以避免这种解决方案/黑客攻击,从而使其成为更高级别(行业级别)的代码?
任何开箱即用的建议都是受欢迎的!
我一直在使用ReSharper一段时间,有时它暗示我反转了if
.我想一个例子可以更好地解释我的情况:
public void myfunction(int exampleParam){
if(exampleParam > 0){
//Do something with form controls for example.
}
}
Run Code Online (Sandbox Code Playgroud)
现在ReSharper建议我将其反转if
为:
public void myfunction(int exampleParam){
if(exampleParam <= 0)
return;
//Do something with form controls for example
}
Run Code Online (Sandbox Code Playgroud)
这会以某种方式"改善"性能吗?还是仅仅是审美?
我们的组织有一个必要的编码规则(没有任何解释):
if ... else如果构造应该用else子句终止
例1:
if ( x < 0 )
{
x = 0;
} /* else not needed */
Run Code Online (Sandbox Code Playgroud)
例2:
if ( x < 0 )
{
x = 0;
}
else if ( y < 0 )
{
x = 3;
}
else /* this else clause is required, even if the */
{ /* programmer expects this will never be reached */
/* no change in value of x */
}
Run Code Online (Sandbox Code Playgroud)
这个设计要处理的边缘情况是什么?
关于其原因的另一个问题是,示例1 …
想象一下以下代码:
void DoThis()
{
if (!isValid) return;
DoThat();
}
void DoThat() {
Console.WriteLine("DoThat()");
}
Run Code Online (Sandbox Code Playgroud)
在void方法中使用return是否可以?它有任何性能损失吗?或者写一个这样的代码会更好:
void DoThis()
{
if (isValid)
{
DoThat();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个需要一些非常复杂的JavaScript处理的项目.这包括许多嵌套if
- else
在很多地方.通过阅读Stack Overflow上的其他技巧,我一般都会尽可能地优化JavaScript代码,但我想知道以下两个结构是否会对速度方面产生任何影响:
if(some_condition) {
// process
return ;
}
// Continue the else condition here
Run Code Online (Sandbox Code Playgroud)
VS
if(some_condition) {
// Process
}
else {
// The 'else' condition...
}
Run Code Online (Sandbox Code Playgroud) 我和一位同事对以下哪一项更优雅存在争议.我不会说谁是谁,所以它是公正的.哪个更优雅?
public function set hitZone(target:DisplayObject):void
{
if(_hitZone != target)
{
_hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);
_hitZone = target;
_hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);
}
}
Run Code Online (Sandbox Code Playgroud)
...要么...
public function set hitZone(target:DisplayObject):void
{
if(_hitZone == target)return;
_hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);
_hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);
_hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);
_hitZone = target;
_hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);
_hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);
_hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码示例:
private void AddEnvelope(MailMessage mail)
{
if (this.CopyEnvelope)
{
// Perform a few operations
}
}
Run Code Online (Sandbox Code Playgroud)
VS
private void AddEnvelope(MailMessage mail)
{
if (!this.CopyEnvelope) return;
// Perform a few operations
}
Run Code Online (Sandbox Code Playgroud)
底部代码会执行得更快吗?为什么ReSharper会提出此建议?
更新
考虑过这个问题后,答案对某些人来说似乎是显而易见的.但是我们很多开发人员从来没有习惯于在一开始就嵌套if
语句.
我有这个代码,我们可以用两种方式编写
第一道路
void func(int val)
{
if(val == 0)
return;
// Do something ...
}
Run Code Online (Sandbox Code Playgroud)
第二种方式
void func(int val)
{
if(val != 0)
{
// Do something ...
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
有没有理由使用第一种方式?是否有任何优势使用第一种方式(在C++或C#中)
是否有可能在Sass mixin的早期爆发/返回?我想做这样的事情:
@mixin foo($bar: false) {
@if $bar {
// return early without applying any of the styles below
}
color: red;
}
Run Code Online (Sandbox Code Playgroud)
编辑:请记住,这个例子是我能想到的最简单的事情,它说明了我的问题.在现实世界中,我的代码要复杂得多,而且用例很清楚.
if
PHP中的简单语句有什么好的替代方法吗?我知道switch
,但我猜想有一些更精致的替代方案,在处理真正重大的if
陈述时会派上用场.
非常感谢,
我写了一个运动检测winform c#desktop app.
运动帧作为单独的jpeg保存到我的硬盘驱动器中.
我记录了4台摄像机.这由变量表示:
camIndex
Run Code Online (Sandbox Code Playgroud)
每个jpeg都在一个文件结构下:
c:\ Year\The Month\The Day\The Hour\The Minute
确保目录中的每个文件都没有太多文件.
我的目的是让我的应用程序全天候运行.应用程序可能会因系统重启或用户选择暂时关闭它等原因而停止.
目前我有一个计时器而不是每5分钟运行一次,以删除超过24小时的文件.
我发现我的以下代码是内存密集型的,并且在几天的时间内,explorer.exe已经爬进RAM内存.
我的动作应用程序需要一直处于打开状态,因此低内存占用对于这种"归档"至关重要......
以下代码很长,在我看来非常低效.有没有更好的方法来实现我的目标?
我用这个代码:
List<string> catDirs = Directory.EnumerateDirectories(Shared.MOTION_DIRECTORY, "*", SearchOption.TopDirectoryOnly).ToList();
for (int index = 0; index < catDirs.Count; index++)
{
for (int camIndex = 0; camIndex < 4; camIndex++)
{
if (Directory.Exists(catDirs[index] + "\\Catalogues\\" + camIndex.ToString()))
{
List<string> years = GetDirectoryList(catDirs[index] + "\\Catalogues\\" + camIndex.ToString(), true);
if (years.Count == 0)
{
Directory.Delete(catDirs[index]);
}
for (int yearIndex = 0; yearIndex < years.Count; yearIndex++)
{
DirectoryInfo …
Run Code Online (Sandbox Code Playgroud)