-
1
- #1
Joe Crescenzi
Programmer
I've never been impressed by the hype over AI, and until recently every time I've tested AI by asking it to write code, I've never actually been given code that actually works.
I did two recent tests to see if Grok could write something that mirrors something simple, then something more complex. In both cases, I gave it explicit instructions about what I needed and within seconds, I got code that not only worked the first time, but it even gave me sample calls that I could use to test it.
Here's what I asked Grok to do a few minutes ago:
To be honest, I thought that one would be too abstract to get an answer without it asking for more information, but a few seconds later, this is what it gave me:
As I said, it even gave me test code:
I saved both parts to PRGs, then ran it and the numbers were accurate.
The second program was even more complex, and once again, it worked without any modifications.
All I can say is I'm no longer a doubter. AI can actually write code, provided you know how to properly request what you want, which makes me wonder if the programmers of the future will focus entirely on writing good specifications, rather than the code itself.
Just keep in mind that bad specifications and bad design will still yield bad results.
Anyone else try AI lately?
I did two recent tests to see if Grok could write something that mirrors something simple, then something more complex. In both cases, I gave it explicit instructions about what I needed and within seconds, I got code that not only worked the first time, but it even gave me sample calls that I could use to test it.
Here's what I asked Grok to do a few minutes ago:
I need to build a foxpro function that can calculate depreciation for fixed assests up to a given date . I would like the function to accept parameters for the initial price (Price), Date of Purchase (PurchDate), Lifespan in years (Life), the effective date, and the method of calculation (CalcMethod). The calculation methods are either "SL" for StraightLine, "ACRS" for Accelerated Cost Recovery System, or "DDB" for Diminishing Balance Method.
To be honest, I thought that one would be too abstract to get an answer without it asking for more information, but a few seconds later, this is what it gave me:
Code:
FUNCTION CalcDepreciation
PARAMETERS Price, PurchDate, Life, EffDate, CalcMethod
* Validate parameters
IF TYPE('Price') != 'N' OR TYPE('PurchDate') != 'D' OR ;
TYPE('Life') != 'N' OR TYPE('EffDate') != 'D' OR ;
TYPE('CalcMethod') != 'C'
RETURN "Invalid parameter type"
ENDIF
IF Price < 0 OR Life <= 0
RETURN "Invalid parameter value"
ENDIF
* Convert CalcMethod to uppercase for consistency
CalcMethod = UPPER(CalcMethod)
* Calculate total days between purchase and effective date
LOCAL lnDays, lnYears, lnDepreciation
lnDays = EffDate - PurchDate
lnYears = lnDays / 365.25 && Accounting for leap years
* If effective date is before purchase date, return 0
IF lnDays < 0
RETURN 0
ENDIF
* Calculate depreciation based on method
DO CASE
CASE CalcMethod = "SL" && Straight Line
* Annual depreciation = Price / Life
* Total depreciation = Annual depreciation * years
lnDepreciation = MIN(Price, (Price / Life) * lnYears)
CASE CalcMethod = "ACRS" && Accelerated Cost Recovery System
* Simple ACRS implementation (using sample percentages)
LOCAL lnRate
DO CASE
CASE Life <= 3
lnRate = 0.3333 && 3-year property
CASE Life <= 5
lnRate = 0.20 && 5-year property
CASE Life <= 10
lnRate = 0.10 && 10-year property
OTHERWISE
lnRate = 0.05 && 15-year+ property
ENDCASE
lnDepreciation = MIN(Price, Price * lnRate * lnYears)
CASE CalcMethod = "DDB" && Double Declining Balance
* Double declining rate = 2 / Life
LOCAL lnRate, lnBookValue
lnRate = 2 / Life
lnBookValue = Price
lnDepreciation = 0
* Calculate for each year up to effective date
FOR i = 1 TO FLOOR(lnYears)
lnDepreciation = lnDepreciation + (lnBookValue * lnRate)
lnBookValue = lnBookValue - (lnBookValue * lnRate)
ENDFOR
* Add partial year if needed
IF lnYears > FLOOR(lnYears)
lnDepreciation = lnDepreciation + ;
(lnBookValue * lnRate * (lnYears - FLOOR(lnYears)))
ENDIF
lnDepreciation = MIN(Price, lnDepreciation)
OTHERWISE
RETURN "Invalid calculation method"
ENDCASE
* Ensure depreciation doesn't exceed original price
RETURN ROUND(lnDepreciation, 2)
ENDFUNC
As I said, it even gave me test code:
Code:
* Example usage:
? CalcDepreciation(10000, DATE(2023,1,1), 5, DATE(2025,3,11), "SL")
* Returns straight-line depreciation for $10,000 asset over 5 years
? CalcDepreciation(5000, DATE(2022,6,1), 3, DATE(2025,3,11), "ACRS")
* Returns ACRS depreciation for $5,000 asset over 3 years
? CalcDepreciation(20000, DATE(2024,1,1), 10, DATE(2025,3,11), "DDB")
* Returns double declining balance depreciation for $20,000 asset
I saved both parts to PRGs, then ran it and the numbers were accurate.
The second program was even more complex, and once again, it worked without any modifications.
All I can say is I'm no longer a doubter. AI can actually write code, provided you know how to properly request what you want, which makes me wonder if the programmers of the future will focus entirely on writing good specifications, rather than the code itself.
Just keep in mind that bad specifications and bad design will still yield bad results.
Anyone else try AI lately?