728x90
λ°μν
μΆμ²
μ€νμΌ GUIDE λ§ν¬ (https://cs50.readthedocs.io/style/c)
μ νλ³ style guide μμ½
style guide μ νμμ±
- reliably analyze your codeβs style
Line length
a line of code is ==80 characters== long in C
- μλλ©΄, old computer κ° '24 lines vertically and 80 characters horizontally' λ₯Ό λ΄μ μ μμκΈ° λλ¬Έ.
javascript
μμμ μ μΈ- μλ° μ€ν¬λ¦½νΈλ νΉμ±μ κ΅μ₯ν μ½λκ° κΈΈμ΄μ§
- your goal should instead be to ==break up lines (as via \n)== in locations that maximize ==readability and clarity==.
- μ¦, 'break up line' μ ν΄μ -> κ°λ μ±μ λμ¬λΌ
comments
- comments νμμ±
- code μ λ ==readable== νκ² λ§λ λ€. - ==readablity== νλ©΄ -> 1) λ€λ₯Έ νμ 2) μκΈ° μμ μκ² λμ - ex) λͺ μκ°, λͺ μ£Ό, λͺ λ¬μ΄ μ§λμ ν΄λΉ μ½λλ₯Ό =='μ μ§λ³΄μ'== ν λ λμμ΄ λλ€. - μλλ©΄, 'μ΄κ² μ΄λ€ λ§₯λ½μμ ν λ§μΈμ§ λͺ¨λ₯Όλκ° μμΌλκΉ.'
- κ·Έλ¬λ©΄, μ΄λ»κ² comment?
- too much, too little μ΄ μλ, ==sweet spot== μ ν΄μΌ ν¨ -> κ·Έλ¬λ©΄ sweet spot μ?
- sweet spot μ? βββ
1. ==What does this block do? 2. ==Why did I implement this block in this way?
- sweet spot μ μ΄λ»κ² μμ±νλ©΄ μ’μ?
- comment λ ==code
μ μ€
== - No need to write in full sentences, but do capitalize
- comment μ¬μ΄μλ ==space ν μΉΈ== λ¨κΈ°κΈ°
Do
Don't do this
file μλ μ΄λ€ νλ‘λ¨μΈμ§ μ€λͺ νλ λΆλΆμ΄ μμ΄μΌ ν¨
.c and .h files should be a comment that summarize what your program (or that particular file) does, as in:
κ° ==function== μλ
what your function is doing
μ λν μ€λͺ μ΄ μμ΄μΌ ν¨.- μ΄λ, ==keyword== λ₯Ό μ¬μ©ν κ² ββ
// Returns the square of n int square(int n) { return n * n; }
- μ΄λ, ==keyword== λ₯Ό μ¬μ©ν κ² ββ
Conditions (쑰건문)
κ°λ μ±μ μν΄ μ°Έκ³ ν style guide
λκ΄νΈ
{ }
- μ μλ μ€μ μ λ§μΆ κ² (line up nicely)
- ==branch== μμ μ΄λ€ λ΄μ©λ¬Όμ΄ μλμ§ λ³΄μ΄κ² ν κ²
if
- if λ€μμ ==single space== λ₯Ό λ κ²
branch μμ λ΄μ©λ¬Όμ μΈ λλ ==4 spaces==
- 보ν΅
tab
ν λ² μ΄λ©΄ λλ λ―.
- 보ν΅
<
λλ>
- κΊ½μ μλ€μ ==single space== λ₯Ό λ κ²
(
λλ)
- κ΄νΈ μ λ€μ ==no space (isn't any space)== λ₯Ό λ κ²
μ’μ μμ (
DO
)if (x > 0) { printf("x is positive\n"); } else if (x < 0) { printf("x is negative\n"); } else { printf("x is zero\n"); }
- `Don't do this`
- { } λ₯Ό νμ€μ μ°λ κ²½μ° (μμ1)
```js
if (x < 0) {
printf("x is negative\n");
} else if (x < 0) {
printf("x is negative\n");
}
- μμ 2
if (x < 0)
{
printf("x is negative\n");
}
else
{
printf("x is negative\n");
}
indentation (λ€μ¬μ°κΈ°)
νμμ±
- μ΄λ€ μ½λ λΈλ‘μ΄ λ€λ₯Έ μ½λ λΈλ‘ 'λ΄λΆ' μ μλμ§λ₯Ό λͺ
νν νκ² νκΈ° μν κ²
make clear which blocks of code are inside of others
- μ΄λ€ μ½λ λΈλ‘μ΄ λ€λ₯Έ μ½λ λΈλ‘ 'λ΄λΆ' μ μλμ§λ₯Ό λͺ
νν νκ² νκΈ° μν κ²
μ£Όμν μ
- editor λ§λ€
\t
λ₯Ό λ λλ§ νλ λ°©μμ΄ λ€λ¦ \t
λ₯Όfour spaces
λ‘ νλ κ²μ΄ μΌλ°μ .- κ·Έλ κ² νμ§ μλ μλν°λ μμμ μ μ
- editor λ§λ€
μ’μ μμ βββββ
- comment + ν¨μ + for λ¬Έ μ΄ κ°μ΄ λμ€λ©΄ μ΄λ κ² μμ±ν΄λ³΄μ βββ
// Print command-line arguments one per line printf("\n"); for (int i = 0; i < argc; i++) { for (int j = 0, n = strlen(argv[i]); j < n; j++) { printf("%c\n", argv[i][j]); } printf("\n"); }
- comment + ν¨μ + for λ¬Έ μ΄ κ°μ΄ λμ€λ©΄ μ΄λ κ² μμ±ν΄λ³΄μ βββ
β«
---
### Loops (λ°λ³΅λ¬Έ)
#### **1) for**
- μΌλ° μμΉ 1οΈβ£
iteration μ νμν ==temporary variable== μλ `i`, `j`, `k` λ₯Ό μ¬μ©. λ§μ½, `more specific names` μ΄ readable μ λμμ΄ λλ€λ©΄, κ°λ₯.
> Whenever you need temporary variables for iteration, use i, then j, then k, unless more specific names would make your code more readable:
- μΌλ° μμΉ 2οΈβ£
==temporary variable== μ΄ ==3κ° μ΄μ==μ΄λ©΄, μ½λ ==design μ λ€μ== μκ°ν κ²
> If you need more than three variables for iteration, it might be time to rethink your design!
- λλμ
- λ°λ³΅λ¬Έμ temporary variable μ λ°λμ νμν μμ ꡬλ! βββ (μ΄κ² μ κΈ°νκΈ΄ ν¨. λκ° μ¨μ΄μλ μμ μ²λΌ λκ»΄μ§κΈ°λ νκ³ )
#### 2) while
- **style guide**
- λκ΄νΈ `{ }`
- μ μλ μ€μ μ λ§μΆ κ² (line up nicely)
- ==branch== μμ μ΄λ€ λ΄μ©λ¬Όμ΄ μλμ§ λ³΄μ΄κ² ν κ²
- `while`
- if λ€μμ ==single space== λ₯Ό λ κ²
- branch μμ λ΄μ©λ¬Ό(==loop body==)μ μΈ λλ ==4 spaces==
- λ³΄ν΅ `tab` ν λ² μ΄λ©΄ λλ λ―.
- `(` λλ `)`
- κ΄νΈ μ λ€μ ==no space (isn't any space)== λ₯Ό λ κ²
- μμ
```python
while (condition)
{
// Do something
}
Variables (λ³μ)
- loop μ iteration μ νμν ==temporary variable== μ κ²½μ°
i
,k
,j
λ₯Ό λ³΄ν΅ μ¬μ© - λ€λ₯Έ λλΆλΆμ λ³μλ
more specifically named
,descriptive
ν΄μΌ ν¨.- ex) λ©λͺ¨λ¦¬μ λ΄κΈ΄ κ°μ νΉμ λ°μ΄ν°λ₯Ό νννκ³ μλ€κ³ κ°μ νμ. -> λ³μλͺ μ ν΄λΉ κ°μ΄ 무μμ νννκ³ μλμ§λ₯Ό μ λ΄κ³ μμ΄μΌ ν¨ βββββ
- 2κ°μ λ¨μ΄κ° νμνλ©΄,
undersocre
λ₯Ό μ¬μ©- ex)
is_ready
- ex)
728x90
λ°μν