Jun 27, 2013

Study Notes on Java Code Conventions September 12, 1997

When I was finding some resources, I happened to find an interesting document:

Java
Code Conventions

September 12, 1997 

I was very curious about what rules our respected programmer predecessors made on how to code Java, and these rules are still the rules today. The document can be found here: http://www.oracle.com/technetwork/java/codeconventions-150003.pdf, and below is the notes that I find useful for myself, and maybe useful for you.

1. Initialization
Try to initialize local variables where they’re declared. The only reason not to initialize a

variable where it’s declared is if the initial value depends on some computation occurring first.

2. Number Per Line
One declaration per line is recommended since it encourages commenting. In other words,
    int level; // indentation level
   int size; // size of table
is preferred over
    int level, size;

3. Return Statements
A return statement with a value should not use parentheses unless they make the return value
more obvious in some way. Example:
    return;
   return myDisk.size();

   return (size ? size : defaultSize);

4. if Statements 
Always use braces {}, and avoid the following error-prone form:
    if (condition) //AVOID! THIS OMITS THE BRACES {}!
        statement;

5. switch Statement
A switch statement should have the following form:
    switch (condition) {
        case ABC:
            statements;
            /* falls through */
        case DEF:
            statements;
            break;
        case XYZ:
            statements;
            break;
        default:
            statements;
            break;
    }
Every time a case falls through (doesn’t include a break statement), add a comment where the
breakstatement would normally be. This is shown in the preceding code example with the
/* falls through */ comment.
Every switch statement should include a default case. The break in the default case is
redundant, but it prevents a fall-through error if later another case is added.

6. Casts should be followed by a blank
    myMethod((byte) aNum, (Object) x);
   myFunc((int) (cp + 5), ((int) (i + 3))+ 1);

7. Variable Assignments
Avoid assigning several variables to the same value in a single statement. It is hard to read.
Example:
    fooBar.fChar = barFoo.lchar = 'c'; // AVOID!

8.Special Comments
Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.


Jun 18, 2013

Eclipse cannot start, Java was started but returned exit code=1

The eclipse problem puzzled me for a long time, and sometimes it can be solved by a reboot. But this is very inconvenient if I have many files working at hand. The ini file I use is like below:

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
com.springsource.sts.ide
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
256M
-vm
C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xmn128m
-Xms40m
-Xmx512m
-Xss1m
-XX:PermSize=128m
-XX:MaxPermSize=256m

I tried many ways to solve this problem, but none of them worked.


And I tried to re-install the latest JDK and JRE, and use the raw ini file, which is like below:
-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.100.v20110502
-product
com.springsource.sts.ide
--launcher.defaultAction
openFile
--launcher.XXMaxPermSize
384M
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xmn128m
-Xms256m
-Xmx768m
-Xss1m
-XX:PermSize=128m
-XX:MaxPermSize=384m

It works.

I don't know whether his can solve your problem, but it's worth to have a try.

[Update~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~]

I found the problem happened again, so it looks that reinstall JDK/JRE doesn't solve the problem. I found out that it's because I am using 1up Bins which is a taskbar icon plugin, and this has some conflicts. I killed Bins.exe from task manager, then Eclipse was able to run again. And the problem never happens anymore.

If you find this article useful, please click the advertisement to help! Thank you.