Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Instantiate an array of objects and class stuff
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2, 3
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Sun Apr 09, 2006 1:27 pm   Post subject: (No subject)

Cornflake wrote:
c++:

void foo(int &bar)
{
    int i = 0;
    for( int j = i; j < i + 10; (j += 2) && (i++) )
    {
        whee();
        bar++;
    }
}


The problem with this, is that it does not take advantage of a lot of subtle visual cues that can aid in parsing the code.

Consider the fact that there's no space between the "for" and the opening parenthesis. This is also a fairly standard convention for function calls. Therefore, even though you intellectually know that "for" is a keyword, your brain has to correct its instant (and subconscious) desire to tag it as a function call.

Consider that opening braces are placed on their own lines. While this is fairly standard C, C++, Obj-C and D convention (C# conventions are rather more complex and nuanced), there is good reason for the standards for Java as set forth by Sun.

Again, it comes down to visual cues. It is perfectly legal to have a set oif braces containing code separate from any other organizational construct in the code. Your brain knows this, and looks for it. Again, your "for(...)" looks like a fucntion call at first glance to your brain. So, the natural thing for you to first assume (subconsciously), is that you have a function call, followed by a block of code, and that you've simply forgotten the semi-colon after the function call. You have to work to correct this with your conscious mind. The conscious mind has a lot less processing power than the sub and unconsious mind, so you're wasting power here.

The last thing I'll note is that, while I understand the goal in introducing extra parentheses in the condition, for such a simple condition, there is no need to disambiguate, and the extra parentheses add an obstacle to the visual parsing of the expression. They force the mind to consciously evaluate where the match is to the opening parenthesis for the "for" construct.

And now, let's look an at example with no pesky syntax to get in the way. Wink

code:
(defun whee ()
   (format t "Whee!~%"))

(defmacro foo (bar)
   `(loop for i = 0 then (+ i 1)
          for j = i then (+ 2 j)
          while (< j (+ i 10))
          do (whee)
             (setf ,bar (+ ,bar 1))))
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Apr 09, 2006 1:44 pm   Post subject: (No subject)

judge yourself: which one is easier to grasp at first sight?

c++:
void foo(int &bar)
{
    int i = 0;
    for( int j = i; j < i + 10; (j += 2) && (i++) )
    {
        whee();
        bar++;
    }

    if( bla )
    {
        ...;
    }
    else
    {
        ...;
    }
}
c++:
void foo(int &bar) {
    int i = 0;

    for (int j = i;  j < i+10;  j += 2, i++) {
        whee();
        bar++;
    }

    if (bla) {
        ...;
    } else {
        ...;
    }
}
Justin_




PostPosted: Sun Apr 09, 2006 2:07 pm   Post subject: (No subject)

To Justin, they are equal. I only prefer the brace on its own seperate line because I think it looks neater.
Dan




PostPosted: Sun Apr 09, 2006 2:23 pm   Post subject: (No subject)

This is geting realy off topic. If we whont to have a debate/discution about formating java code then we should probly make a few topic about it. There for since the question of this thread has been aswered i am locking it. This is not ment to offended any one. Just trying to keep some order.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Sun Apr 09, 2006 2:55 pm   Post subject: (No subject)

The debate rages on at Free Range Code.

http://freerange.compsci.ca/viewtopic.php?pid=218#p218
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 3 of 3  [ 35 Posts ]
Goto page Previous  1, 2, 3
Jump to:   


Style:  
Search: