Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Čistý kód

No description
by

Pavel Jetensky

on 8 April 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Čistý kód

Jak mě jedna knížka vrátila radost z programování...
Čistý kód
// This outputs employee working hours for one week
report.output(workingDaysInWeek * hoursPerDay);
To many arguments?
a) Introduce composite parameter DTO
b) Make reappearing arguments part of constructor
Functions
One responsibility principle
Code design
Kvíz
Comments
Jety - Pavel Jetenský
jetensky.net/blog

/**
* @param sellRequest
*
@throws ManagedComponentException
*/
// Previously, we calculated this way
// int totalLines = pages * linesPerPage;
int totalLines = totalLines / pages;

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()
int employeeOneWeekWorkingHours = workingDaysInWeek * hoursPerDay;
report.output(employeeOneWeekWorkingHours);
Behaviour changing flag arguments
HttpRequestProcessor
public process(HttpRequest request,
boolean sendEmailWhenFinished
)
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
For:
modules, packages
classes, methods
public void renderReport() {
renderHeader();
renderBody();
writer.append("<hr>Powered by black magic</hr>");
FileUtils.write(outputFileName, writer.toString());
}
Consistency
For:
names
code placement
enums
Prefer enums over constants
HttpRequestProcessor
public static long TIMEOUT_READ_IN_SECONDS_SHORT = 10;
public static long TIMEOUT_READ_IN_SECONDS_LONG = 100;
ReadTimeOutEnum
SHORT(10),
LONG(100);

public ReadTimeOutEnum(int timeoutInSeconds) {
this.timeoutInSeconds = timeoutInSeconds;
}

public int getTimeoutInSeconds() {
return timeoutInSeconds;
}
Why?
Proper use of static methods
HourlyPayCalculator.calculatePay(employee, overimeRate);
Avoid them if:
they are doing something for someone :)
should be polymorphic
Use Observer pattern
AbstractSeleniumTest
@After
jiraIntegrator.synchronizeCreatedIssues()
AbstractSeleniumTest
@After
applicationEventPublisher.publishEvent(new OnTestAfterEvent());
JiraIntegrator
implements ApplicationListener<OnTestAfterEvent>
Spring context occasional bean presence
@Autowired(required=false)
EmailReporter emailReporter;

public void reportError(String msg) {
logger.error(msg);
EmailReporter emailReporter = getBean(EmailReporter);
if (emailReporter!=null)
emailReport.reportError(msg);
}

Self explaining functions
if (timer.hasExpired() && !timer.isRecurrent())
if (shouldBeDeleted(timer))
Stay on same level of abstraction
public void pay()
for(Employeee:employees)
if(e.isPayday()) {
Money pay=e.calculatePay();
e.deliverPay(pay):
}
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);
No transitive calls
reportComposer.getReportWriter().append(footer);
GUITesterModule
ProjectAModule
class Test1
@Test
public void test

class Test2
@Test
public void test2
@Test
public void test3
public static void afterTestSuite()
getAllTests() // from classpath
runAllTests
run afterTestSuite()
Full transcript