2011-01-21 3 views
0

다음 코드와 로직에 대해 buddy와 논의했습니다. POST 요청을 처리하는 시스템이 있습니다. 실제로 REST가 아니라 요청을 게시하는 것입니다. 아이디어를 얻으려면 다음 PHP를보십시오.KISS 대 많은 메소드들

class Pancake { 
    public function servePancake() 
    { 
    if (/* check something on the kitchen*/) { 
    echo json_encode(array('status' => 'error', 'message' => 'kitchen offline')); 
    exit; 
    } 

    if (/* check something else on the kitchen */) { 
    echo json_encode(array('status' => 'error', 'message' => 'Santa hates you, no pancakes this time')); 
    exit; 
    } 

    if (/* check if there's something else in the menu */) { 
    echo json_encode(array(
    'status' => 'weDoHaveMenuYouShouldCheckItOut', 
    'message' => 'See the menu for a pancake flavor you wish', 
    'pancakeTypes' => array('cherry', 'blueberry', 'blackberry') 
    )); 
    exit; 
    } 

    // And so on with lot's of options, but pretty simple inside 

    // if everything went fine 
    echo json_encode(array('status' => 'ok', 'message' => 'Here is your pancake')); 
    exit; 
    } 
} 

각 답변에 대해 방법을 만들 이유가 있습니까? 다음과 같은 의미입니다 :

protected function respondWithMenu($message, $menu) 
    { 
    // basically the same json_encode and exit; 
    } 

    protected function respondWithSuccess($message); 
    { 
    // Status is succes + same json_encode 
    } 

    protected function respondWithError($message) 
    { 
    // Status is error + same json_encode 
    } 

    protected function respondWithSomethingElse($message, $somethingElse) 
    { 
    // adding something else to the response 
    // and then.... gues what? 
    // yeah, json_encode, you're correct! 
    } 

직접 json_encode 호출 대신 사용하십시오.

감사합니다.

답변

2

코드가 더 자체적으로 문서화됩니다. 매번 인라인 주석을 볼 때마다 코드를 자체 메서드로 추출하고 메서드 이름에 주석을 통합하는 힌트가 될 수 있습니다.

코드가 다른 사람들과 더 잘 어울립니다. 이것은 당신을위한 고려 사항 일 수도 있고 아닐 수도 있지만 200 줄 기능에서 10 줄을 사용하고자하는 사람이 있다면 그 사람은 가장 쉽게 복사하고 붙여 넣을 수 있습니다. 이는 누구에게도 재미 있지 않습니다.

비슷한 점에서, 방법 테스트는 매우 쉽습니다. 단위 테스트가 훨씬 쉽습니다. 그리고 테스트는 산타를 행복하게 만듭니다.