Skip to content

Instantly share code, notes, and snippets.

@nicholasren
Last active November 12, 2024 14:27
Show Gist options
  • Save nicholasren/93e6d49f62ad8ed0b596 to your computer and use it in GitHub Desktop.
Save nicholasren/93e6d49f62ad8ed0b596 to your computer and use it in GitHub Desktop.
java clean code examples
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 {
}
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 {
public boolean isLogin(User user) {
return user.getSession() != null;
}
public void login(User user, String password) {
if (user.passwordMatched(password)) {
user.setSession(new Session());
}
}
}
@zjx-immersion
Copy link

Functions must only do what the name suggests and nothing else. isLogin is a function that handles a validation action not login

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment