Results 1 to 10 of 10
Like Tree2Likes
  • 2 Post By Tolls

Thread: Question about standard code style

  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Question about standard code style

    why is it that in loops when comparing array the standard is
    i<=array.length-1
    wouldn't it be easier to say
    i<array.length
    ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question about standard code style

    Yep, I use the second form; the first form is for people who hardly know how indexing in BCPL/C/C++/Java/C# works. When I have to iterate through an array backwards I use:

    Java Code:
    for (int i= array.lengh; i-- > 0; ) { ... }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Question about standard code style

    Quote Originally Posted by lenois View Post
    why is it that in loops when comparing array the standard is
    i<=array.length-1
    wouldn't it be easier to say
    i<array.length
    ?
    The former isn't the standard.
    The latter is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: Question about standard code style

    Quote Originally Posted by Tolls View Post
    The former isn't the standard.
    The latter is.
    I made an assumption since all the textbooks I have seen do it the former way.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Question about standard code style

    Then I would consider looking for new textbooks.
    DarrylBurke and rsmccli like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question about standard code style

    Quote Originally Posted by lenois View Post
    I made an assumption since all the textbooks I have seen do it the former way.
    Are your textbooks written by Herbert Schildt? If so, throw them away.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Question about standard code style

    well in India the text books do it the former way and it makes students(like me) grasp the code faster.I don't know how other countries have iteration techniques and remember the both have different outputs.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Question about standard code style

    Quote Originally Posted by harshit shah View Post
    well in India the text books do it the former way and it makes students(like me) grasp the code faster.I don't know how other countries have iteration techniques and remember the both have different outputs.
    I cant see how former way causes students to grasp it any faster, if anything it just complicates things. Both ways are acceptable but the second is quicker and easier to write.

    Regards.
    Last edited by Ronin; 02-08-2013 at 01:56 PM.

  9. #9
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    86
    Rep Power
    0

    Default Re: Question about standard code style

    Quote Originally Posted by Ronin View Post
    I cant see how former way causes students to grasp it any faster, if anything it just complicates things. Both ways are acceptable but the second is quicker and easier to write.

    Regards.
    Now,how about creating a poll that would make it be easier mate.we all have different opinions.

  10. #10
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Question about standard code style

    Quote Originally Posted by harshit shah View Post
    Now,how about creating a poll that would make it be easier mate.we all have different opinions.

Similar Threads

  1. Code question. Where to post.
    By Tullamore in forum New To Java
    Replies: 6
    Last Post: 04-06-2012, 06:10 AM
  2. Question with code
    By maximumammo in forum New To Java
    Replies: 1
    Last Post: 04-03-2012, 11:22 PM
  3. How do I shut off auto code style implementation?
    By StateMachine in forum Eclipse
    Replies: 0
    Last Post: 01-21-2012, 09:30 PM
  4. Replies: 4
    Last Post: 07-19-2011, 02:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •