Python/Python 어렵게 배우기

Python 어렵게 배우기 - Exercise 36: Designing and Debugging

purepleya 2014. 1. 3. 21:00

원문 : http://learnpythonthehardway.org/book/ex36.html



"if", "for", "while" 문을 사용할 때 프로그램의 오류를 줄 일 수 있는 몇가지 방법을 제시 해 본다.



Rules For If-Statements


1. 모든 if 문에는 else 문을 작성 할 것

2. 모든경우 if 문에 걸리게 되어 있어서 else 문이 실행 될 일이 없을거라 생각되는 경우 else 문에 작성할 내용이 딱히 없을 것이다. 이 경우 에러메세지를 출력하고 프로그램을 종료시키는 코드를 짜 넣자

3. if 문을 3단계 이상 중첩해서 사용하지 말자. 가능하면 한단계에서 다 끝내도록 하자. 복잡하다....

4. if 문은 문단 처럼 작성하자. if, elif, else 문 앞, 뒤로을 한줄씩 띄워서 보기 편하게 하자

5. Your boolean tests should be simple. If they are complex, move their calculations to variables earlier in your function and use a good name for the variable.(이걸 뭐라 해석하지?)


If you follow these simple rules, you will start writing better code than most programmers. Go back to the last exercise and see if I followed all of these rules. If not, fix it.


Warning


Never be a slave to the rules in real life. For training purposes you need to follow these rules to make your mind strong, but in real life sometimes these rules are just stupid. If you think a rule is stupid, try not using it.




Rules For Loops


1. 무한 루프를 돌릴때는 while-loop 를 사용하자 (Python 에서만 해당됨, 다른 언어는 다름)

2. 어떤 범위 내에서 루프를 돌릴 때는 for-loop 를 사용하자.




Tips For Debugging


1. 디버거를 사용하지 마라. 디버거는 전체 코드를 풀 스캔 할 거고 딱히 유용한 정보를 얻기도 어렵다. 더 헷갈릴거야

2. 디버깅 하는 가장 좋은 방법은 print 문으로 변수 값 들을 찍어서 눈으로 확인하는 것 이다.

3. 코드를 쭈욱~ 작성한 다음 한번에 돌리면서 테스트 할 생각마라. 쪼끔씩 짜면서 쪼끔씩 테스트 해라



반응형