• Save
Your Business. Your Language. Your Code - dpc13
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Your Business. Your Language. Your Code - dpc13

on

  • 811 views

 

Statistics

Views

Total Views
811
Views on SlideShare
802
Embed Views
9

Actions

Likes
4
Downloads
0
Comments
0

4 Embeds 9

http://lanyrd.com 4
http://librosweb.es 3
https://twitter.com 1
http://translate.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Your Business. Your Language. Your Code - dpc13 Presentation Transcript

  • 1. Your business. Your language.Your code.Stephan Hochdörfer, bitExpert AG
  • 2. Your business. Your language. Your code.About me Stephan Hochdörfer Head of IT at bitExpert AG, Germany enjoying PHP since 1999 S.Hochdoerfer@bitExpert.de @shochdoerfer
  • 3. Your business. Your language. Your code.Focus on core business values!
  • 4. Your business. Your language. Your code.Get rid of the "framework speak"!
  • 5. Your business. Your language. Your code.Trust the framework...
  • 6. Your business. Your language. Your code....but understand the underlying technology!
  • 7. Your business. Your language. Your code.Build your own toolset...
  • 8. Your business. Your language. Your code....but focus on communication first!
  • 9. Your business. Your language. Your code.Work as a team! (Customer included!)
  • 10. What the customer really wanted?Your business. Your language. Your code.
  • 11. A common languageYour business. Your language. Your code.
  • 12. A common languageYour business. Your language. Your code.A domain-specific language (DSL)is a programming language designedto be useful for a specific task.
  • 13. A common language: Key characteristicYour business. Your language. Your code.Identifiable domain
  • 14. A common language: Key characteristicYour business. Your language. Your code.„Small language“
  • 15. A common languageYour business. Your language. Your code."[…] I do think that the greatestpotential benefit of DSLs comes whenbusiness people participate [...]"(Martin Fowler)
  • 16. Domain-specific language: ExamplesYour business. Your language. Your code.How does a domain-specificlanguage look like?
  • 17. SELECT * from slides WHERE presentationId = 4711 ORDER BYpageNo;Domain-specific language: SQLYour business. Your language. Your code.
  • 18. Feature: Serve coffeeIn order to earn moneyCustomers should be able tobuy coffee at all timesScenario: Buy last coffeeGiven there are 1 coffees left in the machineAnd I have deposited 1 dollarWhen I press the coffee buttonThen I should be served a coffeeDomain-specific language: GherkinYour business. Your language. Your code.
  • 19. package { screen :ensure => present,}include apacheapache::vhost { myapp.loc :docroot => /srv/myapp.loc/webroot/}host { myapp.loc:ip => 127.0.1.1}Domain-specific language: PuppetYour business. Your language. Your code.
  • 20. {"require": {"silex/silex": "v1.0.0","zendframework/zend-mail": "2.1.5","monolog/monolog": "1.5.0"},"require-dev": {"phpunit/phpunit": "3.7.19","phing/phing": "2.5.0"}}Domain-specific language: ComposerYour business. Your language. Your code.
  • 21. Domain-specific language: DoctrineYour business. Your language. Your code./** @Entity **/class Slides{/*** @OneToOne(targetEntity="Presentation")* @JoinColumn(name="presentation_id",* referencedColumnName="id")**/private $presentation;// ...}/** @Entity **/class Presentation{// ...}
  • 22. <?phpecho "I am a DSL, too!";Domain-specific language: PHPYour business. Your language. Your code.
  • 23. Why using a DSL?Your business. Your language. Your code.
  • 24. Why using a DSL?Your business. Your language. Your code.Increase your own productivity
  • 25. Why using a DSL?Your business. Your language. Your code.Increase in code quality
  • 26. Why using a DSL?Your business. Your language. Your code.Ignore the implementation details,focus on the domain aspects
  • 27. Why using a DSL?Your business. Your language. Your code.
  • 28. $file = /path/to/a/file.csv;$mimepart1 = new MimePart();$mimepart1->setContent(file_get_contents($file));$mimepart1->setFilename(basename($file));$mimepart2 = new MimePart();$mimepart2->setText(Sample mail body.);$multipart = new MimeMultipart();$multipart->addPart($mimepart1);$multipart->addPart($mimepart2);$message = new MimeMessage();$message->setFrom(foo@bar.org);$message->addTo(bar@foo.org);$message->setSubject(Hello);$message->setContent($multipart);$transport = new MailTransport();$transport->send($mail);Why using a DSL? Non-DSL exampleYour business. Your language. Your code.
  • 29. DSL types: You have to decideYour business. Your language. Your code.
  • 30. DSL types: You have to decideYour business. Your language. Your code.Internal DSLvs.External DSL
  • 31. DSL types: Internal DSLYour business. Your language. Your code.
  • 32. DSL types: Internal DSLYour business. Your language. Your code.Fluent API, Expression Builder,Functions, Closures, Annotations
  • 33. Internal DSL: The Fluent APIYour business. Your language. Your code.
  • 34. Internal DSL: The Fluent APIYour business. Your language. Your code.A fluent interface is an implementationof an object oriented API that aims toprovide for more readable code.
  • 35. $file = /path/to/a/file.csv;$msg = new MailMessage();$msg->from(foo@bar.org)->to(bar@foo.org)->subject(Hello)->body(Sample mail body.)->attachFile($file)->sendWith(new SmtpTransport());Internal DSL: The Fluent APIYour business. Your language. Your code.
  • 36. Internal DSL: The Fluent APIYour business. Your language. Your code.class MailMessage {protected $from;protected $to;public function from($from) {$this->from = $from;return $this;}public function to($to) {$this->to = $to;return $this;}// [...]}
  • 37. Internal DSL: The Fluent APIYour business. Your language. Your code.class MailMessage {protected $from;protected $to;public function from($from) {$this->from = $from;return $this;}public function to($to) {$this->to = $to;return $this;}// [...]}
  • 38. Internal DSL: Expression BuilderYour business. Your language. Your code.
  • 39. Internal DSL: Expression BuilderYour business. Your language. Your code.Provides a fluent interfaceon top of an existing API.
  • 40. class MimeMessage {public function setFrom($from) {}public function addTo($to) {}public function setSubject($subject) {}public function setContent(MimeMultipart $content) {}}Internal DSL: Expression BuilderYour business. Your language. Your code.
  • 41. class MailBuilder {protected $msg;protected $multipart;public function __construct() {$this->msg = new MimeMessage();$this->multipart = new MimeMultipart();}public function from($from) {$this->msg->setFrom($from);return $this;}public function attachFile($file) {$part = new MimePart();$part->setContent(file_get_contents($file));$part->setFilename(basename($file));$this->multipart->addPart($part);return $this;}// [...]}Internal DSL: Expression BuilderYour business. Your language. Your code.
  • 42. Internal DSL: Expression BuilderYour business. Your language. Your code.$file = /path/to/a/file.csv;$msg = new MailBuilder();$msg->from(foo@bar.org)->to(bar@foo.org)->subject(Hello)->body(Sample mail body.)->attachFile($file)->sendWith(new SmtpTransport());
  • 43. Internal DSL: Expression Builder (Improved)Your business. Your language. Your code.class MailBuilder {protected $msg;protected $parts;public function __construct() {$this->msg = new MimeMessage();$this->parts = array();}public function from($from) {$this->msg->setFrom($from);return $this;}public function attachFile($file) {$builder = new PartBuilder($this);$this->parts[] = $builder;return $builder;}// [...]}
  • 44. class PartBuilder {protected $part;public function __construct() {$this->part = new MimePart();}public function content($content) {$this->part->setContent($content);return $this;}public function filename($filename) {$this->part->setFilename($filename);return $this;}// [...]}Internal DSL: Expression Builder (Improved)Your business. Your language. Your code.
  • 45. Internal DSL: FunctionsYour business. Your language. Your code.
  • 46. Internal DSL: FunctionsYour business. Your language. Your code.Using a combination of function callsas a sequence of statements.
  • 47. Internal DSL: FunctionsYour business. Your language. Your code.$file = /path/to/a/file.csv;message();from(foo@bar.org);to(bar@foo.org);subject(Hello);body(Sample mail body.);attachFile($file);sendWith(new SmtpTransport());
  • 48. Internal DSL: Nested FunctionsYour business. Your language. Your code.
  • 49. Internal DSL: Nested FunctionsYour business. Your language. Your code.Using nested function calls to reflectthe hirarchic nature of the language.
  • 50. $file = /path/to/a/file.csv;message(header(from(foo@bar.org),to(bar@foo.org),subject(Hello)),body(Sample mail body.),attachments(file($file)),sendWith(new SmtpTransport()));Internal DSL: Nested FunctionsYour business. Your language. Your code.
  • 51. $file = /path/to/a/file.csv;$file2 = /path/to/a/file2.csv;message(header([from(foo@bar.org),to(bar@foo.org),subject(Hello)]),body(Sample mail body.),attachments([file($file),file($file2)]),sendWith(new SmtpTransport()));Internal DSL: Nested Functions (Improved)Your business. Your language. Your code.
  • 52. Internal DSL: ClosureYour business. Your language. Your code.
  • 53. Internal DSL: ClosureYour business. Your language. Your code.A function or reference to a functiontogether with a referencing environment.
  • 54. $fname = /path/to/a/file.csv;$fname2 = /path/to/a/file2.csv;$msg = new MailMessageBuilder();$msg->header(function($header){$header->from(foo@bar.org)->to(bar@foo.org)->subject(Hello);})->body(Sample mail body.)->attach(function($file){$file->setContent(file_get_contents($fname)->setFilename(basename($fname);})->attach(function($file){$file->setContent(file_get_contents($fname2)->setFilename(basename($fname2);})->sendWith(new SmtpTransport());Internal DSL: ClosureYour business. Your language. Your code.
  • 55. Internal DSL: AnnotationsYour business. Your language. Your code.
  • 56. Internal DSL: AnnotationsYour business. Your language. Your code."An annotation is metadataattached to your code."(Rafael Dohms)
  • 57. Internal DSL: AnnotationsYour business. Your language. Your code./** @Entity **/class Slides{/*** @OneToOne(targetEntity="Presentation")* @JoinColumn(name="presentation_id",* referencedColumnName="id")**/private $presentation;// ...}/** @Entity **/class Presentation{// ...}
  • 58. class MyCustomController {// [...]public function adminAction() {$user = $this->authService->getLoggedInUser();if(!$user->hasRole(ROLE_ADMIN)) {throw new AccessDeniedException();}// proceed with main logic...}}Internal DSL: AnnotationsYour business. Your language. Your code.
  • 59. class MyCustomController {// [...]/*** @Allow(roles="ROLE_ADMIN")*/public function adminAction() {// proceed with main logic...}}Internal DSL: AnnotationsYour business. Your language. Your code.
  • 60. External DSLYour business. Your language. Your code.
  • 61. External DSLYour business. Your language. Your code.External DSLs provide agreater syntactic freedom
  • 62. External DSL: Language developmentYour business. Your language. Your code.
  • 63. Code generationYour business. Your language. Your code.
  • 64. Code generationYour business. Your language. Your code.From DSL to „code“ ;)
  • 65. Code generationYour business. Your language. Your code.Template Generationvs.Transformer Generation
  • 66. Code generation: Template GenerationYour business. Your language. Your code.<?phpclass [className] {protected $service;public function __construct([serviceType] $service) {$this->service = $service;}}
  • 67. Code generation: Transformer GenerationYour business. Your language. Your code.<?phprenderPageHeader();renderMenubar();renderProducts();renderFooter();Output driven approach:
  • 68. Code generation: Transformer GenerationYour business. Your language. Your code.<?phpforeach($products as $product) {renderName($product);renderImage($product);renderPrice($product);}Input driven approach:
  • 69. Internal DSL vs. External DSLYour business. Your language. Your code.What to choose?
  • 70. Problems with DSLsYour business. Your language. Your code.
  • 71. Problems with DSLsYour business. Your language. Your code.Yet-Another-Language-To-Learnsyndrome
  • 72. Problems with DSLsYour business. Your language. Your code.Depending on the DSL complexitycreating a language can be cost intense
  • 73. Famous final words ;)Your business. Your language. Your code."Any fool can write code that acomputer can understand. Goodprogrammers write code thathumans can understand."(Martin Fowler)
  • 74. Recommended booksYour business. Your language. Your code.
  • 75. Thank you!
  • 76. http://joind.in/8640
  • 77. Creditshttp://robonthemoon.wordpress.com/2009/11/24/what-the-customer-really-wanted-–-happy-thanksgiving/