Redesigning with Traits

482 views

Published on

Redesigning with Traits. The Nile Stream trait-based Library. Damien Cassou.

ESUG 2007, Lugano

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
482
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Redesigning with Traits

  1. 1. Redesigning with Traits The Nile Stream trait-based Library Damien Cassou1 Stéphane Ducasse1 Roel Wuyts2 1LISTIC, University of Savoie, France 2IMEC, Leuven and Université Libre de Bruxelles European Smalltalk User Group, 2007
  2. 2. Outline Context Traits Problems Streams Nile Implementation Results Conclusion Further work D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 2 / 19
  3. 3. Context Outline Context Traits Problems Streams Nile Implementation Results Conclusion Further work D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 3 / 19
  4. 4. Context Traits Traits Introduction Traits • group of reusable methods • implemented and required methods • conflict management • implemented in Perl 6, Fortress, Scala, Smalltalk D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 4 / 19
  5. 5. Context Traits Trait sample > >= <= between:and: < = TMagnitude < = ... String < = ... Number < = ... Date • TMagnitude offers four methods and requires two • A class uses traits and implements required methods D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 5 / 19
  6. 6. Context Problems Problems Observation Never used to design framemorks/libraries from scratch Questions • ideal trait granularity? • how much code reuse? • trait or class? • limits, constraints, problems? D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 6 / 19
  7. 7. Context Problems Designing a New Trait-based Stream Library Solution • development of an adapted project • choosing the stream library Needs • compatibility with ANSI Smalltalk • compatibility with the current Squeak implementation • speed efficient D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 7 / 19
  8. 8. Context Problems Streams Streams Streams are used to read and write data sequentially in collections, files and network sockets. Reading • next • next: • peek Writing • nextPut: • nextPutAll: • next:put: Positioning • position: • reset • setToEnd Why streams ? • naturally modeled using multiple inheritance • existing implementations (some of them trait-based) • complex library D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 8 / 19
  9. 9. Context Streams Squeak Implementation atEnd close contents do: flush next next: next:put: nextPut: nextPutAll: upToEnd Stream atEnd contents isEmpty next: peek position position: reset setToEnd skip: upTo: upToEnd collection position readLimit Positionable Stream next next: nextPut: size upTo: upToEnd ReadStream contents flush next nextPut: nextPutAll: position: reset size space cr writeLimit WriteStream contents next next: ReadWriteStream atEnd close flush fullName next next: nextPut: nextPutAll: peek position position: reset size rwmode name fileID buffer1 FileStream • everything in the Stream class • methods disabled • reimplemented methods • methods reactivated • files inherit from collections D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 9 / 19
  10. 10. Context Streams Schärli’s Refactoring OOPSLA 2003 flush close TStreamCommon contents: last originalContents atEnd isEmpty position: reset resetContents setToEnd skip: error: position privatePosition: collection collection: readLimit readLimit: TStreamPositionable position: collection collection: readLimit: TStreamReadWrite Common nextMatchFor: upToEnd do: next: atEnd next TStreamReadable next next: peek peekFor: size upTo: upToEnd position collection position: readLimit: skip: TStreamReadable Positionable nextPutAll: nextPut: error: TStreamWriteable resetToStart nextPut: size position position: skip: collection collection: readLimit readLimit: writeLimit writeLimit: TStreamWriteable Positionable StreamTop collection position readLimit StreamPositionable Stream StreamReadStream close closed contents writeLimit StreamReadWrite Stream -{#size} writeLimit StreamWriteStream Only a refactoring • no positioning for files • peek depends on position and collection • . . . D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 10 / 19
  11. 11. Nile Outline Context Traits Problems Streams Nile Implementation Results Conclusion Further work D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 11 / 19
  12. 12. Nile Implementation Nile overview Core Collection-based Streams File-based Streams Character writing Decoders D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 12 / 19
  13. 13. Nile Implementation Collection-based Streams collection position ReadableCollectionStream collection position writeLimit Writeable CollectionStream collection position writeLimit ReadWriteCollectionStream do: nextMatchFor: next: peekFor: skip: skipTo: upTo: upToEnd upToElementSatisfying: atEnd next peek outputCollectionClass TGettableStream atEnd atStart close isEmpty position: reset setToEnd position setPosition: size TPositionableStream nextPutAll: next:put: print: flush nextPut: TPuttableStream contents size collection setCollection: TCollectionStream nextPut: size writeLimit writeLimit: TWriteableCollection Stream -{#size} Core next peek skip: outputCollectionClass TReadableCollectionStream • a trait per basic functionality • traits for collections and one class for each original Squeak class D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 13 / 19
  14. 14. Nile Implementation Other Implemented Libraries Core Collection-based Streams File-based Streams Character writing Decoders Other Implemented Libraries Files file-based streams Random random number generator SharedQueue queue concurrent access Decoder pipe system to chain streams (compression, multiplexing. . . ) and many others. . . D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 14 / 19
  15. 15. Nile Results New Design Good Code Reuse • good code reuse : 22 classes use TGettableStream • important gain : TCharacterWriting requires implementing one method to gain 8 Better Implementation • 18% fewer methods • 15% less byte-code • not forced to use unwanted behavior Better Performance • faster execution • traits don’t prevent optimization D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 15 / 19
  16. 16. Nile Results Trait-related Problems Discovered • interface pollution • traits require accessors • sometimes an extra getter or setter • IDE support for traits needs improvement • difficult to visualize and navigate D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 16 / 19
  17. 17. Conclusion Outline Context Traits Problems Streams Nile Implementation Results Conclusion Further work D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 17 / 19
  18. 18. Conclusion Further work Further Work • Having an equivalent implementation? • much more entities (11 traits+classes compared to 4 classes in Squeak) • simplify using only read/write access? • lot’s of ugly code in the original implementation D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 18 / 19
  19. 19. Conclusion Further work Conclusion • Context • traits needed to be tested in a new project • a stream library is a good project example • Realized work • existing implementation was analyzed • new design was created • efficient implementation • problem analysis • really improves the design • needs further work D. Cassou, S. Ducasse, R. Wuyts (Savoie, Bruxelles) Redesigning with Traits ESUG 2007 19 / 19

×