不必要的安全代码 [英] Needlessly Safe Code

查看:80
本文介绍了不必要的安全代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常感谢您对以下两种替代方法的评论

给定的方法。这不是一个真正的方法,因为我并不关心真正的工作。它可能正在做,但我想把注意力集中在

这两个

方法中实现的[广泛检查]与[零检查]。


对运行时条件做出一些假设的安全代码很好,

但是不必要的安全呢?


版本1是否不必要地安全。在您看来?


---------------------------------- --------------

版本1 - 广泛的运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

if(Directory.Exists(Path.GetDirectoryName(pathToTheF) ile)))

{

if(File.Exists(pathToTheFile))

{

// Do这里有文件的东西......打开它,不管......

}

其他

{

抛出新的System.IO.FileNotFoundException(文件不是

找到。;

}

}

else

{

抛出新的System.IO.DirectoryNotFoundException(&目录不是

found。);

}

}


-------------------- ----------------------------

版本2 - 否运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

//在这里尝试使用该文件 - 正确的关闭......打开

它,无论如何......

//如果无法找到它,那么system.IO将抛出异常

// ...因此我不需要对文件/目录进行所有检查

存在

}


请注意,我的问题不是处理文件。上面的代码

仅用于提供安全编码的示例。 vs代码使得

假设。


提前致谢!

I would appreciate your comments on the following two alternatives of a
given method. This isn''t a real method, as I''m not concerned about the "real
work" it could be doing, but would like to instead focus our attention on
the [extensive checking] vs [zero checking] implemented in these two
methods.

Safe code that makes few assumptions about its runtime conditions is good,
but what about "needlessly safe"?

Is Version 1 below "needlessly safe" in your opinion?

------------------------------------------------
Version 1 - Extensive "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
if (Directory.Exists(Path.GetDirectoryName(pathToTheF ile)))
{
if (File.Exists(pathToTheFile))
{
// Do something with the file here... open it, whatever...
}
else
{
throw new System.IO.FileNotFoundException("The file was not
found.");
}
}
else
{
throw new System.IO.DirectoryNotFoundException("The directory was not
found.");
}
}

------------------------------------------------
Version 2 - No "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
// Just try to something with the file here - right "off the bat"... open
it, whatever...
// If it cannot be located, then system.IO will throw an exception
// ... therefore I don''t need to do all the checking for file/directory
existance
}

Please note that my question isn''t about working with files. The code above
just serves to provide an example of "safe coding" vs code that makes
assumptions.

Thanks in advance!

推荐答案

4月25日下午2:52,Verde < A ... @ B.comwrote:
On Apr 25, 2:52 pm, "Verde" <A...@B.comwrote:

我将非常感谢您对

给定方法的以下两种选择的评论。这不是一个真正的方法,因为我并不关心真正的工作。它可能正在做,但我想把注意力集中在

这两个

方法中实现的[广泛检查]与[零检查]。


对运行时条件做出一些假设的安全代码很好,

但是不必要的安全呢?


版本1是否不必要地安全。在您看来?


---------------------------------- --------------

版本1 - 广泛的运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

if(Directory.Exists(Path.GetDirectoryName(pathToTheF) ile)))

{

if(File.Exists(pathToTheFile))

{

// Do这里有文件的东西......打开它,不管......

}

其他

{

抛出新的System.IO.FileNotFoundException(文件不是

找到。;

}

}

else

{

抛出新的System.IO.DirectoryNotFoundException(&目录不是

found。);

}


}


---------------- --------------------------------

版本2 - 否运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

//在这里尝试使用该文件 - 正确的关闭......打开

它,无论如何......

//如果无法找到它,那么system.IO将抛出异常

// ...因此我不需要对文件/目录进行所有检查

存在


}


请注意我的问题不是处理文件。上面的代码

仅用于提供安全编码的示例。 vs代码使得

假设。


提前致谢!
I would appreciate your comments on the following two alternatives of a
given method. This isn''t a real method, as I''m not concerned about the "real
work" it could be doing, but would like to instead focus our attention on
the [extensive checking] vs [zero checking] implemented in these two
methods.

Safe code that makes few assumptions about its runtime conditions is good,
but what about "needlessly safe"?

Is Version 1 below "needlessly safe" in your opinion?

------------------------------------------------
Version 1 - Extensive "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
if (Directory.Exists(Path.GetDirectoryName(pathToTheF ile)))
{
if (File.Exists(pathToTheFile))
{
// Do something with the file here... open it, whatever...
}
else
{
throw new System.IO.FileNotFoundException("The file was not
found.");
}
}
else
{
throw new System.IO.DirectoryNotFoundException("The directory was not
found.");
}

}

------------------------------------------------
Version 2 - No "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
// Just try to something with the file here - right "off the bat"... open
it, whatever...
// If it cannot be located, then system.IO will throw an exception
// ... therefore I don''t need to do all the checking for file/directory
existance

}

Please note that my question isn''t about working with files. The code above
just serves to provide an example of "safe coding" vs code that makes
assumptions.

Thanks in advance!



看看你的例子,我不认为版本#1比

版本#2更安全。他们都在做同样的事情;版本#1具有

冗余代码,并且不会增加任何额外的值而不是版本#2。

从广义上讲,问题是是否要主动或

被动。我认为它因情况而异。对于例如如果我必须将
转换为1000个字符串在一个循环中加倍,如果1000个字符串是来自随机文件的
,我宁愿使用Double.TryParse()来检查如果

它是一个有效的双,然后解析它。如果它类似于上面例子中的

,我会抓住异常。

Looking at your example, I dont think Version#1 is any more safer than
Version#2. They are both doing the same thing; Version #1 has
redundant code and does not add any additional value than Version #2.
In a broader sense the question is whether to be proactive or
reactive. I think it varies from case to case. For e.g. if i have to
convert 1000 strings to double in a loop and if the 1000 strings are
from a random file, I would prefer using Double.TryParse() to check if
it is a valid double and then parse it. If it were something like the
above example, I would catch the exception.


Verde写道:
Verde wrote:

我将非常感谢您对

给定方法的以下两种选择的评论。这不是一个真正的方法,因为我并不关心真正的工作。它可能正在做,但我想把注意力集中在

这两个

方法中实现的[广泛检查]与[零检查]。

[...]
I would appreciate your comments on the following two alternatives of a
given method. This isn''t a real method, as I''m not concerned about the "real
work" it could be doing, but would like to instead focus our attention on
the [extensive checking] vs [zero checking] implemented in these two
methods.
[...]



Verde,如果我从字面上看你的例子没有任何区别

(除了字符串中的确切文本)。但是如果其他分支

包含除了抛出之外的任何东西,那将是完全不同的

问题。


请注意版本#1使用与#2相同的策略

,例如,文件的权限不足。或者在Exists检查和

打开之间删除文件的不太可能的

场景。


所以出于这些原因我更喜欢版本#1,也许可以放入

评论,例如FileStream类错误检查


-HH-


Verde, if I take your example literally there is no difference at all
(except maybe the exact text in the strings). But if the else branches
contained anything else than a throw it would be an entirely different
matter.

Please note that version #1 uses the same strategy as #2 when it comes
to, for example, insufficient rights for the file. Or the unlikely
scenario in which the file is deleted between the Exists check and the
Open.

So for those reasons I would prefer version #1, and maybe put in a
comment like "error checking by the FileStream class"

-HH-


4月25日下午2:52,Verde < A ... @ B.comwrote:
On Apr 25, 2:52 pm, "Verde" <A...@B.comwrote:

我将非常感谢您对

给定方法的以下两种选择的评论。这不是一个真正的方法,因为我并不关心真正的工作。它可能正在做,但我想把注意力集中在

这两个

方法中实现的[广泛检查]与[零检查]。


对运行时条件做出一些假设的安全代码很好,

但是不必要的安全呢?


版本1是否不必要地安全。在您看来?


---------------------------------- --------------

版本1 - 广泛的运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

if(Directory.Exists(Path.GetDirectoryName(pathToTheF) ile)))

{

if(File.Exists(pathToTheFile))

{

// Do这里有文件的东西......打开它,不管......

}

其他

{

抛出新的System.IO.FileNotFoundException(文件不是

找到。;

}

}

else

{

抛出新的System.IO.DirectoryNotFoundException(&目录不是

found。);

}


}


---------------- --------------------------------

版本2 - 否运行时环境检查

----------------------------------------- -------

public void DoSomethingWithAFile(string pathToTheFile)

{

//在这里尝试使用该文件 - 正确的关闭......打开

它,无论如何......

//如果无法找到它,那么system.IO将抛出异常

// ...因此我不需要对文件/目录进行所有检查

存在


}


请注意我的问题不是处理文件。上面的代码

仅用于提供安全编码的示例。 vs代码使得

假设。


提前致谢!
I would appreciate your comments on the following two alternatives of a
given method. This isn''t a real method, as I''m not concerned about the "real
work" it could be doing, but would like to instead focus our attention on
the [extensive checking] vs [zero checking] implemented in these two
methods.

Safe code that makes few assumptions about its runtime conditions is good,
but what about "needlessly safe"?

Is Version 1 below "needlessly safe" in your opinion?

------------------------------------------------
Version 1 - Extensive "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
if (Directory.Exists(Path.GetDirectoryName(pathToTheF ile)))
{
if (File.Exists(pathToTheFile))
{
// Do something with the file here... open it, whatever...
}
else
{
throw new System.IO.FileNotFoundException("The file was not
found.");
}
}
else
{
throw new System.IO.DirectoryNotFoundException("The directory was not
found.");
}

}

------------------------------------------------
Version 2 - No "runtime environment" Checking
------------------------------------------------
public void DoSomethingWithAFile(string pathToTheFile)
{
// Just try to something with the file here - right "off the bat"... open
it, whatever...
// If it cannot be located, then system.IO will throw an exception
// ... therefore I don''t need to do all the checking for file/directory
existance

}

Please note that my question isn''t about working with files. The code above
just serves to provide an example of "safe coding" vs code that makes
assumptions.

Thanks in advance!



I * THINK *异常导致开销,因此最好进行检查..

I *THINK* exceptions cause overhead so its better to do the checks..


这篇关于不必要的安全代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆