SlideShare a Scribd company logo
1 of 103
Download to read offline
Making Steaks from Sacred Cows 
@KevlinHenney
© BBC
I see Keep Out signs as suggestions more than actual orders. 
Like Dry Clean Only.
Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose. 
Cargo cult programming can also refer to the results of applying a design pattern or coding style blindly without understanding the reasons behind that design principle. 
http://en.wikipedia.org/wiki/Cargo_cult_programming
A Matter of Great import
I currently have an average of 15-25 imports in each source file, which is seriously making my code mixed-up and confusing. 
Is too many imports in your code a bad thing? 
Is there any way around this? 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
It's normal in Java world to have a lot of imports. 
Not importing whole packages is good practice. 
It's a good practice to import class by class instead of importing whole packages. 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
Why?
It is not a problem. Any IDE will manage imports and show them to you only when needed. 
Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. 
Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
It is not a problem. Any IDE will manage imports and show them to you only when needed. 
Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. 
Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
It is not a problem. Any IDE will manage imports and show them to you only when needed. 
Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. 
Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
What is the Matrix? 
Control. 
The Matrix is a computer-generated dream world built to keep us under control in order to change a human being into this. 
© Warner Bros.
I currently have an average of 15-25 imports in each source file, which is seriously making my code mixed-up and confusing. 
Is too many imports in your code a bad thing? 
Is there any way around this? 
http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
Avoid Long Import Lists by Using Wildcards 
Long lists of imports are daunting to the reader. We don't want to clutter up the tops of our modules with 80 lines of imports. Rather we want the imports to be a concise statement about which packages we collaborate with.
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet;
import java.util.*;
import java.beans.Introspector; 
import java.lang.reflect.Array; 
import java.lang.reflect.Constructor; 
import java.lang.reflect.Method; 
import java.lang.reflect.Modifier; 
import java.lang.reflect.Proxy; 
import java.util.Arrays; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.HashMap; 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.LinkedHashSet; 
import java.util.Map; 
import java.util.Set;
import java.beans.*; 
import java.lang.reflect.*; 
import java.util.*;
import java.awt.*; 
import java.util.*; 
List?
import java.awt.*; 
import java.util.*; 
import java.util.List;
Open–Closed Principle
The principle stated that a good module structure should be both open and closed: 
Closed, because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need. 
Open, because there is no guarantee that we will include right from the start every service potentially useful to some client.
[...] A good module structure should be [...] closed [...] because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need.
Published Interface is a term I used (first in Refactoring) to refer to a class interface that's used outside the code base that it's defined in. 
Martin Fowler http://martinfowler.com/bliki/PublishedInterface.html
There is no problem changing a method name if you have access to all the code that calls that method. Even if the method is public, as long as you can reach and change all the callers, you can rename the method.
There is a problem only if the interface is being used by code that you cannot find and change. When this happens, I say that the interface becomes a published interface (a step beyond a public interface).
Published Interface is a term I used (first in Refactoring) to refer to a class interface that's used outside the code base that it's defined in. 
The distinction between published and public is actually more important than that between public and private. 
The reason is that with a non-published interface you can change it and update the calling code since it is all within a single code base. [...] But anything published so you can't reach the calling code needs more complicated treatment. 
Martin Fowler http://martinfowler.com/bliki/PublishedInterface.html
[...] A good module structure should be [...] open [...] because there is no guarantee that we will include right from the start every service potentially useful to some client.
extends
A myth in the object-oriented design community goes something like this: If you use object-oriented technology, you can take any class someone else wrote, and, by using it as a base class, refine it to do a similar task. Robert B Murray C++ Strategies and Tactics
Design and implement for inheritance or else prohibit it 
By now, it should be apparent that designing a class for inheritance places substantial limitations on the class.
Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: 
Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. 
Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
http://blog.8thlight.com/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html
http://blog.8thlight.com/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html 
I've heard it said that the OCP is wrong, unworkable, impractical, and not for real programmers with real work to do. The rise of plugin architectures makes it plain that these views are utter nonsense. On the contrary, a strong plugin architecture is likely to be the most important aspect of future software systems.
public abstract class Shape 
{ 
... 
} 
public class Square extends Shape 
{ 
... 
public void drawSquare() ... 
} 
public class Circle extends Shape 
{ 
... 
public void drawCircle() ... 
}
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) { for(Shape s : list) if(s instanceof Square) ((Square) s).drawSquare(); else if(s instanceof Circle) ((Circle) s).drawCircle(); }
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
if(s instanceof Square) 
((Square) s).drawSquare(); 
else if(s instanceof Circle) 
((Circle) s).drawCircle(); 
}
public abstract class Shape 
{ 
... 
public abstract void draw(); 
} 
public class Square extends Shape 
{ 
... 
public void draw() ... 
} 
public class Circle extends Shape 
{ 
... 
public void draw() ... 
}
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
s.draw(); 
}
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
s.draw(); 
}
Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: 
Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. 
Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: 
Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. 
Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
This double requirement looks like a dilemma, and classical module structures offer no clue. 
But inheritance solves it. 
A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features.
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
if(s instanceof Square) 
((Square) s).drawSquare(); 
else if(s instanceof Circle) 
((Circle) s).drawCircle(); 
}
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
if(s instanceof Square) 
((Square) s).drawSquare(); 
else if(s instanceof Circle) 
((Circle) s).drawCircle(); 
}
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
public abstract class Shape ... 
public class Square extends Shape ... 
public class Circle extends Shape ... 
static void drawAllShapes(Shape[] list) 
{ 
for(Shape s : list) 
s.draw(); 
}
Don't publish interfaces prematurely. 
Modify your code ownership policies to smooth refactoring.
Constructors that throw
Do not throw from a constructor. 
constructor?
Immutable Value The internal state of a value object is set at construction and no subsequent modifications are allowed.
To some, the most important aspect of exceptions is that they provide a general mechanism for reporting errors detected in a constructor. 
Bjarne Stroustrup The Design and Evolution of C++
Naomi Epel 
The Observation Deck
Exceptional Naming
ClassNotFoundException 
EnumConstantNotPresentException 
IllegalArgumentException 
IllegalAccessException 
IndexOutOfBoundsException 
NegativeArraySizeException 
NoSuchMethodException 
TypeNotPresentException 
UnsupportedOperationException
ClassNotFound 
EnumConstantNotPresent 
IllegalArgument 
IllegalAccess 
IndexOutOfBounds 
NegativeArraySize 
NoSuchMethod 
TypeNotPresent 
UnsupportedOperation
ArithmeticException ArrayStoreException ClassCastException InstantiationException NullPointerException SecurityException
Arithmetic 
ArrayStore 
ClassCast 
Instantiation 
NullPointer 
Security
IntegerDivisionByZero 
IllegalArrayElementType 
CastToNonSubclass 
ClassCannotBeInstantiated 
NullDereferenced 
SecurityViolation
Omit needless words. 
William Strunk and E B White The Elements of Style
@knutivars
Shoulda Woulda Coulda
public static boolean isLeapYear(int year) ... 
@Test 
public void testIsLeapYear() ...
function 
test 
test 
test
method 
test 
test 
test 
method 
method 
test 
test
yearsNotDivisibleBy4... 
yearsDivisibleBy4ButNotBy100... 
yearsDivisibleBy100ButNotBy400... 
yearsDivisibleBy400...
Years_not_divisible_by_4_... 
Years_divisible_by_4_but_not_by_100_... 
Years_divisible_by_100_but_not_by_400_... 
Years_divisible_by_400_...
Years_not_divisible_by_4_should_not_be_leap_years 
Years_divisible_by_4_but_not_by_100_should_be_leap_years 
Years_divisible_by_100_but_not_by_400_should_not_be_leap_years 
Years_divisible_by_400_should_be_leap_years
Years_not_divisible_by_4_are_not_leap_years Years_divisible_by_4_but_not_by_100_are_leap_years Years_divisible_by_100_but_not_by_400_are_not_leap_years Years_divisible_by_400_are_not_leap_years
Years_not_divisible_by_4_are_not_leap_years 
Years_divisible_by_4_but_not_by_100_are_leap_years 
Years_divisible_by_100_but_not_by_400_are_not_leap_years 
Years_divisible_by_400_are_not_leap_years
Years_not_divisible_by_4_are_not_leap_years 
Years_divisible_by_4_but_not_by_100_are_leap_years 
Years_divisible_by_100_but_not_by_400_are_not_leap_years 
Years_divisible_by_400_are_not_leap_years
Propositions are vehicles for stating how things are or might be.
Thus only indicative sentences which it makes sense to think of as being true or as being false are capable of expressing propositions.
Make definite assertions. Avoid tame, colourless, hesitating, noncommittal language. 
Note [...] that when a sentence is made stronger, it usually becomes shorter. Thus brevity is a by-product of vigour. 
William Strunk and E B White The Elements of Style
How Assertive Are You?
Use one assert per test case. 
case?
One of the things that Osherove warns against is multiple asserts in unit tests. 
Owen Pellegrin http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/
Proper unit tests should fail for exactly one reason, that’s why you should be using one assert per unit test. 
http://rauchy.net/oapt/
String[] itinerary = ...; 
String[] expected = 
{ "Bristol", "Oslo", "Prague" }; 
assertArrayEquals(expected, itinerary);
assertNotNull(itinerary); 
assertEquals(3, itinerary.length); 
assertEquals("Bristol", itinerary[0]); 
assertEquals("Oslo", itinerary[1]); 
assertEquals("Prague", itinerary[2]);
@Test(expected=...)
new Expectations() 
{{ 
... 
}}
My guideline is usually that you test one logical concept per test. You can have multiple asserts on the same object. They will usually be the same concept being tested. 
Roy Osherove http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/
One of the most foundational principles of good design is: 
Gather together those things that change for the same reason, and separate those things that change for different reasons. 
This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.
A test case should be just that: it should correspond to a single case.
Making Steaks from Sacred Cows

More Related Content

Similar to Making Steaks from Sacred Cows

Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll Uchiha Shahin
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)Jorge López-Lago
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questionsnicolbiden
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworksYuri Visser
 
Architecture tests: Setting a common standard
Architecture tests: Setting a common standardArchitecture tests: Setting a common standard
Architecture tests: Setting a common standardPiotr Horzycki
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
 
Microservices Chaos Testing at Jet
Microservices Chaos Testing at JetMicroservices Chaos Testing at Jet
Microservices Chaos Testing at JetC4Media
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding GuidelinesDIlawar Singh
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaKonrad Malawski
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++Mohamed Essam
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java ProgrammingKaty Allen
 
Android design patterns
Android design patternsAndroid design patterns
Android design patternsVitali Pekelis
 
Going open source with small teams
Going open source with small teamsGoing open source with small teams
Going open source with small teamsJamie Thomas
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller ColumnsJonathan Fine
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddSrinivasa GV
 
Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.DrupalCampDN
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipseanshunjain
 
Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1benDesigning
 

Similar to Making Steaks from Sacred Cows (20)

Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 
Lunch and learn as3_frameworks
Lunch and learn as3_frameworksLunch and learn as3_frameworks
Lunch and learn as3_frameworks
 
Architecture tests: Setting a common standard
Architecture tests: Setting a common standardArchitecture tests: Setting a common standard
Architecture tests: Setting a common standard
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
Microservices Chaos Testing at Jet
Microservices Chaos Testing at JetMicroservices Chaos Testing at Jet
Microservices Chaos Testing at Jet
 
On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding Guidelines
 
Best practices android_2010
Best practices android_2010Best practices android_2010
Best practices android_2010
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
 
Android design patterns
Android design patternsAndroid design patterns
Android design patterns
 
Going open source with small teams
Going open source with small teamsGoing open source with small teams
Going open source with small teams
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tdd
 
Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.Code Review Looking for a vulnerable code. Vlad Savitsky.
Code Review Looking for a vulnerable code. Vlad Savitsky.
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1
 

More from Kevlin Henney

The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical ExcellenceKevlin Henney
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical DevelopmentKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid DeconstructionKevlin Henney
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test CasesKevlin Henney
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to ImmutabilityKevlin Henney
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-InKevlin Henney
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good NameKevlin Henney
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Kevlin Henney
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantKevlin Henney
 

More from Kevlin Henney (20)

Program with GUTs
Program with GUTsProgram with GUTs
Program with GUTs
 
The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical Excellence
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical Development
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid Deconstruction
 
Get Kata
Get KataGet Kata
Get Kata
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test Cases
 
Agility ≠ Speed
Agility ≠ SpeedAgility ≠ Speed
Agility ≠ Speed
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
 
Old Is the New New
Old Is the New NewOld Is the New New
Old Is the New New
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-In
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good Name
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation Quadrant
 
Code as Risk
Code as RiskCode as Risk
Code as Risk
 
Software Is Details
Software Is DetailsSoftware Is Details
Software Is Details
 
Game of Sprints
Game of SprintsGame of Sprints
Game of Sprints
 
Good Code
Good CodeGood Code
Good Code
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 

Making Steaks from Sacred Cows

  • 1. Making Steaks from Sacred Cows @KevlinHenney
  • 3. I see Keep Out signs as suggestions more than actual orders. Like Dry Clean Only.
  • 4. Cargo cult programming is a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose. Cargo cult programming can also refer to the results of applying a design pattern or coding style blindly without understanding the reasons behind that design principle. http://en.wikipedia.org/wiki/Cargo_cult_programming
  • 5.
  • 6. A Matter of Great import
  • 7. I currently have an average of 15-25 imports in each source file, which is seriously making my code mixed-up and confusing. Is too many imports in your code a bad thing? Is there any way around this? http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 8. It's normal in Java world to have a lot of imports. Not importing whole packages is good practice. It's a good practice to import class by class instead of importing whole packages. http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 10. It is not a problem. Any IDE will manage imports and show them to you only when needed. Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 11.
  • 12. It is not a problem. Any IDE will manage imports and show them to you only when needed. Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 13. It is not a problem. Any IDE will manage imports and show them to you only when needed. Most IDEs support code folding where all the imports are folded down to one line. I rarely even see my imports these days as the IDE manages them and hides them as well. Any good IDE, such as Eclipse, will collapse the imports in one line, and you can expand them when needed, so they won't clutter your view. http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 14. What is the Matrix? Control. The Matrix is a computer-generated dream world built to keep us under control in order to change a human being into this. © Warner Bros.
  • 15. I currently have an average of 15-25 imports in each source file, which is seriously making my code mixed-up and confusing. Is too many imports in your code a bad thing? Is there any way around this? http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code
  • 16.
  • 17. Avoid Long Import Lists by Using Wildcards Long lists of imports are daunting to the reader. We don't want to clutter up the tops of our modules with 80 lines of imports. Rather we want the imports to be a concise statement about which packages we collaborate with.
  • 18. import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet;
  • 20. import java.beans.Introspector; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set;
  • 21. import java.beans.*; import java.lang.reflect.*; import java.util.*;
  • 22. import java.awt.*; import java.util.*; List?
  • 23. import java.awt.*; import java.util.*; import java.util.List;
  • 24.
  • 26.
  • 27. The principle stated that a good module structure should be both open and closed: Closed, because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need. Open, because there is no guarantee that we will include right from the start every service potentially useful to some client.
  • 28. [...] A good module structure should be [...] closed [...] because clients need the module's services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need.
  • 29. Published Interface is a term I used (first in Refactoring) to refer to a class interface that's used outside the code base that it's defined in. Martin Fowler http://martinfowler.com/bliki/PublishedInterface.html
  • 30.
  • 31. There is no problem changing a method name if you have access to all the code that calls that method. Even if the method is public, as long as you can reach and change all the callers, you can rename the method.
  • 32. There is a problem only if the interface is being used by code that you cannot find and change. When this happens, I say that the interface becomes a published interface (a step beyond a public interface).
  • 33. Published Interface is a term I used (first in Refactoring) to refer to a class interface that's used outside the code base that it's defined in. The distinction between published and public is actually more important than that between public and private. The reason is that with a non-published interface you can change it and update the calling code since it is all within a single code base. [...] But anything published so you can't reach the calling code needs more complicated treatment. Martin Fowler http://martinfowler.com/bliki/PublishedInterface.html
  • 34. [...] A good module structure should be [...] open [...] because there is no guarantee that we will include right from the start every service potentially useful to some client.
  • 36. A myth in the object-oriented design community goes something like this: If you use object-oriented technology, you can take any class someone else wrote, and, by using it as a base class, refine it to do a similar task. Robert B Murray C++ Strategies and Tactics
  • 37.
  • 38. Design and implement for inheritance or else prohibit it By now, it should be apparent that designing a class for inheritance places substantial limitations on the class.
  • 39.
  • 40. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
  • 42. http://blog.8thlight.com/uncle-bob/2014/05/12/TheOpenClosedPrinciple.html I've heard it said that the OCP is wrong, unworkable, impractical, and not for real programmers with real work to do. The rise of plugin architectures makes it plain that these views are utter nonsense. On the contrary, a strong plugin architecture is likely to be the most important aspect of future software systems.
  • 43. public abstract class Shape { ... } public class Square extends Shape { ... public void drawSquare() ... } public class Circle extends Shape { ... public void drawCircle() ... }
  • 44. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) if(s instanceof Square) ((Square) s).drawSquare(); else if(s instanceof Circle) ((Circle) s).drawCircle(); }
  • 45. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) if(s instanceof Square) ((Square) s).drawSquare(); else if(s instanceof Circle) ((Circle) s).drawCircle(); }
  • 46. public abstract class Shape { ... public abstract void draw(); } public class Square extends Shape { ... public void draw() ... } public class Circle extends Shape { ... public void draw() ... }
  • 47. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
  • 48. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
  • 49. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
  • 50. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
  • 51. Bertrand Meyer gave us guidance as long ago as 1988 when he coined the now famous open-closed principle. To paraphrase him: Software entites (classes, modules, functions, etc.) should be open for extension, but closed for modification. Robert C Martin "The Open-Closed Principle" C++ Report, January 1996
  • 52. This double requirement looks like a dilemma, and classical module structures offer no clue. But inheritance solves it. A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features.
  • 53. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) if(s instanceof Square) ((Square) s).drawSquare(); else if(s instanceof Circle) ((Circle) s).drawCircle(); }
  • 54. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) if(s instanceof Square) ((Square) s).drawSquare(); else if(s instanceof Circle) ((Circle) s).drawCircle(); }
  • 55. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
  • 56. public abstract class Shape ... public class Square extends Shape ... public class Circle extends Shape ... static void drawAllShapes(Shape[] list) { for(Shape s : list) s.draw(); }
  • 57. Don't publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.
  • 58.
  • 60. Do not throw from a constructor. constructor?
  • 61.
  • 62. Immutable Value The internal state of a value object is set at construction and no subsequent modifications are allowed.
  • 63. To some, the most important aspect of exceptions is that they provide a general mechanism for reporting errors detected in a constructor. Bjarne Stroustrup The Design and Evolution of C++
  • 64. Naomi Epel The Observation Deck
  • 66.
  • 67. ClassNotFoundException EnumConstantNotPresentException IllegalArgumentException IllegalAccessException IndexOutOfBoundsException NegativeArraySizeException NoSuchMethodException TypeNotPresentException UnsupportedOperationException
  • 68. ClassNotFound EnumConstantNotPresent IllegalArgument IllegalAccess IndexOutOfBounds NegativeArraySize NoSuchMethod TypeNotPresent UnsupportedOperation
  • 69. ArithmeticException ArrayStoreException ClassCastException InstantiationException NullPointerException SecurityException
  • 70. Arithmetic ArrayStore ClassCast Instantiation NullPointer Security
  • 71. IntegerDivisionByZero IllegalArrayElementType CastToNonSubclass ClassCannotBeInstantiated NullDereferenced SecurityViolation
  • 72. Omit needless words. William Strunk and E B White The Elements of Style
  • 75. public static boolean isLeapYear(int year) ... @Test public void testIsLeapYear() ...
  • 77. method test test test method method test test
  • 81.
  • 85.
  • 86. Propositions are vehicles for stating how things are or might be.
  • 87. Thus only indicative sentences which it makes sense to think of as being true or as being false are capable of expressing propositions.
  • 88. Make definite assertions. Avoid tame, colourless, hesitating, noncommittal language. Note [...] that when a sentence is made stronger, it usually becomes shorter. Thus brevity is a by-product of vigour. William Strunk and E B White The Elements of Style
  • 89.
  • 91. Use one assert per test case. case?
  • 92. One of the things that Osherove warns against is multiple asserts in unit tests. Owen Pellegrin http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/
  • 93. Proper unit tests should fail for exactly one reason, that’s why you should be using one assert per unit test. http://rauchy.net/oapt/
  • 94. String[] itinerary = ...; String[] expected = { "Bristol", "Oslo", "Prague" }; assertArrayEquals(expected, itinerary);
  • 95. assertNotNull(itinerary); assertEquals(3, itinerary.length); assertEquals("Bristol", itinerary[0]); assertEquals("Oslo", itinerary[1]); assertEquals("Prague", itinerary[2]);
  • 97.
  • 99.
  • 100. My guideline is usually that you test one logical concept per test. You can have multiple asserts on the same object. They will usually be the same concept being tested. Roy Osherove http://www.owenpellegrin.com/blog/testing/how-do-you-solve-multiple-asserts/
  • 101. One of the most foundational principles of good design is: Gather together those things that change for the same reason, and separate those things that change for different reasons. This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.
  • 102. A test case should be just that: it should correspond to a single case.