Skip to content

Instantly share code, notes, and snippets.

@zjx-immersion
Created May 11, 2018 11:47
Show Gist options
  • Save zjx-immersion/6045a5e54f830ce90cfe27d38878cd76 to your computer and use it in GitHub Desktop.
Save zjx-immersion/6045a5e54f830ce90cfe27d38878cd76 to your computer and use it in GitHub Desktop.
Clean Code Examples
public class BuilderMain {
public static void main(String[] args) {
//Pattern
Product product = ProductBuilder()
.setId(39)
.setPrice(new PriceFactory().usingAmount(59).usingUnit(Unit.Dollar))
.setTitle("Product 1"),
.setDescription("Description goes here")
.getResult()
//Without Patten
Price price = new Price(59, Unit.Dollar);
Product product = new Product(39, price, "Product 1", "Description goes here");
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;
public class Duplication {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Integer product = Math2.sum(numbers);
System.out.println(product);
}
}
//with duplication
class Math1 {
public static Integer sum(List<Integer> numbers) {
Integer sum = 0;
for (Integer n : numbers) {
sum += n;
}
return sum;
}
public static Integer multiply(List<Integer> numbers) {
Integer product = 1;
for (Integer n : numbers) {
product *= n;
}
return product;
}
}
//after refactor with lambda
class Math2 {
public static Integer reduce(List<Integer> numbers, Integer unit, BinaryOperator<Integer> operator) {
Integer result = unit;
for (Integer n : numbers) {
result = operator.apply(result, n);
}
return result;
}
public static Integer sum(List<Integer> numbers) {
return reduce(numbers, 0, (x, y) -> x + y);
}
public static Integer multiply(List<Integer> numbers) {
return reduce(numbers, 1, (x, y) -> x * y);
}
}
import java.util.logging.Logger;
class Page {
public String name;
}
class Registry {
public int deleteReference(String name) {
return 0;
}
}
class ConfigKeys {
public int deleteKey(String name) {
return 0;
}
}
//with nested if-else
class NestedIfElse1 {
private static final int E_OK = 1;
private static final int E_ERROR = 2;
private Registry registry;
private ConfigKeys configKeys;
private Logger logger;
public int delete(Page page) {
if (deletePage(page) == E_OK) {
if (registry.deleteReference(page.name) == E_OK) {
if (configKeys.deleteKey(page.name) == E_OK) {
logger.info("page deleted");
} else {
logger.info("configKey not deleted");
}
} else {
logger.info("deleteReference from registry failed");
}
} else {
logger.info("delete failed");
return E_ERROR;
}
return E_OK;
}
private int deletePage(Page page) {
return 0;
}
}
//after refactor with exception
class NestedIfElse2 {
private Logger logger;
private Registry registry;
private ConfigKeys configKeys;
public void delete(Page page) {
try {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name);
} catch (Exception e) {
logger.info(e.getMessage());
}
}
private void deletePage(Page page) {
}
}
public class NestedIfElse {
}
class Order {
//...
public double price() {
double primaryBasePrice;
double secondaryBasePrice;
double tertiaryBasePrice;
// long computation.
//...
}
}
----------------------------------------------
class Order {
//...
public double price() {
return new PriceCalculator(this).compute();
}
}
class PriceCalculator {
private double primaryBasePrice;
private double secondaryBasePrice;
private double tertiaryBasePrice;
public PriceCalculator(Order order) {
// copy relevant information from order object.
//...
}
public double compute() {
// long computation.
//...
}
}
public class SideEffects {
}
class User {
private Session session;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public boolean passwordMatched(String password) {
return false;
}
}
class Session {
}
//with side effects
class SessionService1 {
public Session login(User user, String password) {
if (user.getSession() != null) {
return user.getSession();
} else if (user.passwordMatched(password)) {
Session session = new Session();
user.setSession(session);
return session;
}
return null;
}
}
//after refactor with CQRS
class SessionService2 {
//Why it is a separated method?
public boolean isLogin(User user) {
return user.getSession() != null;
}
public void login(User user, String password) {
if (user.passwordMatched(password)) {
user.setSession(new Session());
}
}
}
public class BuilderMain {
public static void main1(String[] args) {
double temp = 2 * (height + width);
System.out.println(temp);
temp = height * width;
System.out.println(temp);
//Each variable should be responsible for only one particular thing.
final double perimeter = 2 * (height + width);
System.out.println(perimeter);
final double area = height * width;
System.out.println(area);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment