## 1960: "Code Golf"

This forum is for the individual discussion thread that goes with each new comic.

Moderators: Moderators General, Prelates, Magistrates

herbstschweigen
Posts: 41
Joined: Mon Jul 03, 2017 11:03 am UTC
Location: Arrival City

### 1960: "Code Golf"

Title text: "I also enjoy Reverse Regular Golf. I've been playing for years all across the country and I'm still on the first hole."

Reverse Regular Golf, is that standing with your putter next to the hole and waiting for the ball to jump out? And, shouldn't he start at hole 18 then? (or 9, depending on the course.)
It's cooler up here.

cellocgw
Posts: 1967
Joined: Sat Jun 21, 2008 7:40 pm UTC

### Re: 1960: "Code Golf"

herbstschweigen wrote:
Reverse Regular Golf, is that standing with your putter next to the hole and waiting for the ball to jump out? And, shouldn't he start at hole 18 then? (or 9, depending on the course.)

I suspect you're Golf-trolling, but for those who are mercifully ignorant of this horrible "game," [meaning actual club-and-tiny-ball golf] Reverse Golf is to attempt to get the highest possible score (number of strokes).

As to Code Golf -- it's sort of fun and a useful exercise when learning a new language, but seeing as Golfers regularly create a new language or new 'builtin' functions to reduce their score, it's kind of a lame competition.
https://app.box.com/witthoftresume
Former OTTer
Vote cellocgw for President 2020. #ScienceintheWhiteHouse http://cellocgw.wordpress.com
"The Planck length is 3.81779e-33 picas." -- keithl
" Earth weighs almost exactly π milliJupiters" -- what-if #146, note 7

roderik
Posts: 24
Joined: Fri Apr 10, 2009 4:16 pm UTC
Location: Below sealevel

### Re: 1960: "Code Golf"

cellocgw wrote:As to Code Golf -- it's sort of fun and a useful exercise when learning a new language, but seeing as Golfers regularly create a new language or new 'builtin' functions to reduce their score, it's kind of a lame competition.

Actually that is one of those loopholes that are forbidden by default(creating a new language) and built-in functions are often disallowed in the specific challenge itself.

karhell
Posts: 687
Joined: Wed Jun 19, 2013 4:56 pm UTC
Location: Breizh

### Re: 1960: "Code Golf"

Unless I severely misunderstood something, reverse-code-golf is usually called code bowling.
AluisioASG wrote:191 years ago, the great D. Pedro I drew his sword and said: "Indent thy code or die!"
lmjb1964 wrote:We're weird but it's okay.
ColletArrow, katakissa, iskinner, thunk, GnomeAnne, Quantized, and any other Blitzers, have fun on your journey!

rhhardin
Posts: 71
Joined: Fri Apr 09, 2010 2:11 pm UTC

### Re: 1960: "Code Golf"

Our code contest was the slowest sort.

orthogon
Posts: 3019
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

### Re: 1960: "Code Golf"

Is "return 1+thing" another Yoda-style idiom like the stupid "if 1==x" nonsense?
xtifr wrote:... and orthogon merely sounds undecided.

Justin Lardinois
Posts: 51
Joined: Wed Aug 26, 2015 4:47 pm UTC

### Re: 1960: "Code Golf"

Isn't "reverse code golf" just called Java?

Draco18s
Posts: 86
Joined: Fri Oct 03, 2008 7:50 am UTC

### Re: 1960: "Code Golf"

Justin Lardinois wrote:Isn't "reverse code golf" just called Java?

You can golf in Java. Your score is only going to be so good, though, but your score is only compared to other Java entries.
Heck, you can golf in Redstone too

In any case, the thing Randal is doing here isn't Code Golf, it's Code Bowling. The problem is there's no objective winning criteria.

For example I can "out-score" Randal trivially:

Code: Select all

`define thisIsPointlesslyLong = 1 - 1define callMeIshmaelSomeYearsAgoNeverMindHowLongPrecisely():        return thisIsPointlesslyLong`

Which is why the mousover is what it is: he's still on his first hole in golf, because the more strokes he takes before finishing makes him score higher. If he takes a stroke that doesn't cause him to land the ball in the hole, he's objectively incentivized to never land the ball in the hole.

ucim
Posts: 6627
Joined: Fri Sep 28, 2012 3:23 pm UTC

### Re: 1960: "Code Golf"

orthogon wrote:Is "return 1+thing" another Yoda-style idiom like the stupid "if 1==x" nonsense?
What is the "if 1==x" nonsense?

How else would you compare x to one? Unless you mean it should be "if x==1", for which I'm agnostic (it's just what I'm used to, and I can see the benefit of the "1==x" thing).

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

rmsgrey
Posts: 3510
Joined: Wed Nov 16, 2011 6:35 pm UTC

### Re: 1960: "Code Golf"

ucim wrote:
orthogon wrote:Is "return 1+thing" another Yoda-style idiom like the stupid "if 1==x" nonsense?
What is the "if 1==x" nonsense?

How else would you compare x to one? Unless you mean it should be "if x==1", for which I'm agnostic (it's just what I'm used to, and I can see the benefit of the "1==x" thing).

Jose

There's a general default for comparisons to be variable==constant rather than the other way around (and when comparing two variables, for the more volatile to be compared to the less volatile) so, while "1==x" is functional as code, and may well get compiled to exactly the same operations as "x==1" does (depending on how optimising the compiler is, and whether there's any advantage to either order), it's not as readable, so less good under most circumstances.

There are situations where "1==x" is defensible - if the 1 is a placeholder that will be replaced by an expression later, or the code has been modified from an earlier version where there was an expression present (though optimising the expression down to 1 is a questionable decision - most code-level optimisation is premature). Similarly, return 1 + stupidly_long_multi_line_variable_name_that_goes_on_and_on_and_on_for_far_too_long is more readable than return stupidly_long_multi_line_variable_name_that_goes_on_and_on_and_on_for_far_too_long + 1 because the important stuff (that you're returning a value 1 more than something) is all in one place, rather than having the "return" on one line and the "one more than" on another.

ucim
Posts: 6627
Joined: Fri Sep 28, 2012 3:23 pm UTC

### Re: 1960: "Code Golf"

The point of 1==x rather than x==1 is to avoid bugs of the "oops, I typed = rather than ==" variety.
x=1 will compile (it's an assignment).
1=x will not. (You can't assign to an immutable).

So, it catches a particular kind of common bug.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

rmsgrey
Posts: 3510
Joined: Wed Nov 16, 2011 6:35 pm UTC

### Re: 1960: "Code Golf"

ucim wrote:The point of 1==x rather than x==1 is to avoid bugs of the "oops, I typed = rather than ==" variety.
x=1 will compile (it's an assignment).
1=x will not. (You can't assign to an immutable).

So, it catches a particular kind of common bug.

Jose

Ah. I'd not encountered that reason before.

Yeah, that checks out, and should probably have been the convention from the start.

Flumble
Yes Man
Posts: 2110
Joined: Sun Aug 05, 2012 9:35 pm UTC

### Re: 1960: "Code Golf"

rmsgrey wrote:
ucim wrote:The point of 1==x rather than x==1 is to avoid bugs of the "oops, I typed = rather than ==" variety.
x=1 will compile (it's an assignment).
1=x will not. (You can't assign to an immutable).

So, it catches a particular kind of common bug.

Jose

Ah. I'd not encountered that reason before.

Yeah, that checks out, and should probably have been the convention from the start.

Nononononnonoo! The convention should've been "don't write a language where you can perform an assignment within an expression area" from the start. Allowing it makes code less readable and more error-prone in nearly all cases, if not literally all, if not factually all.
Anyway, we got a topic for this specifically. Weird it's not in Religious Wars.

cellocgw wrote:but seeing as Golfers regularly create a new language or new 'builtin' functions to reduce their score, it's kind of a lame competition.

I wonder if there's a language that consistently gets top scores and whether it (well, its programming concepts; of course it needs some syntax changes to be useful) would perform well as general-purpose language.

Bloopy
Posts: 214
Joined: Wed May 04, 2011 9:16 am UTC
Location: New Zealand

### Re: 1960: "Code Golf"

I wonder if Inverse Code Golf would work slightly better. Write a seemingly pointless program and see if anyone can come up with a problem that it solves.

ucim
Posts: 6627
Joined: Fri Sep 28, 2012 3:23 pm UTC

### Re: 1960: "Code Golf"

Flumble wrote:The convention should've been "don't write a language where..."
Unfortunately, end-user-programmers don't get to design their own language - they use the languages that are already developed. You go to war with the army you have.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

herbstschweigen
Posts: 41
Joined: Mon Jul 03, 2017 11:03 am UTC
Location: Arrival City

### Re: 1960: "Code Golf"

cellocgw wrote:
herbstschweigen wrote:
Reverse Regular Golf, is that standing with your putter next to the hole and waiting for the ball to jump out? And, shouldn't he start at hole 18 then? (or 9, depending on the course.)

I suspect you're Golf-trolling, but for those who are mercifully ignorant of this horrible "game," [meaning actual club-and-tiny-ball golf] Reverse Golf is to attempt to get the highest possible score (number of strokes).

No, I'm just ignorant enough of golf to have never heard of this. What I wrote was my first association with "reverse golf". There could be a macro quantum phenomenon that allows a ball in the hole to tunnel through the gravity barrier after all (and thus jump out).

Myself I have only played miniature golf, and Wii Sports golf (which I liked), but never tried the real thing, even though I'm a bit curious and quite regularly travel to Scotland.
It's cooler up here.

herbstschweigen
Posts: 41
Joined: Mon Jul 03, 2017 11:03 am UTC
Location: Arrival City

### Re: 1960: "Code Golf"

rmsgrey wrote:
ucim wrote:The point of 1==x rather than x==1 is to avoid bugs of the "oops, I typed = rather than ==" variety.
x=1 will compile (it's an assignment).
1=x will not. (You can't assign to an immutable).

So, it catches a particular kind of common bug.

Jose

Ah. I'd not encountered that reason before.

Really? Being a C veteran, I'd not encountered any other reason before, and even though I've been programming Java for 10+ years now, "if (x==1)" still looks dangerous to me. And even though I really agree with:
Flumble wrote:Nononononnonoo! The convention should've been "don't write a language where you can perform an assignment within an expression area" from the start. Allowing it makes code less readable and more error-prone in nearly all cases, if not literally all, if not factually all.
It's cooler up here.

Tub
Posts: 417
Joined: Wed Jul 27, 2011 3:13 pm UTC

### Re: 1960: "Code Golf"

ucim wrote:
Flumble wrote:The convention should've been "don't write a language where..."
Unfortunately, end-user-programmers don't get to design their own language[...]

End users can choose to enable compiler flags that turn accidental assignments into warnings or errors. For scripting languages, the same can be done with a linter, which can be integrated in your IDE, your bundling/publishing scripts, a pre-commit hook, your CI or whereever.

This is not an issue that needs to be solved by yoda'ing your code.

cellocgw
Posts: 1967
Joined: Sat Jun 21, 2008 7:40 pm UTC

### Re: 1960: "Code Golf"

Bloopy wrote:I wonder if Inverse Code Golf would work slightly better. Write a seemingly pointless program and see if anyone can come up with a problem that it solves.

I believe the winner in that category is named "Facebook"
https://app.box.com/witthoftresume
Former OTTer
Vote cellocgw for President 2020. #ScienceintheWhiteHouse http://cellocgw.wordpress.com
"The Planck length is 3.81779e-33 picas." -- keithl
" Earth weighs almost exactly π milliJupiters" -- what-if #146, note 7

ucim
Posts: 6627
Joined: Fri Sep 28, 2012 3:23 pm UTC

### Re: 1960: "Code Golf"

Tub wrote:End users can choose to enable compiler flags that turn accidental assignments into warnings or errors.
How does it know which ones are accidental? Might as well let the compiler write my code.

Jose
Order of the Sillies, Honoris Causam - bestowed by charlie_grumbles on NP 859 * OTTscar winner: Wordsmith - bestowed by yappobiscuts and the OTT on NP 1832 * Ecclesiastical Calendar of the Order of the Holy Contradiction * Please help addams if you can. She needs all of us.

Solra Bizna
Posts: 55
Joined: Fri Dec 04, 2015 6:44 pm UTC

### Re: 1960: "Code Golf"

ucim wrote:
Tub wrote:End users can choose to enable compiler flags that turn accidental assignments into warnings or errors.
How does it know which ones are accidental? Might as well let the compiler write my code.

GCC asks you to wrap the assignment in an additional set of parentheses if you really meant to do it. This also makes someone reading your code more likely to look a little closer. Which, of course, still leaves us with:
Flumble wrote:Nononononnonoo! The convention should've been "don't write a language where you can perform an assignment within an expression area" from the start. Allowing it makes code less readable and more error-prone in nearly all cases, if not literally all, if not factually all.

(Though I personally think the slightly-orthogonal "don't write a language where assignment can be confused with comparison" is more important.)

speising
Posts: 2308
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

### Re: 1960: "Code Golf"

yeah, i don't know who ever came up with = as assignment anyway. couldn't have been a mathematician. something like <- would have been a lot more logical and unambigous.

WanderingLinguist
Posts: 237
Joined: Tue May 22, 2012 5:14 pm UTC
Location: Seoul
Contact:

### Re: 1960: "Code Golf"

The first language I wrote production code in used ⇐ for assignment.

That was back before the days of Unicode; the editor (dare I call it an IDE?) used a special code page.

Other special characters used in the language were ≤ ≥ ≠ (rather than <= >= and !=) and the tricolon ⁝ as a delimiter (to allows ; , and : to be used in strings without quoting), and something I can't find in Unicode but that looked something like 【 this 】 for substituting variable into string literals.

While ⇐ could be used in an expression in some cases, it was different enough to type on the keyboard from a = (which was used for comparison) that no problems ever came up.

It was also used for loops:

Code: Select all

`loop i ⇐ 1, 5`

would loop over the indices 1,2,3,4,5 inclusive.

Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 3816
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

### Re: 1960: "Code Golf"

WanderingLinguist wrote:The first language I wrote production code in used ⇐ for assignment.

Pascal uses := for assignment. Also, I think, was never loosely-powerful enough to allow a straight if a:=b then /* assigned */ else /* not assigned */ endif construct to actually attempt this kind of thing.

Which is what is being asked for, above, but (if your version of Pascal or Delphi or Lazarus allows overloading of assignment, to attempt cross-type reassignment) makes for a loss of handy functionality that you have to replace with a wrapper-function to use as if MakdeEqual(x,y) (MadeEqual(A, B) being."Is A equal to B, already? If so, return True (might as well, uness there's a good reason why not). If not, try making A equal to B. Is A now equal to B? If so, then return True. If not, return False". Remember you may need to explicitly pass references. Been decades since I Pascalled, though, and at least five years even since I last used Delphi/Lazarus.)

chalkie
Posts: 44
Joined: Wed Feb 24, 2010 10:03 am UTC
Location: River Trent, UK

### Re: 1960: "Code Golf"

I write in VHDL, and that uses <= for signal assignment. Because that won't get anyone confused

orthogon
Posts: 3019
Joined: Thu May 17, 2012 7:52 am UTC
Location: The Airy 1830 ellipsoid

### Re: 1960: "Code Golf"

Yes, Pascal's ":=" is my favourite. "<=" is nice visually but it already means something else, and "<-" looks rubbish in Courier-like fonts because the minus is too small and doesn't line up with the middle of the "<". And again, the minus sign could be misconstrued. I always thought that if I wrote a language it would be ":=" for assignment and "==" for comparison; "=" would be a syntax error.

Also, having started in Sinclair BASIC, I always think of "x=1" as being shorthand for "LET x=1"; i.e. the "LET" is implicit. Including the "LET" makes statements like "x=x+1" a little less mathematically jarring.

The C assignment-in-conditional thing is possible because of C's "everything is an expression" principle. It has made me view with extreme suspicion any language where the textbook says "everything is an X!" as if that's a good thing.
xtifr wrote:... and orthogon merely sounds undecided.