Code Monkey home page Code Monkey logo

codeguide's Introduction

#Codeguide ##By Kanchanok Kannee (5910545639)

##Useful references:

Clean Code: A Handbook of Agile Software Craftsmanship Robert C. Martin Link
Replace Magic Number with Symbolic Constant By Martin Fowler Link
How to Write Doc Comments for the Javadoc Tool Link

##What Is Clean Code?

###Getting Clean via Emergent Design Beck’s rules of simple code. In priority order, simple code:

  • Runs all the tests
  • Contains no duplication
  • Expresses all the design ideas that are in the system
  • Minimizes the number of entities such as classes, methods, functions, and the like.

###1.Meaningful Names
Use Intention-Revealing Names
Avoid Disinformation For example, hp, aix, and sco would be poor variable names because they are the names of Unix platforms or variants.
A class name should not be a verb.

Example :
names like Person, Student, Game


Methods should have verb or verb phrase names

Example :
names like addDevice, deletePage  
person.setName("Kanchanok");

###2.Methods
Do One Thing Functions should do one thing. They should do it well. They should do it only.

Example :

public void play() { 
for (Family p : persons) 
  playBall(e); 
} 

private void playBall(Family p) { 
  if (p.isChild()) 
     keepPerson(p); 
}  


Don’t Repeat Yourself(DRY) Removing duplication ensures that every concept in the system has a single authoritative representation in the code. A change to a single business concept results in a single change to the code. DRY increases maintainability by isolating change (risk) to only those parts of the system that must change.

public class Device {
 
	public void payMoney() {
 
		System.out.println(“Poor!”);
		sentenceAnswers();
	}
 
	public void withdraw() {
 
		System.out.println(“Don’t have money.”);
		sentenceAnswers();
	}
 
	public void sentenceAnswers() {

		System.out.println(“Umm, Do you have any money ?”);		
	}
}

###3.Comments
Informative Comments It is sometimes useful to provide basic information with a comment.
TODO Comments TODOs are jobs that the programmer thinks should be done.

Example :
 //TODO  these are not needed 


Must have a javadoc.
Javadoc must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values. such as : @return , @param, @see

Example :
        /**
	 * Goods with given name and cost of goods.
	 * @param name is the name of goods.
	 * @param cost is the price of goods.
	 */
	public Goods(String name, int cost) {
		this.name = name;
		this.cost = cost;
	}
	
	/**
	 * Get the name of this object.
	 * @return 
	 */
	public String getName() {
		return name;
	}

###4.Magic Numbers

Replace Magic Numbers with Symbolic Constant
Magic Number refers to the bad programming practice of using numbers directly in source code without explanation.

Bad :
double potentialEnergy(double mass, double height) {
  return mass * height * 9.81;
}

Good :
double potentialEnergy(double mass, double height) {
  return mass * GRAVITATIONAL_CONSTANT * height;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;

##Exersice
To improvement exercise for other students to do. Following TODO.
Exersice1
Exersice2

codeguide's People

Contributors

mailtoy avatar

Watchers

 avatar

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.