Re: New switch/case idea

new topic     » goto parent     » topic index » view thread      » older message » newer message
bill said...

Reading the comments it seems to me that using a fall-through C-type switch is going away from well-structured code. I have mostly programmed in Pascal-type languages (Ada, PL/SQL etc) where sturctures like this do not exist.

It is hard to see how something like Duff's Device is needed in a high-level language. (It is also difficult to see value in a GOTO statement.)

Being able to read my code 6 months later is more important to me than raw speed or extremely succint code.

Obviously not everyone agrees.

What you are able to read is what you know. Someone from from another language may come to ours and get totally tripped up the idea of a sequence, but you've been using them, thus they are easy. switch has some serious value, just look at Euphoria's source code. Switch has made life much, much easier maintaining Euphoria itself, both in the C backend and in the Euphoria front end.

Spend a little time working with it and I'm sure that you will agree.

It's like the while statement. There was a time, when it was first introduced, that people were baffled around it. They cried it's unnecessary, it'll never make it, it's so hard to follow, we have goto, there is no need for a while statement, you're just complicating the language!

switch is the same thing. Learn it and you'll love it. Almost always, if you are testing one condition, a switch is a much cleaner interface, so much easier to read than an if statement. If statements are for multiple (or complex) tests, switches are for single tests. Here's what I mean:

-- This is the reason to use an if statement: 
if validOption(optName) then 
    -- handle option 
elsif equal("--", optName) and has_verbose = TRUE then 
    -- do something 
elsif not has_shown_help and unknownOption(optName) then 
    -- show help and set has_shown_help flag 
elsif open_database_log_file(LOG_FILE) = ERROR then 
    -- exit the app with an error message 
end if 

So, in that I have multiple conditions on different values, use an if. Now, here is a valid use of a switch statement:

switch optName do 
    case "--blue", "--yellow", "--red", "--purple", "--black", "--green", "--orange" then 
        -- set the color of the background 
 
    case "--log-file", "-log-file" then 
        -- set the log file name 
 
    case "--verbose", "--max-logging" then 
        -- turn on verbose logging 
 
    case else 
        -- show help and exit, invalid option 
end switch 

Now, look how readable the switch statement is. Let's rewrite that exact simple switch in an if statement:

if find(optName, { "--blue", "--yellow", "--red", "--purple", "--black", "--green", "--orange" }) then 
    -- set the color of the background 
elsif equal(optName, "--log-file") or equal(optName, "-log-file") then 
    -- set the log file name 
elsif equal(optName, "--verbose") or equal(optName, "--max-logging") then 
    -- turn on verbose logging 
else 
    -- show help and exit, invalid option 
end if 

Which one is more clear, let typing and easier to add to, subtract from? Now, this is an easy example, you can do things differently to make both easier to understand, but it's just an example. As your application becomes more complex, the switch statement gains in clarity exponentially.

Jeremy

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu