4 ms·
This is an interesting post. I'm designing a little programming language as a hobby and I am stuck at how I want to implement function calls. Without named pa
by Xcelerate 14y ago
This is an interesting post. I'm designing a little programming language as a hobby and I am stuck at how I want to implement function calls.
Without named parameters, you end up with something like the Win32API:
ReadSomeFile("file.txt", SOME_FLAG, 0, SOME_OTHER_FLAG, NULL, NULL, CREATE_FILE);
This can largely be avoided by structuring your API into little single-purpose chunks, but it remains a bit of a problem.
Also I think it's somewhat annoying that you have to remember the order of arguments. In my opinion, this order should not be significant. It leads to a lot of bugs (for people who don't check the documentation for every method) with things like:
str_replace(needle, haystack)
vs
str_replace(haystack, needle)
It would almost be easier to have everything take a map
ajax
url: "example.com/newwidget"
method: "post"
error: -> ...
success: -> ...
But if you enforce this for everything you get something like:
square
number: 4
which is just ridiculous. I kind of like Python's way of doing it -- making them optional.
One thing's for certain -- Javascript frameworks need to develop some kind of consistency. Sometimes you pass in parameters, and sometimes you pass in a configuration object. This inconsistency makes it difficult, and it's too hard to remember which calls take parameters and which take a map.
- robomartin 14y agoThis is not criticism, but I'd just like to point out that the farther you get from the raw realities of the efficiency of C-style code you increase the probability of code becoming bloated with constructs and "rigging" under the hood that's there just to make things convenient to a programmer. At one point or another there is no escaping the reality that pointers will end-up as addresses in a register used to make an indirect call or "MOV" of some sort and that other data types will similarly land on registers or various memory areas as they are passed to and from functions. I got my start writing machine code (not assembler, but entering raw hex codes into hexadecimal keypads) and that caused me to always be very aware of where I am just being lazy and loading things-up with code just to make my life easier. Granted, sometimes this is justified. Today's coding tools (editors and IDE's with code completion and "live" API reference) make it harder to screw some of this up. I tend to always go for simple, fast and efficient. But that's my preference.
- Xcelerate 14y agoOh, I certainly agree. I have different projects with different goals. I've spent some time eking out every ounce of performance for a Newton-Rhapson division routine in assembly. But then there are some high level things that I think lead to more elegant (and ultimately simpler) programming. Clojure for example isn't particularly close to the hardware, but it leads to better coding practices that result in easier long term maintenance, less bugs, etc. I think a good balance would be to take a well designed language and add an option for high-performance code when needed. C and C++ do this with inline assembly (although I don't know that anyone would call C "elegant"). It would be nice if more higher-level languages added this ability.
- masklinn 14y ago> It would almost be easier to have everything take a map Smalltalk and Self have demonstrated that it is, indeed, rather easy. And at this point you can just remove the separation between "keyword arguments" and "method name", they are one and the same. > But if you enforce this for everything you get something like: Well you can still have operators: 4**2 or you can use a message to numbers: 4 raisedTo: 2 even a unary one specifically for this operation: 4 square or you can have `square` be a message to some sort of math namespaces: Math square: 4
- masklinn 14y agoAnd to go further, the network example you show (from jQuery) you could express as: Request to: url withMethod: method onSuccess: success onError: error with various overloads for optional component (the minimally complete expression being `Request to: url`) but various Smalltalk distributions & libraries have various ways to do this (usually synchronous so it's not a precise mapping). Squeak's WebClient[0] would be something like that: body := (WebClient httpGet: url) content. [0] http://www.squeaksource.com/WebClient/ http://www.squeaksource.com/WebClient/