Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!mcvax!ukc!kcl-cs!glasgow!taylor From: taylor@glasgow.glasgow.UUCP (Jem Taylor) Newsgroups: net.lang Subject: Re: and if you put this in your language ... Message-ID: <457@glasgow.glasgow.UUCP> Date: Wed, 19-Mar-86 10:36:29 EST Article-I.D.: glasgow.457 Posted: Wed Mar 19 10:36:29 1986 Date-Received: Sat, 22-Mar-86 22:19:54 EST References: <1187@mmintl.UUCP> <6699@cca.UUCP> Reply-To: taylor@glasgow.UUCP (Jem Taylor) Organization: Comp Sci Dept, Glasgow Univ, Scotland Lines: 72 In article <6699@cca.UUCP> g-rh@cca.UUCP (Richard Harter) writes: >In article <> taylor@glasgow.UUCP (Jem Taylor) writes: >> .................. >> if (A) >> { X; >> if (B) Y; >> else Z; >> } >> else Z; >> >>If Z is so large that it is not reasonable to have two copies of >>it's code, it's time to make it a separate procedure anyway. >> > Sorry Jem, the code you suggest is wrong in principle >even though it may be the right thing to do in practice as a >matter of convenience, the principle bei: > > AVOID USING MULTIPLE COPIES OF THE SAME CODE > >Procedures are one way to avoid multiple copies. In any particular As someone else has pointed out, the above creates two source-code sections which need to be kept in step during program development. The solution is to use macros ( or #defines as C calles them ) to locate the text of Z in one place and use the pre-processor to place multiple copies in the construct above. Regardless of the programmer's style, the compiler should detect that two or more identical code sections are specified, and generate the apropriate object code, with BRANCH and/or SUBROUTINE or whatever, to make whatever tradeoff between code size and speed is required. The programmer doesn't need to complicate his problem by using *unnecessary* constructs as has been proposed. >thing to do. The most general solution is to use a flag (which, as >I recall, people were objecting to originally), e.g. > > flag = 0; > if (A) { > X; > if (B) Y; > else flag = 1; > } > else flag = 1; > if (flag) Z; > >[I'm not in love with this code either.] If X, Y, and Z are large >blocks of code one might also consider: > > xflag = 0; > yflag = 0; > zflag = 0; > if (A) xflag = 1; > else zflag = 1; > if (xflag) { > X; > if (B) yflag = 1; > else zflag = 1; > } > if (yflag) Y; > if (zflag) Z; > >This is more long winded in schematic form. However it will be clearer >if X, Y, and Z are complicated pieces of code. I dont find this clearer than what I (and others) proposed. Therefore, for my purposes, it is not what is required. [ I want other people to understand what the code (a) is meant to do (b) does, when I am not there to work it out myself. I certainly dont intend to try and remember myself :-) ]. -Jem. P.S. no-one here agrees with my indenting style either.