Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Zombywuf
Mar 29, 2008

Gazpacho posted:

While I understand that there are many design horrors in PHP, and to a lesser extent Javascript, this hand-wringing over implicit conversion reminds me of the arguments when I was learning BASIC against using = for assignment (as well as comparison), because some poor stupid programmer might flip out over a statement like X = X + 1.

There are people who freak out over that kind of statement. They don't go on to become programmers.

It is lovely syntax though, assignment should be done like this: MOV X, X + 1.

Adbot
ADBOT LOVES YOU

Volte
Oct 4, 2004

woosh woosh
What kind of a noob language are you using that has assignment?

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

geonetix posted:

You can already do it that way by using the "go-to" operator:

code:
for (int i = 10; i --> 0;;) { }
But all silliness aside, simply learn that PHP does coercive comparisons and work around it when dealing with them. Not saying it's nice, clean, helpful or anything. But this really is one of the basic things that you'll run into every day working with PHP.

Except when it violates its own prototype, like the money_format "bug" mentioned a few weeks ago. Then out selectively chooses not to coerce, without warning.

geonetix
Mar 6, 2011


The money_format one is a bit more complex, it used to coerce and then stop doing it. I'm not sure about that as I'm writing this from memory, but it was something incredibly stupid like that going on there.

My point just was that you have to learn what your language does when you want to use it for anything larger than displaying the result of 1+1.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

geonetix posted:

My point just was that you have to learn what your language does when you want to use it for anything larger than displaying the result of 1+1.

And my point is, some languages act logically, and don't decide to, one day, without any warning or notice, stop coercing types for a single function and then blame the user for not verifying he was passing a proper value to the function. I don't know if PHP is unique in this regard, or even rare, but that doesn't mean it's right or smart.

So, I have to learn it, yes, but that implies that what I've learned doesn't randomly change. When it does, that's no longer my fault, and is entirely worthy of complaint.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

geonetix posted:

My point just was that you have to learn what your language does when you want to use it for anything larger than displaying the result of 1+1.

Wow, that's so insightful. You should start a blog, man.

I don't even get your point unless its just the trivially obvious point that to use a language you have to know it, and if that's your point I don't know why you bothered typing it out.

If your point is to excuse it, then that's loving retarded because that excuses any retarded language design.

edit: That came off more abrasive than it should. I'm irritable this morning. :(

Thermopyle fucked around with this message at 18:08 on Sep 2, 2012

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


It's unprovable, but I'm pretty sure there is no language feature so terrible that you cannot find apologists for it.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with.

Sebbe
Feb 29, 2004

carry on then posted:

Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with.

:goonsay: It's pretty good for doing spring cleaning on your hard drive. You never get around to doing it yourself anyway. Besides, for anything important you would have external backups, so you can't really lose anything you couldn't live without if you're at all sensible.

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer

carry on then posted:

Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with.

No one is forcing you to use the addition operator. Instead of 1 + 2 you can simply do 1 - -2 instead.

qntm
Jun 17, 2009

carry on then posted:

Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with.

It's a logical extension of the Chaos Monkey concept. It encourages the development of fault-tolerant operating systems and robust security models.

geonetix
Mar 6, 2011


Thermopyle posted:

Wow, that's so insightful. You should start a blog, man.

I don't even get your point unless its just the trivially obvious point that to use a language you have to know it, and if that's your point I don't know why you bothered typing it out.

My point is that this kind of idiotic behaviour is indeed very commog in PHP and I reckon you have to know about it. The sudden number_format example excluded, because changing functionality without warning or anything is beyond comprehension. I was personally still going on about the whole 'lol "0.00" == 0' thing.

quote:

If your point is to excuse it, then that's loving retarded because that excuses any retarded language design.

edit: That came off more abrasive than it should. I'm irritable this morning. :(

No excusing it, just wondering why people are still amazed this happens.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

carry on then posted:

Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with.
It ensures that people remember to restrict the process's privileges appropriately.

darthbob88
Oct 13, 2011

YOSPOS

geonetix posted:

My point is that this kind of idiotic behaviour is indeed very common in PHP and I reckon you have to know about it. The sudden number_format example excluded, because changing functionality without warning or anything is beyond comprehension. I was personally still going on about the whole 'lol "0.00" == 0' thing.


No excusing it, just wondering why people are still amazed this happens.

I don't think anybody's amazed it happens, I at least have come to terms with the fact that PHP is utter rubbish. What is amazing is how utterly rubbish it is and in what ways, like the whole number_format thing, or the eternally popular "lol 0 =='0.00'", which I agree is starting to drag a little bit.

As far as your point goes, that PHP is acceptable so long as you're aware of its many failings and idiosyncrasies, the bastard offspring of a Yugo and a Ford Pinto would be a perfectly acceptable car, get you wherever you need to go so long as you remain aware of its idiosyncrasies. I'd still drop it in favor of a car that doesn't explode or break down every mile, and I'd drop PHP for a language that doesn't violate common sense and logic.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

geonetix posted:

My point is that this kind of idiotic behaviour is indeed very commog in PHP and I reckon you have to know about it. The sudden number_format example excluded, because changing functionality without warning or anything is beyond comprehension. I was personally still going on about the whole 'lol "0.00" == 0' thing.


No excusing it, just wondering why people are still amazed this happens.

I don't think (hope?) anyone is going around telling everyone how amazed they are at PHP's wackiness. This is the Coding Horrors thread, an appropriate place to post Coding Horrors.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

darthbob88 posted:

I don't think anybody's amazed it happens, I at least have come to terms with the fact that PHP is utter rubbish. What is amazing is how utterly rubbish it is and in what ways, like the whole number_format thing, or the eternally popular "lol 0 =='0.00'", which I agree is starting to drag a little bit.

I don't mind '0.00' equaling 0. What I mind is the intransitivity of type coercions, that 0 = false but '0.00' = true.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane

Golbez posted:

I don't mind '0.00' equaling 0. What I mind is the intransitivity of type coercions, that 0 = false but '0.00' = true.

Then ensure all if-statements use a boolean value instead of relying on type coercion, like Java. It's a questionable design choice, but there are valid reasons why PHP's type coercion leads to intransitivity, so as long as one knows about it, it shouldn't be a major drama.

nielsm
Jun 1, 2009



Golbez posted:

I don't mind '0.00' equaling 0. What I mind is the intransitivity of type coercions, that 0 = false but '0.00' = true.

The correct way to do it is having every value except nil and boolean false evaluate as true.

PT6A
Jan 5, 2006

Public school teachers are callous dictators who won't lift a finger to stop children from peeing in my plane

nielsm posted:

The correct way to do it is having every value except nil and boolean false evaluate as true.

Every type has a 0 value which evaluates to false. The only real inconsistency is that "0" is coerced to an integer.

I think there's a certain merit to Java's strategy of "MUST BE A BOOLEAN," because otherwise you end up with weird personal interpretations of what does and doesn't make sense.

One of the problems with PHP is that it's easy to write awful code, but the same is true in some degree for every language. The solution is to proactively use good coding practices to avoid writing lovely code, even if the language will allow you to do it.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I thought this was the coding horrors thread, not the language debate thread.

darthbob88
Oct 13, 2011

YOSPOS

Jabor posted:

I thought this was the coding horrors thread, not the language debate thread.

Tomato, tomato. One man's feature is another man's shambling horror, somebody else's idea of the Right Way to Do It is always wrong and horrifying. Speaking of, encoding options and flags in prime factorizations: good idea, or best idea?

Thel
Apr 28, 2010

darthbob88 posted:

Tomato, tomato. One man's feature is another man's shambling horror, somebody else's idea of the Right Way to Do It is always wrong and horrifying. Speaking of, encoding options and flags in prime factorizations: good idea, or best idea?

Best idea, especially if you don't document it and have wildly disparate options under the same prime 'key'.

Zombywuf
Mar 29, 2008

Has some government started funding people to defend PHP (and Yugos) on the internet or something?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I'm not sure if this is a horror or not, but I just wrote a testcase:

JavaScript code:
function callback_call_callback(callback) {
    callback(51, 52);
}

const VFuncTester = new Lang.Class({
    Name: 'VFuncTester',

    vfunc_vfunc_callback: callback_call_callback,
});

function testVFuncs() {
    let tester = new VFuncTester();
    let a, b;
    tester.invoke_vfunc_vfunc_callback(function(a_, b_) {
        [a, b] = [a_, b_];
    });
    assertEquals(a, 51);
    assertEquals(b, 52);
}
Language bindings are the best code to write. I implemented support for trampolines (ffi closures; they look like they're native pieces of code that actually call into the bindings, and I install them in the class struct as vfuncs) that can take callbacks.

So, I'm going:
  • binding (test runner)
  • native (vfunc invoker that calls the vfunc_vfunc_caller installed in the class struct)
  • binding (vfunc_vfunc_callback implementation)
  • native (the binding calls "callback" which looks like a native function. It's a native function trampoline for the function passed to the invoker)
  • binding (trampoline callback)
With all the arguments getting marshalled in both directions each way.

that awful man
Feb 18, 2007

YOSPOS, bitch

Zombywuf posted:

There are people who freak out over that kind of statement. They don't go on to become programmers.

Due to the influence of BCPL and B, in the earliest version of C, the & and | operators could either be bitwise operators that evaluate both arguments or short-circuiting logical operators depending on context. Ritchie added the && and || operators to clear things up, but there was already code that depended on & and | having lower precedence than == so operator precedence in C has been hosed ever since.

So no, it's not just people who "don't go on to become programmers" who worry about identical expressions having different semantics due to context.

And let's not even get started about IF IF = THEN THEN THEN = ELSE; ELSE ELSE = THEN; being a valid PL/I statement.

hobbesmaster
Jan 28, 2008

that awful man posted:

And let's not even get started about IF IF = THEN THEN THEN = ELSE; ELSE ELSE = THEN; being a valid PL/I statement.

And #define if fuckYouMaintenanceProgammer is valid C.

tef
May 30, 2004

-> some l-system crap ->
code:
#
/*
 *	UNIX shell
 *
 *	S. R. Bourne
 *	Bell Telephone Laboratories
 *
 */

#define LOCAL	static
#define PROC	extern
#define TYPE	typedef
#define STRUCT	TYPE struct
#define UNION	TYPE union
#define REG	register

#define IF	if(
#define THEN	){
#define ELSE	} else {
#define ELIF	} else if (
#define FI	;}

#define BEGIN	{
#define END	}
#define SWITCH	switch(
#define IN	){
#define ENDSW	}
#define FOR	for(
#define WHILE	while(
#define DO	){
#define OD	;}
#define REP	do{
#define PER	}while(
#define DONE	);
#define LOOP	for(;;){
#define POOL	}


#define SKIP	;
#define DIV	/
#define REM	%
#define NEQ	^
#define ANDF	&&
#define ORF	||

#define TRUE	(-1)
#define FALSE	0
#define LOBYTE	0377
#define STRIP	0177
#define QUOTE	0200

#define EOF	0
#define NL	'\n'
#define SP	' '
#define LQ	'`'
#define RQ	'\''
#define MINUS	'-'
#define COLON	':'
:3:

MarsMattel
May 25, 2001

God, I've heard about those cults Ted. People dressing up in black and saying Our Lord's going to come back and save us all.
In C++, stick this in a global/force include:

code:
#define private public
#define protected public

Hughlander
May 11, 2005

MarsMattel posted:

In C++, stick this in a global/force include:

code:
#define private public
#define protected public

I've seen that and in the same file was:

code:
#define RW
#define RO const
(And it was used at the tail end of a function prototype.)

MarsMattel
May 25, 2001

God, I've heard about those cults Ted. People dressing up in black and saying Our Lord's going to come back and save us all.
I first heard of it being used to help a script system integrate with a game. Something about the scripting system needing access to data it otherwise wouldn't be able to access...

tractor fanatic
Sep 9, 2005

Pillbug

MarsMattel posted:

In C++, stick this in a global/force include:

code:
#define private public
#define protected public

What good is that without

code:
#define class struct

pepperoni and keys
Sep 7, 2011

I think about food literally all day every day. It's a thing.
I always found the best way to screw with someones code to be
code:
#define while if
or

code:
#define return //

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

I've always been partial to

Lisp code:
(define + -) ; yeah yeah yeah it's not C 

hobbesmaster
Jan 28, 2008

tractor fanatic posted:

What good is that without

code:
#define class struct

Unlike some of the others, thats vaguely legit if you're doing unit tests.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

Golbez posted:

I don't mind '0.00' equaling 0. What I mind is the intransitivity of type coercions, that 0 = false but '0.00' = true.

I'd really be interested in seeing some real, in-the-wild example of how this tripped someone up. People complain about it a lot, but PHP type coercion has never done anything but save me time.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
Bug #54547: wrong equality of string numbers

quote:

I'm just gonna paste in that PHP Sadness article to show why this is such a big
issue.

According to php language.operators.comparison, the type-coercing comparison
operators will coerce both operands to floats if they both look like numbers,
even if they are both already strings:

If you compare a number with a string or the comparison involves numerical
strings, then each string is converted to a number and the comparison performed
numerically.
This can become especially important in situations where the developer chooses
to use == to compare two values which will always be strings. For example,
consider a simple password checker:

if (md5($password) == $hash) {
print "Allowed!\n";
}

Assume that the $hash is loaded from a known safe string value from a database
and contains a real MD5 hash. Now, suppose the $password is "ximaz", which has
an all-numeric hex-encoded MD5 hash of "61529519452809720693702583126814". When
PHP does the comparison, it will print "Allowed!" for any password which matches
even the first half of the hash:

$ php -r 'var_dump("61529519452809720693702583126814" ==
"61529519452809720000000000000000");'
bool(true)

The solution, of course, is "never use type-coercing comparison operators" - but
this remains an easily-overlooked bug factory for beginning and even
intermediate developers. Some languages solve this situation by having two
separate sets of comparison operators for numeric or string comparisons so that
the developer can be explicit in their intent without needing to manually cast
their arguments.

Comparing two strings and finding that they're tested for numeric equality is utterly ridiculous.

zergstain
Dec 15, 2005

code:
#define sizeof(x) rand()

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Lysidas posted:

Bug #54547: wrong equality of string numbers


Comparing two strings and finding that they're tested for numeric equality is utterly ridiculous.

Apparently just '0xFF' == '255' is true. That is absurd.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Tiny Bug Child posted:

I'd really be interested in seeing some real, in-the-wild example of how this tripped someone up. People complain about it a lot, but PHP type coercion has never done anything but save me time.

I had a function to query sums from a database, and then, based on a flag passed to the function, either include only non-zero sums or zero sums. Yes, in retrospect I suppose this could have been done several ways, like in the query itself, but eh.

So I decided, for 'include non-zeros', to loop through and use array_filter() without a callback, to trim out all the zero entries. However, because the MySQL field I was summing was a DECIMAL, the SUM() function was returning "0.00" rather than "0", as I was used to. Since "0" coerces into false, but "0.00" coerces into true, array_filter() wasn't removing things properly. This did not get pushed live, thankfully, so it fails your "in the wild" criteria, but it came close. My initial solution was to cast the output to float, then run array_filter(), then I said screw it and wrote "if ($Row['sum'] == 0)", which still hurts my soul. Why that instead of "=== '0.00'"? Because I didn't want to trust it to always only return two decimal places, and I was annoyed with it.

There's a lot of stuff I can change in retrospect as I write this, but it still annoyed me. I don't mind PHP's type coercion, there's times when I rather like it, but what I don't like is the random way in which its internal functions respect this, and I don't like the intransitivity of it. Maybe "0.00" should evaluate to false, as should ".00" and any number of zeros before and after the decimal. Also, give us a money-safe decimal type, rather than having to rely on the bcmath extension.

Adbot
ADBOT LOVES YOU

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Suspicious Dish posted:

Apparently just '0xFF' == '255' is true. That is absurd.

Can we all agree that, when comparing two items of the same type, that there should never be any coercion performed when comparing them?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply