Tag Info

New answers tagged

3

If putting the functions chronologically improves the readability of the code, it means that you are using functions wrong. The primary purpose of dividing the code to function is to reduce the amount of context you need to hold in your mind at any given time. (functions have other usages when you use recursion or higher-order functions, but that's clearly ...


0

You would typically want UI strings like this to be pulled in from a string table instead of being hard-coded in the source code, for localization and ease of updating. So the approach I would take would be to use the inputs to build a lookup key, so something like: var lookupKey = "SALUTATION_" + gender1 + "_" + gender2; var format = ...


8

Titles really belong in the database, but you stated you have no control over this. You have not specified a language tag but the syntax is in the C family, so this will be pseudocode that is almost C++: map<string, string> titles; titles.emplace("M", "Sir"); titles.emplace("F", "Madam"); cout << "Dear " << titles[gender1] << " " ...


3

ratchet freak's answer is quite a good idea if the sentences are all the same pattern, but with two insets, one each only dependent on gender1 respective gender2. Phil W.'s answer is probably the most flexible answer as it allows explicit control over the greeting, though he's quite correct it's a radical change. You might not have the data in that form. ...


17

Radical solution: Let the user specify their own title (from a predefined list that you provide). Your solution (as viewed through English eyes) only appears to cater for Lords ("Sir") and ladies; most men would be addressed as "Mr", most women as either "Miss", "Mrs" or "Ms", depending on their marital status and personal opinions. Then there's a whole ...


2

If your language allows you to do it, you can write switch(gender1+gender2) { case "MM": print "Dear Sirs"; break; case "MF": case "FM": print "Dear Sir and Madam"; break; ... It's not necessarily better than your version, since there's still duplication, but it does avoid the nested switch.


29

Add the title to the parameters of the printf: char* title1; switch(gender1){ case 'M': title1 = "Sir"; break; case 'W': title1 = "Madam"; break; case ...etc. } char* title2; switch(gender2){ case 'M': title2 = "Sir"; break; case 'W': title2 = "Madam"; break; case ...etc. ...



Top 50 recent answers are included