• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Code Convention

 
Laura Pecoraro
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am a new user and I have a question about the "Code Convention" when write my solution...

For exampe the "TestClass" is write correctly, or not... (my code convention have an influence on final points) ?

/**
* The test class...
*/
public TestClass {

//
// Costructors
//
/**
*
*/
public TestClass() {
// do something....
}

}

Thanks all

Laura
 
harilal ithikkat
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Laura Welcome to JAVARANCH

check it out
 
Laura Pecoraro
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
harilal ithikkat wrote:Hi Laura Welcome to JAVARANCH

check it out


Thanks... Laura
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy y'all.

I took a very fast look at the link provided, and apparently the conventions presented there are not totally correct. For instance, this is how it says to write an "if":



And this is wrong... the correct form would be:



So I recommend to stick to the official Java code conventions.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

just reiterating one of Laura's questions.

(my code convention have an influence on final points) ?


I have read the requirements of my assignment but I didnt see any mentioned of java code conventions advised or required.

I did read Andrew Monkhouse 's book and it does discuss java conventions.

Is it really needed?

Will using Eclipse's 'format' function satisfy the requirements for java code conventions?

I am asking about java code convention only; not including javadoc, as I understand that javadoc (and proper that is) is required.

I am hoping for your comments regarding how needed is the java code convention following.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesus,

The format function of Eclipse (which I used too and altered according to the specifics discussed in Andrew's book) will only format your code: so putting in spaces, adding a blank line, remove unused imports,...

The Sun's Java Code Conventions also includes how you should name variables, classes, interfaces, constants,... I followed the naming conventions strictly (because that's just a habit, I do it on my real job too: MyClass, myVariable, myMethod, MY_CONSTANT) and I think following the naming conventions is more important than adding 2 blank lines between package and import statements (just an example). So I would certainly advice you to follow the naming conventions and follow the other conventions as closely as possible (with a format function like Eclipse this is very easy to do), because it will result in more readible code.

If you don't follow naming conventions I think you may lose some points, if you have just 1 line between package and import I don't think you will. But of course this question is difficult to answer (and it is just speculation of myself), because you don't get any feedback why you have lost some points on a particular section (you will only get feedback when you fail).

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, y'all.

Other than that, you'll also have to do some customization to Eclipse's formatting function. For instance, the tools is configured by default to add tabs instead of 4 blank spaces in identation. So what you can do is read the document and apply those rules to the formatting function.

Roel De Nijs wrote:The Sun's Java Code Conventions also includes how you should name variables, classes, interfaces, constants,... I followed the naming conventions strictly (because that's just a habit, I do it on my real job too: MyClass, myVariable, myMethod, MY_CONSTANT) and I think following the naming conventions is more important than adding 2 blank lines between package and import statements (just an example).


Hum... according to the conventions, there should be only 1 line between package and import statements.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Perillo wrote:you'll also have to do some customization to Eclipse's formatting function
That's what I meant with "altering the format function".

Roberto Perillo wrote:according to the conventions, there should be only 1 line between package and import statements.
That's why it was just an example, I didn't study them But if I'm not mistaken, somewhere you have to put 2 blank lines (and according to this link it is between the "beginning comments" and package statement).

Kind regards,
Roel
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:But if I'm not mistaken, somewhere you have to put 2 blank lines (and according to this link it is between the "beginning comments" and package statement).


You know, looking at the document, there are 2 lines between beginning comments and package statement, but if we look at Java's source code (the String class, for instance), there's only 1 line. I myself didn't add any blank line
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My..my..my..

I wont want to wait (long) for my score thinking I didnt do any effort on the java convention. The 'wait' will be more terrifying.

Jeeepsss....

Ill do this last, and last priority, as this so routine. Finish all, then refactor for this.

What do you think?
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do follow the code convention "strictly" then these pointers are crucial:
- extra line between package, import, class, contructor, instance variables, methods, local variables, actual implementation
- consistency with where you put your "opening braces" for classes, constuctors and methods
- use meaningful names
- camelCase for methods and variables

If you really must know code conventions points are considered general consideration.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jesus,

K. Tsang wrote:- consistency with where you put your "opening braces" for classes, constuctors and methods
- use meaningful names
- camelCase for methods and variables
I think K. Tsang named the 3 really important things:
  • consistency throughout all your code (and not only regarding the curly braces, but also on indentation for example. It improves readibility and don't forget that someone else has to look into your code)
  • meaningful names for everything (local variables, instance variables, constants, methods, classes)
  • naming conventions (camelCase, start classes and interfaces with an uppercase letter,...)

  • The blank lines are less important (although they also improve readibility), but in my code I didn't leave a blank line between the local variables and the actual method implementation. I also didn't declare my local variables at the start of the method, but just when I needed them.

    I just would advice to follow these "rules" from the start of your coding, instead of developing your application first and then have to "refactor" everything. It would save you a lot of time in my opinion. If you mean by "refactor" using the Format-function of Eclipse to clean-up your code, put blank lines were you want,... then you can indeed do it at the end (with no wasted time, because it is just some configuration and then a couple of mouse clicks. i did it during development and at the end to be really sure everything was ok ). If you mean refactoring variable names, class names,... I would advise against doing it at the end.

    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic