Results 1 to 13 of 13
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By KevinWorkman
  • 1 Post By JosAH

Thread: Bad Code method

  1. #1
    manole is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default Bad Code method

    Contained in the provided workspace is CodeTestExerciseBadCode, in this class is a method called readFile().

    The instructions for this problem are as follows:

    1. Please list your observations about this class, including bugs, poor programming, inefficiencies, etc...
    2. Provide an alternate implementation that addresses your observations above, please call the method readFileFixed().

    Java Code:
    package codeTest;
    
    import java.io.File;
    import java.io.FileReader;
    
    
    public class CodeTestExerciseBadCode {
    
    	public CodeTestExerciseBadCode(){
    		
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode();
    		System.out.println(part2.readFile());		
    	}
    
    	public String readFile(){
    		File f = null;
    		FileReader fr = null;
    		StringBuffer content = null;
    		try{
    			f = new File("c:/samplefile.txt");
    			fr = new FileReader(f);
    			
    			int c;			
    			while((c = fr.read()) != -1){				
    				if(content == null){
    					content = new StringBuffer();
    				}
    				
    				content.append((char)c);
    			}
    			
    			fr.close();			
    		}
    		catch (Exception e) {
    			throw new RuntimeException("An error occured reading your file");
    		}		
    		
    		return content.toString();
    	}
    }
    I just can't figure out what is wrong with it! Help please!!
    Thank you!
    Last edited by JosAH; 06-27-2011 at 04:56 PM. Reason: Added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    1) What happens if the actual reading from the FileReader throws an IOException? Your FileReader stays open.
    2) What happens if the file is empty? The StringBuffer content will still be null after the loop.
    3) A StringBuilder is a bit faster than a StringBuffer.
    4) You don't need a File object to open a FileReader.
    5) You're not passing back what actually went wrong while attempting to read the file.
    6) Reading entire lines is much faster than reading single characters.

    kind regards,

    Jos
    manole likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,867
    Rep Power
    8

    Default

    So what you're saying is that you want us to do your homework for you? How much are you paying?
    Dark likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Jos is getting soft in his moderatordom...:)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Jos is getting soft in his moderatordom...:)
    I showed the true me: a cute, sweet, little puppy. It must be the weather: it is great out here; I've been working in my garden all day (in front of my laptop, sitting at my picknick table); the fridge in the barn is full of Grolsch and I have enough tobacco. Life is good for once ;-)

    kind regards,

    Jos
    sunde887 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    I showed the true me: a cute, sweet, little puppy.
    Who are you and what have you done with the real Jos?

  7. #7
    manole is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    How else should we supose to learn if not by asking questions?
    What a lovely bunch of ppl us r ;)
    tks anyway

  8. #8
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hi Manole

    The people here are helpful, but you must make it as easy as possible for them to help you.

    You can do that by stating Exactly what your issue is. In my opinion it is no good just pasting some code, and asking what's wrong with it.

    Use the Debugger, find out where your code is going wrong. If you have found out some unexpected behaviour in a certain place in your code, and you're not sure why it is happening. Then ask here. I guarantee you, you'll get some help that way.

    Advice from one n00b to another

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Who are you and what have you done with the real Jos?
    I'm the good side of Jos; I put away that grumpy old Jos for the day because the weather is so nice today! and ... Let me out of here you moron! And don't you dare to touch my supply of Grolsch! Where are my rotating knives? I want my tobacco!

    not so kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,867
    Rep Power
    8

    Default

    Quote Originally Posted by Ciwan View Post
    In my opinion it is no good just pasting some code, and asking what's wrong with it.
    Especially when the entire point of the assignment was to look through the code and list what's wrong with it.

    Looks like somebody owes Jos some money (or whatever payment he takes (I have a feeling I know what he'll ask for)).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Jos would be a wonderful CodeTestExerciseBadCode.
    No bug ever had to calculate its fitnessfunction.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Jodokus View Post
    Jos would be a wonderful CodeTestExerciseBadCode.
    I'd rather be a GrolschAndStrongTobaccoSink object ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Yes, I didn't become what I wished for either
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. How to call an XML-RPC method from Java Code
    By sreekanth.srk in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-31-2010, 12:36 PM
  2. java method injection in existing code
    By abanmitra in forum Advanced Java
    Replies: 6
    Last Post: 04-30-2010, 12:02 PM
  3. Java method to run code from string
    By jforce93 in forum Advanced Java
    Replies: 2
    Last Post: 02-02-2010, 10:33 PM
  4. Sychronizing a part of a code in a method
    By SarahB in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-03-2010, 10:22 PM
  5. What will be output by the following code? using trim method.
    By racewithferrari in forum New To Java
    Replies: 4
    Last Post: 11-17-2009, 09:40 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
  •