다음의 두 가지 코드 블럭 중 어느 것이 전반적으로 더 좋은가?더 많은 반환 문장들과 많은 들여 쓰기
하나의 return 문, 더 들여 쓰기 코드 :
struct dnode *dllist_push_front(struct dllist *dllist, void *data)
{
struct dnode *dnode = malloc(sizeof(struct dnode));
if (dnode) {
dnode->data = data;
dnode->next = dllist->sentinel->next;
dnode->prev = dlllist->sentinel;
dnode->next->prev = dnode;
dllist->sentinel->next = dnode;
dllist->size++;
}
return dnode;
}
또는
두 반환 문에서도 덜 들여 쓰기 코드 :
struct dnode *dllist_push_front(struct dllist *dllist, void *data)
{
struct dnode *dnode = malloc (sizeof(struct dnode));
if (!dnode)
return NULL;
dnode->data = data;
dnode->next = dllist->sentinel->next;
dnode->prev = dlllist->sentinel;
dnode->next->prev = dnode;
dllist->sentinel->next = dnode;
dllist->size++;
return dnode;
}
맨 위 코드 세그먼트에도 두 개의 return 문이 있습니다. –
웁스! 그것은 실수입니다. 내가 수정하자. – oddlogic
2 가지 방법의 또 다른 효과는 if()에서 부정 (또는 not)입니다. 부정이없는'if()'에는 미묘한 이점이 있습니다. if (working()) ... if (실패()) ...와 if (! working()) ... if (! failed()) ... '. 그것은 당신의 질문을 넘어서는 것이 아니라 배려입니다. 법원 판결과 같은'if()'를 피하려고합니다. "대법원은 부정적 리뷰를 삭제하라는 명령을 뒤집습니다" – chux