Giter Club home page Giter Club logo

codeguide's Introduction

#CodeGuide

principles or guidance for writing good code that OOP student can apply.

###In public class use accessor methods While creating a class, do not write a class like this

public class Student {
    public String name;
    public long id;
    ...
}

The data field (these attributes) can access directly. So that it's hard to maintain the value of variable, enforce invariants. And it's not Encapsulation. Public class should always be the private field, setting the attributes to be private, use public accessor (getters) methods to get the value of each attribute, and public mutator (setters) methods to set each attribute.

public class Student {
    //attributes
    private String name;
    private long id;

    //constructor
    ...

    //accessor method for name
    public String getName() {
        return this.name;
    }

    //accessor method for id
    public long getId() {
        return this.id;
    }

    //mutator method for name
    public void setName( String newName ) {
        this.name = newName;
    }

    //mutator method for id
    public void setId( long newId ) {
        this.id = newId;
    }

}

Exercise: Student class

Reference: Effective Java Chapter 4 Item:14, Joshua Bloch, Second Edition.

===== ###Use interfaces only for definition type Interface class is used to type classes. The classes which implement the same interface are the same type. From the code below, it's interface which group classes that have shape such as triangular, square, circle, rectangle, etc.

public interface Shape {
    public boolean contains(Point P);
    public double perimeter(Point[] points);
}

Which classes implement Shape interface, it means you can invoke contain(Point p) that tell this shape has point at p(x,y) or not, and perimeter(Point[] points) tell this shape's perimeter. So it's inappropriate to use the interface in another purpose. Constant interface is interface that have no method, have only constant variable. Such the code below

Public interface AtomConstant {
  static final double ELECTRON_MASS = 9.10938356e-31;
  static final double ELECTRON_CHARGE = -1.60217662e-19;
  static final double PROTON_MASS = 1.6726219e-27;
  static final double PROTON_CHARGE = 1.60217662e-19;
}

Imagine that you have 'A' class implement AtomConstant and you have 'B' class extends the 'A' class. So both A and B can use constants in AtomConstant although only A has to use these constants. This makes confusing and pollute name spaces in memory.

Exercise: Triangular class implements Shape interface and Point class is used.

Reference: Effective Java Chapter 4 Item:19, Joshua Bloch, Second Edition.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.