Users can have a Cookie called lang that contains either the value "default=1" (English) or "default=2" (French). This cookie is used to determine their preferred language. I have a function that needs to take the cookie and return the language has an Enum. If the cookie doesn't exist, I want to return English. I cannot change the cookie value.

Language enum:

  Public Enum Language
Undefined = 0
English = 1
French = 2
End Enum


Function:

  Public Function GetLangFromCookie() As Language
End If
Return Language.English
End Function


I feel like this is not efficient and not secure but I don't know how to improve it. Any suggestions?

• You query HttpContext.Current.Request.Cookies.Get("lang") twice. Access it once and store it in a variable.
• You will get a problem if in the future you have to support more than 9 languages. Instead of accessing the last char from langCookie you should Split() langCookie by = and take the second array element. You can then use [Enum].TryParse() to get the enum.
• Some horizontal spacing (new lines) would help to easier read the code.

Your code could look like so

Public Function GetLangFromCookie() As Language

Return Language.English
Else
End If

End Function

Private Function GetLanguage(languageCookieValue As String) As Language