我经常想知道。就“最佳实践”而言,在编写函数时,无论是在本地还是在模块/类中,最好使用:
Public Function WhichIsBetter(ByVal tmpUser As String, _
tmpPassword As String) As Boolean
'Do something
End Function
或
Public Class User
Public Property tmpUser As String
Public Property tmpPassword As String
我想知道,使用至少三个相同类型的参数编写方法的最佳实践是什么。问题是,在使用这种方法时,您可能会混淆参数。例如
method(int userId, int productId, int weight, int price)
我看到的唯一解决方案是使用某些holder类(可能使用构建器模式),并将其作为方法参数传递。
如果函数的参数可以为空,那么构造函数的最佳方式是什么?
例如
function newDate($day, $time = null, $overRide) {
Do something with the variables
}
# newDate('12', '', 'yes');
是否可以简单地按如下方式重新构造函数:
function newDate($day, $overRide, $time = null) {
Do something with the variables
如何使用/维护与另一个R包中的S4类同名的S4类?
我正在编写一个大型的R包,其中包含一个名为'FeatureSet‘的S4类。不幸的是,这也是广泛使用的‘S4’包中的一个虚拟S4类的名称。当我先装入我的包,然后装入oligo,然后调用
new("FeatureSet")
我得到了错误
Error in new("FeatureSet") :
trying to generate an object from a virtual class ("FeatureSet")
在名称空间文件中,我尝试包含'exportClasses
如果一个类在对象创建过程中需要多个字段信息,并且它允许的information.Than更少,我们有两个选择
提供多个构造函数,或者允许客户端在创建对象时传递空参数。
其中,这是最佳实践。例如:
案例1:
public class Test {
Test(A ob1,B ob2, C ob3){
}
Test(A ob1,B ob2){
this(ob1, ob2, null);
}
public static void main(String args[]){
Test ob = new Test(ne
在编写单元测试时,我通常使用pytest.mark.parametrize装饰器。我突然意识到,当测试引发异常的函数时,我可以执行如下操作:
bar.py
def foo(n: int, threshold: int = 1) -> int:
if n >= threshold:
return n
else:
raise ValueError(f'n: {n} < {threshold} (threshold)')
test_bar.py
import pytest
from bar import foo
te
我正在开发一个包,它依赖于zoo (在Imports中列出,而不是在DESCRIPTION中列出Depends )。
在一个函数中,我需要用[操作符对二维zoo对象进行子集。但是,只要未加载zoo包,R就会使用基本[,它将返回numeric而不是zoo。
标准的解决方案是使用zoo名称空间(如zoo::`[.zoo`())。但是,当我尝试在函数R中执行此操作时,会抛出错误Error: '[.zoo' is not an exported object from 'namespace:zoo',因此我得出结论,该操作符没有在zoo名称空间中导出(即使我可以在中看到
使用空实例是一种好的做法还是坏的做法?
有"product“类,以id开头:
class product
{
__construct($id = 0)
{
// populate class, there is no product with id = 0
}
function new($name, $data)
...
}
$product = new Product(123); // normal using
但我还需要创建新产品,所以没有初始对象,我创建了一个空对象:
$product = new Pr
我认为下面的第一个调用效率更高,因为它只检查一次@myInputParameter是否小于5000。
如果检查失败,我将完全避免查询。然而,我见过其他人编写类似第二个示例的代码,他们说它即使不是更高效,也同样有效。
谁能告诉我哪一个更快?第二种方法似乎要慢得多,特别是当调用是在梳理大型数据集的时候。
第一次呼叫:
IF (@myInputParameter < 5000)
BEGIN
SELECT
@myCount = COUNT(1)
FROM myTable
WHERE someColumn=@so