Skip to main content
deleted 89 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

You could chain these together, which will make it slightly shorter:

methods.append(toGetterMethod(type, name));
methods.append("\n\n");
methods.append(toSetterMethod(type, name));
methods.append("\n\n");
methods.append(toPropertyMethod(type, name));
methods.append("\n\n");

like this:

methods.append(toGetterMethod(type, name))
.append("\n\n")
.append(toSetterMethod(type, name))
.append("\n\n")
.append(toPropertyMethod(type, name))
.append("\n\n");

You could chain these together, which will make it slightly shorter:

methods.append(toGetterMethod(type, name));
methods.append("\n\n");
methods.append(toSetterMethod(type, name));
methods.append("\n\n");
methods.append(toPropertyMethod(type, name));
methods.append("\n\n");

like this:

methods.append(toGetterMethod(type, name))
.append("\n\n")
.append(toSetterMethod(type, name))
.append("\n\n")
.append(toPropertyMethod(type, name))
.append("\n\n");
deleted 89 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

This is really hard to read:

String code = "import javafx.beans.property.*;\n" + "\n" + "/**\n"
            + " * Class Information\n" + " * @author Your Name\n" + " */\n"
            + "public class %s {\n" + "\n" + "%s\n" + "\n"
            + "    public %s() {\n" + "%s\n" + "    }\n" + "\n" + "%s\n"
            + "\n" + "}";

For one thing, why write a string segmented like this:

"import javafx.beans.property.*;\n" + "\n" + "/**\n"

why not as:

"import javafx.beans.property.*;\n\n/**\n"

And the whole expression would become a lot easier to read if you break a line after each actual \n in the string, like this:

String code = "import javafx.beans.property.*;\n\n"
        + "/**\n"
        + " * Class Information\n"
        + " * @author Your Name\n"
        + " */\n"
        + "public class %s {\n\n"
        + "%s\n\n"
        + "    public %s() {\n"
        + "%s\n"
        + "    }\n\n"
        + "%s\n\n"
        + "}";

You don't need to call .toString() on parameters when formatting a string:

return String.format(code, className, fields.toString(), className,
            initCodes.toString(), methods.toString());

That is:

return String.format(code, className, fields, className, initCodes, methods);

This is really hard to read:

String code = "import javafx.beans.property.*;\n" + "\n" + "/**\n"
            + " * Class Information\n" + " * @author Your Name\n" + " */\n"
            + "public class %s {\n" + "\n" + "%s\n" + "\n"
            + "    public %s() {\n" + "%s\n" + "    }\n" + "\n" + "%s\n"
            + "\n" + "}";

For one thing, why write a string segmented like this:

"import javafx.beans.property.*;\n" + "\n" + "/**\n"

why not as:

"import javafx.beans.property.*;\n\n/**\n"

And the whole expression would become a lot easier to read if you break a line after each actual \n in the string, like this:

String code = "import javafx.beans.property.*;\n\n"
        + "/**\n"
        + " * Class Information\n"
        + " * @author Your Name\n"
        + " */\n"
        + "public class %s {\n\n"
        + "%s\n\n"
        + "    public %s() {\n"
        + "%s\n"
        + "    }\n\n"
        + "%s\n\n"
        + "}";

You don't need to call .toString() on parameters when formatting a string:

return String.format(code, className, fields.toString(), className,
            initCodes.toString(), methods.toString());

That is:

return String.format(code, className, fields, className, initCodes, methods);
deleted 89 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Code duplicationDuplicated string literals in FXMLController.initialize

You're duplicating the names used by the enums:

propertyTypeList.addAll("String", "Integer", "Boolean", "Double",
        "Float", "Long");

This is not good. As usual, if you make a change in one place, you have to remember to make the same change at the other too. It's fragile.

It would be better to iterate over the values of the enum, for example:

for (JavaDataType javaDataType : JavaDataType.values()) {
    propertyTypeList.add(javaDataType.toString());
}

Or if you fancy Java 8:

propertyTypeList.addAll(Arrays.asList(JavaDataType.values()).stream()
        .map(JavaDataType::toString)
        .collect(Collectors.toList()));

Code duplicationDuplicated string literals in JavaDataType.fromString

Similar to the earlier point, it's a pity to duplicate the string values of the enums in the case statements. Better build a static map from them and use it, without duplicating anything.

private static final Map<String, JavaDataType> STRING_TO_ENUM = new HashMap<>();

static {
    for (JavaDataType javaDataType : values()) {
        STRING_TO_ENUM.put(javaDataType.toString(), javaDataType);
    }
}

public static JavaDataType fromString(String type) {
    return STRING_TO_ENUM.get(type);
}

Hardcoded Java class names

Still about JavaDataType... Instead of hardcoding the class names of String, Double, ..., how about using the real class types instead?

STRING(String.class),
INTEGER(Integer.class),
BOOLEAN(Boolean.class),
DOUBLE(Double.class),
FLOAT(Float.class),
LONG(Long.class);

private final String value;

JavaDataType(Class<?> klass) {
    this.value = klass.getSimpleName();
}

Minor things

This javadoc comment is really pointless, especially on a private field:

/**
 * String value
 */
private final String value;

to be continued...

TTGTB, will continue tomorrow morning, probably, unless others finish up first...

Code duplication in FXMLController.initialize

You're duplicating the names used by the enums:

propertyTypeList.addAll("String", "Integer", "Boolean", "Double",
        "Float", "Long");

This is not good. As usual, if you make a change in one place, you have to remember to make the same change at the other too. It's fragile.

It would be better to iterate over the values of the enum, for example:

for (JavaDataType javaDataType : JavaDataType.values()) {
    propertyTypeList.add(javaDataType.toString());
}

Or if you fancy Java 8:

propertyTypeList.addAll(Arrays.asList(JavaDataType.values()).stream()
        .map(JavaDataType::toString)
        .collect(Collectors.toList()));

Code duplication in JavaDataType.fromString

Similar to the earlier point, it's a pity to duplicate the string values of the enums in the case statements. Better build a static map from them and use it, without duplicating anything.

private static final Map<String, JavaDataType> STRING_TO_ENUM = new HashMap<>();

static {
    for (JavaDataType javaDataType : values()) {
        STRING_TO_ENUM.put(javaDataType.toString(), javaDataType);
    }
}

public static JavaDataType fromString(String type) {
    return STRING_TO_ENUM.get(type);
}

Hardcoded Java class names

Still about JavaDataType... Instead of hardcoding the class names of String, Double, ..., how about using the real class types instead?

STRING(String.class),
INTEGER(Integer.class),
BOOLEAN(Boolean.class),
DOUBLE(Double.class),
FLOAT(Float.class),
LONG(Long.class);

private final String value;

JavaDataType(Class<?> klass) {
    this.value = klass.getSimpleName();
}

Minor things

This javadoc comment is really pointless, especially on a private field:

/**
 * String value
 */
private final String value;

to be continued...

TTGTB, will continue tomorrow morning, probably, unless others finish up first...

Duplicated string literals in FXMLController.initialize

You're duplicating the names used by the enums:

propertyTypeList.addAll("String", "Integer", "Boolean", "Double",
        "Float", "Long");

This is not good. As usual, if you make a change in one place, you have to remember to make the same change at the other too. It's fragile.

It would be better to iterate over the values of the enum, for example:

for (JavaDataType javaDataType : JavaDataType.values()) {
    propertyTypeList.add(javaDataType.toString());
}

Or if you fancy Java 8:

propertyTypeList.addAll(Arrays.asList(JavaDataType.values()).stream()
        .map(JavaDataType::toString)
        .collect(Collectors.toList()));

Duplicated string literals in JavaDataType.fromString

Similar to the earlier point, it's a pity to duplicate the string values of the enums in the case statements. Better build a static map from them and use it, without duplicating anything.

private static final Map<String, JavaDataType> STRING_TO_ENUM = new HashMap<>();

static {
    for (JavaDataType javaDataType : values()) {
        STRING_TO_ENUM.put(javaDataType.toString(), javaDataType);
    }
}

public static JavaDataType fromString(String type) {
    return STRING_TO_ENUM.get(type);
}

Hardcoded Java class names

Still about JavaDataType... Instead of hardcoding the class names of String, Double, ..., how about using the real class types instead?

STRING(String.class),
INTEGER(Integer.class),
BOOLEAN(Boolean.class),
DOUBLE(Double.class),
FLOAT(Float.class),
LONG(Long.class);

private final String value;

JavaDataType(Class<?> klass) {
    this.value = klass.getSimpleName();
}

Minor things

This javadoc comment is really pointless, especially on a private field:

/**
 * String value
 */
private final String value;
added 470 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading