xsharp.eu • 'Do Case' - good example to share ??? - Page 2
Page 2 of 3

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 8:05 am
by Frank Maraite
Wolfgang,

I hope you don't take it personal. We all have good and not so good code. But here we want to help each other. In this sense I take the opportunity to show how code can be made better. And: Phil asked for 'good example to share'.

I'm very happy about that you show us a bunch of code. This has great value for the community. Thanks a lot!

I wish you, your family and all other people here and their families a merry christmas and a happy new year.

Frank

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 8:31 am
by wriedmann
Hi Frank,

unfortunately I'm not a very good programmer, I have only a bit of experience.

But "good code" or "better code" IMHO is not an absolute thing, but a personal opinion (like "what is a beautiful woman/man"). And it depends very much on the context, unfortunately.

Wolfgang

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 9:14 am
by Frank Maraite
Hi Wolfgang,

I would not say that. You have a complex and feature rich application running almost bug free. You must be a good programmer!

If you or others like I will show in more detail later how I would refactor your code and why. BTW: My code looks he same after the first run. And if I'm in time pressure I let it like it is, because it works. But later, especially when I want to add features I do refactoring first. It simply makes live easier.

Fore those who ask me why I do not show my own code before and after: I'm not Phil who is good in documenting all what he is doing. I do not have a source control system. I do not work to present the formal transformation. Saying that: I do not hve a before/after example becaus 'before' is deleted or hard to recover. I know this is not how professionals should work :( .

Frank

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 10:46 am
by Phil Hepburn
Thanks Karl,

Will work on it.

I have to admit that "Do Case" and "If Else Endif" are a part of program design that leaves me feeling quite uncomfortable.

Its probably the area where most logical errors are entered by the developer.


Speak soon,
phil.

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 11:00 am
by Phil Hepburn
Thank you guys, one and all,

I knew before I asked the Forum community that this topic of Do Case and Switch was a potentially difficult one - to get right in my eNotes and to also show all the possibilities etc..

Maybe we need to work together some more - to get stuff 100% correct.

There is no point in me just providing simple examples for either/both 'Do Case' and 'switch'.

Possibly the problems that we all have is that in this current/modern world we write our 'Do Case' code just as we did MANY years back, and maybe it is time for us to get a new approach to the multiple switch code situation !?

Thanks for your help - keep it coming please ;-0)

Have a joyful Christmas and a Happy New Year :kiss: :kiss: :kiss:

Cheers,
Phil.
Wales, UK

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 11:14 am
by FFF
Frank,
i still miss a sample how to code any comparison other than equality with Switch...

Karl

'Do Case' - good example to share ???

Posted: Fri Dec 22, 2017 11:30 am
by Frank Maraite
Hi Karl,

the answer is simple: not possible yet!

Until C# 6 switch only works by comparison with bool, int, const, enum, char and a literal string.

C# 7 introduces all kinds of comparisons.

As I know and would like to leave as that in the future X# reflects C# 6 behaviour.
For the C# 7 enhancements we have DO CASE already.

Frank

'Do Case' - good example to share ???

Posted: Fri Oct 23, 2020 8:30 am
by Horst
Hallo
I know this topic is already 3y old. But i need to be sure.
this:
DO CASE
CASE cProgramm == "login.exe" ; cString := StartLogin (oRequest, cInputString)
CASE cProgramm == "logout.exe" ; cString := StartLogin (oRequest, cInputString)
OTHERWISE ; cString := WriteErrorHtml (cError, {"blabla",CRLF+StrTran(cInputString," ","&nbsp")})
ENDCASE
i can change to switch and it will be faster ?
switch cProgramm
CASE "login.exe" ; cString := StartLogin (oRequest, cInputString)
CASE "logout.exe" ; cString := StartLogin (oRequest, cInputString)
OTHERWISE ; cString := WriteErrorHtml (cError, {"blabla",CRLF+StrTran(cInputString," ","&nbsp")})
ENDswitch

and this i cant change to switch
CASE ( nRecno := Val (DeCryptIvo (CheckLeftParameter (aParams, "IM_Dele"))) ) != 0
cRecno := SubStr3 (NTrim (nRecno),1,SLen (NTrim (nRecno))-1)
Kontroll_Delete (Val (cRecno))
CASE ( nRecno := Val (DeCryptIvo (CheckLeftParameter (aParams, "IM_Kopi"))) ) != 0
cRecno := SubStr3 (NTrim (nRecno),1,SLen (NTrim (nRecno))-1)
Kontroll_Kopie (Val (cRecno))

Horst

'Do Case' - good example to share ???

Posted: Fri Oct 23, 2020 9:29 am
by wriedmann
Hi Horst,
than will work, be faster and more error prone:

Code: Select all

switch cProgramm
CASE "login.exe" ; cString := StartLogin (oRequest, cInputString)
CASE "logout.exe" ; cString := StartLogin (oRequest, cInputString)
OTHERWISE ; cString := WriteErrorHtml (cError, {"blabla",CRLF+StrTran(cInputString," ","&nbsp")})
ENDswitch
Your third sample cannot be changed to the switch statement as it does not uses a constant expression.

Since I work with X# I'm checking all my case statements, and I'm able to change very few of them because most of them are relying on expressions and are to be evaluated for each branch.
Wolfgang

'Do Case' - good example to share ???

Posted: Fri Oct 23, 2020 9:31 am
by Frank Maraite
Horst,
the big advantage of switch over do case is: it prevents for some typos. For example

switch cProgramm
CASE "login.exe" ; cString := StartLogin (oRequest, cInputString)
CASE "login.exe" ; cString := StartLogin (oRequest, cInputString) // typo !!
OTHERWISE ; cString := WriteErrorHtml (cError, {"blabla",CRLF+StrTran(cInputString," ","&nbsp")})
END switch

will not compile.

Leave your last example as it is. You have two different funcion calls. I would say this is more elegant now. You may try if switch works in this case. I don't know.
But you can do this with if elseif else endif. For me it would be more clear what you want to do. Don*t forget else or otherwise. Fill with NOP if nothing to do.

HTH Frank