【问题标题】:Naming a function that compute a thing命名计算事物的函数
【发布时间】:2020-01-15 14:04:01
【问题描述】:

考虑一个 Python 模块(但它与其他语言相关),其中包含一系列旨在按顺序使用的函数。 即,函数根据以下方案在语义上链接:

def function_a_to_b(thing_a):
    """Compute the thing_b."""
    thing_b = thing_a**2
    return thing_b

def function_b_to_c(thing_b):
    """Compute the thing_c."""
    thing_c = thing_b**3
    return thing_c

在这个简单的示例中,function_a_to_b 的候选名称可以是 squaringthing_a 可以命名为 square,同样我们可以使用 cubincube。 现在,如果thing_a 是不支持动词的复杂事物,请说weighted_glonk。 我如何命名function_a_to_b 以保持简短、明显,并避免变量冲突或容易出错的命名空间细节? 对于该功能,我倾向于compute_weighted_glonk。另一种选择是匈牙利命名,比如array_weighted_glonk

【问题讨论】:

    标签: naming-conventions naming


    【解决方案1】:

    这取决于你想用那个 glonk 做什么,但你对 compute_wighted_glonk 的判断基本正确。如果你想计算他,那么将这个函数命名为compute_glonk(),如果你想得到他的名字,这个函数可能是get_name_of_glonk()。很多人会有很多建议,有很多方法可以命名你的函数。我的回答够吗?

    【讨论】:

      【解决方案2】:

      如果它是一个不支持动词的复杂事物,那么我建议为它创建一个类并由构造函数创建。

      【讨论】:

        【解决方案3】:

        我的建议是在命名时找到简短和冗长之间的平衡 --Dmitri Pavlutin

        不要害怕使用助词。用简单的英语来说,您不会与“植物生长......”而斗争,而只需使用“植物呈现生长......”。

        回到你的例子,比如立方体/立方,你可以写 glonk/obtainGlonk 或 glonk/calculateGlonk。


        此外,更关注可读性,而不是保持语言标准,这在一天结束时是程序员同事会阅读的内容。你的动词,是什么,一个效果?保持这种状态。

        Your action should describe a change that has happened in your system

        【讨论】:

          【解决方案4】:

          我的 50 美分:我反对引入命名约定来揭示计算结构(罗伯特·马丁:“我们有足够的编码来处理而不会增加我们的负担”[Clean Code, p.23])。如果您的函数要以严格的顺序调用,您应该将它们封装在另一个函数中,这正是这样做的;或者创建某种闩锁,如果合适的话。

          【讨论】:

            【解决方案5】:

            我不知道python,所以我想从javascript角度回答。我无法弄清楚您所说的 glonk 和在编程上下文中 不支持动词化 的意思。 thing_a 最多可以像另一个数组、对象或另一个可调用函数一样复杂——在这些情况下,您可以在function_a_to_b 中进行类型检查。另外,Compute the thing_b 如果涉及多个操作,可以用另一个函数替换。我要做的是:

            //I'm using $ for var names just for semantics.
            //$var is a php usage but I want to emphasise 
            //what things are variables.
            function myComputer($thing){  
            	if(typeof($thing) == ("object")) {
            		//take total no of properties or whatever you want
            		return Object.keys($thing).length;			
            	}
            	
            	else if(typeof($thing) == ("number")) {
            
            		return $thing;
            	}
            	else if(typeof($thing) == ("string")) {
            		return $thing.length;
            	}
            
            	else {
            		return 0;
            	}
            }
            
            function mySquarer($a) { //sementic naming instead of function_a_to_b	
            	var $b = Math.pow(myComputer($a),2); //In your original version whats the point of computing thing_b if you wanna reassign a different value afterwards? 
            	return $b;
            }
            function myCuber($b) { //When you call myCuber you can do myCuber(mySquarer($anything));
            	var $c = Math.pow(myComputer($b),3);
            	return $c;
            }
            
            console.log(mySquarer(3));
            console.log(myCuber(mySquarer(2)));

            【讨论】:

              猜你喜欢
              • 2018-02-17
              • 1970-01-01
              • 2017-03-23
              • 2011-12-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-11-06
              • 1970-01-01
              相关资源
              最近更新 更多