Reply to topic
Languages compared
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Here's an idea. Post some short code snippets in your language of choice, and then others can rewrite the code in whatever their favorite is. Let's say a max of 15 or so lines for the original snippets. I'll start with Ruby:

Code:

languages = [
   {:name => 'Perl', :desc => 'unreadable'},
   {:name => 'CF', :desc => 'taggy'},
   {:name => 'Ruby', :desc => 'beautiful'},
   {:name => 'PHP', :desc => 'spaghetti'}
]

languages.sort_by { |lang| lang[:desc] }.each do |lang|
  puts "#{lang[:name]} is full of #{ lang[:desc] } code" unless (lang[:name] == "CF")
end


Output:
Ruby is full of beautiful code
PHP is full of spaghetti code
Perl is full of unreadable code


Points awarded for clarity, brevity, or whatever else you may value in your language Smile
andystops


Joined: 18 Mar 2005
Posts: 34
Location: Deleware Newark
Reply with quote
Ok Heres the same code in CF. I did a cross between scripted and tagged so it gives a nice balance.

Code:

<cfscript>
   lang = structnew();
   StructInsert(lang, "Perl", "unreadable");
   StructInsert(lang, "CF", "taggy but clear");
   StructInsert(lang, "Ruby", "beautiful");
   StructInsert(lang, "PHP", "spaghetti");
   langsort = arraytolist(StructSort( lang, "text", "DESC" ));
</cfscript>

<cfloop list="#langsort#" index="i">
   <cfif i NEQ "Ruby">
      <cfoutput>#i# is full of #lang[i]# code.<br/></cfoutput>
   </cfif>
</cfloop>


Output:
Perl is full of unreadable code.
CF is full of taggy but clear code.
PHP is full of spaghetti code.


Pretty similar to that of others and especially C based languages. but for many programmers it is easy to see where code begins and ends.
Well, I have to say, I used to be a big fan of Rails, but...
comprug
Forum Regular

Joined: 15 Feb 2006
Posts: 341
Reply with quote
I used to be a big fan of rails, and still am, but I don't see any reason to move from cldfusion as it is just as maintanable with CFCs and that styff, and if you want a MVC framework you can use CF on Wheels. As for Ajax, there's the Yahoo UI library which is just as good, and the Script.aculo.us libraries and prototype libraries which RoR uses can be used for any language. and like andy stops said, it DOES make sense to see where code ends, and that stuff.... Now for the languages compared... Try this .......
Code:
<?php
// This creates a system that makes sure someone doesn't log in from a very different IP than when they lst logged in, however, the IP will be different because of Dynamic IPS, so this sees whether the Ip is close enough to be the person.
function validip($ip, $newip) {
$differencethree = abs($three - $nthree);
$differencefour = abs($four - $nfour);
list($one, $two, $three, $four) = split(".", $ip);
list($none, $ntwo, $nthree, $nfour) = split(".", $newip);
if ($one != $none | $two != $ntwo) {
echo "can't be sure about ip, please log in again";
} else if ($differencethree >= 20) {
echo "can't be sure of ip, please log in again";
}
else if ($differencefour >= 50) {
echo "can't be sure of ip, please log in again";
}
else {
echo "test passes";
}
}
//test function
validip("24.93.264.645", "24.13.754.535");



?>
cspencer


Joined: 14 Dec 2004
Posts: 4
Reply with quote
At the risk of putting all other languages to shame, here's a little Python ;)

Code:

languages = {
    'Perl':'line noise',
    'CF':'taggy',
    'Ruby':'yuck',
    'PHP':'spaghetti',
    'Python':'goodness'
}
for lang,desc in sorted(languages.items()):
    if lang != 'CF':
        print '%s is full of %s' % (lang,desc)

pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
OK, since no one else was man enough, this is how bad it is in PHP:
Code:
$languages = array(
array("name" => 'Perl', "desc" => 'unreadable'),
array("name" => 'CF', "desc" => 'taggy'),
array("name" => 'Ruby', "desc" => 'beautiful'),
array("name" => 'PHP', "desc" => 'spaghetti')
);

usort($languages, "lang_compare");

foreach($languages as $lang)
{
        if($lang['name'] != "CF")
                echo $lang['name'] . " is full of " . $lang['desc'] . " code <br/>";
}

function lang_compare($a, $b) {
        return strncasecmp($a['name'], $b['name'], 1);
}


Yes, that's a completely ugly/almost incomprehensible callback function to be able do a sort on an array of arrays(array of hashes in Ruby)


Last edited by pmeserve on Fri Jun 16, 2006 7:01 pm; edited 1 time in total
cspencer


Joined: 14 Dec 2004
Posts: 4
Reply with quote
Javascript is an under-appreciated language.
Code:
<html>
    <body>
    </body>
    <script type="text/javascript">
        var languages = {
            'Perl':'line noise',
            'CF':'taggy',
            'Ruby':'yuck',
            'PHP':'spaghetti',
            'Python':'goodness'
        }
        for(var lang in languages){
            if(lang != 'CF'){
                var desc = languages[lang]
                document.body.innerHTML += lang+' is full of '+desc+'<br/>'
            }
        }
    </script>
</html>
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Hah, Python ties Ruby in lines of code - I think I'd just about have to give it the edge in clarity there too. One question I have is, does that sorted function have the proper ability to sort based on an element of a complex object. i.e. in the original, I could add a third element to the hashes, like :year_created => 1995, and then just change the sort to say languages.sort_by { |lang| lang[:year_created] }

This was the worst part about using PHP, and I was actually surprised that CF seems to have a nice multi-dimensional sort function that can do that

Update: And where's the Javascript sort?
Let's try this....
comprug
Forum Regular

Joined: 15 Feb 2006
Posts: 341
Reply with quote
Now in JSP
Code:
<%
 String langs[] =
       {"Perl",
         "CF":
         "Ruby",
            "PHP",
            "Python",
"JSP"
};
String vals[] = {
"unreadable" , "organized", "beautiful", "spaghetti", "ok", "obsolete"};
 for(int i = 0 & int j = 0; i < langs.length & j < vals.length; i++ & j++) {
      System.out.println(langs[i] + "is full of" + vals[j] + "code");
   }
%>


No sort, but no too bad.
Also, sort may exist, or this code may be wrong, especially the &'s because I have only dabbled with JavaServer Pages.
cspencer


Joined: 14 Dec 2004
Posts: 4
Reply with quote
pmeserve wrote:
Hah, Python ties Ruby in lines of code - I think I'd just about have to give it the edge in clarity there too. One question I have is, does that sorted function have the proper ability to sort based on an element of a complex object. i.e. in the original, I could add a third element to the hashes, like :year_created => 1995, and then just change the sort to say languages.sort_by { |lang| lang[:year_created] }

This was the worst part about using PHP, and I was actually surprised that CF seems to have a nice multi-dimensional sort function that can do that


A sorted(a,b) call basically does a.__cmp__(b), so it'll work fine as long as a.__cmp__ knows how to handle b. For Python's base types, this is well defined. In my code, I compare tuples, which know to compare the first element, then the second if the first are equal, then the third if the second are equal, etc. You can also override this by giving sorted() your custom compare or key function.

For example, if I have a list of tuples:
Code:
langs = [('PHP', 'noddles', 1995), ('Perl', 'noise', 1981), ('Python', 'goodness', 1990)]


I could sort by year using either:
Code:
print sorted(langs, cmp=lambda a,b:cmp(a[2], b[2]))
print sorted(langs, key=lambda a: a[2])



Update: And where's the Javascript sort?


I got lazy Razz However, it's almost as easy as Python. For the curious:
Code:
<html>
    <body>
    </body>
    <script type="text/javascript">
        var languages = [
            ['Perl','line noise'],
            ['CF','taggy'],
            ['Ruby','yuck'],
            ['PHP','spaghetti'],
            ['Python','goodness']
        ]
        languages.sort()
        for(var i in languages){
            var lang = languages[i][0]
            var desc = languages[i][1]
            if(lang != 'CF'){
                document.body.innerHTML += lang+' is full of '+desc+'<br/>'
            }
        }
    </script>
</html>
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Here's the IP validate code in Ruby. Probably could be nicer, but I think it's pretty readable. The collect part is to convert the split/exploded string parts from strings to INTs for later comparison

Code:
def validip(ip, newip)
        ip1 = ip.split(".").collect{|i| i.to_i}
        ip2 = newip.split(".").collect{|i| i.to_i}

        if (ip1[0] != ip2[0] || ip1[1] != ip2[1])
                puts "can't be sure about ip, please log in again"
        elsif ((ip1[2] - ip2[2]).abs >= 20)
                puts "can't be sure of ip1, please log in again"
        elsif ((ip1[3] - ip2[3]).abs >= 50)
                puts "can't be sure of ip2, please log in again"
        else
                puts "test passes"
        end
end

validip("24.93.264.645", "24.13.754.535")


And the sort is the whole fun of the first one comprug! PHP was a complete pain to implement the sort, and given that that's a pretty standard thing to want in a language I think it reflects badly on PHP. That said, I would kill for a PHP strtotime equivalent in Ruby

Well looks like pretty good coverage so far, who's gonna step up and do Perl and .NET? I won't even suggest Java... Cool
As you can tell I'm a complete novice
comprug
Forum Regular

Joined: 15 Feb 2006
Posts: 341
Reply with quote
A couple of important things....
1). as you can tell i'm a complete novice... I just geussed on the JSP, and I didn't even use arrays corectly... I'm also a novice at PHP, and many would also probably conside me a novice at CF too...... What do you think of the JSP? it's not actually that bad... I actually tried to test out my JSP code on my old Linux Builder Plus (stil has JSP) but it gave me a 503 Confused
2). I probably also shoudl have used the levenshtein() function for the validating of IPS, because it senses the % difference in a string from the specified or existing string/float.
3). There are so many functions in one language that you can find in others, but not the one you use, yet the one you use has an advantage. A prime example is the levenshtein() strtotime() and shell_exec(). I really wish I could execute shell in coldfusion so I could use image magick, but instead I have to post to a php script which can be a huge security risk.... I unsuccessfully lobbied to get cfexecute...... It annoys me so much.. That's why they should make CML (combined markup language) combining the best functions from all languages. Allthough, I do have to say that I really do liek CF's Contains operator rather than having to go through strpos() in PHP or grep(). Although CF has is meant for businesse in particular. It has many functiosn PHP doesn't have like Currency conversion and that stuff... I also wonder if Ruby/PHP has module that could rival cfgraph....
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
I really have no familiarity with JSP. It seems interesting, but at this point I'd rather try to focus on expanding my meager knowledge of the languages I do know. Jack of all trades type of thing...

If I was going to generalize a lot, I would say:
- php has a function for just about anything you'd ever want to do(internally)
- cf has all sorts of high-level stuff tacked on to let you create certain really amazing things(mainly end-user) very easily
- ruby is incredibly intuitive and hits home runs on a lot of the basics(anything from switch statements with ranges to a really comprehensive and understandable object model - Rails does crazy things like adding and changing built-in Ruby object methods for ints and dates...I don't think you'll ever have something like that in CF)
cspencer


Joined: 14 Dec 2004
Posts: 4
Reply with quote
pmeserve wrote:
Here's the IP validate code in Ruby. Probably could be nicer, but I think it's pretty readable. The collect part is to convert the split/exploded string parts from strings to INTs for later comparison

Here's the equivalent Python code:
Code:
def validip(ip, newip):
    ip1 = [int(n) for n in ip.split('.')]
    ip2 = [int(n) for n in newip.split('.')]
    for i,n in enumerate(zip(ip1,ip2)):
        n1,n2 = n
        if (i <= 1 and n1 != n2) or (i == 2 and abs(n1-n2) >= 20) or (i == 3 and abs(n1-n2) >= 50):
            print "can't be sure about ip, please log in again"
            return
    print "test passes"

validip("24.93.264.645", "24.13.754.535") # fails
validip("24.93.750.545", "24.93.754.535") # passes
pmeserve
HostMySite Tech

Joined: 19 Mar 2004
Posts: 178
Reply with quote
Python definitely looks like good stuff to my eyes. The one thing that freaks me out is the whole "identation as syntax" thing. It seems in certain ways like a good idea, but just having to deal with Ruby hating on my line breaks was world-shaking enough Wink
An ideal language.... UFPL
comprug
Forum Regular

Joined: 15 Feb 2006
Posts: 341
Reply with quote
This is what I think would be the ideal programming language for this solution and how it would accomplish this situation. First of all, I would like to say that my language is called UFPL (User Friendly Programming Language). UFPl Statements start with
UFPL: If you want to output that without executing code you do this: ~UFPL:~ ~~ escapes aything. So this is how I would accomplish this with UFPL, how it should be accomplished:
Code:
UFPL:
langs is  [ name value
CF : taggy but organized
PHP : complete but spaghetti
Perl : Unreadable
Python :  ok
Ruby : Beautiful, but weird
JSP : Obsolete
Javascript : nice but weird
Lasso : Unknown
Java : Maintainable in 1000 years
Asp.net : ok
UFPL : English
]
end
langs -order name then value then -sort
end
for each langs -write "lang's name is full of lang's value code"
end
UFPL;

Produces:
CF is full of taggy, but organized code
PHP is full of complete, but spaghetti code
Perl is full of unreadable code
Python is full of ok code
Ruby is full of beautiful, but weird code
JSP is full of obsolete code
Javascript is full of nice, but weird code
Lasso is full of unknown code
Java is full of maintanable in 1000 years code
Asp.net is full of ok code
UFPL is full of English code
the basics of UFPL are the following six:
1). Only use quotations for output
2). Do not worry about what datatype a variable is... (e.gints floats etc... just output like cfdump in coldfusion)
3). Use english terms such as i and then just like Coldfusion
4). put dashes before actions so it doesn't confuse it as variable names, operators or output or arrays
5). Use 's instead of dots for sub classes and variables in a structure as shon above with the help of lang's name
6). end ends statements and variables and arrays and ifs and lops and that stuff. every function or variable must have and end. if statements end at the end of the statement block
This is the IP code:
Code:
UFPL:
ValidIP using ip and newip is
ip1 is ip's part 1 in -split ip by .
end
ip2 is newip's part 2 in -split ip by .
end
for each in ip1,
if -abs of -matchobject ip2 is  greater than 20 then write "can't be sure about ip, please log in again"
else if COMMENT: This is just to be sure COMMENT; -abs of -matchobject ip2 is not greater than 20 then write test passes".
end
end
-call ValidIP using 24.93.264.645 and 24.13.754.535
end
-call ValidIP using 24.93.750.545 and 24.93.754.535
end
UFPL;

Produces:
can't be sure about ip, please log in again
test passes
What do you think of UFPL. If I actually had time (enough time to create a language that was open Source) I would create ti exactly like that. Thanks!!
Languages compared
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 2  

  
  
 Reply to topic