
char * foo(int s)
{
char *output;
if (s > 0)
output = (char *) malloc (size);
if (s == 1
return NULL; /* if s==1 then memory leaked */
return(output);
}
main()
{
char *str;
str = ( char * ) malloc (10);
if (global == 0)
free(str);
free(str); /* str is already freed */
}
char *ch = NULL;
if (x > 0)
{
ch = 'c';
}
printf("%c", *ch); // ch may be NULL
*ch = malloc(size);
ch = 'c'; // ch will be NULL if malloc returns NULL
switch(i)
{
case 0: s = OBJECT_1; break;
case 1: s = OBJECT_2; break;
}
return (s); //s not initialized for values other than 0 or 1
dataArray[80];
for (i = 0; i <= 80; i++)
dataArray[i] = 0;
typedef enum{A, B, C, D} grade;
void foo(grade X)
{
int l, m;
l = GLOBAL_ARRAY[x-1]; //Underflow possible
m = GLOBAL_ARRAY[x+1]; //Overflow possible
}
void mygets(char *str) {
int ch;
while ( ch = getchar() != '\n' && ch != '\0' )
*(str++) = ch;
*str = '\0';
}
main() {
char s2[4];
mygets( s2 );
}
변수/필드/메소드 등(예: thisIsAnExample)
대문자(예: SPEED_OF_LIGHT)
명사(구), 대문자 시작
소문자 시작 동사구(setTitle)
값을 설명하는 명사구 함수(areaOfTriangle)
getter/setter, boolean은 get, is 관례
용도 힌트 제공, 모호한 이름 피함, 위치에 따라 길이 조절(매개변수 짧게/필드 길고 의미있게)
소문자 명사
lAccountNum, strName){} 사용(의도와 다른 실행 방지)if (flag) validate(); update();if (flag) {
validate();
update();
}// Good
if (username == null) {...}
//
Less Good if(username == null) {...}if (x >= 0)
if (x > 0) positiveX();
else // 들여쓰기 때문에 첫번째 if에 관련된 것으로 착각함!
negativeX()
if (x >= 0){
if (x > 0) positiveX();
}
else { // 우리가 의도한 코드
negativeX()
}
// Extraneous but useful parentheses.
int width = (( buffer * offset ) / pixelWidth ) + gap;
evaluate(String vehicleP) //스트링으로만 한정하면 잘못된 값 들어올 수 있음
evaluate(SpecializedVehicle vehicleP) //확실한 매개변수를 가진 함수를 사용
private int hr; // The hour of the day, in 0..23.
private double[] temps; // temps[0..numRecorded-1] are the recorded temperatures
private int numRecorded; // number of temperatures recorded
@param, @return 등@param b one of the sides of the triangle
@return The area of the triangle
/** Print the sum of a and b. */
public static void printSum(int a, int b) { ... }
/** An object of class Auto represents a car.
Author: Eun Man Choi.
Date of last modification: 25 November 2019 */
public class Auto { ... }
// Truthify x >= y by swapping x and y if needed.
if (x < y) {
int tmp= x;
x= y;
y= tmp;
}


public class CheckoutConroller {
Patron p;
public String checkout(String callNo) {
BDNgr dbm = new DBMge();
Document d = dbm.getDocument(callNo);
String msg = " ";
if (d != null) {
Loan l = new Loan(p, d);
dbm.save(l);
d.setAvailable(false);
dbm.save(d);
msg = "Checkout successful.";
} else {
msg = "Document not found.";
}
return msg;
}
}

1. 소규모의 변경 - 단일 리팩토링
2. 코드가 전부 잘 작동되는지 테스트
3. 전체가 잘 작동하면 다음 리팩토링 단계로 전진
4. 작동하지 않으면 문제를 해결하고 리팩토링 한 것을 되돌려 시스템이 작동되도록 유지


재사용할 확률이 많은 코드는 메서드로 정의하고 이를 호출한다.
...
if (i != min) {
int temp = num[i];
num[i] = num[min];
num[min] = temp;
}
...
if (i != min) {
Swap(ref num[i], ref num[min]);
}
void Swap(ref int a, ref int b) {
int temp = a;
a = b;
b = temp;
}
Custom 클래스의 일부로 phone이 포함되어 있는 것은 클래스 하나에 고유한 책임을 갖게 만드는 객체지향 모델의 정신과 맞지 않는다. 따라서 두 개의 단일 책임 클래스로 분할한다.
public class Customer {
private String name;
private String workPhoneAreaCode;
private String workPhoneNumber;
}
public class Customer {
private String name;
private Phone workPhone;
}
public class Phone {
private String areaCode;
private String number;
}


TDD 작업 과정
public class MyUnit {
public String concatenate(String one, String two) {
return one + two;
}
}import org.junit.Test;
import static org.junit.Assert.*;
public class MyUnitTest {
@Test
public void testConcatenate() {
MyUnit myUnit = new MyUnit();
String result = myUnit.concatenate("one", "two");
assertEquals("onetwo", result);
//예상하는 출력(onetwo)와
//실제 호출된 메서드 result의 출력을 비교
}
}@Test: JUnit에게 신호를 보내는 것으로 실행되어야하는 단위 테스트임을 나타냄.assertEquals(): “실제 호출된 메서드의 출력”을 “예상하는 출력”과 비교한다.두 사람이 같은 컴퓨터를 사용하며 함께 프로그래밍
소통 향상, 상호 학습, 창의적 문제 해결에 도움
드라이버: 키보드를 사용하여 실제로 코드를 작성하는 개발자로 코드의 구체적인 구현에 집중한다.
내비게이터: 코드를 실시간으로 검토하고, 설계나 전략적인 방향을 제시한다. 네비게이터는 코드의 논리적 오류나 개선점을 찾아내고 드라이버에게 피드백을 제공한다.
모든 사람에게 맞지는 않는다. 혼자 개발하는 것을 선호나는 사람들도 존재함.
적절히 다루지 못하면 대화에 시간이 너무 많이 걸린다.
파트너와의 교육, 경험, 코딩 스타일 등의 차이점에 적응해야한다.