-
+77
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
public class SomeEntityBean implements javax.ejb.EntityBean {
private boolean loadMember;
private Wrapper list;
/* ... */
public void ejbLoad() {
this.loadMember = false;
load();
}
private void load() {
/* a LOT of code */
String[] attrList = this.loadMember ? a.attrListMember() : a.attrList();
this.list = new Wrapper(attrList);
/* a LOT of code */
}
public Something getSomething() {
/* a LOT of code */
this.loadMember = true;
load();
/* do something with this.list */
this.loadMember = false;
return something;
}
}
Имена персонажей были умышленно изменены.
Разумеется, loadMember больше нигде не используется.
Яркий пример повторного использования кода.
roman-kashitsyn,
16 Июня 2011
-
+86
- 1
- 2
- 3
- 4
- 5
- 6
public static String hello()
{
String s = "";
s += "Добро пожаловать на наш сайт.";
return s;
}
redenemy,
16 Июня 2011
-
+85
- 1
boolean ROLLBACK = new Boolean(false).booleanValue();
И это пишет тимлид (сеньор) в моей конторе
Loord,
15 Июня 2011
-
+70
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
import java.util.Random;
public class A {
public static void main(String args[]) {
int minimalSuccessTime = Integer.MAX_VALUE;
int maximalSuccessTime = Integer.MIN_VALUE;
for(int i = 0; i < 10000; i++) {
Random rnd = new Random();
boolean state = rnd.nextBoolean();
final byte prisonersCount = 100;
boolean prisoners[] = new boolean[prisonersCount];
byte prisonersCounted = 1;
int daysPassed = 0;
while(true) {
daysPassed++;
int tmp = rnd.nextInt(prisonersCount);
if(tmp == 0) {
if(state) {
state = false;
prisonersCounted++;
if(prisonersCounted == prisonersCount) {
break;
}
}
} else {
if(!state && !prisoners[tmp]) {
state = true;
prisoners[tmp] = true;
}
}
}
if(daysPassed < minimalSuccessTime) {
minimalSuccessTime = daysPassed;
}
if(daysPassed > maximalSuccessTime) {
maximalSuccessTime = daysPassed;
}
}
System.out.println("Minimal success time ~= " + minimalSuccessTime/365 + " years!");
System.out.println("Maximal success time ~= " + maximalSuccessTime/365 + " years!");
}
}
One hundred prisoners have been newly ushered into prison. The warden tells
them that starting tomorrow, each of them will be placed in an isolated cell,
unable to communicate amongst each other. Each day, the warden will choose
one of the prisoners uniformly at random with replacement, and place him in
a central interrogation room containing only a light bulb with a toggle switch.
The prisoner will be able to observe the current state of the light bulb. If he
wishes, he can toggle the light bulb. He also has the option of announcing that
he believes all prisoners have visited the interrogation room at some point in
time. If this announcement is true, then all prisoners are set free, but if it is
false, all prisoners are executed.
The warden leaves, and the prisoners huddle together to discuss their fate.
Can they agree on a protocol that will guarantee their freedom?
guest2011,
14 Июня 2011
-
+92
- 1
- 2
- 3
- 4
- 5
public Boolean checkPermission(String login) {
if (login == null && login.trim().length() == 0)
throw new Exception("Не указан логин пользователя.");
/* ... */
}
Чудесное условие. Есть в этом Exception что-то неуловимое - его никто никогда не поймает.
roman-kashitsyn,
14 Июня 2011
-
+85
- 1
String methodName = (new Exception()).getStackTrace()[1].getMethodName();
Вот как надо получать имя метода. Это вам не __func__ ...
roman-kashitsyn,
14 Июня 2011
-
+83
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
try
{
nIndex = Integer.parseInt(sRowIndex.trim());
}
catch(NumberFormatException e)
{
//异常处理
nIndex = -2;
}
if(nIndex < 0) return;
Китайцы продолжают отжигать.
lucidfox,
14 Июня 2011
-
+74
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
public void removeFromBase()
throws SQLException{
for(ServiceInstance serv: getServiceInstances())
serv.removeFromBase();
for(Pay pay: pays)
pay.removeFromBase();
String query;
PreparedStatement ps = null;
Connection connection = null;
try{
connection = TIDatasource.getInstance().getConnection();
query = "DELETE FROM orders WHERE id=?";
ps = connection.prepareStatement(query);
ps.setLong(1, getId());
int res = ps.executeUpdate();
if(res != 1)
throw new SQLException("Unexpected number of orders have deleted: " + res);
}catch(SQLException sqlex){
throw new SQLException("Can't remove order from base because of error: " + sqlex);
}finally{
ps.close();
connection.close();
}
}
PRIMARY KEY(`id`)
volatile,
13 Июня 2011
-
+80
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
thread = new Thread() {
@Override
public void run() {
try {
while( !dataProcessor.isFinished() ) {
dataProcessor.execute();
Thread.sleep( 60 * 1000L );
}
} catch ( Throwable t ) {
logger.fatal( "Fatal error in daemon thread", t );
}
}
};
thread.run();
Вот такое оно параллельное выполнение. Задачка для догадливых.
galak,
13 Июня 2011
-
+76
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
List<Record> getNewestRecords( int maxResults ) {
List<TenantIssue> allRecords = recordsDao.getAllRecords();
if ( allRecords.size() > maxResults ) {
Collections.sort( allRecords, new Comparator() {
public int compare( Record r1, Record r2 ) {
return (int) r1.getDate().getTime() - r2.getDate().getTime();
}
} );
Collections.reverse( allRecords );
List<Record> newestRecords = new ArrayList<Record>();
for ( int i = 0; i < maxResults; i++ ) {
newestRecords.add( allRecords.get( i ) );
}
} else {
return allRecords;
}
return allRecords;
}
galak,
13 Июня 2011