visit
Let's look at some possible solutions.
Most of these smells are just hints of something that might be wrong. They are not rigid rules.
This is part V. Part I can be found here, Part II here, Part III is here, Part IV here, part V, VI, VII, VIII, IX and the last one (for now).
Let's continue...Not operator is our friend. Not not operator is not our friend.
if ( !work.isNotFinished() )
if ( work.isDone() )
We can tell linters to check for Regular Expressions like !not or !isNot etc as a warning.
It’s harder to read code than to write it.
Joel Spolsky
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import components.set.Set;
import components.set.Set1L;
public abstract class SetTest {
protected abstract Set<String> constructor();
@Test
public final void testAddEmpty() {
Set<String> s = this.constructor();
s.add("green");
s.add("blue");
assertEquals("{green. blue}", s.toString());
//This is fragile since it dependes on set sort (which is not defined)
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import components.set.Set;
import components.set.Set1L;
public abstract class SetTest {
protected abstract Set<String> constructor();
@Test
public final void testAddEmpty() {
Set<String> s = this.constructor();
s.add("green");
assertEquals("{green}", s.toString());
}
@Test
public final void testEntryAtSingleEntry() {
Set<String> s = this.createFromArgs("red");
Boolean x = s.contains("red");
assertEquals(true, x);
}
}
The amateur software engineer is always in search of magic.
Grady Booch
for (i = 0; i < colors.count(), i++) {
print(colors[i]);
}
foreach (color of colors) {
print(color);
}
//Closures and arrow functions
colors.foreach(color => print(color));
If you get tired of writing for loops, take a break and continue later.
David Walker
<?
final class DatabaseQueryOptimizer {
public function selectWithCriteria($tableName, $criteria) {
//Make some optimizations manipulating criterias
}
private function sqlParserOptimization(SQLSentence $sqlSentence): SQLSentence {
//Parse the SQL converting it to an string and then working with their nodes as strings and lots of regex
//This was a very costly operation overcoming real SQL benefits.
//But since we made too much work we decide to keep the code.
}
}
<?
final class DatabaseQueryOptimizer {
public function selectWithCriteria($tableName, $criteria) {
//Make some optimizations manipulating criterias
}
}
It is very hard to predict, especially the future.
Niels Bohr
<?
final class Point {
public $x;
public $y;
}
final class DistanceCalculator {
function distanceBetween(Point $origin, Point $destination) {
return sqrt((($destination->x - $origin->x) ^ 2) + (($destination->y - $origin->y) ^ 2));
}
}
<?
final class Point {
private $rho;
private $theta;
public function x() {
return $this->rho * cos($this->theta);
}
public function y() {
return $this->rho * sin($this->theta);
}
}
final class DistanceCalculator {
function distanceBetween(Point $origin, Point $destination) {
return sqrt((($destination->x() - $origin->x() ^ 2) + (($destination->y() - $origin->y()) ^ 2)));
}
}
A data structure is just a stupid programming language.
Bill Gosper