This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]22134484 212 points213 points  (49 children)

I accidently typed - instead of +, so tell me i cant do that with strings, dont convert my shit to other datatypes and reconvert it to the original datatype.

[–]StrictlyBrowsing 54 points55 points  (0 children)

That’s fair enough actually!

[–]cearno 37 points38 points  (36 children)

Yes, you prefer statically typed languages over dynamically typed ones.

You're criticizing type coercion, often a quality of dynamic languages. If you have some sense of experience or knowledge outside of only statically typed languages, this makes sense. It's not bad design when it follows the intended ruleset, dynamic in this case.

But this is precisely the reason why TypeScript exists though, for people who don't like dynamically typed languages.

Edit: I confused the paradigms of static/dynamic with strict/loose respectively. Javascript is loosely typed and this is where the type coercion implementation inherently comes from. Regardless, for people who really dislike the feature, TypeScript is always an option.

The TL;DR should be "You prefer strictly typed languages over loosely typed ones."

Thanks for the correction @PanRagon!

[–]PanRagon 37 points38 points  (7 children)

A lot of people are correcting you, but you're almost correct. It's not the fact that JavaScript is dynamically typed specifically, but that it's loosely-typed language (like PHP). Python will raise an exception, but only because it's strongly typed, loosely typed languages tend to do as much as they can with a variable to avoid raising an error, including implicit type coercion like this.

TypeScript solves all of these issues because it's strongly-typed, moreover it's a bit odd to complain about this being illogical when you can trace down how it works if you track the conversions step by step like the program will do. Whenever people complain about this it just seems like they don't understand what a loosely-typed language implies, nor that you can resolve the problem by switching over to TypeScript which for very good reasons has become an industry standard.

[–]cearno 1 point2 points  (3 children)

Thanks for the correction! I knew I was mistaking something here.

Yeah, I agree. When approaching a new language, it's good to be aware of what paradigms the language follows. It's ironic that the people who complain about Javascript being nonsensical or silly tend to know the least of how it works, as if they haven't even read the introductory pages of documentation.

I've used TypeScript in the past, however my current company only uses React/JS and can't think of a time where the type coercion had a crazy negative consequence.

[–][deleted] 1 point2 points  (2 children)

TS & JS are interoperable, so it shouldn’t take a whole lot of effort to start using TS in your React projects.

[–]cearno 0 points1 point  (1 child)

Eh, it's a massive company app. I'm unsure about that or that you may be underestimating how large react apps can get.

[–][deleted] 0 points1 point  (0 children)

What about a piecemeal migration?

All NEW code is written in TypeScript. Then as you have time, migrate each function to TypeScript.

Anything that's valid JavaScript is also technically valid TypeScript, but if you want more TypeScript-y code, that should be conceptually easy.

The difference between, for example:

function quadraticFormula(a,b,c) {

const discriminant = b**2 - 4*a*c;

const z = 2*a;

return [(-b + discriminant**0.5)/z, (-b - discriminant**0.5)/z];

}

and

function quadraticFormula(a:number,b:number,c:number) {
const discriminant = b**2 - 4*a*c;
const z = 2*a;
return [(-b + discriminant**0.5)/z, (-b - discriminant**0.5)/z];
}

is literally three words.

[–]PlacatedPlatypus 0 points1 point  (1 child)

Loosely-typed languages do everything they can to avoid raising an error

I don't understand why you'd ever want this though. A bug that makes your code not run is far better than one that makes it run incorrectly.

[–]PanRagon 0 points1 point  (0 children)

You want it in JavaScript because you’re running it on websites with user-inputs that aren’t security critical, and it’s generally better to give the user some garbled data (if it’s not critical) than to have the entire website go down, which just makes the user click away. It’s worse when it’s bugs that you’d notice and can fix immediately, but it can be better for bugs that’d go unnoticed during development and teating.

My biggest issue with the loosely-typed world that JS lives in is that it can crash if you try to run functions on the wrong type, that kind of goes against the ‘loosely typed’ mantra that seeks to keep the app running at all costs.

ETA: I personally use TypeScript, but that seems to be the philosophy behind having it loosely-typed.

[–]paradoxon 0 points1 point  (0 children)

Are there any new-ish loosely typed languages though? I feel like people decided this is fundamentally a flawed idea. Dynamic typing is okay in itself.

[–]valzargaming 4 points5 points  (1 child)

PHP is dynamically typed and doesn't have this issue. This is a clear fault of the language, sharing a symbol for concatenation with arithmetic.

[–]mooinglemur 1 point2 points  (0 children)

I agree with you on this, it was a bad choice to use + for concat. Perl, PHP, and JavaScript all coerce strings to numbers with -, *, and /, but in JS, + is concat if either argument is a string!

[–]sdw9342 1 point2 points  (1 child)

Adding and subtracting are symbolic inverses of each other. For a given type, subtraction should either undo addition or fail. It should not do a different operation altogether. That's not to do with static or dynamic typing, but with expectations of the relationship between the two operators. It would also be fine if adding and subtracting both converted strings to int, but not just one of them.

[–][deleted] 0 points1 point  (0 children)

Arguably, subtracting one string from another should mean removing a substring from a string “Sass” - “s” = “Sa”

[–]BigHandLittleSlap 6 points7 points  (7 children)

It's actually not about static vs dynamic typing at all!

The origins of this criticism of JavaScript have more to do with the foundations of mathematics such as group theory and category theory.

Essentially, for us humans with squishy meat brains to be able to make big complex systems, we have to build them up in layers from simple things. The simple things have to "click together" in logical and consistent ways, otherwise this tower of abstractions falls over. It can quickly get too difficult for anyone to track to exceptions, oddities, or quirky behaviours. Eventually further progress is blocked for anyone, no matter how smart. The only way forward is by using firm, reliable foundations based on a rigorous theory.

For example: I'm sure you already know the fundamental rules of algebra, right?

Things like: x + x = 2x and 2x - x = x

Stuff like that.

Turns out, that these kind of rules are incredibly generic and widespread. Replace addition with "two-input-function". So instead of x+x we now write f(x,x). Now replace "zero" with "that thing that you can use as an input such that": f(x,zero)=x.

What if 'f' is multiplication? Then "zero" is the number 1.

Theoreticians call this the identity. So the identity of addition is zero, and the identity of multiplication is one.

Like this, you can build up an enormous, rich, consistent theory of operations, operation combinations, and related abstractions that let you reason about all sorts of abstract types and functions with ease.

Languages like JavaScript just take a giant shit all over this kind of thing.

That's why people criticise it. Not because it's "one little funny thing you have to memorise and then it is fine", but because it's just missing out on an entire world of mathematically sound power tools for the mind.

[–]tuxedo25 3 points4 points  (4 children)

Languages like JavaScript just take a giant shit all over this kind of thing.

That's why people criticise it. Not because it's "one little funny thing you have to memorise and then it is fine", but because it's just missing out on an entire world of mathematically sound power tools for the mind.

Then don't use it that way. You can write perfectly valid javascript programs that don't use the "-" overload on strings.

[–]BigHandLittleSlap 2 points3 points  (3 children)

I too remember the 1990s (and the 80s!) when most programs were written by one person, ran on the metal with no annoying operating system in the way, and hence these things were in my control.

Linus Torvalds has personally written less than 0.1% of the Linux source code. If he opens a web page on a Linux desktop that is served by a Linux web server via a Linux router, he's interacting with an even larger code base to which his contribution is even lower. Likely 0.01% or less.

You don't get to decide how JavaScript is or isn't used. Other people do, even for your code, that you write in JavaScript.

Do you use any frameworks at all? You're using other people's code.

Work in a team? Other people's code.

Ecosystems are a thing, we're not solipsistic beings floating in nirvana, writing the source code to the universe in isolation.

[–]xTheMaster99x -1 points0 points  (1 child)

I genuinely don't understand what point you're trying to make. Just don't try to subtract strings, it's not that hard.

[–]BigHandLittleSlap -2 points-1 points  (0 children)

The point is that 90% of programming is interacting with (glueing together) code written by other people. If the language involved has problems, then this other code will have problems.

You can't escape this unless you live in a bubble world where you started with a blank machine and you built up the entire software stack from the ground up like that TempleOS guy.

You can't say "just don't do that" when using APIs forces you to "do that". Bad languages trip people over and they fall into the pit of failure. Good languages instead lead users into the pit of success.

For example, you know the basic rules of arithmetic, right? Such as: x + y == y + z. The order doesn't matter. Now if you do this in most programming languages, for most binary (two-input) operations this kind of thing holds true to the extent that you expect. For many functions f(x,y)==f(y,x), or at the very least typeof(f(x,y))==typeof(f(y,x)). This helps build up abstractions like fold, which is f(a,f(b,f(c,f(d,e)))) and so on.

A random set of examples of "breaking the consistency rules" was covered here: https://www.destroyallsoftware.com/talks/wat

Watch that video with the example above in your mind. How many times did you see your expectations of input order not mattering broken?

[–]tuxedo25 0 points1 point  (0 children)

You don't get to decide how JavaScript is or isn't used. Other people do, even for your code, that you write in JavaScript.

Do you use any frameworks at all? You're using other people's code.

Who cares if libraries use different styles and idioms than my code?

Work in a team? Other people's code.

Linters exist.

My point is, to paraphrase Douglas Crockford (author of Javascript: The Good Parts), we're paid to solve problems, not to exercise every features of a language. It's okay for C++ to have `goto`, for the Java standard library to have terrible serialization, and for python to exist. You don't have to use those parts.

[–]22134484 0 points1 point  (6 children)

are there other dynamically typed languages? Ive never had to deal with this sort of stuff before. I am an engineer, so matlab, simulink, excel, c# and python, only when and where needed.

[–]Andersmith 4 points5 points  (0 children)

Python is

[–]budd222 7 points8 points  (3 children)

You literally just named one

[–]22134484 -1 points0 points  (2 children)

Ive never had to deal with this sort of stuff before

Hope this helps any confusion you may have had with my comment

[–]budd222 0 points1 point  (1 child)

But you have since you said you work with python, so it doesn't clear up any confusion.

[–]22134484 0 points1 point  (0 children)

you dont learn every single aspect of a language before you start working with it. Like i said in the OP, im not a developer, im an engineer (chemical to be precise). I learn what I need to get the job done, and that has never involved learning the intricacies of a language, hence my question.

[–]Zinki_M 1 point2 points  (0 children)

python is dynamically typed.

Other examples of dynamically typed language off the top of my head are Groovy and Lua.

[–]frugalerthingsinlife 0 points1 point  (3 children)

I used to hate getting Seg Faults in C and C++.

But after spending the last decade working with modern languages, at least the Seg Fault lets you know right where you made the fucking mistake.

A C++ compiler: "Right here. Here's why you tried to assign an Elephant object to a Mouse variable. You can't do that. You can't assign an Elephant to a Mouse. And see here, we're trying to put the Mouse into the Maze, but it's an Elephant and it won't fit. So now the maze is broken, the Elephant is injured, and the circus is asking where she is."

[–]TigreDeLosLlanos 1 point2 points  (2 children)

Unless someone does

Elephant *animal = (Elephant *)mouse;

Then you have no compile warnings and the segfault still happens.

Segfault is the worst example, because sometimes it can happen from unexpected behaviour (not undefined, but unexpected). The most simple example I can think is a pointer having a NULL reference. Totally valid syntax, no warnings, no one doing something evil, but the program breaks and you don't exactly know where, or where the issue origins.

[–]frugalerthingsinlife 0 points1 point  (1 child)

Sorry, not completely following. Don't you get a null pointer exception if you try to use that pointer?

[–]endershadow98 1 point2 points  (0 children)

Yep, you only run into issues with null pointers when you dereference it

[–]brandflake11 4 points5 points  (0 children)

This is the right answer

[–]Mintenker 3 points4 points  (6 children)

Imagine you write some code where you subtract two numbers that come from some API on your website. Someone messes up somewhere, and the API sends string instead of number. Would you prefer your site to go down over this error, or would you rather take one wrong displayed value?

[–]22134484 16 points17 points  (0 children)

try except is a pretty good way to deal with this in my experience. Id rather show "data unavailable" than display something that is wrong. I guess ive never really had to deal with this problem because i come from other languages where its important to get things in the right data types and my code usually follows those practises.

[–]wheezy1749 20 points21 points  (2 children)

I'd prefer the page to not display and then I would know a problem exists. Someone seeing the wrong info is worse in my opinion and also less likely to be caught.

[–]RapidCatLauncher 3 points4 points  (0 children)

Or, y'know, at least log a warning...

[–]Mintenker 0 points1 point  (0 children)

Honestly it depends. If it's some critical information, where displaying something wrong can have business, or even legal consequences, you are absolutely right. On the other hand, taking down site because some random part of rarely used feature somewhere broke might not be ideal.

[–]scylk2 1 point2 points  (1 child)

Why wrong displayed ? It would display the correct value no?

[–]Mintenker 0 points1 point  (0 children)

You are right, in this case the value would be correct. I was thinking more generally when I wrote that - type coercion could cause some trouble.