develop maintainable apps - eduiconf
TRANSCRIPT
Reference the Interface
public void onEvent (ErrorEvent event) {
mainView.showVideoUnavailableMessage(); }
Implement the Interface@Override public void showVideoUnavailableMessage () {
Toast.makeText( this, getString(R.string.video_unavailable), Toast.LENGTH_LONG ) .show();
}
Review…
• Keep Code Simple
• Use MV* Pattern
• Functional Tests
• Use the Tools
@brwngrldev
Domain Design
2
Increase Readability?
“Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard.”
apply plugin: ‘checkstyle’
task checkstyle(type: Checkstyle) { description 'Checks if the code passes quality standards' group 'verification' configFile file(‘checkstyle.xml') …}
<module name=“MethodLength"> <property name="max" value=“60"/> </module> <module name=“LineLength"> <property name="max" value=“120"/> </module><module name=“CyclomaticComplexity"> <property name="max" value=“8"/> </module> …
playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false)); playerControlConfig.setShowClosedCaptionsButton(a.getBoolean(R.styleable.WapoVideoView_showClosedCaptionsButton, false));
public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break; case "CAT2" : if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.smallImage.png"); }
public void overlyComplexMethod(Video video) { if (video != null && video.getStreamUrl() != null) { updateVideoBasedOnCategory(video); } }
private void updateVideoBasedOnCategory(Video video) { switch (video.getCategory()) { case "CAT1" : playVideo(video); if (video.getLargeImageUrl() == null) { video.setLargeImageUrl("http://www.largeImage.png"); } updateMetadata(video); break;
7
switch (video.getCategory()) { case "CAT1" : playVideo(video); updateMetaDataAndUrl(video, "http://www.largeImage.png"); break; …
4
@brwngrldev
JUnit
• Framework for writing repeatable tests
• Assertions for testing expected results
• Features for sharing common test data
• Test suites for organizing tests
Mockito
• Enables mock creation and verification
• Mock concrete classes and interfaces
• Simple annotation support
Test Setup@Mock MainView mainView;
@Before public void setUp () {
initMocks( this );
mainPresenter = new MainPresenter(); mainPresenter.setMainView( mainView );
}
@brwngrldev
Sample Test
@brwngrldev
@Test public void configurationLoaded_shouldHideSplashScreen () {
ConfigurationLoadedEvent event = new ConfigurationLoadedEvent();
mainPresenter.onEvent( event );
}
Sample Test
@brwngrldev
@Test public void configurationLoaded_shouldHideSplashScreen () {
ConfigurationLoadedEvent event = new ConfigurationLoadedEvent();
mainPresenter.onEvent( event );
verify( mainView ).hideSplashScreen(); }
Review
• Listen to Robert Martin!
• JUnit + Mockito
• Continuous Integration
• Code CoverageTesting
4
@brwngrldev
Resources
• Clean Code - http://amzn.to/1DJybxH
• Effective Java - http://amzn.to/1Ku8Xel
• Working Effectively with Legacy Code - http://amzn.to/1Jqe1PA
• Unit Testing Idioms - http://goo.gl/Bx1WbL
• Google Code Style - http://goo.gl/8Pf6J3
• Architecting Android - http://goo.gl/UKvmbq
• Conquering Cyclomatic Complexity - http://goo.gl/lRoPXN
@brwngrldev
Photo Credits• Slide 5 - https://www.flickr.com/photos/tshirbert/118250140
• Slide 19 - http://uncompromisedmen.com/2015/02/17/top-10-crazy-beards-wish-youd-seen-person/
• Slide 34 - https://www.flickr.com/photos/cast_fish/2888442781
• Slide 48 - https://www.flickr.com/photos/desertbusforhope/8207412726/
• Slide 50 - https://www.flickr.com/photos/sokabs/2668975758
• Slide 64 - https://www.flickr.com/photos/pasukaru76/5268559005
@brwngrldev