Learn the principles and practices of Clean Code Development. This is the second session in the Clean Code Development track. Go on with the next level and dig into Clean Code.
Interfaces for decoupling • Use more small Interfaces, not one large • Coupling only between the really necessary parts • Less dependencies = easier to switch backend implementations • Every public class/method is an API Dienstag, 27. August 13
fly(); void run(); void bark(); } public class Bird implements Animal { public void bark() { /* do nothing */ } public void run() { // write code about running of the bird } public void fly() { // write code about flying of the bird } } public class Cat implements Animal { public void fly() { throw new Exception("Undefined cat property"); } public void bark() { throw new Exception("Undefined cat property"); } public void run() { // write code about running of the cat } } public class Dog implements Animal { public void fly() { } public void bark() { // write code about barking of the dog } public void run() { // write code about running of the dog } } Dienstag, 27. August 13
fly(); } public interface Runnable { void run(); } public interface Barkable { void bark(); } public class Bird implements Flyable, Runnable { public void run() { // write code about running of the bird } public void fly() { // write code about flying of the bird } } public class Cat implements Runnable{ public void run() { // write code about running of the cat } } Dienstag, 27. August 13
parent-type • Functions that use references to base classes must be able to use objects of derived classes without knowing it • Contravariance of method arguments in the subtype • Covariance of return types in the subtype • Think twice before deriving classes, how they will behave Dienstag, 27. August 13
subtype must not throw exceptions • Parent-Type allows a value range, subtype can extend the range but not limit it • Parent-Typ returns defines a return type, subtype can return a smaller data type but not a larger one Dienstag, 27. August 13
fly(); void run(); void bark(); } public class Bird implements Animal { public void run() { // write code about running of the bird } public void fly() { // write code about flying of the bird } public void bark() { /* do nothing */ } } public class Cat implements Animal { public void fly() { throw new Exception("Undefined cat method"); } public void run() { // write code about running of the cat } public void bark() { throw new Exception("Undefined cat method"); } } Dienstag, 27. August 13
surprises • You expect Control-C to „Copy“, not to „Delete all files“ • „GetValue“ has to return a value, not changing something • Does your Method-Names/Class-Names tell the truth? • Ever met strange Code? Dienstag, 27. August 13
as little as possible • Expose as much as needed • Exposing too much (methods, data) leads to a tight coupling • Tight coupling prevents flexibility Dienstag, 27. August 13
One Class/File/Module • Tests support QA, find Bugs before QA does • Tests create trust and confidence • Do you rule the code or does the code rule you? Dienstag, 27. August 13
any production code unless it is to make a failing unit test pass • You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures • You are not allowed to write any more production code than is sufficient to pass the one failing unit test. Dienstag, 27. August 13
Tests? • Do Tests fail, when you add Bugs? • Coverage Target: 100% • All Branches • All Exception Cases • 90% sufficient – other 10% would take 90% more time Dienstag, 27. August 13