// DON'T
int d; // elapsed time in days
// DO
int elapsedTimeInDays;
// DON'T
Set<Account> accountList;
// DO something like
Set<Account> accounts;
// DON'T
copyChars(char[] a1, char[] a2)
// DO
copyChars(char[] source, char[] copy)
// DON'T
getActiveAccount();
// how is this different?
getActiveAccountInfo();
// DON'T
public interface IPerson;
// DO
public interface Person;
// DON'T
public class ListImpl implements List;
// DO
public class ArrayList implements List;
public class LinkedList implements List;
public class CopyOnWriteArrayList implements List;
// DON'T
private Date genymdhms;
// DO
private Date generationTimeStamp;
// DON'T
// Is it Control or Central?
private String cntrl;
public class PrimeChecker {
private static final int MAX_PRIME = 1_000_000;
private boolean[] sieve;
public PrimeChecker() {
this.sieve = new boolean[MAX_PRIME];
Arrays.fill(sieve, true);
sieve[0] = false;
sieve[1] = false;
for (int i = 2; i * i < MAX_PRIME; ++i) {
if (sieve[i]) {
for (int j = i * i; j < MAX_PRIME; j += i) {
sieve[j] = false;
}
}
}
}
public boolean isPrime(int n) {
return sieve[n];
}
}
this.sieve = new boolean[MAX_PRIME];
inicializarCriba(MAX_PRIME);
* * *
private void initSieve(int MAX_PRIME) {
this.sieve = new boolean[MAX_PRIME];
}
En vez de
Usemos
Arrays.fill(criba, true);
uncrossAllNumbers();
* * *
private void uncrossAllNumbers() {
Arrays.fill(sieve, true);
}
En vez de
Usemos
sieve[0] = false;
sieve[1] = false;
crossObviousNumbers();
* * *
private void cross(int n) {
sieve[n] = false;
}
private void crossObviousNumbers(int n) {
cross(0);
cross(1);
}
En vez de
Usemos
for (int i = 2; i * i < MAX_PRIME; ++i)
crossMultiplesOfKnownPrimes();
* * *
private void crossMultiplesOfKnownPrimes() {
for (int i = 2; i * i < MAX_PRIME; ++i) {
if (sieve[i]) {
for (int j = i * i; j < MAX_PRIME; j += i) {
sieve[j] = false;
}
}
}
}
En vez de
Usemos
public PrimeChecker() {
this.sieve = new boolean[MAX_PRIME];
Arrays.fill(sieve, true);
sieve[0] = false;
sieve[1] = false;
for (int i = 2; i * i < MAX_PRIME; ++i) {
if (sieve[i]) {
for (int j = i * i; j < MAX_PRIME; j += i) {
sieve[j] = false;
}
}
}
}
public PrimeChecker() {
initSieve();
crossObviousNumbers();
crossMultiplesOfKnownPrimes();
}
Pasamos de
A esto
for (int i = 2; i * i < MAX_PRIME; ++i)
for (int n = 2; hasMutipleNotCrossed(n); ++n)
* * *
private boolean hasMutipleNotCrossed(int n) {
return n * n < MAX_PRIME;
}
En vez de
Usemos
if (sieve[i])
if (isPrime(n))
En vez de
Usemos
for (int j = i * i; j < MAX_PRIME; j += i) {
sieve[j] = false;
}
crossMutiplesOf(n);
* * *
private void crossMultiplesOf(int n) {
for (int j = n * n; j < MAX_PRIME; j += n) {
sieve[j] = false;
}
}
En vez de
Usemos
for (int j = i * i; j < MAX_PRIME; j += i)
for (int x = firstPossiblyUncrossMultiple(n);
x < MAX_PRIME; x = nextMultiple(x, n))
* * *
public int firstPossiblyUncrossMultiple(int n) {
return n * n;
}
public int nextMultiple(int x, int n) {
return x + n;
}
En vez de
Usemos
sieve[j] = false;
cross(x);
En vez de
Usemos
public PrimeChecker() {
initSieve();
crossObviousNumbers();
crossMultiplesOfKnownPrimes();
}
private void crossObviousNumbers() {
cross(0);
cross(1);
}
private void crossMultiplesOfKnownPrimes() {
for (int n = 2; hasOnePossibleUncrossMultiple(n); ++n) {
if (isPrime(n)) {
crossMultiplesOf(n);
}
}
}
private void crossMultiplesOf(int n) {
for (int x = firstPossiblyUncrossMultiple(n);
x < MAX_PRIME; x = nextMultiple(x, n)) {
cross(x);
}
}
public boolean hasOnePossibleUncrossMultiple(int n) {
return n * n < MAX_PRIME;
}
public int firstPossiblyUncrossMultiple(int n) {
return n * n;
}
public int nextMultiple(int x, int n) {
return x + n;
}
private void initSieve(int MAX_PRIME) {
this.sieve = new boolean[MAX_PRIME];
}
private void uncrossAllNumbers() {
Arrays.fill(sieve, true);
}
private void cross(int n) {
sieve[n] = false;
}
public boolean isPrime(int n) {
return sieve[n];
}
***
***
***
*
***
*****
public static String square(int size) {
StringBuilder builder = new StringBuilder();
for (int row = 1; row <= size; ++row) {
for (int column = 1; column <= size; ++column) {
builder.append('*');
}
builder.append(String.format("%n"));
}
return builder.toString();
}
public static String pyramid(int size) {
int totalSpaces = size - 1;
int totalChars = 1;
for (int row = 1; row <= size; ++row) {
for (int space = 1; space <= totalSpaces; ++space) {
builder.append(' ');
}
for (int character = 1; character <= totalChars; ++character) {
builder.append('*');
}
builder.append(String.format("%n"));
--totalSpaces;
totalCharacters += 2;
}
}
private static final String CHARACTER = '*';
private static final String SPACE = ' ';
Primero eliminemos
los números mágicos
private static String makeString(char c, int size) {
StringBuilder builder = new StringBuilder();
for (int n = 1; n < size; ++n) {
builder.append(c);
}
return builder.toString();
}
Extraemos la funcionalidad
de hacer un String usando
un caracter varias veces
public static String square(int size) {
StringBuilder builder = new StringBuilder();
for (int row = 1; row <= size; ++row) {
builder.append(makeString('*'), size)
.append(String.format("%n"));
}
return builder.toString();
}
public static String pyramid(int size) {
int totalSpaces = size - 1;
int totalChars = 1;
for (int row = 1; row <= size; ++row) {
builder.append(makeString(' '), totalSpaces)
.append(makeString('*'), totalChars)
.append(String.format("%n"));
--totalSpaces;
totalCharacters += 2;
}
}
private static String makeFigure(int size, int spaces,
int characters, int modifierSpaces, int modifierCharacters) {
StringBuilder builder = new StringBuilder();
for (int row= 1; row <= size; ++row) {
builder.append(makeString(spaces, SPACE))
.append(makeString(characters, CHARACTER))
.append(String.format("%n"));
spaces += modifierSpaces;
characters += modifierCharacters;
}
return builder.toString();
}
public static String square(int size) {
return makeFigure(size, 0, size, 0, 0);
}
public static String pyramid(int size) {
return makeFigure(size, size - 1, 1, -1, 2);
}
// DON'T
public Circle(double x, double y, double radius)
// DO
public Circle(Point center, double radius)
// DON'T
public void execute(String query)
// DO
public void execute(Query query)
public void delete(Page page) {
try {
doDelete(page);
} catch (AppException e) {
logger.log(e);
}
}
private void doDelete(Page page) throws AppException {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name);
}