Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1 - encapsulate xml marshalling in a class
- byte[] asXml(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;
- }
- // 2 - refactor to iliminate all null checks and branching
- class AmazonS3File {
- private final String path;
- private Metadata metadata;
- void setMetadata(@Nullable Metadata metadata) {
- this.metadata = metadata;
- }
- @Nullable
- Metadata getMetadata() {…}
- }
- class AmazonS3Bucket {
- void upload(AmazonS3File file) {
- … upload ...
- if (file.getMetadata() != null) {
- uploadMetadata(file.getMetadata());
- }
- }
- }
- // 3 - refactor to conform to command-query separation
- class Registration {
- private final RegistrationForm form;
- Optional<ValidationErrors> complete() {
- if (!form.isNotValid()) {
- … complete registration ...
- } else {
- return form.validationErrors();
- }
- }
- }
- // 4 - does the method violate the principle of least astonishment? why?
- Ban ban(User user) {
- Ban ban = new Ban(user.ipAddress(), Period.ofDays(3));
- user.banned();
- return ban;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement