1

I have 24 Strings that are pretty verbose, like this i.e. "Enter a gene symbol or common name..." Now, typically with shorter string constants and lesser properties, I declare inner enums within the class. However, since there are 24 of these strings, I'm not sure if there is a more efficient or cleaner way of organizing my code.

Additional info:

  • I'm only using these strings once
  • Need to put them into a String array for it to be used for a specific purpose
  • This class containing these Strings, is the only class using them

Here is my current code:

    private static String[] getTemplateDefaults()
    {
        String[] defaults = new String[]
        {
            "Enter Gene symbol or common name",
            "Enter blah blah blah",
            "Enter blah blah blah", 
            "Enter blah blah blah",
            "Enter blah blah blah",
            // continued for 19 more times...
        };

        return defaults;
    }

Really just looking for some thoughts to organize this without it looking so cumbersome.

Thanks!

cno
  • 184
  • 7
  • If you're using each string once, what's the point in defining a constant for it? And what's the point in putting them all in an array? What do you gain? Nothing. What do you lose? Readability. `System.out.println("Enter a gene symbol or common name...")` is much more readable than `System.out.println(getTemplateDefaults()[5])` – JB Nizet Jul 25 '14 at 15:36
  • The method I'm passing it to requires an array. I'm just wondering if I should do it on the fly like the way I have it or do it another way. – cno Jul 25 '14 at 15:46
  • 1
    If the goal is to have a constant, immutable list of strings, there's really no reason to generate a new array every time. Define it as a `static final List MESSAGES` and initialize this constant with a `Collections.unmodifiableList()`. – JB Nizet Jul 25 '14 at 15:49
  • I've decided to just use a private static final variable that can't be modified: `private static final String[] DEFAULTS = new String[]{"too many strings"};` as opposed to calling a method. Doing this allows no further initialization and the reader can make changes without referencing another file. – cno Jul 25 '14 at 17:32
  • An array is modifiable. That's why I recommended an unmodifiable list. – JB Nizet Jul 25 '14 at 18:04

2 Answers2

7

Really just looking for some thoughts to organize this without it looking so cumbersome.

I would rather put these constant Strings to some properties file and read it on load, that way you will have ability to modify these Strings without having to recompile code and it would be neat

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Also it would support converting the app to another language without changing code – Loki Jul 25 '14 at 15:38
  • I upvoted your answer because it's a good general solution. However for my problem, the strings do need to be present visibly.Thanks for your input! – cno Jul 25 '14 at 17:26
  • why not this suggestion ? – jmj Jul 25 '14 at 17:29
  • I still need the strings to be present in the class, so using your suggestion isn't applicable for me. Please see my comment on my original question for what I've used instead. – cno Jul 25 '14 at 17:37
  • You can still do that, they are just persisted outside, on load of app [you can still read it with one line of code into memory and populate array or List](http://stackoverflow.com/questions/8285595/reading-properties-file-in-java) – jmj Jul 25 '14 at 17:38
0

Java provides support for string tables. You should be using these rather than hard-coding strings into your application.

You will also be able to generalise to multi-language support with ease if you adopt this technique.

Most integrated development environments support easy editing of string table resources.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483