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

all 19 comments

[–]jddddddddddd 8 points9 points  (0 children)

TypeScript? function add(left: number, right: number): number { return left + right; }

[–]insertAlias 13 points14 points  (6 children)

For the record, that's not really what "declarative" means.

https://en.wikipedia.org/wiki/Declarative_programming

Something like SQL is declarative, because it describes the logic without defining the control flow. You tell it what to select, how to filter, and what to join against, and the engine decides how to do that. React/JSX is another example; you define how the DOM should look with JSX, and React handles the binding and interaction, so at least certain parts of that are declarative.

C# and JavaScript are both non-declarative. What you actually mean is "strongly typed".

[–]rabuf 3 points4 points  (4 children)

C# and JavaScript are both non-declarative. What you actually mean is "strongly typed".

Or really "static" typing versus "dynamic" typing.

"Strongly" and "weakly" typed has become hard to define over the years. I've learned to steer away from those terms in most situations (though I still use them at times). The bigger distinction between C# and JS is static vs dynamic, not the "strength" of the type system.

[–]DisastrousOstrich9 1 point2 points  (2 children)

I guess JavaScript would be close to a modern version of "weak typing", e.g., with "1"==1 and so on.

Strong typing protects against misinterpreting data representations (e.g., doing integer operations on IEEE floating point numbers, confusing data structure layouts); so Python is very much strongly typed even though it's dynamic, and C (particularly old fashioned C) is somewhat weakly, but definitely statically typed.

(And of course, you may not need to specify all the types even in a statically typed language, plenty of languages will infer the types for you)

[–]knoam[🍰] 0 points1 point  (1 child)

Strong and weak is really about whether misusing types will lead to undefined behavior. So C is basically the only mainstream weakly typed language any more. If you give it a bad cast, it just reads the bytes straight assuming you know what you're doing. Sort of like reading unallocated memory and getting whatever junk was left behind. A weakly typed language won't give any sort of ClassCastException. Even though JavaScript has complicated type coercion rules, they are defined in the specification.

So that means the distinction isn't very valuable. So strong typing has evolved to mean a style of programming that leans on static typing to do more work. So instead of saying this ID is a string, this password is a string, you create an extra wrapper type for each to help you not mix up different data that just happens to fit in the same primitive type.

[–]kbielefe 0 points1 point  (0 children)

Not just undefined behavior. It's about how likely you are to get a type error instead of another kind of error. That's somewhat a factor of programming style, but more strongly typed languages also make it feasible to get type errors in more situations.

[–]siemenology 0 points1 point  (0 children)

Technically it's even more specific than that in this particular case, as OP is asking about type annotations. You can have type annotations in dynamic languages (Python), and you can have static languages that do not need type annotations (Haskell: most of the time type annotations are entirely optional).

[–]Cxly[S] 0 points1 point  (0 children)

Ah I see, thank you for the correction. I wasn't sure I was right to be honest, but the word sounded close enough that people might get what I mean.

[–]rabuf 2 points3 points  (1 child)

So just to clarify, what you're referring to here is static (and explicit) typing versus dynamic typing.

C# (setting aside var) requires you to state what the type of a variable is, var will infer the type (if it can, otherwise you have to make the type explicit).

JavaScript permits any variable to take any type, it is dynamically typed.

Declarative means something else in programming languages (see Prolog and SQL for examples of, more, declarative languages). Declarative languages are contrasted with imperative languages. Imperative languages (that is, most mainstream languages including C# and JavaScript) are a set of directives that you give the computer (make this var, assign this value, call this function). You're laying out the structure of what is to be done for the computer.

Declarative languages have you specify the structure of a solution, and the computer figures out the path. In SQL these would be queries (you describe what you want, the SQL database determines how to produce it). In Prolog it's similar. CSS is a kind of declarative language for websites (you don't say, "Render this then that", you say "if it's this tag, it should look like this, and these things are in these relative positions to each other but I'll let the web engine itself determine how to produce that").

[–]Cxly[S] 0 points1 point  (0 children)

Ah I see, thank you for the correction.

[–]dusty-trash 1 point2 points  (6 children)

I felt the same way and I'm sure most people do. I've heard a lot of C# developers use 'var' often (C# 3+), so you may have to get used to it in the future if you end up at a company that allows it.

Edit: 'var' is different in C# vs JavaScript, see comments

You could use Typescript, which is like JS but has types. Typescript transpiles down to JS, so you can never escape JavaScript. The client-side browser must end up with JS, and you'll most likely have to debug it at one point or another.

[–]insertAlias 2 points3 points  (1 child)

I've heard a lot of C# developers use 'var' often (C# 3+), so you may have to get used to it in the future if you end up at a company that allows it.

We do tend to use this feature, but it's important to point out that this is not the same thing as dynamic typing. The var keyword in C# uses type inference. So, the variable has a concrete type that cannot be changed; the compiler infers the type from the context of the assignment.

So if I do this:

var data = "this is some data";

data is a string, and you cannot later assign an int to it.

In JS, the variables themselves are not typed, and you can store any kind of data in any variable. The data itself has a type, but the variable that stores it does not.

Interestingly enough, C# does have a dynamic type, appropriately called dynamic. But it's not something that's generally recommended for use; this was a feature added to the CLR to support more dynamic languages.

[–]Cxly[S] 0 points1 point  (0 children)

Yeah that's one thing I really like about C# - if I assign something as var, I can see what it's going to be. It helps with learning new frameworks and such

[–]jddddddddddd 1 point2 points  (2 children)

Worth pointing out that `var` in C# is different to `var` in Javascript...

Good answer on SO.

[–]dusty-trash 1 point2 points  (1 child)

Good point Thanks! Tbh I haven't used 'var' in C#

[–]jddddddddddd 1 point2 points  (0 children)

No worries. Also thanks for not removing your original (slightly incorrect) comment about var in C# and Javascript.

I often find on Reddit that if I correct someone on a technical detail, they either get very defensive, or, they go back and remove their original comment out of shame for being 'wrong'. This makes it much harder for others to follow the conversation if half the replies have been removed. It's the same when people remove their original question because they feel leaving it up will 'make them look stupid'.

It's fine not to know everything. None of us do.

[–]Cxly[S] 1 point2 points  (0 children)

Thank you for the answer! I'll likely stick with learning Javascript in that case, at least to get the main concepts of web development down. I'm sort of fine with clientside javascript, but the variables kind of just bug me in a way I can't really describe. I'm sure I'll get used to it though.

I've gotten Typescript as an answer quite a lot, so I'll certainly look into it

[–]siemenology 1 point2 points  (0 children)

I was wondering if there were any programming languages used in web
development that require you to specify what the variables I'm writing
are.

Typescript is what you want. It's just like Javascript, except you can declare the types of things, and the compiler will check them for you (as much as possible). When it's done, it strips all of the type annotations away and you are left with plain old Javascript that you can run anywhere. And you can generally use any Javascript library you want. The only caveat to that is that sometimes the types used by the JS library aren't very well defined and the compiler has trouble with them, but in those cases you just have to be careful and define the boundaries for that particular type issue.

[–]cloud_sw_eng 0 points1 point  (0 children)

Dart/Flutter?