3 ms·
> What are 'dynamic objects' and what is the way that 'C++ simple cannot do thing'? This is the foremost proof that knowing C++ doesn't mean you know anything
by hajile 4y ago
> What are 'dynamic objects' and what is the way that 'C++ simple cannot do thing'?
This is the foremost proof that knowing C++ doesn't mean you know anything about JS.
JS objects are dynamic with the ability to add/remove class properties and methods or to dynamically change what class/object it inherits from all as the program executes. C++ classes are static and determined at compile time.
> Most programmers learn javascript before C++ because it's so easy to try things out in a web browser.
Most C++ programmers started programming C++ long before JS became popular. Most people who "know JS" don't actually know the language to any meaningful degree. I'm not talking about subtle things like the difference between `-0 === 0` and `Object.is(-0, 0)` or why you might use `x === x`. I mean that most can't tell me what a closure is, why you'd want to use one, or even how to use it in something like a factory. For the record, this question isn't even JS-specific, but applies to Ruby, Python, Lua, PHP, the ML family, Rust (actually just a ML with C-like syntax), Erlang, most lisps, etc.
> Where do these ideas come from? Every modern language has elements that were originally considered "functional".
JS is multi-paradigm offering imperative, OOP, and functional approaches, but because its functions inherited heavily from Scheme (I really wish Eich could have implemented scheme), it gets a lot of really nice properties.
Consider the following code (leaning heavily into the point-free side for sake of illustration)
let prop = key => obj => obj[key]
let map = fn => arr => arr.map(fn)
let filter = fn => arr => arr.filter(fn)
let reduce = fn => arr => arr.reduce(fn)
let sum = (acc, x) => acc + x
let pipe = (...fns) => val => fns.reduce((acc, fn) => fn(acc), val)
let data = [
{
fname: "John",
lname: "Doe",
age: 41,
sex: 'M',
children: [
{fname: "Fred", lname: "Doe", age: 11},
{fname: "Jill", lname: "Doe", age: 8},
{fname: "Elliot", lname: "Doe", age: 3},
]
}
]
let printSumOfChildenAgeOfMiddleAgedFathers = pipe(
filter(x => x.sex === 'M' && x.age > 40), //get all males over 40
map( //for each male
pipe(
prop('children'), //get their children and age
map(prop('age')),
reduce(sum), //add ages together for father
)
),
reduce(sum), //add ages for all fathers
)
Sure, it is pointless code off the top of my head to illustrate my point (though with some changes it could be actual code I've seen). If you'd like to counter, write this functional-style code in C++ without changing to imperative code. It'll be 10x as long and an unreadable mess.
> Even labeling someone as a "C++ dev" or "javascript dev" is a giant red flag of inexperience.
I consider language design as a bit of a hobby and I've spent time with a LOT of very different languages. Even so, I only consider myself proficient in a handful of them. Even setting aside the impossible task of learning all those standard libraries completely (even most experienced Java or C# devs don't actually know the entire standard library of those behemoths), just learning the warts and weird edge cases of almost any language you can name takes at least a couple years and with large and/or convoluted languages (eg, C++ or JS) it can take many years.
- CyberDildonics 4y agoJS objects are dynamic with the ability to add/remove class properties and methods or to dynamically change what class/object it inherits from all as the program executes. You realize they're just hash maps / dictionaries right? Most C++ programmers started programming C++ long before JS became popular. Do you think most C++ programmers today started 30 years ago in the early 90s? You realize you can write everything you wrote pretty directly in C++, python, lua or a lot of other modern languages out there right? I get the impression that you don't know much about modern C++ and just base what you are saying off things you have heard from other javascript programmers trying to avoid learning other languages.
- hajile 4y ago> You realize they're just hash maps / dictionaries right? That's only a conceptual idea. In reality, the JIT uses real arrays and dynamically generated structs. You can't actually implement them directly as conceptually described anyway. You have to have a has of boxed pointers and when you get around to adding functions with types, things get really weird. The class implementation is actually much more simple in many ways (not to mention being much faster). > You realize you can write everything you wrote pretty directly in C++ I know you can't write curried functions "pretty directly" in C++ (Python and Lua are the languages I was discussing directly and both are far better for UI work than C++). Give it a try and post the results. I'd be very impressed if it took even just twice as many lines of typical C++ code.
- CyberDildonics 4y agoI don't even know what point you're trying to make anymore. You originally said you think C++ takes five times as long as javascript and that most C++ programmers learned it before javascript existed. Have you ever used modern C++?