Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Toward simpler, more elegant code Posted by Actor on 6 Feb 2012 at 12:26 AM
I was composing a post earlier this afternoon about grandtotals but decided that we need to cover some other ground before I can adequately explain. Below is some code I have extracted from your posts. It is just as you posted except I have tried to restore some indentation (which I hope you are using) and omitted some blank lines.

I want to discuss your procedure add.
(*184*)       
(*185*) Procedure Add;
(*186*) {*************************************************************************
(*187*) * This Procedure is used to ask the user which database he would like to *
(*188*) * use to add records to the respective groups. *
(*189*) *************************************************************************}
(*190*) var
(*191*)    Ans : char;
(*192*)    choice:integer;
(*193*) begin
(*194*)    clrscr;
(*195*) 
(*196*)    Textcolor(Black);
(*197*)    Textbackground(White);
(*198*)    GotoXY(8,2);
(*199*)    Writeln ('ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»');
(*200*)    Gotoxy(8,3);
(*201*)    Writeln ('º º');
(*202*)    Gotoxy(8,4);
(*203*)    Writeln ('º Welcome to the Add option of this database. º');
(*204*)    Gotoxy(8,5);
(*205*)    Writeln ('º Here you can add members in different groups . º');
(*206*)    Gotoxy(8,6);
(*207*)    Writeln ('º Enjoy! º');
(*208*)    Gotoxy(8,7);
(*209*)    Writeln ('º º');
(*210*)    Gotoxy(8,8);
(*211*)    Writeln ('ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ');
(*212*) 
(*213*)    WRITELN;
(*214*)    Writeln('Here you will choose which database you want to add someone to.');
(*215*)    Writeln;
(*216*)    Writeln('1-Add person to DDC database.');
(*217*)    Writeln('2-Add person to DPI database.');
(*218*)    Writeln('3-Add person to DSGO database.');
(*219*)    Writeln('Press any number more than "3" to go to main menu');
(*220*)    Writeln;
(*221*)    Writeln('Press the respective number then press Enter to continue.');
(*222*)    Readln(choice);
(*223*) 
(*224*)    case choice of
(*225*)       1:Add_MemberDDC;
(*226*)       2:Add_MemberDPI;
(*227*)       3:Add_MemberDSGO;
(*228*)    END;
(*229*) 
(*230*)    Readln;
(*231*)    Mainmenu;
(*232*)


Note lines 225, 226, 227. Depending on the value of choice the program calls one of three procedures: Add_MemberDDC, Add_MemberDPI or Add_MemberDSGO. Examination of these three procedures shows that they are nearly identical, the differences being which file the procedure accesses. Add_MemberDDC accesses file DDCDatabasefile, Add_MemberDPI accesses DPIDatabasefile and Add_MemberDSGO accesses DSGODatabasefile.

This makes for a lot of duplicated code. It would be great to have just one procedure that could handle all three files. The problem with that is that all three files have different structures. One solution to that is to simply write three different procedures (your solution). This is a legitimate choice. Another is to load the procedure with a lot of if..then..else statements (or case statements). Very ugly and I don’t recommend it.

My solution is to find a structure that satisfies the requirements of all three databases, such as ...
Type
    MemberDatabase	= record
        Member_ID 		: string[10] ;
        Member_Name		: string[30] ;
        Member_TicketPayment	: integer ;
        Advert			: string
    End ;

The obvious objection to such an approach is that only the DDC database uses the field Advert. Its inclusion in the DPI and DSGO databases wastes hard disk space. At one time, when hard disk space was expensive, that was a valid objection but today disk space is cheap enough (measured in gigibytes) that its expenditure is a small price to pay for simpler, more elegant code.

If we adopt this approach then we need to declare the following.
Type
    {
        this declaration is necessary because
        “file of MemberDatabase” cannot be passed 
        to a procedure or function.
    }
    Databasefiletype	= file of MemberDatabase ;

var
    DDCDatabasefile,
    DPIDatabasefile,
    DSGODatabasefile : Databasefiletype ;

and to make the assignments
begin { main program }
    assign(DDCDatabasefile, ‘memberdb.ddc’) ;
    assign(DPIDatabasefile, ‘memberdb.dpi’) ;
    assign(DSGODatabasefile, ‘memberdb.dsg’) ;

    viewmenu ;
    mainmenu
end.  { main program }

And now lines 224 .. 228 become
(*224*)    case choice of
(*225*)       1 : Add_Member (DDCDatabasefile, 'DDC') ;
(*226*)       2 : Add_Member (DPIDatabasefile, 'DPI') ;
(*227*)       3 : Add_Member (DSGODatabasefile, 'DSGO')
(*228*)    END;

and the single procedure Add_Member becomes
Procedure Add_Member (var databasefile : Databasefiletype ; db : string) ;
{*****************************************
 * This Procedure is used to add members *
 * into any database                     *
 *****************************************}
Var
    Databaserec : Memberdatabase ;
BEGIN
    {$I-}
        clrscr;

        Reset(Databasefile);
            If IOResult <>0 then
                Rewrite(Databasefile);

            Textcolor(Black);
            Textbackground(White);
            GotoXY(23,8);
            Writeln('------------------------------------------------');
            GotoXY(23,9);
            Writeln('------------------------------------------------');
            GotoXY(32,10);
            Writeln(' Welcome to the ', db, ' database. ');
            GOTOXY(23,11);
            Writeln(' ');
            GOTOXY(23,11);
            Writeln('-----------------------------------------------');
            Gotoxy(23,12);
            Writeln('----------------------------------------------- ');
            With databaserec do begin	 
                Writeln;
                write  ('Enter the Member ID.');
                readln(Member_ID);
                Writeln;
                write  ('Enter the Member full name.');
                Readln(Member_Name);
                Writeln;
                repeat
                    write  ('Enter the Member Ticket Price');
                    readln(Member_TicketPayment);
                    writeln
                until ioresult = 0      
            end ;

            seek(Databasefile, filesize(Databasefile));
            Write(Databasefile, DatabaseRec);
            Write('Now there are ', fileSize(databasefile), ' records in the ', db, ' Database ');
            ReadLn;
        Close(Databasefile)
    {$I+}
end ;



If I may be permitted a small digression and allowed to be Sherlock Holmes, I observe that lines 33, 85 and 136 all have the same typographical error. From this I deduce that you wrote one of the procedures, then copied and pasted it twice and made the necessary modifications.


Thread Tree
pampaz Help(Project crisis) on 9 Jan 2012 at 12:51 PM
Actor Re: Help(Project crisis) on 13 Jan 2012 at 9:08 PM
pampaz Re: Help(Project crisis) on 14 Jan 2012 at 3:37 PM
pampaz Re: Help(Project crisis) on 14 Jan 2012 at 3:38 PM
Actor Re: Help(Project crisis) on 16 Jan 2012 at 9:38 AM
pampaz Re: Help(Project crisis) on 19 Jan 2012 at 12:41 PM
Actor Re: Help(Project crisis) on 21 Jan 2012 at 10:35 PM
Actor Re: Help(Project crisis) on 22 Jan 2012 at 8:35 PM
Actor Re: Help(Project crisis) on 22 Jan 2012 at 10:31 PM
Actor Re: Help(Project crisis) on 24 Jan 2012 at 7:58 PM
pampaz Re: Help(Project crisis) on 26 Jan 2012 at 2:59 PM
Actor Re: Help(Project crisis) on 27 Jan 2012 at 12:26 PM
Actor Re: Help(Project crisis) on 1 Feb 2012 at 7:28 PM
pampaz Re: Help(Project crisis) on 26 Jan 2012 at 3:06 PM
Actor Re: Help(Project crisis) on 1 Feb 2012 at 7:35 PM
Actor Re: Help(Project crisis) on 24 Jan 2012 at 8:01 PM
Actor Getting rid of recursive calls on 2 Feb 2012 at 10:39 PM
pampaz Re: Getting rid of recursive calls on 4 Feb 2012 at 7:58 PM
pampaz02 Re: Getting rid of recursive calls on 7 Feb 2012 at 5:43 PM
pampaz02 Re: Getting rid of recursive calls on 7 Feb 2012 at 5:46 PM
Actor Two examples on 8 Feb 2012 at 4:24 PM
Actor Toward simpler, more elegant code on 6 Feb 2012 at 12:26 AM
Actor Grand Totals on 6 Feb 2012 at 6:53 PM
pampaz02 Re: Grand Totals on 10 Feb 2012 at 5:02 PM
Actor Your call on 11 Feb 2012 at 1:09 PM
pampaz02 Re: Your call on 12 Feb 2012 at 6:35 PM
Actor Re: Your call on 12 Feb 2012 at 7:50 PM
pampaz02 Re: Your call on 13 Feb 2012 at 12:05 PM
Actor Weather on 12 Feb 2012 at 7:59 PM
Actor When is this project due? on 12 Feb 2012 at 8:07 PM
Actor This compiles on 11 Feb 2012 at 5:10 PM
Actor Error 1 on 12 Feb 2012 at 11:47 AM
Actor Error 2 on 12 Feb 2012 at 12:17 PM
Actor Error 3 on 12 Feb 2012 at 1:46 PM
Actor Error 4 on 12 Feb 2012 at 2:54 PM
Actor Error 5 on 12 Feb 2012 at 7:15 PM
Actor Error 6 on 12 Feb 2012 at 7:27 PM
Actor Error 7 on 13 Feb 2012 at 10:13 AM
Actor Two changes on 13 Feb 2012 at 12:10 PM
Actor Testing on 14 Feb 2012 at 11:41 AM
Actor21 Re: Grand Totals on 15 Feb 2012 at 4:47 PM
Actor21 Viewing with one procedure on 15 Feb 2012 at 9:29 PM
pampaz02 Re: Viewing with one procedure on 16 Feb 2012 at 11:41 AM
pampaz02 Re: Grand Totals on 10 Feb 2012 at 5:04 PM
pampaz02 Re: Grand Totals on 10 Feb 2012 at 5:06 PM
Actor goto on 10 Feb 2012 at 4:37 PM
pampaz02 Re: goto on 10 Feb 2012 at 6:49 PM
pampaz02 Re: goto on 10 Feb 2012 at 6:51 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.