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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public From: "Robert Martin" Subject: Re: Software landmines (loops) Date: 1998/09/01 Message-ID: <6sihrq$gkm$1@hirame.wwa.com>#1/1 X-Deja-AN: 387020009 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.48044143@ <904556531.666222@miso.it.uq.edu.au> <35EAEC47.164424A7@s054.aone.net.au> <35EBBFAF.DE38C061@s054.aone.net.au> <35EC28BD.351F33DF@s054.aone.net.au> <35ECB8F1.6B33FA56@ac3i.dseg.ti.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Organization: WorldWide Access - Midwestern Internet Services - www.wwa.com Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-01T00:00:00+00:00 List-Id: John Volan wrote in message <35ECB8F1.6B33FA56@ac3i.dseg.ti.com>... >There are situations where extraneous calls in the final loop iteration >would not be so harmless. For instance, suppose you wished to implement >an 'out' function for a LIST class that generated strings of the form: > > "{}" for an empty list > "{A}" for a list with one element "A" > "{A, B}" for a list with two elements "A" and "B" > etc... Another solution to this problem is (in C++) void PrintList(vector& list) { cout << "{"; string separator = ""; vector::iterator i; for (i = list.begin(); i != list.end(); i++) { cout << separator << *i; separator = ", "; } cout << "}" << endl; } This is a bit inefficient because the separator variable is needlessly set to ", " in every iteration of the loop. If this inefficiency is really too much to deal with, then we can create a special class to deal with it: class Separator { public: virtual void Separate(ostream& o) = 0; static Separator* theSeparator = &initial; static InitialSeparator initial; static CommanSeparator comma; }; class InitialSeparator : public Separator { public: virtual void Separate(ostream& o) { theSeparator = , } }; class CommaSeparator : public Separator { public: virtual void Separate(ostream& o) { o << ", "; } }; void PrintList(vector& list) { cout << "{"; vector::iterator i; for (i = list.begin(); i != list.end(); i++) { theSeparator.Separate(); cout << *i; } cout << "}" << endl; } This uses the a variation of the 'State' pattern from the Design Patterns book to implement a little finite state machine that ensures that the list is output properly. Actually, a finite state machine is probably a good way to represent this algorithm... The following is a simple state transition table: currentstate event newstate action ------------ ----- -------- ---------------- Starting Start Initial {OutputOpenBrace} Initial Item Subsequent {OutputItem} Initial Done EndState {OutputClosingBrace} Subsequent Item Subsequent {OutputComma OutputItem} Subsequent Done EndState {OutputClosingBrace} EndState * * {} Now we could use SMC (see the freeware section of my website) to build the program: class ListPrinter { public: void OutputOpenBrace() {cout << "{";} void OutputClosingBrace() {cout << "}"} void OutputItem() {cout << itsItem;} void OutputComma() {cout << ", ";} void SetItem(string& s) {itsItem = s;} private: string itsItem; }; /////////////////////////// // Input to SMC // Ouput will be a C++ class named ListPrinterFSM // Context ListPrinter FSMName ListPrinterFSM Initial Starting { Starting Start Initial {OutputOpenBrace} Initial Item Subsequent {OutputItem} Initial Done EndState {OutputClosingBrace} Subsequent Item Subsequent {OutputComma OutputItem} Subsequent Done EndState {OutputClosingBrace} EndState * * {} } void PrintList(vector& list) { ListPrinterFSM fsm; fsm.Start(); vector::iterator i; for (i=list.begin(); i != list.end(); i++) { fsm.SetItem(*i); fsm.Item(); } fsm.Done(); } Robert C. Martin | Design Consulting | Training courses offered: Object Mentor | rmartin@oma.com | Object Oriented Design 14619 N Somerset Cr | Tel: (800) 338-6716 | C++ Green Oaks IL 60048 | Fax: (847) 918-1023 | http://www.oma.com "One of the great commandments of science is: 'Mistrust arguments from authority.'" -- Carl Sagan