owned this note
owned this note
Published
Linked with GitHub
# Coding Style
2020, jeeeerrrpop
---
每個人都有自己寫Code的風格習慣。
---
neoj-2333 訊息處理
```cpp
#include <iostream>
int cnt[26];
int main() {
int n; std::cin >> n;
for(int i = 0; i < n; i++) {
char c; std::cin >> c;
if(c >= 'a' && c <= 'z') {
std::cout << c;
cnt[c - 'a']++;
}
if(c >= 'A' && c <= 'Z') {
std::cout << char(c - ('A'- 'a'));
cnt[c - 'A']++;
}
}
std::cout << std::endl;
for(int i = 0; i < 26; i++) {
std::cout << cnt[i] << " \n"[i == 25];
}
return 0;
}
```
---
```cpp
#include <iostream>
int cc[26];
int main(){int n;
std::cin>>n;
for(int i=0;i<n;i++)
{char c;
std::cin>>c;
if(c>='a'&&c<='z'){
std::cout<<c;
cc[c-'a']++;
}if(c >= 'A'&&c<='Z'){
std::cout<<char(c-('A'-'a'));cc[c-'A']++;
}
} std::cout<<std::endl;
for(int i=0;i<26;i++)std::cout<<cc[i]<<" \n"[i == 25];
return 0;
}
```
---
- 任何人都可以寫得出電腦看得懂的程式,但要讓人看得懂,不是一件容易的事情!
- 永遠不要相信未來的自己會懂你
---
並沒有哪種 Coding Style 是絕對最完美的
(宗教戰爭)
---
[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
[Google C++ Style Guide 中文版](http://www.slmt.tw/google-cpp-style-guide-zh-tw/index.html#連結)
[C++ Core Guideline](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#main)
[C/C++ Programming Style
Guidelines](http://squall.cs.ntou.edu.tw/cpp/102spring/C%20&%20C%2B%2B%20Programming%20Style%20Guidlines.pdf)
---
## Overall Goal
- 易讀
- 易改
- 一致
- 看起來舒服
---
命名規則

---
- i, j, k, l, m
- a, aa, aaa, aaaa
- a, b, c, d, e, f.....
- tmp1, tmp2, tmp3, tmp4
- _, __, ___, ____
---
變數命名
- 描述性
- 慣例
- 一致
- 格調
---
## 程式碼命名方式
```cpp
void f(int a[], int n) {
int s = 0;
for(int i = 0; i < n; i++) {
s += a[i];
}
return s;
}
```
---
TL;DR
camelCase
PascalCase
snake_case
---
- 小駝峰(lower camel case) 第一個單字的首字母小寫,其餘單字首字母大寫
- 大駝峰(upper camel case,Pascal Case) 首字母也大寫
- snake_case 用 _ 來分隔單字
- [匈牙利命名法](https://zh.wikipedia.org/wiki/匈牙利命名法)
---
```cpp
void CalcArraySum(int array[], int array_size) {
int arraySum = 0;
for(int i = 0; i < array_size; i++) {
arraySum += array[i];
}
return arraySum;
}
// 使用哪個命名規則也要一致
```
---
Google says....
---
## 版面與樣式編排

---

[source](https://www.ncyu.edu.tw/files/site_content/cc/coding%20style.pdf)
---

[source](https://www.ncyu.edu.tw/files/site_content/cc/coding%20style.pdf)
---
```cpp
if () {
} else {
}
```
```cpp
if(){
}
else{
}
```
---

---
## 適時加入空白
```cpp
//for(int i=0;i<n;i++)
for(int i = 0; i < n; i++)
//int c=(a+b)/2
int c = (a + b) / 2;
//cin>>a>>b>>c;
cin >> a >> b >> c;
// function(int a,int b,intc)
function(int a, int b, int c)
```
---
## Tab vs space
```cpp
meow
meow
```
---
各有優缺點
重要的是要一致以避免轉換平台的時候排版亂掉

---
## 檔案名稱
- 有意義
- 避免空白、特殊字元
- 避免中文
---
## 註解
- 適時加上註解有助於理解
- 避免非ASCII的文字
---
## 宣告變數
- 要注意變數的scope
- 在靠近使用那個變數的地方宣告他
- 避免第10行宣告、第100行才用
- 要初始化變數 (避免WA)
---
```cpp
void CalcArraySum(int array[], int array_size) {
int arraySum;
for(int i = 0; i < array_size; i++) {
arraySum += array[i];
}
return arraySum;
}
```
---
```cpp
for(int i = 0; i < n; i++) {
for(int i = 0; i < n; i++) {
int tmp = i;
}
}
```
---
## 函數
對於有特別意義或是重複的操作,可以寫成function,以維持易讀性。
---
```cpp
if(x >= 1 && x <= n && y >= 1 && y <= n) {
......
}
//
bool InGrid(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n;
}
......
if(InGrid(x, y)) {
......
}
```
---
[The International Obfuscated C Code Contest](https://zh.wikipedia.org/wiki/国际C语言混乱代码大赛)