News for hackers. Stuff that matters.
Re:My Opinion on “5” == 5 by withinboredom on Thursday July 29, 2021 @06:56AM (#27995011)
In php, this is also true or false if you have strict types on.

I’d argue that there are some valid use cases for “5” and 5 to be equal. For example, if you take user input where on an admin screen for a user ID or username, you don’t know if the type is a string or number. Another common example is when you don’t have a known encoding for a packet yet, so you treat it as a binary string. In these cases, it’s easier to grok, as a human what is expected.

Re:My Opinion on “5” == 5 by lexicality on Thursday July 29, 2021 @12:45PM (#27998465)
If you're dealing with user input, you should always be validating the type before working with it.

If you want to compare input to an int, you should be verifying the input is a valid int before comparing it.

Just hoping the language will do the right thing is a great way to inadvertently allow exploits in your service

Re:My Opinion on “5” == 5 by crote on Thursday July 29, 2021 @01:14PM (#27998823)
On the other hand, what about "V" == 5? Or one of the literally dozens of unicode characters representing the number five in different writing systems? Maybe even "five" == 5?

Accepting both a numerical user id and a username string is a very valid scenario. However, I believe you should handle that by explicitly trying to parse it as a number, not hoping that automatic coercion will do the right thing. It's just a footgun waiting to go off.

Re:My Opinion on “5” == 5 by infogulch on Thursday July 29, 2021 @01:37PM (#27999092)
Good point.

    "5" == 5
    "⑤" == 5
    "⑸" == 5
    "⁵" == 5
    "𝟝" == 5
    "𝟓" == 5
    "𝟻" == 5
    "V" == 5
    "Ⅴ" == 5
    "𝍸" == 5   # https://unicode-table.com/en/1D378/
    "𐄋" == 5   # https://unicode-table.com/en/1010B/
    "৫" == 5    # https://unicode-table.com/en/09EB/
If you think the first should work, you should justify why the others shouldn't.
Re:My Opinion on “5” == 5 by tetha on Thursday July 29, 2021 @01:35PM (#27999061)
> Accepting both a numerical user id and a username string is a very valid scenario. However, I believe you should handle that by explicitly trying to parse it as a number, not hoping that automatic coercion will do the right thing. It's just a footgun waiting to go off.

This is why many people recommend questioning the use of basic or naked types. Details vary between the language, but this should be a type UserID with a UserID::from(String) and maybe a UserID::from_symbolic_name(String) and everything but some edge code should work on UserIDs. This makes code easier to read, too.

Re:My Opinion on “5” == 5 by Koshkin on Thursday July 29, 2021 @12:45PM (#27998464)
> it’s easier to grok, as a human

People usually understand the difference between "cat" and a cat.

Re:My Opinion on “5” == 5 by llimos on Thursday July 29, 2021 @01:00PM (#27998646)
PHP also has a nice thing I wish more languages had, which stops you shooting yourself in the foot due to this - the concatentaion operator (.) is different from addition (+).

So "5" + 1 is 6, just as 5 + 1 is 6.

Re:My Opinion on “5” == 5 by coding123 on Thursday July 29, 2021 @01:17PM (#27998859)
The reason other languages don't have that is very few languages will implicitly convert that "5" to a number. Instead other languages generally only have the implicit convert to string as it's considered less magical.
Re:My Opinion on “5” == 5 by perl4ever on Thursday July 29, 2021 @01:06PM (#27998720)
I can't think offhand of a language that does overload + for strings by default.

  Perl uses .
  Oracle SQL uses ||
  Microsoft SQL and VBA use &
  C obviously doesn't use +.
  Neither does Pascal.
  Power Query doesn't.
Re:My Opinion on “5” == 5 by poizan42 on Thursday July 29, 2021 @02:46PM (#27999917)
> Microsoft SQL and VBA use &

Nope, Microsoft SQL uses +.

* https://docs.microsoft.com/en-us/sql/t-sql/language-elements...

* https://i.imgur.com/7z81o8t.png

* https://i.imgur.com/VDR9qer.png

> C obviously doesn't use +.

But C++ does for std::string.

> Neither does Pascal.

Original Pascal didn't, but I think most modern Pascals do (e.g. Object Pascal known from Delphi).

Re:My Opinion on “5” == 5 by perl4ever on Friday July 30, 2021 @12:18AM (#28004388)
>Nope, Microsoft SQL uses +.

I was referring to Access.

>Original Pascal didn't, but I think most modern Pascals do

I'm pretty sure concat() was used in THINK and Metrowerks Pascal in the 90s.

Re:My Opinion on “5” == 5 by int_19h on Thursday July 29, 2021 @04:29PM (#28001093)
Pascal does, at least in all the more common dialects.

VBA (and VB in general) overloads "+" for strings in addition to providing "&", the difference being mostly in what gets converted to what in various situations: https://docs.microsoft.com/en-us/office/vba/language/referen...

Re:My Opinion on “5” == 5 by perl4ever on Saturday July 31, 2021 @10:18PM (#28023222)
Is there any reason anyone in their right mind would use + that way in VBA?
Re:My Opinion on “5” == 5 by int_19h on Monday August 02, 2021 @11:34AM (#28038015)
I've seen plenty of code in VBA and VB6 that does so. I think it stems mostly from & being a late addition to BASIC (earlier BASICs only had +).
Re:My Opinion on “5” == 5 by Frondo on Thursday July 29, 2021 @01:09PM (#27998760)
C# does, as do quite a few others. Heh, this is kind of interesting:

https://www.rosettacode.org/wiki/String_concatenation

Re:My Opinion on “5” == 5 by dahfizz on Thursday July 29, 2021 @01:21PM (#27998901)
Java and Python, from the top of my head, allow "str1" + "str2"
Re:My Opinion on “5” == 5 by rspeele on Thursday July 29, 2021 @01:20PM (#27998886)
C#, GoLang, Javascript, Python, and Ruby are several.

Python and Ruby even overload *, for the dubiously useful construction:

"hello" * 3 == "hellohellohello"

Re:My Opinion on “5” == 5 by infogulch on Thursday July 29, 2021 @01:47PM (#27999232)
I like Julia's standard string concat which subverts a Python user's intuition: "string1" * "string2" == "string1string2". Why? Because monad.
Re:My Opinion on “5” == 5 by Zababa on Thursday July 29, 2021 @01:24PM (#27998918)
JavaScript, Python, Ruby, Go all use +.
Re:My Opinion on “5” == 5 by smnrchrds on Thursday July 29, 2021 @12:59PM (#27998634)
So that's why some websites fail to log you in with numerical usernames and/or passwords that start with zero.
Re:My Opinion on “5” == 5 by tyingq on Thursday July 29, 2021 @12:59PM (#27998633)
There are several languages where this sort of type coercion exists. Javascript, Awk, Perl, Tcl, and PHP as you mentioned.

For the most part, the developers in those spaces seem to know the pitfalls and how to avoid them.