Naming Convention

``````double func(double a) {
const double b = 3.14;
return a * a * b;
}``````
``````double get_circle_area(double radius) {
const double PI = 3.14;
}``````

Naming Convention

``````for (int i = 0; i < n; i++) {
std::cout << "this is ok\n";
}``````

Naming Convention

under_score

camelCase

``int get_area();``
``int getArea();``
``int student_count;``
``int studentCount;``

2020/3/28

2021/3/6

Naming Convention

``````void demoFunction(int demo_variable) {
int other_variable;
/* do something */
return;
}``````

Naming Convention

camelCase還可以分成大/小駝峰

class 的名稱使用大駝峰

Naming Convention

可讀性

``int thisIs_aJoke();``

Spacing

``````double get_area(double radius) {
const double PI=3.14;
}``````
``````double get_area(double radius) {
const double PI = 3.14;
}``````

Spacing

indent

``````for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i > j)
std::cout << "bigger\n";
``````
``````for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i > j)
std::cout << "bigger\n";``````

indent

Spacing

``````double get_circle_area(double radius) {
const double PI = 3.14;
}

int get_rectangle_area(int height, int width) {
return height * width;
}

int main() {

int height = 3;
int widht = 4;
int rectangle_area = get_rectangle_area(height, width);

return 0;
}``````

Spacing

``````if (a + b > c) {
// good
}``````
``````if( a + b > c ){
// not so good
}``````
``````if(a+b>c){
// you wanna fight?
}``````

Curly Braces

``````char* get_student_class(char* student_name) {
// return one of "C++", "Python", "algo"
// or "none" if student is not in any class

...
}``````

Misc

1. 在宣告變數時，宣告在需要這個變數的附近地方

2. 盡量不要使用全域變數

3. 初始化剛宣告的變數

4. 盡量不要 using namespace std;

5. 編輯器使用dark theme (不可靠研究顯示coding速度+37%)

By JT

• 567