Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CHALLENGE (work in pairs):
- // Improve code snippets listed below –
- //
- // 1 - represent Xml class in the system
- class XmlMarshaller {
- byte[] marshallToXml(Invoice invoice) {
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- JaxbMarshaller jaxbMarshaller = new JaxbMarshaller(Invoice.class);
- jaxbMarshaller.marshallObject(invoice, outStream);
- byte[] resultXml = outStream.toByteArray();
- IOUtils.closeQuietly(outStream);
- return resultXml;
- }
- }
- class Xml<T> {
- public Xml(T entity) {..}
- public byte[] bytes() {
- try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
- JaxbMarshaller jaxb = new JaxbMarshaller(T.class);
- jaxbMarshaller.marshallObject(entity, out);
- return out.toByteArray();
- }
- }
- }
- // 2 - refactor to eliminate all null checks and branching
- class AmazonS3File {
- private final String path;
- private Optional<Metadata> metadata;
- public AmazonS3File(String path) {
- this.path = path;
- }
- void attach(Metadata metadata) {
- this.metadata = Optional.of(metadata);
- }
- Optional<Metadata> metadata() {
- return metadata;
- }
- }
- class AmazonS3Bucket {
- void upload(AmazonS3File file) {
- … upload ...
- file.metadata().ifPresent(this::upload);
- }
- }
- void upload(Metadata metadata) { .. }
- }
- // 3 - refactor to conform to command-query separation (remember principle – "do or die")
- class Registration {
- private final RegistrationForm form;
- Optional<ValidationErrors> validationErrors() {
- return Optional.of(form.validationErrors());
- }
- void complete() {
- if(!form.isValid()) {
- throw new ValidationFailed(form.validationERrors());
- }
- ... complete registration ...
- }
- }
- // 4 - does the method violate the principle of least astonishment? is there something suspicious with the design? why?
- Ban ban(User user) {
- Ban ban = new Ban(user.ipAddress(), Period.ofDays(3));
- user.banned(); // sets "banned" boolean in User to "true"
- return ban;
- }
- void ban(Period period) {
- this.ban = new Ban(ipAddress(), period);
- }
- boolean isBanned() {
- return this.ban;
- }
- // not a fan
- Ban banDetails() {
- return ban;
- }
- // 5 – any ideas how to improve this code?
- void sort() {
- Collection<User> users = ...
- users.sort((one, another) -> Long.compare(one.getId(), another.getId()));
- users.sort(Comparator.comparing(User::getId));
- }
- // 6 – every raw() invocation leads to remote call (via Vault). Please fix
- class SecurePassword {
- private final Vault vault;
- private final Supplier<String> rawPassword;
- private SecurePassword(Vault vault) {
- this.vault = vault;
- this.rawPassword = Suppliers.memoized(vault::verySecurePassword);
- }
- public String raw() {
- return rawPassword.get();
- }
- }
- // 7 - implement "fullName" method, so that it returned "firstName lastName" if nickname is missing
- // or "firstName <nickname> lastName" if nickname is present.
- // for example – "Robert Martin" or "Robert <Uncle Bob> Martin"
- // don't optimize prematurely (e.g. prefer simple String concatenation over StringBuilder).
- class User {
- private final Optional<String> nickName;
- private final String firstName;
- private final String lastName;
- String fullName() {
- return nickName
- .map(it -> firstName + " "+ it +" "+ lastName)
- .orElse(firstName + " " + lastName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement