reset password
Author Message
cysun
Posts: 2935
Posted 17:23 May 21, 2009 |

I looked at some source code in the homework and wasn't very impressed with what I saw. The problem is not about how the code works, but how the code looks. So I feel I should talk a little bit about coding conventions, especially naming conventions.

Code naming conventions are conventions for naming the variables, methods, classes, ... in your program. For example, it tells you whether you should call a variable blogId, BlogID, or blog_id. It sounds like something trivial, but following established naming conventions has at least two benefits. First, it makes your code at least looks professional. It shows that you are a disciplined programmer and are well trained in that programming language. This may give you an edge when you compete for a programming job. Second, and more importantly, it makes your code more readable, which will save tons of time when you (or somebody else) have to debug or maintain your code.

Different languages have different naming conventions, e.g. the ID of a blog in Java is blogId, in C it's blog_id, and in Hungarian Notation (used by many Windows application developers) is iBlogId where i indicates the variable type. For Java, please check out Java Coding Conventions. It's an easy read. If you are really busy, at least check out Section 9. Naming Conventions, which shouldn't take more than a few minutes to read.

Here are some examples of good names:

  • Class names: Blog, BlogEntry, Comment
  • Variable names: id, blogEntryId

And some bad ones:

  • Class names: blog, Blog_Entry, comment
  • Variable names: ID, blog_entry_id

Note that good names go beyond just mixing cases. Considering the following example:

public class BlogEntry {
int id;
Blog blogId;
User author;
List<Comment> comment;
}

There are two problems here. First, blogId should be blog, because it's a reference to a blog, not to the id of a blog. Second, comment should be comments - using the plural form distinguishes the collection from an element of the collection.

Generally speaking, your code should reads like English, so that it takes minimal amount of mental effort to understand. This will also help you with your object oriented design, because OO is supposed to be modeling the real world. For example, considering

(a) public class BlogEntry{ User author;}

Each blog entry has an author and an author is a user - this reads fine.

(b) public class BlogEntry{ Integer author; }

Each blog entry has an author and an author is a Integer - this doesn't sound right.

(c) public class BlogEntry { Integer authorId;}

Each blog entry has an author id - this doesn't sound right either.

 

SQL has naming conventions, too, and they are different from Java's. Here are three simple rules for SQL:

  • Use plural form for table names.
  • Use singular form for column names.
  • Do not use mixed cases in names - use either all lower cases or all upper cases, and use underscores to concatenate multiple words. This is because many DBMS treate names as case-insensitive (MySQL is a notable exception).

For example, for class BlogEntry:

public class BlogEntry {
int id;
Blog blog;
User author;
List<Comment> comments;
}

The table should be like

create table blog_entries (
id integer primary key,
blog_id integer references blogs(id),
author_id integer references users(id),
);
trivedidr
Posts: 54
Posted 09:24 May 22, 2009 |

Thank you for this basic knowlege  regarding Naming conventions, Dr. Sun.Smile