Monday, June 29, 2009
Protected vs. Private methods in Java
Unless you have a specific reason to make the method private - like you expressly want to prevent subclasses from accessing the method - methods should be protected. This way we can subclass this tag.
if/else vs. switch
When writing code that branches on enum values, instead of if/else blocks, use the switch statement like so:
Lengthy and nested methods: Methods should small, atomic and easy to read. Break it up like so:
switch (selectedTab) {
case (Tab.SITES):
//do something for sites
case (Tab.CATEGORY):
//do something for category
case (Tab.DEMOGRAPHIC):
//do something for demographics
case (Tab.ALL_SITES):
//do something for all sites
}
Lengthy and nested methods: Methods should small, atomic and easy to read. Break it up like so:
switch (selectedTab) {
case (Tab.SITES):
renderSitesTab();
case (Tab.CATEGORY):
renderCategoryTab();
case (Tab.DEMOGRAPHIC):
renderDemographicsTab();
case (Tab.ALL_SITES):
renderAllSites();
}
Comparison in Enums
Tip on Enums
Instead of using id to do equality comparisons for enums, use the enum itself.
Example: Instead of
if (selectedTab.getId() == Tab.SITES.getId()) {
use:
if (selectedTab.equals(Tab.SITES)) {
Instead of using id to do equality comparisons for enums, use the enum itself.
Example: Instead of
if (selectedTab.getId() == Tab.SITES.getId()) {
use:
if (selectedTab.equals(Tab.SITES)) {
StringBuilder vs. StringBuffer
A tip on StringBuilder vs. StringBuffer
- We should be using StringBuilder over StringBuffer. It’s a drop-in replacement, all the method names and arguments are the same.
- StringBuilder is not synchronized and therefore doesn’t require object locks everytime a method is called. The only time we’d use StringBuffer is when it’s a member variable with a possibility of multiple threads writing to it.
Thursday, February 12, 2009
Check the version of applications running on Linux
OS version:
uname -a
cat /etc/redhat-release
Apache version:
httpd -v
/usr/local/apache/bin/httpd -version
Jrun version
For JRun 3.0, look at logs
/opt/jrun/bin/jrun -version
Perl
/usr/sbin/perl -version (-v works too)
Sendmail
/usr/lib/sendmail -d0.1 -bt
cat /proc/version
Monday, December 29, 2008
Problem with date format in JDBC session
The other day I came across this issue and spent some time figuring out what was going on. Hope it can help someone.
Issue:
Say there’s a stored procedure in Oracle named test_date which uses a variable of type DATE. The variable is not returned to java but simply used elsewhere in the procedure.
If I execute this procedure in oracle and check the date format, I see it’s using the format dd-mon-yyyy
If I execute this procedure over jdbc, the format I see is mm-dd-yyyy
This makes me believe that the date format used throughout the session is defined by the client (oracle vs. jdbc).
Fix:
Simple way to fix this is to alter the session at runtime and explicitly define the format used throughout the session
alter session set nls_date_format=''DD-MON-YYYY''
Issue:
Say there’s a stored procedure in Oracle named test_date which uses a variable of type DATE. The variable is not returned to java but simply used elsewhere in the procedure.
If I execute this procedure in oracle and check the date format, I see it’s using the format dd-mon-yyyy
If I execute this procedure over jdbc, the format I see is mm-dd-yyyy
This makes me believe that the date format used throughout the session is defined by the client (oracle vs. jdbc).
Fix:
Simple way to fix this is to alter the session at runtime and explicitly define the format used throughout the session
alter session set nls_date_format=''DD-MON-YYYY''
Friday, December 5, 2008
Good coding practices I use

- Write flexible code that can be changed and extended, changes in underlying technologies shouldn't ripple through the whole system
- Code should be maintainability – long shelf life
- Layers should not be tightly coupled. Each layer needs to be independent from the other. Use interfaces, dependency injection etc
- Indent all files using spaces (Indent once = 4 spaces)
- If you are using an IDE, use the formatter and tools that comes with it to organize and clean up your code
- Comment your code often. Use Javadoc standards.
- Use sensible naming conventions for variable, method and class names.
- Use exceptions only if required. Do not use exceptions to handle errors. If errors occur we want the message to propagated up the call stack.
- Do not use scriplets in your JSP code. Use JSTL Expression Languages where required.
- All jsp file names should be in lowercase
- All text in JSP's should either come from Database or Resource bundles. No text should be hard coded in HTML
- HTML pages should be XHTML Transitional
- Validate your HTML pages using w3c validators
- All styles should be written in css. No HTML style attributes
- If your code causes warnings or errors in other code, fix other code as well. There shouldn't be any code in CVS which has errors or warnings.
Subscribe to:
Posts (Atom)