본문 바로가기
Practice & Study/프로그래머스

프로그래머스>코딩테스트 입문>숫자 비교하기 (C++)

나의 코드

#include <string>
#include <vector>

using namespace std;

int solution(int num1, int num2) {
    int answer = 0;
    if (num1 == num2) {
        answer = 1;
    }
    else {
        answer = -1;
    }
    return answer;
}

 

다른 사람의 코드

#include <string>
#include <vector>

using namespace std;

int solution(int num1, int num2) {
    return num1 == num2 ? 1 : -1;
}

 

짧고 간결한 코드가 무조건 좋은 것일까?

그렇게 생각하던 때도 있었지만 나중에 그렇게 짠 코드를 볼 때

오히려 한 눈에 알아보기 힘들었던 기억도 있다. 

 

이런 간단한 코드라면 저렇게 삼항연산자를 쓰는게 좋을 것 같다고 생각하긴 하지만...