Introducing 

Prezi AI.

Your new presentation assistant.

Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.

Loading…
Transcript

Kvíz

Code design

GUITesterModule

getAllTests() // from classpath

runAllTests

run afterTestSuite()

Consistency

Stay on same level of abstraction

For:

  • names
  • code placement
  • enums

ProjectAModule

public void renderReport() {

renderHeader();

renderBody();

writer.append("<hr>Powered by black magic</hr>");

FileUtils.write(outputFileName, writer.toString());

}

No transitive calls

class Test1

@Test

public void test

class Test2

@Test

public void test2

@Test

public void test3

public static void afterTestSuite()

One responsibility principle

  • reportComposer.getReportWriter().append(footer);

For:

  • modules, packages
  • classes, methods

Prefer enums over constants

public void pay()

for(Employee e:employees)

payIfNecessary(e);

private void payIfNecessary(Employee e)

if (e.isPayDay()) calculateAndDeliverPay(e);

private void calculateAndDeliverPay(e)

Money pay = e.calculatePay();

e.deliverPay(pay);

HttpRequestProcessor

  • public static long TIMEOUT_READ_IN_SECONDS_SHORT = 10;
  • public static long TIMEOUT_READ_IN_SECONDS_LONG = 100;

public void pay()

for(Employeee:employees)

if(e.isPayday()) {

Money pay=e.calculatePay();

e.deliverPay(pay):

}

Why?

  • ReadTimeOutEnum
  • SHORT(10),
  • LONG(100);
  • public ReadTimeOutEnum(int timeoutInSeconds) {
  • this.timeoutInSeconds = timeoutInSeconds;
  • }
  • public int getTimeoutInSeconds() {
  • return timeoutInSeconds;
  • }

Proper use of static methods

  • Avoid them if:
  • they are doing something for someone :)
  • should be polymorphic

Use Observer pattern

  • HourlyPayCalculator.calculatePay(employee, overimeRate);

AbstractSeleniumTest

  • @After
  • jiraIntegrator.synchronizeCreatedIssues()
  • AbstractSeleniumTest
  • @After
  • applicationEventPublisher.publishEvent(new OnTestAfterEvent());

Spring context occasional bean presence

  • JiraIntegrator
  • implements ApplicationListener<OnTestAfterEvent>
  • @Autowired(required=false)
  • EmailReporter emailReporter;
  • public void reportError(String msg) {
  • logger.error(msg);
  • EmailReporter emailReporter = getBean(EmailReporter);
  • if (emailReporter!=null)
  • emailReport.reportError(msg);
  • }

Functions

To many arguments?

a) Introduce composite parameter DTO

b) Make reappearing arguments part of constructor

Code duplicity removal tip

  • A)
  • Extract method from block of code
  • Introduce parameters of what should differ
  • B)
  • Extract local variables of what should differ
  • Extract method from block of code

Behaviour changing flag arguments

HttpRequestProcessor

  • public process(HttpRequest request, boolean sendEmailWhenFinished)

Self explaining functions

if (timer.hasExpired() && !timer.isRecurrent())

  • if (shouldBeDeleted(timer))

Comments

/**

* @param sellRequest

* @throws ManagedComponentException

*/

// Previously, we calculated this way

// int totalLines = pages * linesPerPage;

int totalLines = totalLines / pages;

Jety - Pavel Jetenský

jetensky.net/blog

// This outputs employee working hours for one week

report.output(workingDaysInWeek * hoursPerDay);

int employeeOneWeekWorkingHours = workingDaysInWeek * hoursPerDay;

report.output(employeeOneWeekWorkingHours);

HttpRequestProcessor

  • public process(HttpRequest request)
  • private processHeaders(HttpRequest request, Writer httpOutput)
  • private processCookies(HttpRequest request, Writer httpOutput)

HttpRequestProcessor

  • HttpRequestProcessor(HttRequest request) {this.writer = new StringWriter(); this.request = request;}
  • public process()
  • private processHeaders()
  • private processCookies()

Čistý kód

Jak mě jedna knížka vrátila radost z programování...

Learn more about creating dynamic, engaging presentations with Prezi