From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public From: "John G. Volan" Subject: Re: Software landmines (loops) Date: 1998/09/08 Message-ID: <35F534F6.D3CCE360@sprintmail.com>#1/1 X-Deja-AN: 389032486 Content-Transfer-Encoding: 7bit References: <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1glm$bvh$1@nnrp1.dejanews.com> <6r9f8h$jtm$1@nnrp1.dejanews.com> <6renh8$ga7$1@nnrp1.dejanews.com> <6rf59b$2ud$1@nnrp1.dejanews.com> <6rfra4$rul$1@nnrp1.dejanews.com> <35DBDD24.D003404D@calfp.co.uk> <6sbuod$fra$1@hirame.wwa.com> <35f51e53.480 <904556531.666222@miso.it.uq.edu.au> <6sgror$je8$3@news.indigo.ie> <6sh3qn$9p2$1@hirame.wwa.com> <6shbca$66c$1@news.indigo.ie> <6shhq7$lut$1@hirame.wwa.com> <6sjbso$1lk$2@news.indigo.ie> <6sjijg$36r$1@hirame.wwa.com> <6skhcm$1dr$2@news.indigo.ie> <6skqf3$9g0$1@hirame.wwa.com> <6smmhv$1kp$1@nnrp1.dejanews.com> <6smsi3$n8i$1@hirame.wwa.com> <35EEBA15.30C3CC76@tisny.com> <6sn2lv$t6m$1@hirame.wwa.com> X-Posted-Path-Was: not-for-mail Content-Type: text/plain; charset=us-ascii X-ELN-Date: Tue Sep 8 06:44:50 1998 Organization: EarthLink Network, Inc. Mime-Version: 1.0 Reply-To: johnvolan@sprintmail.com Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-08T00:00:00+00:00 List-Id: duncan@yc.estec.esa.nlx wrote: > > int doSomething(Tree *pTop) > { > int errorCode; > > if (pTop != NULL) > { > if (pTop->pChild != NULL) > { > if (pTop->pChild->pChild != NULL) > { > /* lots of nesting omitted */ > > if (pTop->pChild...->pChild != NULL) > { > errorCode = reallyDoSomething(pTop->pChild...->pChild); > } else > { > errorCode = ERRORX; > } else > { > errorCode = ERROR3; > } > } else > { > errorCode = ERROR2; > } > } else > { > errorCode = ERROR1; > } > return(errorCode); > } For what it's worth, the above could be transformed into: int doSomething(Tree *pTop) { int errorCode; if (pTop == NULL) { errorCode = ERROR1; } else if (pTop->pChild == NULL) { errorCode = ERROR2; } else if (pTop->pChild->pChild == NULL) { errorCode = ERROR3; } ... /* lots of nesting omitted */ ... else if (pTop->pChild...->pChild == NULL) { errorCode = ERRORX; } else { errorCode = reallyDoSomething(pTop->pChild...->pChild); } return(errorCode); } As you can see, the nesting has been considerably reduced, yet this function is still single entry/single exit. It also shares with the early-return version the advantage of showing the choice of error-code close to the test that determines the error. Best of both worlds? :-) > int compareThings(Thing *pLeft, Thing *pRight) > { > assert(pLeft != NULL && pRight != NULL); > > if (pLeft->x != pRight->x) > { > if (pLeft->x < pRight->x) > { > return(-1); > } else > { > return(+1); > } > } > if (pLeft->y != pRight->y) > { > /* similar logic */ > } > /* other members */ > return(0); > } Again, the above can be transformed to: int compareThings(Thing *pLeft, Thing *pRight) { assert(pLeft != NULL && pRight != NULL); int result = 0; if (pLeft->x < pRight->x) { result = -1; } else if (pLeft->x > pRight->x) { result = +1; } else if (pLeft->y < pRight->y) { result = -1; } else if (pLeft->y > pRight->y) { result = +1; } /* other members */ else { result = 0; } return result; } -- indexing description: "Signatures for John G. Volan" self_plug: "Ex Ada guru", "Java 1.1 Certified", "Eiffelist wannabe" two_cents: "Java would be even cooler with Eiffel's assertions/DBC, % %generics, true MI, feature adaptation, uniform access, % %selective export, expanded types, etc., etc..." class JOHN_VOLAN_SIGNATURE inherit SIGNATURE invariant disclaimer: not (opinion implies employer.opinion) end -- class JOHN_VOLAN_SIGNATURE