Bad Code

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Bad Code

Post by ryl on Wed Jul 06, 2011 4:32 pm
([msg=59513]see Bad Code[/msg])

Hi

Before we get started I know this code is bad and its littered with mistakes thats the point :D ( it took me along time to write bad code...... im not sure if it will run), anyway there are a number of mistakes in the code and one serious one. See if you can find them.
Code: Select all
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class HTS {
   private final static int PORT = 5555;
   
   private static void createImage(InputStreamReader is){
      InputStreamReader inputStream = is;
      int read;
      byte[] data = new byte[1084];
      
      while((read = inputStream.read(data, 0, data.length))!=-1){
         //do some other stuff
      }
   }
   
   public static void main(String[] args) throws Exception{
      ServerSocket sock = new ServerSocket(PORT);
      Socket clisock = sock.accept();
      InputStreamReader is = new  InputStreamReader(clisock.getInputStream());
      createImage(is);
      
   }

}


Ill try and write some thing a bit more serious later :)
ryl
New User
New User
 
Posts: 2
Joined: Tue Jul 05, 2011 4:27 pm
Blog: View Blog (0)


Re: Bad Code

Post by JoeyPardella on Thu Jul 07, 2011 3:53 am
([msg=59534]see Re: Bad Code[/msg])

Code: Select all
while((read = inputStream.read(data, 0, data.length))!=-1){


The method read(char[], int, int) in the type InputStreamReader is not applicable for the arguments (byte[], int, int)

take a look at this:

Code: Select all
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class ImagesDownloader {
    public static void main(String[] args) throws IOException {
        getImages("cars", "bikes", "motors");
    }
   
    private static void getImages(String... folders) throws IOException {
        String imagesRootPath = "http://www.mysite.com/images";
        for (String folder : folders) {
            for (int i = 1; i < 31; i++) {
                URL url = new URL(String.format("%s/%s/%d.gif", imagesRootPath, folder, i));
                InputStream in = url.openStream();
                OutputStream out = new BufferedOutputStream(new FileOutputStream(String.format("images/%s/%d.gif", folder, i)));
                for (int b; (b = in.read()) != -1; ) {
                    out.write(b);
                }
                out.close();
                in.close();
                System.out.println(String.format("Image %d.gif from folder %s has been successfully downloaded.", i, folder));
            }
        }
    }
}



it's not tested but I guess it should work.
JoeyPardella
Experienced User
Experienced User
 
Posts: 81
Joined: Tue Jan 04, 2011 8:43 am
Blog: View Blog (0)


Re: Bad Code

Post by thetan on Fri Jul 08, 2011 9:27 am
([msg=59566]see Re: Bad Code[/msg])

I personally don't consider 25 lines of broken code to be bad code as it's simply just broken code instead. Bad code consist of things that can actually compile and be ran for the most part.

Bad code is a multi-thousand line multi-file big ball of mud project. One of the worse examples of bad coding i see most common is the abuse and misuse of OOP. What i'm referring to specifically is the practice of throwing large chunks of spaghetti procedural code in a class definition and pretending like you're really taking advantage of OOP.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)


Re: Bad Code

Post by martyj2009 on Sun Aug 07, 2011 1:09 am
([msg=60647]see Re: Bad Code[/msg])

So you are just trying to get images from the web? Or are you trying to share images? I'm guessing the first.

ServerSocket is not how you connect to another server. It is how people connect to you.

http://download.oracle.com/javase/6/doc ... ageIO.html
ImageIO is your friend.

Code: Select all
import javax.imageio.ImageIO;
import java.io.File;
import java.net.Url;
public class HTS
{
   private final static string IMAGE_URL = "http://yourwebsite.com/imageLocation/imageName.png";
   private final static string OUT_FILE_NAME = "C:/users/YourUserName/Desktop/";
   public static void main(String[] args)
   {
      try
      {
         ImageIO.write(ImageIO.read(new Url(IMAGE_URL), "png", new File(OUT_FILE_NAME)));
      }
      catch(IllegalArgumentException iae)
      {
         System.out.println("An error occured! " + iae.getMessage());
      }
      catch(IOException ioe)
      {
         System.out.println("Could not read/write the image file! " + ioe.getMessage());
      }
   }
}


This should work. I'm too lazy to compile it and try it. If you have any problems just post.
martyj2009
New User
New User
 
Posts: 5
Joined: Sun Aug 07, 2011 12:24 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests