6 ms·
Given a certain level of understanding of the problem, certain languages can make it easier to write clear and correct code. For example, code that does physics
by mattnewport 7y ago
Given a certain level of understanding of the problem, certain languages can make it easier to write clear and correct code. For example, code that does physics or 3D graphics calculations is clearer in a language that can represent matrices and vectors as types providing the expected operations (e.g. C++ over C) and if the code involves physical quantities it can be easier to enforce correctness in a language that supports units in the type system.
- _n_b_ 7y agoAgree. This is one of the reasons that Fortran remains popular for scientific computing: * dead-simple array/vector operations (http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/6-Fortran/array.html http://www.mathcs.emory.edu/~cheung/Courses/561/Syllabus/6-F...) * many useful built-ins (e.g., DOT_PRODUCT, MAXVAL/MAXLOC, MATMUL, TRANSPOSE) * standard math libraries (e.g., LAPACK) with lots of very fast functions for linear algebra
- jcora 7y agoThis. I hate this relativism about the merits of languages. There's an entire field of study dedicated to that and just because you can write a web app in all your languages of choice doesn't actually invalidate the vast conceptual differences that exist and have real effects on how you program
- majkinetor 7y agowhat field of study?
- vraivroo 7y agoProgramming Language Theory
- fauigerzigerk 7y agoBoth C and C++ can represent matrices and vectors as types with related operations. In my view, the key difference between these two languages is RAII.
- jcelerier 7y agobut only C++ allows you to write `c = a * x + b` instead of mat a, b, x, tmp, c; // ... mat_mul(&tmp, &a, &x); mat_sum(&c, &tmp, &b); which is what OP meant if I'm not mistaken.
- fauigerzigerk 7y agoTrue, operator overloading is definitely convenient for math.
- Mister_Snuggles 7y agoAs a real world (-ish) example, I wrote some Python code to do some effects on DMX lights. OpenLighting did all the talking to the lights, so the Python code was really just focused on constructing a list of numbers that would eventually find their way into OpenLighting and onto a DMX cable. Inside the Python code, each light has a Colour, and I built the Colour class with support for various math operators. Want to make a Colour that's half as bright as another? Colour = OtherColour * 0.5. Want to blend two Colours? BlendedColour = OneColour + AnotherColour. Doing this made all of the stuff that the code was doing incredibly obvious and straight forward. The alternative would have involved non-obvious helper functions and object methods or doing math directly on the rgb components encapsulated by each Colour object. This was, for me, somewhat of a "light bulb" moment for operator overloading.
- maxxxxx 7y agoIf it's done in good taste overloading is fine. But if abused it makes code totally unreadable and unmaintainable.
- fauigerzigerk 7y agoColor me skeptical :) The reason why unnamed operators work so well for math code is that their meaning is already well established. OneColor + AnotherColor does not have any obvious meaning. If colors are RGBA vectors, it could be vector addition. But is that what your blend operation does? What's wrong with: BlendedColor = blend(OneColor, AnotherColor)