Requirements, there are only two of them:
1. Apart from if statements which work as guards, the function must have a normal if-else statement.
2. It must be a function that cannot be changed to plain if-else if-else. That is, it should do something more tricky than simple decision making.
The following example satisfy the 1st requirement:
Code: Select all
GetFormatByExtension(Path)
{
; 1st guard
If (FileExist(Path) == "")
Return "The path doesn't exist"
; 2nd guard
If (InStr(FileExist(Path), "D") != 0)
Return "The path is a directory"
; 3rd guard
SplitPath, Path,,, Extension
If (Extension == "")
Return "The file has no extension"
; 4th guard
If Extension Not In odt,md,txt
Return "The file has unsupported extension"
; Normal if-else statement. Not a guard.
If (Extension = "odt")
Return "OpenDocument Text"
Else If (Extension = "md")
Return "Markdown"
Else
Return "Plain text"
}
Code: Select all
; Version without guards converted to long if-else statement.
GetFormatByExtension(Path)
{
SplitPath, Path,,, Extension
If (FileExist(Path) == "")
Return "The path doesn't exist"
Else If (InStr(FileExist(Path), "D") != 0)
Return "The path is a directory"
Else If (Extension == "")
Return "The file has no extension"
Else If Extension Not In odt,md,txt
Return "The file has unsupported extension"
Else If (Extension = "odt")
Return "OpenDocument Text"
Else If (Extension = "md")
Return "Markdown"
Else
Return "Plain text"
}