Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

How to fix this duplicated code

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Code is in Java.

 

I am trying to figure out a way to do it without the duplicated code.

 

The only thing I can think of is to build a Java class that looks at the string directory of the image file and check

the substring after the word "link" and before the "." (period). This gives me a string value representing

the number which I can parse into an integer value and use possibly with a for loop to continously add the image into the list.

 

Is it worth the effort or is this duplicated code an exception to good software engineering principles?

 

try {
            image = ImageIO.read(new File("src/Link/leftAnimation/link0.PNG"));
        
        } catch (IOException e) {
                                    // TODO Auto-generated catch block
            e.printStackTrace();
        }
            leftAnimationList.add(image);
 
try {
            image = ImageIO.read(new File("src/Link/leftAnimation/link1.PNG"));
        
        } catch (IOException e) {
                                    // TODO Auto-generated catch block
            e.printStackTrace();
        }
            leftAnimationList.add(image);

Share this post


Link to post
Share on other sites
Advertisement

It is not an exception and it is bad engineering.

 

Unfortunately, it’s Java, and I don’t remember the syntax to make it “right” or if that way works at all in Java, but if you know C++ you may be able to convert the code snippet I present to Java on your own (or someone else may be able).

static const char * s_szLeftAnims[] = {
    "src/Link/leftAnimation/link0.PNG",
    "src/Link/leftAnimation/link1.PNG",
    …
};
 
for ( int I = 0; I < s_szLeftAnims.GetArrayLength(); ++I ) {
    // Access the s_szLeftAnims array to get each file name sequentially.
}

Reading a file to get the list of files you want to open is another option.

 

 

L. Spiro

Edited by L. Spiro

Share this post


Link to post
Share on other sites

It is not an exception and it is bad engineering.

 

Unfortunately, it’s Java, and I don’t remember the syntax to make it “right” or if that way works at all in Java, but if you know C++ you may be able to convert the code snippet I present to Java on your own (or someone else may be able).

static const char * s_szLeftAnims[] = {
    "src/Link/leftAnimation/link0.PNG",
    "src/Link/leftAnimation/link1.PNG",
    …
};
 
for ( int I = 0; I < s_szLeftAnims.GetArrayLength(); ++I ) {
    // Access the s_szLeftAnims array to get each file name sequentially.
}

Reading a file to get the list of files you want to open is another option.

 

 

L. Spiro

Ah yes! You gave me an idea!

Store the string directories in an array of strings and use the for loop that access the elements of the string array!

 

By the way, do I necessarily have to read a file for example creating a textfile that has the directory paths? Seems like extra work to me. When the array implementation on the directories seems to be enough to get it done.

 

The code below will work assume linkLeftAnimationArray is the array that contains the string directories.

for(int i = 0; i < linkLeftAnimationArray.length();i++)
{
try {
            image = ImageIO.read(new File(linkLeftAnimationArray[i]));
        
        } catch (IOException e) {
                                    // TODO Auto-generated catch block
            e.printStackTrace();
        }
            leftAnimationList.add(image);
}
 
Edited by warnexus

Share this post


Link to post
Share on other sites

An expression I read about three decades ago:  Programmers never repeat themselves. They loop.

That's an awesome motto! That is a true statement! Definitely, the for loop seems to be the solution that works everytime when I run into a code duplication. Thanks frob and L. Spiro!

Share this post


Link to post
Share on other sites

Yeah, checked exceptions often create more problems than they solve. I often find myself writing methods like the following to keep the call sites clean.

/**
 * Loads an image from disk.
 * @return the image or null if loading failed.
 */
public static Image tryRead(String path) {
	try {
		return ImageIO.read(new File(path));
	} catch(IOException e) {
		e.printStackTrace();
		return null;
	}
}

/**
 * Loads an image from disk. Throws if loading failed.
 * @return the image
 */
public static Image read(String path) {
	try {
		return ImageIO.read(new File(path));
	} catch(IOException e) {
		throw new RuntimeException("Couldn't read image.", e);
	}
}

Share this post


Link to post
Share on other sites

Programmers have another saying: Don't catch exceptions. Another one: Throw early, catch late.

 

I know that IOException is annoying in Java, so I sometimes do the second version of lwm's suggestion. Often, though, I'm willing to add the throws IOException onto my method.

 

You're not dealing with the exceptions very well by dumping a call trace and sticking nulls in your list. Wrapping each individual line of exception-throwing code in an auto-generated try statement is not a good software engineering principle.

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!