Skip to main content

Writing Maintainable Codes


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: int x = Integer.parse(request.getParameter("x").toString()); switch(x) { case 1: ...// } 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: int redirectPageKey = Integer.parse(request.getParameter("redirectPageKey")); ... 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: public int calculate(int num1, int num2){...} public void fillList(){...} Use something like: public int getSum(int num1, int num2){...} 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

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byAnonymous (not verified)on Mon, 07/06/2009 - 16:45

For the latter example, I would prefer a combination of your method names: calculateSum() 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.
Submitted byAnonymous (not verified)on Mon, 07/06/2009 - 18:15

In reply to by Anonymous (not verified)

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!

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.