Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Writing Maintainable Codes
arjay_nacion's picture
Language: Java
Category: Miscellaneous

When working on a project where in you have to add features to an existing code written by other members of your team, you may have encountered problems understanding parts of the code. Sometimes the problems are caused by codes not well commented or sometimes the codes are simply unreadable.

Take this code for example:

  1. int x = Integer.parse(request.getParameter("x").toString());
  2. switch(x)
  3. {
  4. case 1:
  5. ...//
  6. }

The code is well structured except for the fact that the variable name "x" doesn't explain its purpose. One of the conventions to follow in writing variables is that you must name your variables words that describe its meaning.

In this example, if the programmer intends to use the variable "x" for evaluating where the current user will be redirected, he may use a more meaningful variable like:

  1. int redirectPageKey = Integer.parse(request.getParameter("redirectPageKey"));
  2.  
  3. ...

This way, the programmer who will maintain the codes could easily understand what your code intends to do.

Another thing is to write descriptive method names:

Instead of:

  1. public int calculate(int num1, int num2){...}
  2. public void fillList(){...}

Use something like:

  1. public int getSum(int num1, int num2){...}
  2. public void getStudentList(){...}

By writing readable codes, you can reduce the amount of comment lines in your code. A code having a large amount of comments means that the codes are not readable enough.

Readable Code = Maintainable Code

0
Your rating: None



nice, and there is more

For the latter example, I would prefer a combination of your method names:

  1. calculateSum()
  2. fillStudentList()

This would add a hint about the semantics of a method.

I've recently read the book Clean Code from Robert C. Martin: It builds up on these ideas and changed the way I thought about writing readable code.

kind regards
Bender.

i agree...

I agree with your convention of using calculateSum() or fillStudentList().

For the article I wrote, i just used the method naming convention of java for getters and setters. That is using "get" at the beginning of a method name which retrieves or gets a value and "set" for methods which alters a value.

Clean Code from Robert Martin is nice! Cheers!

Post new comment

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...