Re: How to redesign big switch case?
From: Razvan Popovici (me_at_razvan_nospam.de)
Date: 03/22/04
- Next message: jblazi: "Re: Length of a file"
- Previous message: Gary: "Re: These are identical, right? char const* and const char*"
- In reply to: Siemel Naran: "How to redesign big switch case?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 22 Mar 2004 17:26:19 +0100
"Siemel Naran" <SiemelNaran@REMOVE.att.net> schrieb im Newsbeitrag
news:Ggu7c.14785$PY1.328948@bgtnsc05-news.ops.worldnet.att.net...
> How to redesign big switch case?
>
> I wrote an editor that reads chars, and does a switch (char) to process
the
> character. For example, if char is HOME then go to the beginning of the
> line, and if char is CTRL-RIGHT then go to the start of the next word. As
I
> added more cases and support for reverse incremental history mode, the
code
> grew longer and longer. It is now 562 lines, or 58% of the entire file.
> This seems a bit clumsy. Fortunately the code does not have any goto
> statements and seems pretty structured and clear to me, so it's not
exactly
> sphaghetti code. Is it worthwhile to change the code? If yes, then how?
> Finally, it would be nice to allow users to derive their own editor class
> from my one and add support for more special keys.
>
> Thanks.
>
>
Hi,
What about if you would write for each keystroke class a function? Class I
mean you will maybe threat the keys from 0 to 9 as a set, same for the
letters A to Z.
If you go OOP, then these functions need to be members of your class and
virtual, so you would be able to add more functionality to your object
though derivation.
Draft (sorry for writting the functions inline, no access specificators,
etc, etc, don't do it in "real"):
class CMyEdit
{
virtual void onKey(Key k)
{
switch(k)
{
case PGDOWN:
onPgDn();
break:
case '0'..'9':
onNumberKey(k);
break;
// and so on with each
event/class............................
}
}
virtual void onPgDn()
{
// do page down
}
virtual void onNumberKey(Key k)
{
// do page down
}
// and so on with each event function
};
Therefore, if you will need to write a new editor that works same as the old
one but beeps on each key and log each PgDn, then you write:
class CMyEditBeepLog: public CMyEdit
{
virtual void onKey(Key k)
{
doBeep();
CMyEdit::onKey(k);
}
virtual void onPgDn()
{
doLog();
CMyEdit::onPgDn();
}
};
In my oppinion using stl as suggested above won't bring much more
clarity/reusability.
Regards,
Razvan
- Next message: jblazi: "Re: Length of a file"
- Previous message: Gary: "Re: These are identical, right? char const* and const char*"
- In reply to: Siemel Naran: "How to redesign big switch case?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|