2
\$\begingroup\$

I'm developing a program for users from a VBulletin's forum. This forum has a lot of people interested in write good articles but some of them don't know how to use the formatting bar in his favor. Sometimes, the others members don't read this threads/articles because it doesn't have a interesting formatting (crazy world, huh?). So here's my idea:

Idea

The user don't need to worry about formatting the thread, so he can be focused just on the writing and the content. The formatting part (the boring part) will be generated by the program, the user just need choose colors for titles and text content.

Code

I've created a class named Markup where all models for formatting will be set.

public class Markup {}

Inside this class I created a private enum to define Which BBcodes are accepted by VBulletin, something like:

enum BBcode {
  BOLD("B"),
  ITALIC("I"),
  IMAGE("IMG"),
  UNDERLINE("U"),
  VIDEO("VIDEO");
  /* many others... */

  final String code;
  private BBcode(String code) {
    this.code = code;
  }

  public String code(){
    return this.code;
  }
}

Still inside Markup class I've created a private class named Syntax to define how the BBcode syntax will created., like this:

class Syntax {
  public String in(BBcode bbcode){
    return "[" + bbcode.code() + "]"; // will return "[BOLD]" for example.
  }

  public String inValued(BBcode bbcode, String value){
    return "[" + bbcode.code() + "=" + value + "]"; // will return "[COLOR=#333]" for example.
  }

  public String out(BBcode bbcode){
    return "[/" + bbcode.code() + "]"; // will return "[/BOLD]" for example.
  }
}

So I can handle it like:

public String setContents(BBcode bbcode, String contents){
  return syntax.in(bbcode) + contents + syntax.out(bbcode); // will return "[BOLD]foo[/BOLD]
}

My questions:

  1. The names for class and methods are ok? Other peoples can be part of the project and I want them to know what the class does.
  2. All this stuff are inside the Markup class (because the BBcode and the Syntax will be util just for the Markup). It's a good practice group similar things inside a unique class?
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

First of all you make 1 big fault by saying that your classes inside MarkUp are private.
They aren't, they are package private, more info on site of oracle.

Then, let's start with the enum.
Your final String code is also package private. => change them to private.
Your getter for the code is also a little off the normalisation, let's try to call it getCode.

Final result could be :

private enum BBcode {

    BOLD("B"),
    ITALIC("I"),
    IMAGE("IMG"),
    UNDERLINE("U"),
    VIDEO("VIDEO");
    /* many others... */

    private final String code;

    private BBcode(String code) {
        this.code = code;
    }

    public String getCode() {
        return this.code;
    }
}

The class Syntax you could add constants in stead of repeating your chars.
Then also, you have repeating stuff what you can put together.
The naming in, out are for me to broad.

Example of how I should make the class :

private class Syntax {

    private static final char START_OPENING_TAG = '[';
    private static final String START_CLOSING_TAG = "[/";
    private static final char CLOSING_TAG = ']';
    private static final char EQUAL_SIGN = '=';

    public String getOpeningTag(BBcode bbcode) {
        return addStartOpeningTag( bbcode.code()); // will return "[BOLD]" for example.
    }

    public String getOpeningTag(BBcode bbcode, String value) {
        return addStartOpeningTag(bbcode.code() + EQUAL_SIGN + value); // will return "[COLOR=#333]" for example.
    }

    public String getClosingTag(BBcode bbcode) {
        return addStartClosingTag( bbcode.code()); // will return "[/BOLD]" for example.
    }

    private String addStartClosingTag(String value) {
        return START_CLOSING_TAG + value + CLOSING_TAG;
    }

    private String addStartOpeningTag(String value) {
        return START_OPENING_TAG + value + CLOSING_TAG;
    }
}

As you can see now, I can easily refactor the code.
Let's say you class grows and we need to change the ] to >
For me it's one little change and it's completly done.
Your old class, you have already to change it 3 places, possible fault's .(forgetting one to change) waiting to happen when your class grows.

\$\endgroup\$
3
  • \$\begingroup\$ +1 for the access controll info, thanks. I tested your code and it returned "[/[/B" in getClosingTag, isn't the result I'm expecting. \$\endgroup\$ Commented Dec 11, 2014 at 16:25
  • 1
    \$\begingroup\$ edited the answer, now it should be correct. was 7 am when I wrote it so wasn't really good awake I guess ;) \$\endgroup\$
    – chillworld
    Commented Dec 11, 2014 at 19:39
  • \$\begingroup\$ Thank you. Very interesting the logic you've used to call a method inside another one. \$\endgroup\$ Commented Dec 13, 2014 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.