owned this note
owned this note
Published
Linked with GitHub
# 安全程式設計 第$2$組
----
## 組員
110590004 林奕廷
110590034 楊榮鈞
110590049 藍振耘
110590066 吳宥駒
---
## 連結
- [報告簡報(hackmd)](https://hackmd.io/@catJam/SPD)
- 測試程式 Vampire-Survivors-KEKW
- [github](https://github.com/Error0229/Vampire-Survivors-KEKW)
- [sonarcloud report](https://sonarcloud.io/summary/overall?id=Error0229_Vampire-Survivors-KEKW)
- 測試程式 ao_oni
- [github](https://github.com/kyynk/ao_oni)
- [sonarcloud report](https://sonarcloud.io/summary/new_code?id=kyynk_ao_oni)
---
## Bugs
### Identical sub-expressions on both sides of operator ",".
```cpp
void VSObject::update_collide()
{
// apply the collision to _position
// maight integrated into update_pos in the future if we need the real vector implentment
this->_position += _collision;
❌_collision = (0, 0);
}
```
語法錯誤,最相似的用法應為``_collision = {0, 0}``
<br>
### The left operand of '+' is a garbage value
```cpp
void TextDevice::print_all()
{
if (texts.empty())
return;
ptr_CDC = game_framework::CDDraw::GetBackCDC();
ptr_CDC->SetBkMode(TRANSPARENT);
ptr_CDC->SetTextColor(RGB(255, 255, 255));
1️⃣int x, y;
Text* ptext;
VS_font* pfont;
RECT rc;
for (int i = 0; i < (int)texts.size(); i++) {
// offset position
ptext = &texts.back();
pfont = &fonts[ptext->font_id];
2️⃣if (ptext->align_id == ALIGN_LEFT)
x = ptext->pos.x + VSObject::player_dx;
3️⃣else if (ptext->align_id == ALIGN_CENTER)
x = ptext->pos.x - ((pfont->width * (ptext->str.size())) >> 1) + VSObject::player_dx;
4️⃣else if (ptext->align_id == ALIGN_RIGHT)
x = ptext->pos.x - (pfont->width * ptext->str.size()) + VSObject::player_dx;
5️⃣else if (ptext->align_id == MULTILINE_LEFT)
x = ptext->pos.x + VSObject::player_dx;
y = ptext->pos.y - (pfont->height >> 1) + VSObject::player_dy;
❌rc = { x, y, x + 250, y + 50 };
...
```
1️⃣宣告完``int x,y``後,若2️⃣3️⃣4️⃣5️⃣條件都不成立,最後進到❌時``x, y``的值未被指派。
修改方式為在1️⃣時給定初始值``int x=0, y=0``
<br>
### left operand of comma operator has no effect
```cpp
Dialog::Dialog() {
_now = none;
1️⃣_cursorX, _cursorY, _boxX, _boxY,
2️⃣_txtX, _txtY, _headX, _headY,
3️⃣_nameX, _nameY, _nBoxX, _nBoxY,
4️⃣_lineSpacing = 0;
_isShow = false;
_isChoose = false;
}
```
1️⃣、2️⃣、3️⃣行都不會賦到值,只有4️⃣的`_lineSpacing`有給到`0`。
所以應該寫成
```cpp
_cursorX = 0;
_cursorY = 0;
...
_nBoxX = 0;
_nBoxY = 0;
_lineSpacing = 0;
```
或是
```cpp
_cursorX = _cursorY = ... = _nBoxX = _nBoxY = _lineSpacing = 0;
```
---
## Bad smells
### "using" should be preferred to "typedef" for type aliasing.
```cpp
☢️typedef struct Wave{
// enemy
int time_min, amount, interval_msec;
int type[3];
double weight[3];
// boss
int boss_type[2];
bool drop_chest[2], spawned_boss[2];
// swarm
int swarm_type[2], swarm_amount[2], swarm_duration_sec[2], swarm_repeat[2], swarm_chance[2], swarm_delay_msec[2];
} Wave;
```
C++11以後,語法``using``的功能比``typedef``強大。
或是這裡可以改成``struct Wave{ ... };``就可以了。``typedef``的方法為C的語法。
<br>
### Replace this "enum" with "enum class".
```cpp
☢️enum enemy_names {
BAT1,
BAT2,
BAT3,
BAT4,
BAT5,
FLOWER1,
FLOWER2,
GHOST,
...
}
```
``enum``為``unscoped``(類似全域),應採用C++11以後的``enum class``或``enum struct``
<br>
### Use "std::array" or "std::vector" instead of a C-style array.
```cpp
☢️Wave _wave[31];
```
傳統的C陣列使用上不如``std::array``或是``std::vector``方便
<br>
### Replace "srand" with the facilities in \<random\>
```cpp
☢️srand((unsigned)time(NULL));
```
使用``srand``與``rand``生成變數不適完全隨機,應嘗試使用random函式庫的其他工具
<br>
### Do not assign data members in a constructor. Initialize member "_condition", "_triggered", "_dialogindex", "_dialogcount", "_transmap" in an initialization list.
```cpp
Event::Event() {
_condition = false;
_triggered = false;
_dialogindex = 0;
_dialogcount = 0;
_transmap = false;
}
```
不推薦在`constructor`裡面直接給值像是`_dialogindex = 0`或是`_condition = false`等等,因為當我們有做`setter`另外給`_dialogindex`或是`_condition`之類的變數賦值的時候,在`constructor`裡面做像是`_dialogindex = 0`的動作會變得浪費空間。
Sonarcloud 有一個比較推薦的方式是像底下的code,在`constructor`的時候直接做`setter`的動作。
```cpp
class S {
int i1;
int i2;
int i3 = 42; // In-class initializer
public:
S( int halfValue, int i2 = 0 ) : i1(2*halfValue), i2(i2) {}
};
```
----