LOG kills
Never activates log in DEBUG mode. That kills memory and performance. Always use INFO. Debug should be used with AOP to trace execution only.
This blog is about all stupid line of codes wrote by java developers.
Never activates log in DEBUG mode. That kills memory and performance. Always use INFO. Debug should be used with AOP to trace execution only.
Posted by
amertum
at
23:06
0
comments
Labels: log, memory, performance
Money
int value;
String currency;
Money add(Money initial, Money toAdd) {
int value = initial.getValue() + dogAmount.getValue();
Money money = new AnimalMoney(value, initial.getCurrency())
return money;
}
Money cat = new AnimalMoney(5, "cat");
Money dog = new AnimalMoney(1, "dog");
Money deal = add(cat, dog);
Posted by
amertum
at
22:15
0
comments
Labels: bug, code style, type
How appears a vicious OutOfMemoryError on 2/3 of the dev computer ?
The same JVM, the same sources, the same web server...
Everything is the same ! Everything ? Not the application config file nor the log4j config file.
Yep, OutOfMemory because of DEBUG level, others with INFO. Why ? Because of the fuck root handler the gives the DEBUG level for every API that use log4j, and so for the one that log the application database access. More than 1GB while we use only 300MB ordinary.
Becareful with logger. Always use root INFO and provides category for your application packages.
Posted by
amertum
at
01:50
0
comments
Labels: bug, log, OutOfMemoryError
Book myBook = getBookFromSomeWhere();
System.out.println("myBook: " + myBook);
if (myBook == null) {
System.out.println("myBook is null");
}
else {
System.out.println("myBook is not null");
}
myBook: null
myBook is not null
Book {
...
public String toString() {
return "null";
}
Posted by
amertum
at
18:33
0
comments
Basically the implementation look like :
if client => send email to client
if enterprise => send email to client
if others => send email to client
BUT this behavior was not as specified. It should be :
if client => send NO email
if enterprise => send email to enterprise
if others => send email to enterprise
And guess what, it was in production because of ME. Yes, ME. I'm gonna kill myself. How it could be, how I could make such a mistake. By focusing too much on code aspect.
This teach me a great thing. Of course the code aspect, the architecture and so on are important, but the business target IS MORE IMPORTANT than every thing.
Today, I was the stupid guy because Errare Humanum Est.
Posted by
amertum
at
16:15
0
comments
// defined previously
// Date aDate = ...
Date date = new Date();
date = new Date(date.getTime());
if (aDate != null && aDate == date) {
date = new Date(date.getTime() + 10);
}
Date date = new Date();is stupid.
date = new Date(date.getTime());
Date date = new Date();is sufficient.
Posted by
amertum
at
14:13
0
comments
public static int roundSup(float n){
int round=0;
if(n > (new Float(n).intValue()))
round = new Float(n+1).intValue();
else round=new Float(n).intValue();
return round;
}
public static int roundSup(
final float n
) {
return ((int) n) + 1;
}
Posted by
amertum
at
15:52
0
comments
Microsoft 1975 Charles Simonyi's explication of the Hungarian notation identifier naming convention. What to say ? Most people thinks that they need to prefix program identifier (say variable, member...) with the system type. For boolean prefix with 'b' like bDone. For int prefix with 'i' like iCount. That's stupid to be so close to the system type. The thing that is important it's the meaning, count, or index, not the hard system type that can be changed. Let's read these: I’m Hungary Hungarian Notation Cleaner, more elegant, and harder to recognize Dropping the 'I' from interface names?
Posted by
amertum
at
10:08
0
comments
Labels: code style, naming conventions
public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
return i;
}
finally {
i = 3;
System.out.print("c");
return i;
}
} // end
public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
{ // finally block
int another_i = 3;
System.out.print("c");
return another_i;
}
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
{ // finally block
int another_i = 3;
System.out.print("c");
return another_i;
}
return i;
}
} // end
public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
return i;
}
finally {
i = 3;
System.out.print("c");
// return i;
}
} // end
public static int doSomething() {
int i = 0;
try {
i = 1;
System.out.print("a");
{ // finally block
int another_i = 3;
System.out.print("c");
}
return i;
}
catch (Exception e) {
i = 2;
System.out.print("b");
{ // finally block
int another_i = 3;
System.out.print("c");
}
return i;
}
} // end
Posted by
amertum
at
14:47
0
comments
/**
* Return the concatenation of first name and last name separated by a space.
*/
public String getFullname() {
String s = "";
if (this.getFirstName() != null) {
s += this.getFirstName() + " ";
}
if (this.getLastName() != null) {
s += this.getLastName();
}
return s;
}
Posted by
amertum
at
18:24
0
comments
client.setSiteId(clientForm.getSiteId());
client.setPrestaId(clientForm.getSiteId());
Posted by
amertum
at
23:05
0
comments
Labels: copy/paste, data, database, test
try {
doSomethingInSpace();
}
catch (SolarFlaresException e) {
LOG.warn("Watch the solar winds !" + e);
}
Posted by
amertum
at
13:57
0
comments
Labels: catch, joke, solar flares, solar winds, try
new Boolean(true);
new Boolean(true) != new Boolean(true);
Boolean.TRUE == Boolean.TRUE;
Boolean.valueOf(true) == Boolean.valueOf(true);
Posted by
amertum
at
12:27
0
comments
Labels: boolean
for (Iterator iter = myList.iterator(); iter.hasNext(); /* noop */) {
Object element = iter.next();
...
} // end for iter
Iterator iter = myList.iterator();
while (iter.hasNext()) {
Object element = iter.next();
...
} // end while iter
Posted by
amertum
at
17:57
0
comments
// FYI: Constants is a class (not an interface)
if (ts != null && ts.getTs().intValue() == 1){
nbPc = Constants.CONST1A;
Constants.CONST1A = Constants.CONST1A + 1;
nbPs = Constants.CONST1B;
Constants.CONST1B = Constants.CONST1B + 1;
} else if (ts != null && ts.getTs().intValue() == 2){
nbPc = Constants.CONST2A;
Constants.CONST2A = Constants.CONST2A + 1;
nbPs = Constants.CONST2B;
Constants.CONST2B = Constants.CONST2B + 1;
} else if (ts != null && ts.getTs().intValue() == 3){
nbPc = Constants.CONST3A;
Constants.CONST3A = Constants.CONST3A + 1;
nbPs = Constants.CONST3B;
Constants.CONST3B = Constants.CONST3B + 1;
} else if (ts != null && ts.getTs().intValue() == 4){
nbPc = Constants.CONST4A;
Constants.CONST4A = Constants.CONST4A + 1;
nbPs = Constants.CONST4B;
Constants.CONST4B = Constants.CONST4B + 1;
}
Posted by
amertum
at
14:10
0
comments
Labels: code design, constant, if
How to test that a String is empty?
A lot of people use:
myString.equals("")
"".equals(myString)
myString.length() == 0
myString != null && myString.length() == 0
org.apache.commons.lang.StringUtils.isEmpty(myString)
myString != null && myString.length() == 0
Posted by
amertum
at
11:08
0
comments
Labels: apache.org, isEmpty, String
Let's have:
if (condition)
doSomething();
if (condition)
doSomething();
doOtherThing();
if (condition) {
doSomething();
}
if (condition) {
doSomething();
doOtherThing();
}
Posted by
amertum
at
09:43
1 comments
Labels: brackets, code style, if
// sql is a StringBuffer
// params is an array of String defined previously
// paramsTmp is a String.
switch(i) {
case 1 :
sql.append(", ");
i++;
params[1] = paramsTmp;
break;
case 2:
sql.append(", ");
i++;
params[2] = paramsTmp;
break;
case 3:
sql.append(", ");
i++;
params[3] = paramsTmp;
break;
case 4 :
sql.append(", ");
i++;
params[4] = paramsTmp;
break;
case 5 :
sql.append(", ");
i++;
params[5] = paramsTmp;
break;
case 6 :
sql.append(", ");
i++;
params[6] = paramsTmp;
break;
case 7 :
sql.append(", ");
i++;
params[7] = paramsTmp;
break;
}
sql.append(", ");
params[i] = paramsTmp;
i++;
final String[] myValues = {"a", "b", "c"};
final String value = myValues[index];
/**
* MyEnum declaration.
*/
enum MyEnum {
A("A"),
B("B");
private final String mValue;
MyEnum(
final String value
) {
this.mValue = value;
}
public String getValue() {
return this.mValue;
}
public static MyEnum forValue(
final String value
) {
for (final MyEnum myEnum : values()) {
if (myEnum.getValue().equals(value)) {
return myEnum;
}
return null;
}
}
}
final String aValue = getValueSomewhere(...)
MyEnum.forValue(aValue);
Posted by
amertum
at
23:41
0
comments
Labels: code design, switch
(Integer) Integer.getInteger("12345")
Wow, that a nice stupid code. I lost 1 hour because of it. Thanks to YOU! The static Integer.getInteger(String) method return the value of a SYSTEM, I repeat a SYSTEM, property as an Integer. Hey! Lazy developers, read the javadoc before code completion.
Use this instead:
(Integer) Integer.valueOf(String)
Posted by
amertum
at
17:56
0
comments