views:

153

answers:

4

For instance, just for kicks, I was thinking about having a PHP script that all of my JavaScript files would pass through; now, this combined with the lovely little buggers that are regular expressions leads to a whole world of possibilities (for better or worse). At the moment, I've only made it so that JavaScript is Pythonic in structure, like so:

A simple function which takes a parameter and displays the number if it is a multiple of 2 and/or 3:

<script>
function show_mults_23(k):
    for (x = 0; x < k; x++):
     if (x % 2 == 0):
      document.body.innerHTML += x;
      document.body.innerHTML += ' is a multiple of 2.<br/ >';
     if (x % 3 == 0):
      document.body.innerHTML += x;
      document.body.innerHTML += ' is a multiple of 3.<br />';
</script>

It's purdy, huh? ; p

Seriously, though, I'm curious as to whether or not such a concept has ever been implemented in a development environment. There's obviously a tremendous decline in speed, but aside from that, could such an idea have any ground?

+16  A: 

Show some mercy on the maintenance programmers that have to follow you and do not do this. In order to fix a bug, they not only need to know Javascript and PHP but also this unholy thing that you have created.

1800 INFORMATION
Agreed, this would ruin my day if I was handed something like this to maintain :)
Kev
+5  A: 

In general, I'd say it's a very good idea, and a very bad idea.

Doing fun tricks like this can help lead to a better understanding of both languages and how they work. It can be fun, and a great learning experience.

On the other hand, doing this for anything in production is something I would hugely avoid. The code you write isn't going to make sense to anybody but you, and is probably going to be slow, error prone, etc.

However, there have been times where I've had to hack together something horrible for "real" work, and playing games like this has helped me figure out how to work around some supposedly pretty firm restrictions in my environment/language/library/etc.

Reed Copsey
+1  A: 

GWT is something similar. It compiles Java code into javascript, although it's much more functional and has a lot more niceties like IDE support, a large community behind it, etc...

Writing your own language which compiles to javascript sounds like a fun project. Personally, though, I would not want to develop in a language developed and maintained by one person and written without any justification other than syntactic sugar.

mweiss
+1  A: 

Maybe the title of this question should be "Should you pretend you are coding in a different language from the one you are actually coding in", and my answer would be "definitely not."

In your example you are inventing a new language with the same capabilities as javascript, except that you are the only person who knows it. The only purpose it serves is to tell the reader that you prefer the structure of Python to that of Javascript. I don't want to learn a new language just so you can express that opinion.

Using macros to make C look like Pascal is another example of this, and derided for the same reason.

A milder form of this (and one of my pet hates) is using whitespace to misrepresent the syntax, typically in C, as in:

int* p;

(where you are pretending the * is bound more closely to int than to p, the opposite of how the parser sees it.)

finnw