Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site cca.UUCP Path: utzoo!watmath!clyde!burl!ulysses!ucbvax!decvax!cca!g-rh From: g-rh@cca.UUCP (Richard Harter) Newsgroups: net.lang Subject: Re: and if you put this in your language ... Message-ID: <6811@cca.UUCP> Date: Sat, 22-Mar-86 19:54:35 EST Article-I.D.: cca.6811 Posted: Sat Mar 22 19:54:35 1986 Date-Received: Tue, 25-Mar-86 03:44:32 EST References: <1187@mmintl.UUCP> <6699@cca.UUCP> <> Reply-To: g-rh@cca.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge Lines: 69 Summary: You can run but you can't hide [.... The continuing saga of the coding problem that created the line eating bug .....] In article <> taylor@glasgow.UUCP (Jem Taylor) writes: >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. > I went on to outline a schematic suggestion for using flags and Jem wasn't happy with that. Frankly, I'm not enthusiastic about flags either. The real difficulty is that there is not good solution to the problem which can be roughly stated as follows: Given a net of if-then-else logic in which a functional block of code occurs more than once, how do you structure the code? Some possible solutions are: (a) Duplicate the block (b) Replace the block by a procedure (c) Replace the block by a macro (d) Separate the logic structure and the executable structure, using flags to control the execution (e) Programming tricks based on language syntax All of these solutions have merits and defects. If the duplicated block is short, alternative (a) is often preferable even though it involves duplication of code. (Suppose Z is 'i++;'.) Alternative (b) can be a big win; it loses [For Strunk and White's sake, the word is 'lose' and not 'loose'] if you have to export part of the environment that the block is embedded in. Alternative (d) is useful if you have complicated logic, but is not desirable in the cited example because flag setting and executable code are intermingled. Alternative (e) can be the method of choice if everything is simple and there are no side effects. Finally there are macros (c). Sometimes macros are the appropriate and elegant solution; sometimes they lead to the most godawful messes. Finally there is: (f) Reformulate the problem. We can't do this in the cited example because we don't know what X, Y, and Z are. In practice we do and quite often we find that the problem can be decomposed in a different way that has cleaner logic. There are deep problems with reformulation also; it leads to only doing those things which are easy to do in the language of choice. Richard Harter, SMDS Inc.