I'm currently practicing with a simple program to understand the equations involved in deriving various metrics from Halstead's software science. I do believe I'm doing it correctly, but I feel like I haven't registered all operands and operators so that I can start with the mathematics.
我目前正在用一个简单的程序来练习,以理解从Halstead的软件科学中推导出各种度量的公式。我相信我做的是对的,但是我觉得我还没有注册所有的操作数和运算符所以我可以从数学开始。
The program I'm using is:
我使用的程序是:
/*01*/ // counts how many items in sArray[] are also in tArray[]
/*02*/ int matched(int sArray[], int tArray[], int sMax, int tMax)
/*03*/ {
/*04*/ int count, i, first, middle, last;
/*05*/
/*06*/ for (i = 0; i < sMax; ++i)
/*07*/ {
/*08*/ last = tMax - 1;
/*09*/ for (int first = 0; first <= last;)
/*10*/ {
/*11*/ middle = (first + last) / 2;
/*12*/ if (tArray[middle] == sArray[i])
/*13*/ {
/*14*/ count++;
/*15*/ break;
/*16*/ }
/*17*/ if (tArray[middle] < sArray[i])
/*18*/ {
/*19*/ first = middle + 1;
/*20*/ }
/*21*/ else
/*22*/ {
/*23*/ last = middle - 1;
/*24*/ }
/*25*/ }
/*26*/ }
/*27*/ return count;
/*28*/ }
And I've come out with
我想出来了
- n1 = the number of distinct operators = 10
- n1 =不同运算符的个数= 10
- n2 = the number of distinct operands = 9
- n2 =不同的操作数= 9。
- N1 = the total number of operators = 24
- N1 =操作符的总数= 24
- N2 = the total number of operands = 34
- N2 =操作数的总数= 34
These notes show the distinct operators and operands found:
这些说明显示了不同的操作符和操作数:
Operators
= Assignment (line 6, 8, 9, 11, 19, 23) = 6
< Less Than (line 6, 17) = 2
++ Increment (line 6, 14) = 2
- Subtract (line 8, 23) = 2
<= Less Than or Equal to (line 9) = 1
+ Addition (line 11, 19) = 2
/ Division (line 11) = 1
== Equal to (line 12) = 1
[] index (line 2*2, 12*2, 17*2 = 6
break (line 15) = 1运营商=赋值(6号线、8、9、11、19、23)= 6 <不到(第6行,17)= 2="6(15行)=" + +增量(第6行,14)="2" -减去(第8行,23)="2" <="小于或等于(第9行)=" 1 +添加(第11行,19)="2" 部门(第11行)="1" =="=(第12行)=" 1[]指数(2 * 2行,12 2,17 1< p>
Operands
count (line 4, 14) = 2
i (line 4, 6*3, 12, 17) = 6
first (line 4, 9*2, 11, 19) = 5
middle (line 4, 11, 12, 17, 19, 23) = 6
last (line 4, 8, 9, 11, 23) = 5
sArray (line 2, 12, 17) = 3
tArray (line 2, 12, 17) = 3
sMax (line 2, 6) = 2
tMax (line 2, 8) = 2操作数计数(第4行,14)= 2 i(4号线,6 * 3、12、17)= 6第一(9 * 2,11日,4号线19)中间= 5(4号线、11、12、17、19、23)= 6最后(4号线,8,9,11日23)= 5 sArray(2号线、12、17)= 3 tArray(2号线、12、17)= 3 sMax(第2行,6)= 2最高温度(2号线,8)= 2
Is there anything vital I've missed out? From my understanding:
有什么重要的事我错过了吗?从我的理解:
- Operands are values
- 操作数是值
- Operators manipulate and check operands
- 操作符操作并检查操作数
2 个解决方案
#1
2
The point of Halstead's metrics is to answer a lot of questions like "How difficult is the code to read", "How much effort was put into writing the code", etc. The formula for Halstead's Difficulty metric should provide a hint on how the first question answered:
Halstead的度量标准的要点是回答很多问题,比如“代码读起来有多难”、“花了多少精力去写代码”等等。
Difficulty = (Unique Operators / 2) * (Operands / Unique Operands);
You can see that having more unique operators, obviously, makes the code harder to read.
您可以看到,拥有更独特的操作符,显然会使代码更难读。
On braces: A lot of sources on the subject consider {}
to be operators, which I don't see the point of. Curly braces act as a structure (punctuation) element and in a lot of ways makes code easier to understand, not harder. (Take, for example, conditional block with and without braces)
在大括号中:主体上的许多源都认为{}是操作符,我不认为这一点。花括号作为结构(标点)元素,在许多方面使代码更容易理解,而不是更难理解。(以带括号和不带括号的条件块为例)
Counting the function name matched
is relevant only in a more general context, but not when you measure the metrics of the function implementation (given there is no recursion).
计算函数名匹配只在更一般的情况下是相关的,但是当您度量函数实现的度量时(给定的是没有递归)时就不需要计算了。
On operators: counting operators can be tricky. For example, []
appearing in function declaration and []
on lines 12 and 17, are actually different things. The first one is array declaration, the second is operator[]
- accessing element by index. The same with postfix and prefix ++
, having them both in the program makes it harder to read.
操作人员:计算操作人员可能会很棘手。例如,在函数声明中出现的[]和在第12行和第17行中出现的[]实际上是不同的东西。第一个是数组声明,第二个是操作符[]-按索引访问元素。后缀和前缀++也一样,在程序中同时使用它们会使读取变得更加困难。
The same logic applies to language keywords: for
, if
, else
, break
, return
. The more of them in the code the harder it is to read.
同样的逻辑也适用于语言关键字:for, if, else, break, return。代码中它们的数量越多,就越难读。
On types: type names in variable declaration is also tricky. Some attribute them to operators, some to operands. But if we look again at the Difficulty formula, we would see that type names would better go to operators, in the sense that having more different types in the code make it harder to read, not easier.
关于类型:变量声明中的类型名称也很棘手。有些人将其归为运算符,有些则归为操作数。但是如果我们再看一下难度公式,我们会发现类型名最好是操作符,因为代码中有更多不同的类型会使读取变得更困难,而不是更容易。
Your counts for operands seems to be alright.
操作数的计数似乎没问题。
Operators
= Assignment (line 6, 8, 9, 11, 19, 23) = 6
< Less Than (line 6, 17) = 2
++ Prefix Increment (line 6) = 1
++ Postfix Increment (line 14) = 1
- Subtract (line 8, 23) = 2
<= Less Than or Equal to (line 9) = 1
+ Addition (line 11, 19) = 2
/ Division (line 11) = 1
== Equal to (line 12) = 1
[] declaration (line 2) = 2
[] index (line 12, 17) = 4
for (line 6, 9) = 2
if (line 12, 17) = 2
else (line 21) = 1
break (line 15) = 1
return (line 27) = 1
int declaration = 7
Operands
count (line 4, 14) = 2
i (line 4, 6*3, 12, 17) = 6
first (line 4, 9*2, 11, 19) = 5
middle (line 4, 11, 12, 17, 19, 23) = 6
last (line 4, 8, 9, 11, 23) = 5
sArray (line 2, 12, 17) = 3
tArray (line 2, 12, 17) = 3
sMax (line 2, 6) = 2
tMax (line 2, 8) = 2
Metrics
n1 = 17
n2 = 9
N1 = 37
N2 = 34
Difficulty = (n1 * N2) / (2 * n2) = 32.1
I was referring to Wiki and this page on Virtual Machinery.
我指的是维基和这个关于虚拟机器的页面。
By the way, most things said are my opinion, and may not coincide with more official sources.
顺便说一下,大多数事情都是我的意见,可能与更多的官方消息不一致。
By the way: 2, here is exact and strict definition on what should be counted as operators and operands in a C++ code: http://www.verifysoft.com/en_halstead_metrics.html.
顺便说一句:2,这里有一个关于在c++代码中应该作为操作符和操作数的精确而严格的定义:http://www.verifysoft.com/en_halstead_metrics.html。
#2
0
firstly, initialize count to 0 and next operators are not values they are variables.
首先,将计数初始化为0,然后操作符不是值,它们是变量。
operators
matched -1
() -6
[] -6
{} -6
int -7
for -2
if -2
else -1
return -1
= -6
< -2
<= -1
++ -2
- -2
+ -2
/ -1
== -1
break -1
operands
2 -line no. 11 -1
1 (8,19,23) -3
0 -1
count -3
i -6
first -5
middle -6
last -5
sArray -3
tArray -3
sMax -2
tMax -2
N1=50
N2=40
n1=18
n2=12
The book I am referring to is Software Metrics and Software Metrology By Alain Abran. You can download it from here -> http://profs.etsmtl.ca/aabran/English/Accueil/ChapersBook/Abran%20-%20Chapter%20005.pdf
我指的书是Alain Abran写的软件计量和软件计量。你可以从这里下载——> http://profs.etsmtl.ca/aabran/english/accueil/chapersbook/abran%20chapter%20005.pdf
I hope it will solve all your doubts.
我希望它能解决你所有的疑惑。
And function names,braces,type names,all other key words and all other well known operators come under operator section
函数名、括号、类型名称、所有其他关键字和所有其他已知操作符都属于操作符部分。
Variables and constant values that are input to any functions or operators are operands.
输入到任何函数或操作符的变量和常量值都是操作数。
Hence, I come up with this answer.
因此,我想到了这个答案。
#1
2
The point of Halstead's metrics is to answer a lot of questions like "How difficult is the code to read", "How much effort was put into writing the code", etc. The formula for Halstead's Difficulty metric should provide a hint on how the first question answered:
Halstead的度量标准的要点是回答很多问题,比如“代码读起来有多难”、“花了多少精力去写代码”等等。
Difficulty = (Unique Operators / 2) * (Operands / Unique Operands);
You can see that having more unique operators, obviously, makes the code harder to read.
您可以看到,拥有更独特的操作符,显然会使代码更难读。
On braces: A lot of sources on the subject consider {}
to be operators, which I don't see the point of. Curly braces act as a structure (punctuation) element and in a lot of ways makes code easier to understand, not harder. (Take, for example, conditional block with and without braces)
在大括号中:主体上的许多源都认为{}是操作符,我不认为这一点。花括号作为结构(标点)元素,在许多方面使代码更容易理解,而不是更难理解。(以带括号和不带括号的条件块为例)
Counting the function name matched
is relevant only in a more general context, but not when you measure the metrics of the function implementation (given there is no recursion).
计算函数名匹配只在更一般的情况下是相关的,但是当您度量函数实现的度量时(给定的是没有递归)时就不需要计算了。
On operators: counting operators can be tricky. For example, []
appearing in function declaration and []
on lines 12 and 17, are actually different things. The first one is array declaration, the second is operator[]
- accessing element by index. The same with postfix and prefix ++
, having them both in the program makes it harder to read.
操作人员:计算操作人员可能会很棘手。例如,在函数声明中出现的[]和在第12行和第17行中出现的[]实际上是不同的东西。第一个是数组声明,第二个是操作符[]-按索引访问元素。后缀和前缀++也一样,在程序中同时使用它们会使读取变得更加困难。
The same logic applies to language keywords: for
, if
, else
, break
, return
. The more of them in the code the harder it is to read.
同样的逻辑也适用于语言关键字:for, if, else, break, return。代码中它们的数量越多,就越难读。
On types: type names in variable declaration is also tricky. Some attribute them to operators, some to operands. But if we look again at the Difficulty formula, we would see that type names would better go to operators, in the sense that having more different types in the code make it harder to read, not easier.
关于类型:变量声明中的类型名称也很棘手。有些人将其归为运算符,有些则归为操作数。但是如果我们再看一下难度公式,我们会发现类型名最好是操作符,因为代码中有更多不同的类型会使读取变得更困难,而不是更容易。
Your counts for operands seems to be alright.
操作数的计数似乎没问题。
Operators
= Assignment (line 6, 8, 9, 11, 19, 23) = 6
< Less Than (line 6, 17) = 2
++ Prefix Increment (line 6) = 1
++ Postfix Increment (line 14) = 1
- Subtract (line 8, 23) = 2
<= Less Than or Equal to (line 9) = 1
+ Addition (line 11, 19) = 2
/ Division (line 11) = 1
== Equal to (line 12) = 1
[] declaration (line 2) = 2
[] index (line 12, 17) = 4
for (line 6, 9) = 2
if (line 12, 17) = 2
else (line 21) = 1
break (line 15) = 1
return (line 27) = 1
int declaration = 7
Operands
count (line 4, 14) = 2
i (line 4, 6*3, 12, 17) = 6
first (line 4, 9*2, 11, 19) = 5
middle (line 4, 11, 12, 17, 19, 23) = 6
last (line 4, 8, 9, 11, 23) = 5
sArray (line 2, 12, 17) = 3
tArray (line 2, 12, 17) = 3
sMax (line 2, 6) = 2
tMax (line 2, 8) = 2
Metrics
n1 = 17
n2 = 9
N1 = 37
N2 = 34
Difficulty = (n1 * N2) / (2 * n2) = 32.1
I was referring to Wiki and this page on Virtual Machinery.
我指的是维基和这个关于虚拟机器的页面。
By the way, most things said are my opinion, and may not coincide with more official sources.
顺便说一下,大多数事情都是我的意见,可能与更多的官方消息不一致。
By the way: 2, here is exact and strict definition on what should be counted as operators and operands in a C++ code: http://www.verifysoft.com/en_halstead_metrics.html.
顺便说一句:2,这里有一个关于在c++代码中应该作为操作符和操作数的精确而严格的定义:http://www.verifysoft.com/en_halstead_metrics.html。
#2
0
firstly, initialize count to 0 and next operators are not values they are variables.
首先,将计数初始化为0,然后操作符不是值,它们是变量。
operators
matched -1
() -6
[] -6
{} -6
int -7
for -2
if -2
else -1
return -1
= -6
< -2
<= -1
++ -2
- -2
+ -2
/ -1
== -1
break -1
operands
2 -line no. 11 -1
1 (8,19,23) -3
0 -1
count -3
i -6
first -5
middle -6
last -5
sArray -3
tArray -3
sMax -2
tMax -2
N1=50
N2=40
n1=18
n2=12
The book I am referring to is Software Metrics and Software Metrology By Alain Abran. You can download it from here -> http://profs.etsmtl.ca/aabran/English/Accueil/ChapersBook/Abran%20-%20Chapter%20005.pdf
我指的书是Alain Abran写的软件计量和软件计量。你可以从这里下载——> http://profs.etsmtl.ca/aabran/english/accueil/chapersbook/abran%20chapter%20005.pdf
I hope it will solve all your doubts.
我希望它能解决你所有的疑惑。
And function names,braces,type names,all other key words and all other well known operators come under operator section
函数名、括号、类型名称、所有其他关键字和所有其他已知操作符都属于操作符部分。
Variables and constant values that are input to any functions or operators are operands.
输入到任何函数或操作符的变量和常量值都是操作数。
Hence, I come up with this answer.
因此,我想到了这个答案。