1

hi guys I already searched a lot but weren't really satisfied with what I found. hope it's the right place to ask this question.

I'm doing Java now for a small amount of time (changed from C) and have problems of getting a grip of how to structure my code best for OOP.

let's give a simple example:

If I'm using some predefined strings (let's say e.g. filepaths or error messages) I'm currently creating an own class doing something like:

private static final String libPath = "\\this\\is\\a\\path\\";
private static final String notFoundMessage = "This hasn't been found";

public static String getLibPath() {
return libPath;
}

public static final String getNotFoundMessage() {
return notFoundMessage;
}

...

Would it be better to create a Map, add everything to it and get it by key? Or am I doing it completely wrong?

Second example: let's say I return an error string somewhere

public String getSomething() {
  if (something != null) {
    return something;
  } else {
   //handle error, return string below
  }
return "I HAVE AN ERROR";
}

And anywhere else in my program I'm checking for the return value:

if (!string.equals("I HAVE AN ERROR")) {
//do something
}
else {
// handle error
}

that's obviously a bad way having to change the code twice once the error message changes. and yeah, I could define the error string the same way I'm doing it in the first example but as I'm not satisfied with that one either I'm reaching a dead end.

would be glad to hear some of your suggestions how to properly do OOP !

12dollar
  • 635
  • 3
  • 14

3 Answers3

2

First example :

private static final String libPath = "\\this\\is\\a\\path\\";
private static final String notFoundMessage = "This hasn't been found";

public static String getLibPath() {
  return libPath;
}

public static final String getNotFoundMessage() {
  return notFoundMessage;
}

...

In this case, no need to create a Map. That is the right way to do it. Just note that the libPath would be better defined like this :

private static final Path libPath = Paths.get("this", "is", "a", "path");

(The class Path exists since Java 7, current version is Java 8)

Second example:

public String getSomething() {
  if (something != null) {
    return something;
  } else {
    //handle error, return string below
  }
  return "I HAVE AN ERROR";
}

No : Never return error codes in Java. Prefer using an exception.

Example :

public class ElementNotFoundException extends Exception {
  ...
}

public String getSomething() {
  if (something == null) {
    throw new ElementNotFoundException();
  } else {
    return something;
  }

}

Then, you handle the exception like this :

try {
  myObject.getSomething();
} catch(ElementNotFoundException e) {
  //handle error
}
Arnaud Denoyelle
  • 25,847
  • 10
  • 73
  • 128
  • thanks for the fast response and the information about Paths.get. About throwing an exception. As your ElementNotFoundException() seems to be an uncatched one (RuntimeException) I'm confused about how throwing an exception would be preffered over handling an error so the user doesn't actually gets a grip of it. would be glad if you could specify it some more – 12dollar Feb 23 '15 at 10:00
  • @TwelveDollar In the example, you define the Exception yourself. Hence you decide if it extends `RuntimeException` or not. Not extending RuntimeException will force the developer to handle the exception. An error code is evil because it will not warn if it is not caught and causes silent bugs. – Arnaud Denoyelle Feb 23 '15 at 10:04
  • yeah your recent edit already answered my questions. I was confused about not adding try/catch to handle the exception (which would be my preferred way over a RuntimeException). thanks again – 12dollar Feb 23 '15 at 10:14
0

For the first example, take a look at Internationalization: http://docs.oracle.com/javase/tutorial/i18n/

You can use statics or maps, but sooner or later you will need to show the messages in several languages.

For the second example, it's better to use Exceptions as they are intended to be used when an abnormal condition (like an error) happens.

Anyway, with Exceptions take care not to use it as flow control structures: Why not use exceptions as regular flow of control?

Community
  • 1
  • 1
antonio
  • 17,130
  • 4
  • 43
  • 56
-1

Here are some examples for handling constants throug out your code:

1. Class

public final class MyConstants {
     public static final int ERROR_CODE = -1;
}

if (getSomething() == MyConstants.ERROR_CODE) { 
   // ...
}

2. Interface

public interface MyConstantsHolder {
     int ERROR_CODE = -1;
}

public MyClass implements MyConstantsHolder {
     public void myMethod() {
       if (getSomething() == ERROR_CODE) { 
          // ...
       }
     }
}
Stephan
  • 37,597
  • 55
  • 216
  • 310