-4

I could imagine the below code being somewhat useful. Is there a reason this pattern hasn't made it into programming languages?

To be clear the string was just what I chose as an example you could substitute near any type you like for the input and my question would still apply.

public string switch findLastName(string firstName)
{
    case "John":
        return "Johnson";
    case "Susan":
        return "Stevens";
    case "Tyler":
        return "Gomez";
    default:
        return "Smith";
}

Instead of:

public string findLastName(string firstName)
{
    switch(firstName)
    {
        case "John":
            return "Johnson";
        case "Susan":
            return "Stevens";
        case "Tyler":
            return "Gomez";
        default:
            return "Smith";
    }
}

closed as primarily opinion-based by GrandmasterB, user40980, gnat, Bart van Ingen Schenau, GlenH7 Oct 24 '15 at 17:00

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    This could easily be a function by adding 1 extra line; just write switch(firstname) at the top – Richard Tingle Oct 23 '15 at 20:12
  • 1
    Eric Lippert on How many Microsoft employees does it take to change a lightbulb? – user40980 Oct 23 '15 at 20:16
  • 1
    Looks like a data structure that is hard-coded, and in code ;) – Erik Eidt Oct 23 '15 at 20:20
  • @Jack it does make sense, and it is a thing in some languages (personally, I don't like it, but that's me) - docs.oracle.com/javase/7/docs/technotes/guides/language/… . But that takes time to implement. Associated rationale for it - blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy – user40980 Oct 23 '15 at 20:23
  • @Jack The reason this isn't a thing in C is that C's switch statements are meant to map directly to machine instructions (or at least originally they were), and string comparison is not a single machine instruction. But in a language like Javascript, which copies a lot of C's syntax but has very different semantics, code like your example is totally valid. – Ixrec Oct 23 '15 at 20:28
7

Fundamentally it isn't in any language because it doesn't give you much over a function; the standard way to create a reusable "chunk" of code. For example your example can be created easily as follows

public string findId(string firstName) {
    switch(firstname) {
        case "John": return "Johnson"; 
        case "Susan": return "Stevens"; 
        case "Tyler": return "Gomez"; 
        default: return "Smith";
    }
}

(This particular use of a switch statement probably isn't a good idea but I understand that this is just an example)

If we are to support named switch statements should we also support named while and for loops, named if statements etc. All these can easily be implemented already as functions but in a far more flexible way

  • 1
    Thanks for yout answer. It's interesting. I see your point and I agree it would probably just complicate things unnecessarily. It's interesting think about named if's and while's and for's though. It almost seems like using generic ifs/whiles/for are like div soup in html and would be better on being able to be named. However, I have no experience creating language so I'll leave that to the pros :D – Jack Fraser Oct 23 '15 at 20:55
  • NB the OP has brought my example into the question but I think the answer remains valid – Richard Tingle Oct 24 '15 at 13:09
4

You mean a String switch?

Most language constructs translate directly to a few instructions.

However deciding which string a given one matches is more complicated. The simplest method is trying each one by one. The more complicated and memory intensive but faster way is using a prefix trie or hashmap (or hashtable) out of which you can get an integer which you can switch on. However there are several implementations of both and different hash functions, each with their own trade-offs; forcing a implementation-dependent one-size-fits-all on the programmer will not be a good thing™.

Or do you mean mapping a string (or any other object) to another value?

That is solved using (again) a prefix trie or hashmap (or hashtable).

As for why that type of mapping isn't built in...

It's a single line of code that you save for an idiom that is frankly not all that common. That's not worth worrying about.

  • 1
    Whoa cool. I never realized that a switch statement had more to do with being a low level shortcut than just being a fancy way of writing long if - else if - else statements – Jack Fraser Oct 23 '15 at 20:34
  • 2
    I don't thus this is what the OP means, i think the OP just happens to be using a string switch. I think the named nature of the switch statement is what the OP is asking about – Richard Tingle Oct 23 '15 at 20:35
  • @RichardTingle yes the string was just an example – Jack Fraser Oct 23 '15 at 20:35
  • @RichardTingle, ratchet freak's answer still stands. A switch is a cumbersome way of mapping keys to expressions; a hash map is more efficient. Why clutter a language with special case functions/methods when switch isn't a great tool in the first place? – David Arno Oct 24 '15 at 8:21
4

Many languages support a construct called pattern matching, and this can be used as a fairly close approximation to what you're doing there. For example, in Haskell (and the Haskell experts will have to forgive my syntax, because it's been a while since I did any work with it) your code looks like this:

findLastName :: string -> string
findLastName "John" = "Johnson"      
findLastName "Susan" = "Stevens"
findLastName "Tyler" = "Gomez"
findLastName x = "Smith"
0

It's not in the C language. One of the reasons is that in C, switch cases are compared for equality, and strings are pointers, so unless you introduce weird rules, strings would be compared with pointer comparison which would be absolutely useless ("John" == "John" can be either true or false in C, depending on the implementation).

For just one example of a much more powerful switch / case statement, you can download the Swift language book; there are many other languages with similar features. So your assumption that this hasn't made it into programming languages is totally wrong.

  • I believe the question is about the entire switch statement being named (like functions), not the switch happening to use strings – Richard Tingle Oct 24 '15 at 13:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.