Originally Posted by christop
And I wouldn't say it's using a function wrong when the user of the program can very easily and unintentionally trigger this undefined behavior in your program by including a real but unhandled Morse code character in the input, like "-..-." (which comes up often in amateur radio repeater IDs). It's easy enough to return something to indicate an unknown Morse code character on input. Just put return '?'; or similar at the end of morseToChar() (errors on input will simply be converted to "?" on output).
Besides, how can a function like decode() be expected to use morseToChar() the "right" way when it's working with a string that contains arbitrary user input? Should decode() ensure that every Morse symbol is valid before asking morseToChar() to convert it? I suppose decode() could scan through the Morse code symbol list and convert a symbol only if it's valid, but that's basically doing the same thing morseToChar() does! So yes, I would call the function flawed since it requires the caller to do practically the same work as the function. (Let me put it this way: Imagine if the C standard library function strstr() required the caller to check that the "haystack" string contained the "needle" string before it can call strstr(), otherwise Bad Things will happen. THAT would be a flawed function!)