By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Why Golf’s Error Checks Might Save You From Yourself | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Why Golf’s Error Checks Might Save You From Yourself | HackerNoon
Computing

Why Golf’s Error Checks Might Save You From Yourself | HackerNoon

News Room
Last updated: 2025/04/21 at 1:33 PM
News Room Published 21 April 2025
Share
SHARE

When talking about safety in programming, it is memory safety that usually takes the spotlight. However, here we’ll touch on another common safety issue that typically causes application logic errors, meaning your program won’t function correctly. It’s about checking the status of any kind of statement that provides it. Not checking the status could mean that the result of a statement isn’t correct, yet your program is proceeding as if it were. Of course, this is bound to cause problems, and this is true for any programming language.

Golf now has a built-in mechanism to help prevent issues like this. The approach taken (at this time at least) is not to force the developer to check status for every statement. Rather if status isn’t checked, your program will stop in an orderly fashion and tell you exactly in which source file and which line number you didn’t check the status that may cause an issue. It’s a default fine line between code bloat and safety. Not every status needs to be checked. And this approach helps pinpoint the most pressing spots in your code where failure is the most likely to happen.

Another future enhancement is in the queue, which would allow (as an option) to force status checking on all statements.

Put the following code in file “file-name.golf” in a separate directory:

 %% /file-name public
     silent-header // don't output HTTP header
     get-param fname // get file name we'll read
     change-dir run-dir // change to directory we ran this program from
     read-file fname to file_content // read file
     // Transform file contents
     match-regex "File: ([0-9]+).imp" in file_content replace-with "\1" result res cache
     // Print out the result
     p-out res
 %%

This will take file name (“fname”) as an input parameter, read that file, and transform any text in it that starts with “File: ” followed by a file name that’s made up of digits (without an extension), and outputs those file names. So a bit contrived example, and arguably the point here has nothing to do with matching regex patterns. But it’s an example from a real-world application, so I plugged it in just the same.

So, let’s say the input file is “myfile” with contents:

File: 99281.imp
File: 3716243.imp
File: 124.imp

To test this, let’s create an application:

gg -k file-transform

Make the application:

gg -q

Run it:

gg -r --req="/file-name/fname=myfile" --exec

And the output is, as it should be:

99281
3716243
124

Okay, so far so good. But let’s run this program by specifying a non-existent file:

gg -r --req="/file-name/fname=nonexistent" --exec

The output is:

File [file-name.golf], line [5], Statement failed without status being obtained and/or checked [-1]

Aha! So your program will safely stop as soon there’s a failure in code where you didn’t check for status, namely this line (number 5) from the code above:

 read-file fname to file_content // read file

Now, let’s add status and check it, so replace “file-name.golf” with this:

 %% /file-name public
     silent-header // don't output HTTP header
     get-param fname // get file name we'll read
     change-dir run-dir // change to directory we ran this program from
     read-file fname to file_content status st// read file
     if-true st not-equal GG_OKAY
         @Sorry, cannot open file <<p-out fname>>!
     end-if
     // Transform file contents
     match-regex "File: ([0-9]+).imp" in file_content replace-with "\1" result res cache
     // Print out the result
     p-out res
 %%

Compile it and run again:

gg -q
gg -r --req="/file-name/fname=nonexistent" --exec

And the result is:

Sorry, cannot open file nonexistent!

So now you can see how useful the automatic “static checking” feature is. It prevented unsafe execution (logic-wise) from moving forward, and provided you with a clear clue as to how to fix the issue. For more on Golf, go to golf-lang.com .

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Save $550 on the Anker Solix C1000 portable power bank and get ready for summer camping
Next Article Surround Yourself With Savings: The JBL Bar 9.1 Soundbar is Less Than Half Price
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Today's NYT Mini Crossword Answers for May 12 – CNET
News
Ex-Verizon customer has a cautionary tale for anyone planning to leave
News
Your Next Mechanical Keyboard Purchase Could Come With a Hefty FedEx Tariff Bill
News
Upgrade your PC’s operating system to Windows 11 Pro for $15
News

You Might also Like

Computing

Shotcut 25.05 Video Editor Working Toward HDR Support

1 Min Read
Computing

DragonFlyBSD 6.4.2 Released With Fixes To Help QEMU & Chrome

1 Min Read
Computing

The HackerNoon Newsletter: Nurses Are Being Hired Like Ubers (5/11/2025) | HackerNoon

3 Min Read
Computing

Range Over Function Types: The New Feature in the Go 1.23 Release | HackerNoon

32 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?