how to clean code in
play

how to Clean Code in asynchronous programs. Helge Betzinger CTO - PowerPoint PPT Presentation

Developer Days 2014 Write Event-based programs again sequentially or how to Clean Code in asynchronous programs. Helge Betzinger CTO pcvisit Software AG Developer Age genda da Days 2014 What is the problem and how to escape?


  1. Developer Days 2014 Write Event-based programs again sequentially or how to Clean Code in asynchronous programs. Helge Betzinger CTO pcvisit Software AG

  2. Developer Age genda da Days 2014 • What is the problem and how to escape? • coasync4cpp let you program TODAY without callbacks! • Where to go from here? • No more Callbacks!

  3. Developer What is is the prob oblem em and how to to escap cape? Days 2014 A typical requirement for a application these days … If the user clicks the button, than replace the image within his clipboard by a URL with a copy of this image within the cloud.

  4. Developer What is is the prob oblem em and how to to escap cape? Days 2014 A typic A ical al requir irement ement for a a applic icati ation on these days … If the user clicks the button, than replace the image within his clipboard by a URL with a copy of this image within the cloud. please wait for the next slide clicking won’t make it come any faster

  5. Developer What is the problem and how to escape? Days 2014 A A typic ical al requir irement ement for a a applic icati ation on these days … If the user clicks the button, than replace the image within his clipboard by a URL with a copy of this image within the cloud. The UI must stay responsive all the time.

  6. Developer What is the problem and how to escape? Days 2014 Async becoming the norm!

  7. Developer Days 2014

  8. Developer Example: Concurrent waiting with signals Days 2014 void MainView::uploadImageFromFile(const QString &filePath) { 1) Manage the QJsonObject object; control flow of the // configure object … application EnginioReply *reply = mModel->append(object); connect( reply, &EnginioReply::finished, this, &MainView::beginUpload); 2) Manage } resources of the infrastructure void MainView::beginUpload(EnginioReply *reply) { reply->deleteLater(); // use result/reply here .. 3) Business logic } related code

  9. Developer Example: Concurrent waiting with futures Days 2014 C++11 File saveCliprdToDisk(); std::future<File> f = std::async(saveCliprdToDisk); f.get() ; // this blocks, until saveCliprdToDisk is done! // even the destructor of std::future blocks!

  10. Developer Example: Concurrent waiting with boost Days 2014 C++ standard proposal N3558, Boost.Thread 1.55.0 boost::future<File> f = boost::async(saveCliprdToDisk); f.then( [] (boost:: future<File> savedF ) { // use result.get() here ... uploadImage( savedF.get()).then( [=] (future<Reply> uploadedFile) { requestUrl (uploadedFile.get()).then( ... ); } ); });

  11. Developer What is the problem and how to escape? Days 2014 And what about Clean Code?

  12. Developer Days 2014 And what about Clean Code?

  13. Developer Days 2014

  14. Developer … how to escape? Days 2014 Document number: N3721 Date: 2013-08-30 Reply-to: Niklas Gustafsson <niklas.gustafsson@microsoft.com> Artur Laksberg <arturl@microsoft.com> Herb Sutter <hsutter@microsoft.com> Sana Mithani <sanam@microsoft.com> Improvements to std::future<T> and Related APIs

  15. Developer What is the problem and how to escape? Days 2014 coasync4cpp let you do asynchronous programming without callbacks

  16. Developer Coasync4cpp - How it works Days 2014 File saveCliprdToDisk(); std::future<File> f = std::async(saveCliprdToDisk); File f = f.get() ; // this blocks, until saveCliprdToDisk is done! File f = Task( boost::async( saveCliprdToDisk )); File f = await Task( boost::async( saveCliprdToDisk )); File f = await boost::async( saveCliprdToDisk );

  17. Developer Coasync4cpp - How it works Days 2014 File saveCliprdToDisk(); std::future<File> f = std::async(saveCliprdToDisk); File f = f.get() ; // this blocks, until saveCliprdToDisk is done! File f = Task( boost::async( saveCliprdToDisk )); File f = await Task( boost::async( saveCliprdToDisk )); File f = await boost::async( saveCliprdToDisk );

  18. Developer Overview coasync4cpp Days 2014 T ask<…> Wrap around a awaitable to make code simpler Allows to use Task/await within a routine

  19. Developer Overview coasync4cpp Days 2014 await Unwraps value of a given awaitable without blocking your thread

  20. Developer Unde dersta stand ndin ing g async Tasks ks Days 2014 Click Message Pump with TaskDispatcher bindAsTask(void Button_Click()) { QUrl url = await clip2UrlAsync ()); url2clip(url); } Task<QUrl> clip2UrlAsync () { … return Task<QUrl>(); } Url T ask available..

  21. Developer Example using await Days 2014 Button.connect( bindAsTask( &MainView::convertIntoUrl, this )); File saveCliprdToDisk(); QNetworkReply * uploadImage ( File ); QNetworkReply * requestUrl ( QNetworkReply * ); void put2clipboard(Qurl); void convertIntoUrl() { File tmpFile = await boost::async( saveCliprdToDisk()); QNetworkReply * uploadedFile = await uploadImage( tmpFile ); QNetworkReply * fileUrl = await ( requestUrl, uploadedFile ); put2clipboard( fileUrl->result()); }

  22. Developer Example using Task Days 2014 Button.connect( bindAsTask( &MainView::convertIntoUrl, this )); Task<File> saveCliprdToDiskAsync(); Task<QNetworkReply * > uploadImageAsync( File ); Task<QUrl> requestUrlAsync(QNetworkReply * ); void put2clipboard(QUrl); void convertIntoUrl() { auto tmpFile = saveCliprdToDiskAsync(); auto uploadedFile = uploadImageAsync( tmpFile ); auto fileUrl = requestUrlAsync( uploadedFile ); put2clipboard(fileUrl); }

  23. Developer Days 2014 Task Task Factories Dispatcher creates empowers Task<> from methods await awaits Awaitables

  24. Developer Task Factories Days 2014 bindAsT ask make_task Creates an Creates an Task<R> from anything, std::function < Task<R> (…) > that is callable from anything, that is callable Starts the method immediatelly Start the method later, with invocation of the function object

  25. Developer Task Factories Days 2014 taskify auto taskify( method, placeholders ::CALLBACK, Args…) -> Task< std::tuple< P… > > ; Starts the method immediatelly Transforms the callback into an awaitable Task Returns a Task with a std::tuple , containing the parameters of the CALLBACK . method can be anything, that is callable CALLBACK must be a function object. placeholders::EXCEPTION also supported

  26. Developer Awaitables Days 2014 T ask<…> boost::future<R> Operation is already running await directly Store and await later Create a Task from it and get result or await later

  27. Developer Helper: TaskDispatcher Days 2014 T askDispatcher4StdThread T askDispatcher4QtThread ThreadWithT asks Creates an dispatcher for Tasks within current thread or creates a new thread with a dispatcher in it Prerequisite to get Task<> working within a particallary thread!

  28. Developer Summary Usage Days 2014 1. Instanciate suitable T askDispatcher in your thread 2. Call async method as T ask, using a T ask Factory 3. Use await/T ask with any Awaitable within this method

  29. Developer Example using Task Days 2014 Button.connect( bindAsTask( &MainView::convertIntoUrl, this )); Task<File> saveCliprdToDiskAsync(); Task<QNetworkReply * > uploadImageAsync( File ); Task<QUrl> requestUrlAsync(QNetworkReply * ); void put2clipboard(QUrl); void convertIntoUrl() { auto tmpFile = saveCliprdToDiskAsync(); auto uploadedFile = uploadImageAsync( tmpFile ); auto fileUrl = requestUrlAsync( uploadedFile ); put2clipboard(fileUrl); }

  30. Developer What is the problem and how to escape? Days 2014 coasync4cpp makes consuming async APIs simple

  31. Developer What is the problem and how to escape? Days 2014 Where to go from here?

  32. Developer Where to go from here? Days 2014 Play around with testcoasync4cpp and testcoasync4qt to understand https://github.com/helgebetzinger/coasync4cpp

  33. Developer coasync4cpp Days 2014 test coasync4qt testcoasync4qt utilize test coasync4cpp testcoasync4cpp depends on depends on Googletest coroutine; threading Google C++ Testing Framwork https://github.com/helgebetzinger/coasync4cpp

  34. Developer What can you expect from version 0.10? Days 2014 Simple integration with legacy code https://github.com/helgebetzinger/coasync4cpp

  35. Developer What can you expect from version 0.10? Days 2014 More More Awaitables T ask Factories QFuture* QNetworkReply* taskifyQtSignal EnginioReply* More Msg-Dispatchers https://github.com/helgebetzinger/coasync4cpp

  36. Developer What can you expect from version 0.10? Days 2014 Extended build support clang, cmake https://github.com/helgebetzinger/coasync4cpp

  37. Developer What is the problem and how to escape? Days 2014 Watch the project and stay tuned Comment and report issues and requirements Contribute added features or fixed bugs coasync4cpp@pcvisit.com https://github.com/helgebetzinger/coasync4cpp

  38. Developer What is the problem and how to escape? Days 2014 No more callbacks! Questions? coasync4cpp@pcvisit.com https://github.com/helgebetzinger/coasync4cpp

  39. Developer What is the problem and how to escape? Days 2014 coasync4cpp@pcvisit.com https://github.com/helgebetzinger/coasync4cpp

  40. Developer What is the problem and how to escape? Days 2014 Best Practices for App- developers

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend