Your SlideShare is downloading. ×
Back to basics
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Back to basics

1,592
views

Published on

No matter how good your software development process is, you will still have to write some code at some point.

No matter how good your software development process is, you will still have to write some code at some point.

Published in: Technology, Business

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,592
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. back to basics www.webshell.it twitter.com/paolopolcemercoledì 23 novembre 11
  • 2. back to basics no matter how good your software development process is, you will still have to write some code at some point. twitter.com/paolopolcemercoledì 23 novembre 11
  • 3. Writing Software Is Engineering twitter.com/paolopolcemercoledì 23 novembre 11
  • 4. Writing Software Is Engineering • Software Projects Failure Reports 60 Successful Challenged 45 Failed 30 15 0 1995 2001 twitter.com/paolopolcemercoledì 23 novembre 11
  • 5. Writing Software Is Engineering • Engineering components are real, while software is not. • Interactions are more predictable in engineering. • Software is required to change during development. • There has always been the attempt to carry out software projects as if they were engineering products. But it never worked. twitter.com/paolopolcemercoledì 23 novembre 11
  • 6. Bad news. Programming is not engineering. twitter.com/paolopolcemercoledì 23 novembre 11
  • 7. Writing Software Is Science twitter.com/paolopolcemercoledì 23 novembre 11
  • 8. Writing Software Is Science • The scientific method is “a method of procedure that has characterized natural science since the 17th century, consisting in systematic observation, measurement, and experiment, and the formulation, testing, and modification of hypotheses” twitter.com/paolopolcemercoledì 23 novembre 11
  • 9. Writing Software Is Science • The scientific method is “a method of procedure that has characterized natural science since the 17th century, consisting in systematic observation, measurement, and experiment, and the formulation, testing, and modification of hypotheses” debug driven design? twitter.com/paolopolcemercoledì 23 novembre 11
  • 10. Writing Software Is Art twitter.com/paolopolcemercoledì 23 novembre 11
  • 11. Writing Software Is Art • In fact, as in film, music, sculpture, painting and all the other arts... software is... twitter.com/paolopolcemercoledì 23 novembre 11
  • 12. Writing Software Is Art Men Women • In fact, as in film, 90 music, sculpture, 67,5 painting and all the 45 other arts... software is... 22,5 0 2011 equally distributed between men and women twitter.com/paolopolcemercoledì 23 novembre 11
  • 13. Very disappointing. Programming is either not an art, or is not perceived as such. twitter.com/paolopolcemercoledì 23 novembre 11
  • 14. There is no “Book”. Programming sits somewhere between engineering, science and art. Do not expect to find “The Book” that makes everything clear about software. twitter.com/paolopolcemercoledì 23 novembre 11
  • 15. Who writes software? I have met software architects, software analystis, even software evangelists and then... resources! twitter.com/paolopolcemercoledì 23 novembre 11
  • 16. Technical Skills Matter! twitter.com/paolopolcemercoledì 23 novembre 11
  • 17. Do lots of deliberate practice (J. Jagger “97 things every programmer should Know”) Athletes know the difference between training and performing. twitter.com/paolopolcemercoledì 23 novembre 11
  • 18. TDD with MockObjects twitter.com/paolopolcemercoledì 23 novembre 11
  • 19. Story #1 - News Aggregator • as a user I want a NewsAggregator that shows me news of my interest so that I can read something, rather than having to listen to this very boring keynote. twitter.com/paolopolcemercoledì 23 novembre 11
  • 20. Story #1 - News Aggregator design • a NewsAggregator is something that • receives user input in some way • fetches the news somehow... ??? • displays the news somewhere ??? ??? twitter.com/paolopolcemercoledì 23 novembre 11
  • 21. Story #1 - News Aggregator design • are we supposed to write a NewsAggregator without knowing how it will get the news and how it will show them to us !? Yes, we are. twitter.com/paolopolcemercoledì 23 novembre 11
  • 22. Story #1 - News Aggregator signature candidates... public class NewsAggregator { public ????? show(String searchQuery) {...} } twitter.com/paolopolcemercoledì 23 novembre 11
  • 23. Story #1 - News Aggregator signature candidates... public class NewsAggregator { public List<News> show(String searchQuery) {...} } we tend to add useless getters only to be able to test our object via assertions on its internal state (i.e. the collection of news). But we are not supposed to access an object’s internals. Getters (and Setters) break the most important OOP principle: Encapsulation. List<News> is only a data container and is allowed to have a getter ;). But NewsAggregator is not a value object. twitter.com/paolopolcemercoledì 23 novembre 11
  • 24. Story #1 - News Aggregator signature candidates... public class NewsAggregator { public void show(String searchQuery) {...} } ... but how do we test it, now ?! Tip: replace the word “method” with “message”. rather than saying “call the method Show() on the NewsAggregator”, we will say “send the message Show() to the object NewsAggregator”. twitter.com/paolopolcemercoledì 23 novembre 11
  • 25. Story #1 - News Aggregator testing without accessing the state... Following the Single Responsibility Principle, we don’t want our object to do all the work. Instead, we ask the NewsAggregator to collaborate with other two objects: a NewsFetcher and a Display. note: I don’t generally prefix interfaces with the Create two empty interfaces. “I”, especially in Java. public interface INewsFetcher {} public interface IDisplay {} Update the signature. public class NewsAggregator { public void show(String searchQuery, INewsFetcher newsFetcher, IDisplay anOutputDisplay) {...} } twitter.com/paolopolcemercoledì 23 novembre 11
  • 26. Story #1 - News Aggregator testing without accessing the state... So far, we haven’t written any working code. We have only established that our NewsAggregator should work with a NewsFetcher and a Display which we have not written yet. And we are not going to! We’ll keep our focus on the NewsAggregator and “mock out” the two collaborators. public class NewsAggregatorTest { ... public void setUp() throws Exception { m_context = new Mockery(); m_mockDisplay = m_context.mock(IDisplay.class); m_mockNewsFetcher = m_context.mock(INewsFetcher.class); } } twitter.com/paolopolcemercoledì 23 novembre 11
  • 27. Story #1 - News Aggregator testing without accessing the state... @Test public void testNewsAggregator() { final List<News> fakeNews = new Vector<News>(); fakeNews.add(new News("a news")); fakeNews.add(new News("some other news")); m_context.checking(new Expectations() { { oneOf(m_mockNewsFetcher).fetch("conferences"); will(returnValue(fakeNews)); exactly(2).of(m_mockDisplay).display(with(any(News.class))); } }); NewsAggregator newsAggregator = new NewsAggregator(); newsAggregator.show("conferences", m_mockNewsFetcher, m_mockDisplay); } twitter.com/paolopolcemercoledì 23 novembre 11
  • 28. Story #1 - News Aggregator testing without accessing the state... There is still no need to implement any of the collaborators. We only provide them with the signatures needed for the test. public interface IDisplay { void display(News notizia); } public interface INewsFetcher { List<News> fetch(String queryString); } twitter.com/paolopolcemercoledì 23 novembre 11
  • 29. Story #1 - News Aggregator public class NewsAggregator { public void show(String searchQuery, INewsFetcher newsFetcher, IDisplay anOutputDisplay) { List<News> someNews = newsFetcher.fetch(searchQuery); for (News aNews : someNews) { anOutputDisplay.display(aNews); } } } twitter.com/paolopolcemercoledì 23 novembre 11
  • 30. Story #1 - News Aggregator keep the focus! • The nice thing about the MockObjects technique is that it lets you focus on what you are testing. • We don’t know how the NewsFetcher or the Display will be implemented. And we actually don’t care. What we do know is that once the collaborators will be available, our NewsAggregator will work. twitter.com/paolopolcemercoledì 23 novembre 11
  • 31. Story #1 - News Aggregator best practices with mockobjects • do not Mock value objects • It’s very bad and useless. • do not Mock classes. • It’s a code smell you can easily avoid. • avoid retro-fitting mocks into existing tests. • only mock interfaces. twitter.com/paolopolcemercoledì 23 novembre 11
  • 32. Story #1 - News Aggregator refactoring public class NewsAggregator { public void show(String searchQuery, INewsFetcher newsFetcher, IDisplay anOutputDisplay) {...} } move const dependencies to the constructor public class NewsAggregator { public NewsAggregator(INewsFetcher newsFetcher, IDisplay aDisplay) {...} public void show(String searchQuery) {...} } twitter.com/paolopolcemercoledì 23 novembre 11
  • 33. Story #1 - News Aggregator typical usage constructor injection NewsAggregator aggregator = new NewsAggregator( new GoogleNewsAggregator(), new ConsoleDisplay()); aggregator.show("sport"); twitter.com/paolopolcemercoledì 23 novembre 11
  • 34. Object Composition twitter.com/paolopolcemercoledì 23 novembre 11
  • 35. Story #1 - News Aggregator go patterns go! GoogleNewsFetcher() YahooNewsFetcher() BingNewsFetcher() CompositeNewsFetcher(G,Y, B) NoDuplicatesDecoratorNewsFetcher(C) new NewsAggregator( NoDupes, new ConsoleDisplay()) twitter.com/paolopolcemercoledì 23 novembre 11
  • 36. Story #1 - News Aggregator go patterns go! GoogleNewsFetcher() YahooNewsFetcher() CompositeNewsFetcher BingNewsFetcher() List<News> fetch(String query) { CompositeNewsFetcher(G,Y, B) gNews = m_googleFetcher.fetch(query); yNews = m_yahooFetcher.fetch(query); bNews = m_bingFetcher.fetch(query); return g + y + b; NoDuplicatesDecorator NewsFetcher }(C) new NewsAggregator( NoDupes, new ConsoleDisplay()) twitter.com/paolopolcemercoledì 23 novembre 11
  • 37. Story #1 - News Aggregator go patterns go! GoogleNewsFetcher() YahooNewsFetcher() BingNewsFetcher() CompositeNewsFetcher(G,Y, B) NoDuplicatesDecoratorNewsFetcher(C) new NewsAggregator( NoDupes, new ConsoleDisplay()) twitter.com/paolopolcemercoledì 23 novembre 11
  • 38. Story #1 - News Aggregator go patterns go! GoogleNewsFetcher() YahooNewsFetcher() BingNewsFetcher() NoDupesDecoratorNewsFetcher List<News> fetch(String query) { Composite (G,Y, B) someNews = m_Fetcher.fetch(query); NewsFetcher return removeDuplicates(someNews); } NoDuplicatesDecoratorNewsFetcher(C) new NewsAggregator( NoDupes, new ConsoleDisplay()) twitter.com/paolopolcemercoledì 23 novembre 11
  • 39. Story #1 - News Aggregator go patterns go! GoogleNewsFetcher() YahooNewsFetcher() BingNewsFetcher() CompositeNewsFetcher(G,Y, B) NoDuplicatesDecoratorNewsFetcher(C) new NewsAggregator( NoDupes, new ConsoleDisplay()) twitter.com/paolopolcemercoledì 23 novembre 11
  • 40. Getters vs Messages The Concert twitter.com/paolopolcemercoledì 23 novembre 11
  • 41. Push your design, try to: • remove getters • remove singletons, or at least move them to the constructor, and use them as a service locator • remove train wrecks • refactor as you go. • split responsibility as soon as your method reaches 5 or 10 lines of code. • do not make your tests more complicated than the actual code, or you will have to test the tests. twitter.com/paolopolcemercoledì 23 novembre 11
  • 42. Push your design, try to: • don’t be skeptical! • learn new stuff.You haven’t finished yet. No one has. • make your code a pleasant place to live in (see Richard Gabriel’s “code habitability”) twitter.com/paolopolcemercoledì 23 novembre 11
  • 43. Credits • mock roles, not objects (Mackinnon/Freeman/Pryce/Walnes) • http://www.jmock.org/oopsla2004.pdf • 97 things every programmer should know (K. Henney et al.) • http://www.amazon.com/Things-Every-Programmer-Should-Know/dp/0596809484 • why writing software is not like engineering (T. Parr) • http://www.cs.usfca.edu/~parrt/doc/software-not-engineering.html twitter.com/paolopolcemercoledì 23 novembre 11