t5 research

Upload: isac-ali

Post on 08-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 T5 RESEARCH

    1/21

    Henrik [email protected]

    (MSc Computer Science, PhD candidate in Statistics)

    Mathematical Statistics

    Centre forMathematical Sciences

    Lund University, Sweden

    ObjectObject--oriented programming andoriented programming and

    programming style guidelines for Rprogramming style guidelines for R

  • 8/7/2019 T5 RESEARCH

    2/21

    2 of21

    Outline

    Objects and Classes

    Concepts of object-oriented programming

    A comp

    lete ex

    amp

    le in [R] Sh

    apes

    References in [R]

    [R] Programming Style Guidelines with a

    few coding conventions.

  • 8/7/2019 T5 RESEARCH

    3/21

    3 of21

    Part I:

    Object-oriented programming in [R]

  • 8/7/2019 T5 RESEARCH

    4/21

    4 of21

    Objects and Classes

    MicroarrayData

    layout: Layout

    R: double[][]G: double[][]

    Rb: double[][]

    Gb: double[][]

    nbrOfSlides(): int

    nbrOfSpots(): intswapDyes(...)

    append()

    as.data.frame(): data.frame

    getLayout(): LayoutsetLayout(layout)

    subtractBackground(...)normalizeWithinSlide(...)

    normalizeAcrossSlides(...)plot(...)

    plotSpatial(...)boxplot(...)

    hist(...)

    static read(...):MicroarrayData

    write(...)

    Class name

    Fields

    Methods

    MicroarrayData

    Layout

    MicroarrayData

    MicroarrayData

    a class is a data type -

    an object is an instance of a class

    Objects of different classes

    A class is the recipe for a certain cake...

    ...and the objects are the actual cakes of that kind.

  • 8/7/2019 T5 RESEARCH

    5/21

    5 of21

    Encapsulation, Inheritance, and Polymorphism

    Encapsulation means that a group of related properties,methods, and other members are treated as a single unit orobject. Objects can control how properties are changed andmethods are executed.

    Why: Makes it easier to change your implementation at alater date by letting you hide implementation details of yourobjects, a practice called data hiding.

  • 8/7/2019 T5 RESEARCH

    6/21

  • 8/7/2019 T5 RESEARCH

    7/21

    7 of21

    Encapsulation, Inheritance, and Polymorphism

    Polymorphism means that you can have multiple classesthat can be used interchangeably, even though each classimplements the same properties or methods in differentways. Polymorphism is essential to object-oriented

    programming because it allows you to use items with thesame names, no matter what type of object is in use at themoment.

    Why: Inheritance becomes more flexible. Subclasses can

    keep some methods inherited from their super classes andoverride others.

  • 8/7/2019 T5 RESEARCH

    8/21

    8 of21

    Overloading and Overriding

    Overloaded members are used to provide different versionsof a property or method that have the same name, but thataccept a different number of parameters, or parameters withdifferent data types. Currently not supported in [R].

    Overridden properties and methods are used to replace aninherited property or method that is not appropriate in aderived class. Overridden members must accept the samedata type and number of arguments (not enforced in [R]).

    Derived classes inherit overridden members.

  • 8/7/2019 T5 RESEARCH

    9/21

    9 of21

    Unified Modeling Language (UML) class diagram

    10..*

    abstractstatic

    private

    association

    (using)

    inheritance

    (is a)

  • 8/7/2019 T5 RESEARCH

    10/21

    10 of21

    # Create different Shape objects and store them in alist

    allShapes

  • 8/7/2019 T5 RESEARCH

    11/21

    11 of21

    setClassS3("Point", function(x=0, y=0) {

    extend(Object(), "Point",.x = x, # private

    .y = y # private

    );

    })

    setMethodS3("getX", "Point", function(this) {

    this$.x;

    })

    setMethodS3("getY", "Point", function(this) {

    this$.y;})

    setMethodS3("getXY", "Point", function(this) {

    c(this$.x, this$.y);

    })

    setMethodS3("setX", "Point", function(this, newX) {

    this$.x

  • 8/7/2019 T5 RESEARCH

    12/21

  • 8/7/2019 T5 RESEARCH

    13/21

    13 of21

    References, how?

    References are notsupported by [R] since everything iscopy-by-value. Have to return new instance:

    setValue

  • 8/7/2019 T5 RESEARCH

    14/21

    14 of21

    Part II:

    [R] Programming Style Guidelines

  • 8/7/2019 T5 RESEARCH

    15/21

    15 of21

    Programming Style Guidelines

    80% of the lifetime cost ofa piece of softwaregoes to maintenance.

    Hardly any software is maintained for itswhole life by the originalauthor.

    Code conventions improve the readability ofthe software, allowing programmers tounderstand new code more quickly andthoroughly.

    If you ship your source code as a product, youneed to make sure it is as well packaged andclean as any other product you create.

  • 8/7/2019 T5 RESEARCH

    16/21

    16 of21

    [R] Coding Convention

    Currently there is no RCC and people

    invent their own conventions or not at

    all.

    We suggest to adapt a modified version

    of the Java coding convention, which

    has proved to be successfuland is ade

    facto stand

    ard.

  • 8/7/2019 T5 RESEARCH

    17/21

    17 of21

    Class names

    Names representing classes must be nouns and

    written in mixed case starting with upper case.

    Shape, Rectangle, Point,MicroarrayData, Layout

    Avoid . (period) in class names, because it might lead toambiguities. , e.g. my.very.own.class is nota good

    name.

  • 8/7/2019 T5 RESEARCH

    18/21

    18 of21

    Field and variable names

    Variables and fields names must be in mixed

    case starting with lower case.

    x, y, nbrOfSlides, locus

    To maintain readability of the code, do notshorten variablenames, e.g. nbrOfGrids (orngrids) is much better than

    ngr.

    Avoid using . (period) in variable names to make names

    more consistent with other naming conventions. However,private fields, e.g. layout., may contain periods for

    improving readability.

  • 8/7/2019 T5 RESEARCH

    19/21

    19 of21

    Method names

    Names representing methods (functions) must

    be verbs and written in mixed case starting with

    lower case.

    getLayout(), normalize(method, slides)

    To maintain readability of the code, do notshorten methodnames, e.g. normalizeWithinSlides() is much better

    than normWSl().

    For same reasons as before avoid using . (period) in methodnames, e.g. get.layout() is notgood.

  • 8/7/2019 T5 RESEARCH

    20/21

    20 of21

    File names

    Classes should be declared in individual files

    with the file name matching the class name.

    Point.R, GenePixData.R

    Results in well organized file structure and also gives quick

    access to the source code, Listing all *.R files in a source

    directory will give you an overview of all the classes.

    For stand-alone functions one may adapt the same policy;intToHex.R, col2rgb.R

  • 8/7/2019 T5 RESEARCH

    21/21

    21 of21

    Where to start

    Tutorials and source code:

    R Programming Style Guidelines

    Programming with References in R

    Implementing support for references in R

    http://www.maths.lth.se/help/R/