3
\$\begingroup\$

I am trying to format my code properly. I have already read the programming standards document and I believe that my code follows these rules. I have also used the CtrlShiftF keys in Eclipse which should format the code automatically.

Could someone tell me if this looks ok? It would be nice to get some feedback back and to see if it is being created automatically. I already know that the program does work. Any help would be great. If someone knows a better way to format the code, it would be great to hear it as well.

public class UniString {
    public static void main(String[] args) {
        char[] det = { '\u20ac', '1', '1', '8' };
        // char[]det is an array of characters.

        for (char a : det) {
            /*
            * the for loop takes in the char values and prints them off in
            * order the "det" array enters into the for loop  through "det" ,
            * goes into char a and can be written to the console through
            * System.out.println(a).
            */
            System.out.print(a);
        }
        System.out.println();

        char[] name = { 'j', 'o', 'e' };
        // This is another char array.

        String nameString = new String(name);
        /*
        * The char array is made into a String by loading "name" into a String
        * class
        */

        String changeCase = nameString.toUpperCase();
        /*
        * The String "nameString" is changed to Upper case letters. The string
        * is then stored as "caseChange".
        */

        System.out.println(changeCase);
        // "caseChange" is printed out.
    }
}
\$\endgroup\$
2
\$\begingroup\$

Comments rarely go under a piece of code. Move it above it.

Other than that I'd personally also put the opening parenthesis of the "for" loop right next to the "for:

for(char a : det) { }
\$\endgroup\$
5
\$\begingroup\$

I think all the comments should be removed. The point of a comment is to explain why you wrote this code, not what the code does. If anyone reads the code, they will be able to understand what it does without reading the comments. When you write comments, always ask yourself : "Will this comment help me understand why I wrote this code 5 years ago?" (Or.. 2 weeks if you have a bad memory like mine), if you can't answer, you probably don't need comments!

The formatting looks perfect in my opinion.

\$\endgroup\$
4
\$\begingroup\$

I disagree with @user1021726: Keeping a space after the for keyword is good — it's a keyword, not a function. It should be kept more like return something than return(something).

Comments should go above the code that they refer to. Also, in multi-line comments, the asterisks should line up, like in the JavaDoc guide:

    /**
     * The for loop takes in the char elements of the "det"
     * array and prints them to the console using
     * System.out.print(a).  Afterwards, System.out.println()
     * adds a newline.  The code below should be equivalent
     * to System.out.println(new String(det)); — though
     * probably slower.
     */
    for (char a : det) {
        System.out.print(a);
    }
    System.out.println();
\$\endgroup\$
  • \$\begingroup\$ I absolutely agree. However, I noticed that you changed the multiline comments /* ... */ to JavaDoc comments /** ... */ although the comment refers to code, not a method, field or class. Does this have any special significance? \$\endgroup\$ – amon Oct 22 '13 at 20:18
  • \$\begingroup\$ No, comments inside a method have absolutely no semantic significance, and are certainly not interpreted as JavaDoc. I just expect them to follow the same formatting conventions. I would be just as happy with multi-line // comments. \$\endgroup\$ – 200_success Oct 22 '13 at 22:34
  • \$\begingroup\$ I always use // comments within a method body to allow temporarily commenting out different sections in the method. \$\endgroup\$ – Dave Jarvis Oct 28 '13 at 22:15
4
\$\begingroup\$
// char[]det is an array of characters.
// This is another char array.
...

These are so obvious things. In my opinion, code should have comments to justify "why's" of the code and not "what's" of it.

I feel none of the comments in the code are required. Those just make your code look verbose and do not have any meaning what so ever.

\$\endgroup\$
3
\$\begingroup\$

I'll address some of the naming:

  • det is not a very clear name. If it is short for something, spell it out.

  • The name changeCase is a verb, which doesn't make sense for a variable since it doesn't perform an action; it stores something. Since it receives nameString in uppercase, it can be renamed to something like uppercaseNameString.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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