- If you have a block of code that appears more than once, don't make it a method!
finally {
if(rs != null) {
try {
rs.close();
} catch (Exception e) {
// Ignore
}
}
if(ps != null) {
try {
ps.close();
} catch (Exception e) {
// Ignore
}
}
}Sprinkled into every single method in a database access class creates more lines of code and makes you look like a busy little coding bee.
- When you have to reuse the same string more than once in a class, forget about declaring static variables.. only sissies do that. Adventurous people get joy out of discovering typos. Think of how many hours you can spend debugging and looking busy!
- Don't oversimplify SQL.. sure you can use..
"SELECT MAX(COLUMN) +1 FROM TABLE"
.. but this is much more exciting and it'll impress all the chicks in the office:
"SELECT MAX(NVL(COLUMN_NAME,0)) + 1 NEW_VAR FROM TABLE_NAME"
- Don't forget to make sure all SQL syntax is as database-specific as you can get it. If they ask for compatibility with another database, more hours of billable time!
- Even if you are using triggers for setting update time on tables, use "update_time=sysdate" just in case that trigger doesn't fire.. you can't be too careful!
- Make sure your SQL code is nicely formatted..
"SELECT Column_One, Column_Two, Column_Three FROM Table_Name WHERE Id=?"
looks so much prettier than..
"SELECT column_one, column_two, column_three FROM table_name where id=?" - When throwing exceptions, throw away that silly old SQL exception.. throw something new and exciting. The database was probably just complaining about something silly anyway. For simplicity, keep all your exception messages the same. "Operation failed" works just fine and to the point.
- Don't use Interfaces! They're a part of a communist plot to hide information. Be nice to the caller, let them know exacly how you implement things..
ArrayList list = new ArrayList(); :-)
List list = new ArrayList(); :-( - Import statements are a waste of space. See how many more lines the second variation takes?
java.util.List list = new java.util.ArrayList();-----------------
import java.util.ArrayList;
import java.util.List;.
.
.List list = new ArrayList();
- Ordering and alphabetizing import statements is a waste of time. It's not like they get included in compiled code anyway.
- Don't break up long lines, it's what the wrap function in a text editor is for. Why use up more lines?
- Comment your code heavily! "return rval;" may mean something different than what it looks like! Some programmers are sneaky that way. Don't be one of them.
- Declare all variables globally. Keeping variables local just because you only use them once is silly.. what if you decide to use it again? huh? What then?
- Chain your calls!
Object o = new Object(b.getVal(), c.getMoreVal(d.getAnotherVal()), (g == f ? (y.getBah() == z.getBleah() ? duh : new f())y.getVal() : x.getObject(new g())));You can do all this on just one line! Why waste space?
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/114
Object o = new Object(b.getVal(), c.getMoreVal(d.getAnotherVal()), (g == f ? (y.getBah() == z.getBleah() ? duh : new f())y.getVal() : x.getObject(new g())));
Perl programmer wannabe.
And it's so smooth and intuitive to trace into a heavily chained call.
*enter method*
*leave method*
*enter method*
*leave method*
*enter method*
*leave method*
*hit wrong key and leave method by mistake*
*start over after saying unkind things about your coworker*
When we started a majar Java project one of the first coding practices to come in was to finish every block that used Statements and ResultSets like this:
}finally{
JDBCTools.closeResultSet( rs );
JDBCTools.closeStatement( ps );
}
I think some guys made it macro in their editor/IDE.
There was some debate though as to whether the closeResultSet() was even necessary as closing the statement closed all ResultSets. The team was then split between the "if you can close it, close it" camp and the "we don't need no extra stinking code" camp.
We used to ask for code samples from developers who were planning to interview to join the team. Some sent code that was so disgusting that we rejected them there and then...
#HOW THE HELL DO YOU WRITE THIS SHIT!PLEASE TELL ME!!!!!!!!!!!!!!!!!!!!!!
#It's easy you just cut & paste from google searches.
java is something i do not under stand but would like to what is a fast way to get started i learn fast
#> For simplicity, keep all your exception messages the same. "Operation failed" works just fine and to the point.
http://www.tbray.org/ongoing/When/200x/2003/08/03/DataPotato
I'd thus recommend "Data Potato doo-wop doo-wop"...
#Could someone here be bothered to go look up "aspect oriented programming"?
In particular that story about the close resultsets argument makes me very glad I didn't happen to work in that office, seeing as how I probably would've been reduced to bringing guns to work by the time that argument broke out.
#