Need a good example of a use of a guard statement Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Need a good example of a use of a guard statement

25 Sep 2020, 17:41

I know what the guard statements are. All I need is a good example which will satisfy the requirements below. This is for a manual I'm working on.

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"
}
but not the second, since we can easily change it to

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"
}
Any ideas?
Last edited by john_c on 25 Sep 2020, 21:15, edited 1 time in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Need a good example of a use of a guard statement

25 Sep 2020, 19:28

It sounds like a homework assignment for a course. Can I get college credit for this? I saw some examples of guard statements on Wikipedia. :D
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Need a good example of a use of a guard statement

25 Sep 2020, 19:52

@mikeyww I'm here from 2017. Are you sure it's a homework?

https://en.wikipedia.org/wiki/Guard_(computer_science) - I don't see any good example on Wikipedia. And sure, I'm aware of some really good articles on this topic, but examples there doesn't satisfy the second requirement as well.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Need a good example of a use of a guard statement

25 Sep 2020, 19:55

Not sure at all, and I won't discriminate based on your age! Sorry I'm not more helpful. Others here may have better info. But I think you are asking a general programming question, not really AHK?
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Need a good example of a use of a guard statement  Topic is solved

10 Oct 2020, 06:41

Just in case and to mark this thread as solved, here is the function I searched for:

Code: Select all

Capital(Country, CompleteSentence)
{
    Country := GetCountry(Country)
    If (Country == "")
        Return "Information about the country is missing"

    Capital := GetCapital(Country)
    If (Capital == "")
        Return "Information about the capital is missing"

    If (CompleteSentence)
        Message := "The capital of" . A_Space . Country . A_Space
            . "is" . A_Space . Capital
    Else
        Message := Capital
    Return Message
}
If you remove guards, you will end up with ugly and deep nesting (see examples below). The situation will be even worse if you have MainStreetOfCapital() function. There will be three (not two) guards and hence, if you remove them, even deeper nesting.

Code: Select all

MainStreetOfCapital(Country, CompleteSentence)
{
    Country := GetCountry(Country)
    If (Country == "")
        Return "Information about the country is missing"

    Capital := GetCapital(Country)
    If (Capital == "")
        Return "Information about the capital is missing"

    MainStreet := GetMainStreet(Capital)
    If (MainStreet == "")
        Return "Information about the main street is missing"

    ...
}

MainStreetOfCapital("United Kingdom", False)  ; => "Oxford Street"
Ugly versions:

Code: Select all

Capital(Country, CompleteSentence)
{
    Country := GetCountry(Country)
    If (Country != "") {
        Capital := GetCapital(Country)
        If (Capital != "") {
            If (CompleteSentence)
                Message := "The capital of" . A_Space . Country . A_Space
                    . "is" . A_Space . Capital
            Else
                Message := Capital
        }
        Else
            Message := "Information about the capital is missing"
    }
    Else
        Message := "Information about the country is missing"
    Return Message
}

Code: Select all

Capital(Country, CompleteSentence)
{
    Country := GetCountry(Country)
    If (Country == "")
        Message := "Information about the country is missing"
    Else {
        Capital := GetCapital(Country)
        If (Capital == "")
            Message := "Information about the capital is missing"
        Else {
            If (CompleteSentence)
                Message := "The capital of" . A_Space . Country . A_Space
                    . "is" . A_Space . Capital
            Else
                Message := Capital
        }
    }
    Return Message
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, peter_ahk and 324 guests