Results 1 to 4 of 4
  1. #1
    Krystah is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Which would be the most effecient alternative?

    This is my first post on the forums, so first I'd like to say

    Hello :)

    I'm wondering what would be the preferred method to do this, and why one is better than the other (if any difference indeed exists).

    Example:

    Option 1 (My professor always use this.. ))
    FileReader fr = new FileReader("example.txt");
    BufferedReader br = new BufferedReader(fr);

    Option 2 (This seems more intuitive to me))
    BufferedReader br = new BufferedReader(new FileReader("example.txt"));

    *THIS QUESTION IS NOT SPECIFIC TO BUFFEREDREADER OR ANY READERS*
    I ask of this because I want to develop a good habit in programming.

    Thanks in advance!

    Krystah

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Hi Krystah, welcome to the forums.

    As far as I know the difference is really quite minimal(they do the same thing), it's generally better to limit chaining when possible though.

    It may help to have the file reader available to you to re-use later. Generally though you will be closing the stream when you are done using it in the finally clause. Others here will probably have more input, but I tend to prefer the first approach(option 1).

    You should consider asking your professor this question as well to see what he thinks.

  3. #3
    Krystah is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the quick reply, sunde887 :)

    I have already asked him this, and he tells me that we should make it a habit to do the first option, although I have my doubts in his habits, seeing how he actually took points away from a student on an assignment for writing like this:

    blabla(){
    codeblabla;
    }

    He said writing with the curlybraces like that is bad habits from old programmers, and that we should all be writing like this:

    blabla()
    {
    codeblabla;
    }

    So I don't really know what to believe. :P

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That's odd that he gave points off for something like that. I don't know about others, but I find code a little nicer looking like this
    Java Code:
    blablah(){
      //code
    }
    However, I have seen lot's of different looks, including
    Java Code:
    blablah()
    {  //do stuff
    }
    I prefer the first method, like I said. It's not a rule, but it's good to try and keep chaining to a minimum. If you get too comfortable with it before you know it you will have code which chains 100 things together.

    I saw a post recently with nearly 100 replace calls all chained together and it leads to some nasty looking code.

    I am however, interested to see what others share as far as which is better and why.

Similar Threads

  1. alternative for ajax
    By site4u in forum New To Java
    Replies: 6
    Last Post: 04-28-2011, 01:15 PM
  2. Alternative to JNLP
    By tecno40 in forum Java Applets
    Replies: 0
    Last Post: 12-22-2010, 12:38 AM
  3. Is there an alternative to OBEX?
    By danielpereira in forum Advanced Java
    Replies: 0
    Last Post: 10-17-2010, 11:36 PM
  4. Replies: 18
    Last Post: 11-29-2009, 11:58 PM
  5. Any Alternative to Swing?
    By Niveditha in forum AWT / Swing
    Replies: 13
    Last Post: 06-27-2008, 09:39 AM

Posting Permissions

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