# 技术大咖分享：如何评价一段代码？

• 规模
• 执行效率
• 占用空间
• 可读性
• 扩展性

1

## 1.1 初学者追求简单

```for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if (D[i][j] > D[i][k] + D[k][j])
{
D[i][j] = D[i][k] + D[k][j];
P[i][j] = P[i][k];
}
}
}```

## 1.2 大牛们追求省事

```typedef struct _tagNode
{    int m_nID;    int m_nSN;    int m_nMode;    int m_nCode;
}Node;

Node arrNodes[100];```

`int arr[100][4];`

`int arr[100][50][30][5];`

2

## 2.1 以空间换时间

```void main(){    int arr[10] = {2, 5, 15, 18, 7, 10, 13, 11, 9, 0};    int arrSort[20] = { 0 };    for (int i = 0; i < 10; i++)
{
arrSort[arr[i]]++;
}    for (int i = 0; i < 20; i++)
{        if (arrSort[i] > 0)
{            printf("%d ", i);
}
}
}```

## 2.2 不能牺牲可读性

`int a = 10;int b = a / 2;`

`int a = 10;int b = a >>1;`

3

```#define NEED_MAX 800int* p = new int[NEED_MAX];if (p == NULL)
{    return -l;
}delete[] p;```

4

```void Swap(int& a, int& b){
a = a ^ b;
b = a ^ b;
a = a ^ b;
}```

```void Swap(int& a, int& b){    int t = a;
a = b;
b = t;
}```

` g_nScore = student.GetScore() >= p->m_pNext->m_nScore ? student.GetScore() : p->m_pNext->m_nScore;`

```if (student.GetScore() >= p->m_pNext->m_nScore)
{
g_nScore = student.GetScore();
}else{
g_nScore = p->m_pNext->m_nScore;
}```

5

6

239 篇文章50 人订阅

0 条评论

## 相关文章

2705

1242

### 解锁 Leetcode 新题：寻找明星

Suppose you are at a party with n people (labeled from 0 to n - 1) and among the...

3856

3477

2220

### 一行Python代码能干嘛？

python有很多优雅有趣的代码写法，同时还很简短，以至于当我刚开始接触这个编程语言的时候，就爱不释手。而前几天的编程语言榜单中python也超越了java成为...

1330

4004

2944

3381

1943