SlideShare a Scribd company logo
1 of 100
Download to read offline
Seven Ineffective Coding
Habits of Many Java
Programmers
@KevlinHenney
It turns out that style matters in
programming for the same
reason that it matters in writing.
It makes for better reading.
Douglas Crockford
JavaScript: The Good Parts
Noisy Code
Signal-to-noise ratio (often abbreviated SNR or
S/N) is a measure used in science and engineering
that compares the level of a desired signal to the
level of background noise.
Signal-to-noise ratio is sometimes used informally
to refer to the ratio of useful information to false or
irrelevant data in a conversation or exchange.
http://en.wikipedia.org/wiki/Signal_to_noise_ratio
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them?
William Shakespeare
Hamlet
Continuing existence or cessation of
existence: those are the scenarios. Is it
more empowering mentally to work towards
an accommodation of the downsizings and
negative outcomes of adversarial
circumstance, or would it be a greater
enhancement of the bottom line to move
forwards to a challenge to our current
difficulties, and, by making a commitment
to opposition, to effect their demise?
Tom Burton
Long Words Bother Me
public class RecentlyUsedList
{
private List<String> items;
public RecentlyUsedList()
{
items = new ArrayList<String>();
}
public void add(String newItem)
{
if (items.contains(newItem))
{
int position = items.indexOf(newItem);
String existingItem = items.get(position);
items.remove(position);
items.add(0, existingItem);
}
else
{
items.add(0, newItem);
}
}
public int size()
{
int result = items.size();
return result;
}
public String get(int index)
{
int position = 0;
for (String item : items)
{
if (position == index)
return item;
++position;
}
throw new IndexOutOfBoundsException();
}
}
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public void add(String newItem)
{
items.remove(newItem);
items.add(newItem);
}
public int size()
{
return items.size();
}
public String get(int index)
{
return items.get(size() - index – 1);
}
}
public class RecentlyUsedList
{
private List<String> items;
public RecentlyUsedList()
{
items = new ArrayList<String>();
}
public void add(String newItem)
{
if (items.contains(newItem))
{
int position = items.indexOf(newItem);
String existingItem = items.get(position);
items.remove(position);
items.add(0, existingItem);
}
else
{
items.add(0, newItem);
}
}
public int size()
{
int result = items.size();
return result;
}
public String get(int index)
{
int position = 0;
for (String item : items)
{
if (position == index)
return item;
++position;
}
throw new IndexOutOfBoundsException();
}
}
Comments
A delicate matter, requiring taste and judgement. I tend to err on the side of
eliminating comments, for several reasons. First, if the code is clear, and uses
good type names and variable names, it should explain itself. Second, comments
aren't checked by the compiler, so there is no guarantee they're right, especially
after the code is modified. A misleading comment can be very confusing. Third,
the issue of typography: comments clutter code.
Rob Pike, "Notes on Programming in C"
There is a famously bad comment style:
i=i+1; /* Add one to i */
and there are worse ways to do it:
/**********************************
* *
* Add one to i *
* *
**********************************/
i=i+1;
Don't laugh now, wait until you see it in real life.
Rob Pike, "Notes on Programming in C"
A common fallacy is to assume authors
of incomprehensible code will somehow
be able to express themselves lucidly
and clearly in comments.
Kevlin Henney
https://twitter.com/KevlinHenney/status/381021802941906944
Unsustainable Spacing
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them?
William Shakespeare
Hamlet
Continuing existence or cessation of
existence: those are the scenarios. Is it
more empowering mentally to work towards
an accommodation of the downsizings and
negative outcomes of adversarial
circumstance, or would it be a greater
enhancement of the bottom line to move
forwards to a challenge to our current
difficulties, and, by making a commitment
to opposition, to effect their demise?
Tom Burton
Long Words Bother Me
Continuing existence or cessation of existence:
those are the
more empowe
to work towa
accommodati
downsizings
outcomes of
circumstance
a greater enh
the bottom li
forwards to a
our current d
by making a
opposition, t
demise?
The way many programmers lay out their code
Column 80
The way people read
To answer the question "What is clean design?"
most succinctly: a clean design is one that
supports visual thinking so people can meet their
informational needs with a minimum of
conscious effort.
Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
You convey information by the way you arrange
a design's elements in relation to each other. This
information is understood immediately, if not
consciously, by the people viewing your designs.
Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
This is great if the visual relationships are
obvious and accurate, but if they're not, your
audience is going to get confused. They'll have to
examine your work carefully, going back and
forth between the different parts to make sure
they understand.
Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
public int howNotToLayoutAMethodHeader(int firstArgument,
String secondArgument)
public int ensureArgumentsAreAlignedLikeThis(
int firstArgument,
String secondArgument)
public int orEnsureArgumentsAreGroupedLikeThis(
int firstArgument, String secondArgument)
public int butNotAlignedLikeThis(int firstArgument,
String secondArgument)
int doNotFormat = likeThis(someArgumentOrExpression,
anotherArgumentOrExpression);
int insteadFormat =
somethingLikeThis(
someArgumentOrExpression,
anotherArgumentOrExpression);
int orFormat = somethingLikeThis(
someArgumentOrExpression,
anotherArgumentOrExpression);
int asItIs = unstable(someArgumentOrExpression,
anotherArgumentOrExpression);
int butThisIs =
stable(
someArgumentOrExpression,
anotherArgumentOrExpression);
int andThisIs = stable(
someArgumentOrExpression,
anotherArgumentOrExpression);
public ResultType arbitraryMethodName(FirstArgumentType firs
SecondArgumentType sec
ThirdArgumentType thir
LocalVariableType localVariable = method(firstArgument,
secondArgument)
if (localVariable.isSomething(thirdArgument,
SOME_SHOUTY_CONSTANT)) {
doSomethingWith(localVariable);
}
return localVariable.getSomething();
}
public ResultType arbitraryMethodName(
FirstArgumentType firstArgument,
SecondArgumentType secondArgument,
ThirdArgumentType thirdArgument) {
LocalVariableType localVariable =
method(firstArgument, secondArgument);
if (localVariable.isSomething(
thirdArgument, SOME_SHOUTY_CONSTANT)) {
doSomething(localVariable);
}
return localVariable.getSomething();
}
XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX
XX XXXXXXXXXXXXX XXXXXXXXXXX
XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
public ResultType arbitraryMethodName(
FirstArgumentType firstArgument,
SecondArgumentType secondArgument,
ThirdArgumentType thirdArgument) {
LocalVariableType localVariable =
method(firstArgument, secondArgument);
if (localVariable.isSomething(
thirdArgument, SOME_SHOUTY_CONSTANT)) {
doSomething(localVariable);
}
return localVariable.getSomething();
}
XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX
XX XXXXXXXXXXXXX XXXXXXXXXXX
XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
public ResultType arbitraryMethodName(
FirstArgumentType firstArgument,
SecondArgumentType secondArgument,
ThirdArgumentType thirdArgument)
{
LocalVariableType localVariable =
method(firstArgument, secondArgument);
if (localVariable.isSomething(
thirdArgument, SOME_SHOUTY_CONSTANT))
{
doSomething(localVariable);
}
return localVariable.getSomething();
}
XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX
XX XXXXXXXXXXXXX XXXXXXXXXXX
XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXX XXXXXXXXXXXXX
XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
class AttributedBody extends Body
implements Attributed
{
AttributedImpl attrImpl = new AttributedImpl();
...
}
public static String quotedString(
String from, char start, char end)
{
int startPos = from.indexOf(start);
int endPos = from.lastIndexOf(end);
if (startPos == -1) // no start found
return null;
else if (endPos == -1) // no end found
return from.substring(startPos);
else // both start and end found
return from.substring(startPos, endPos + 1);
}
public void replaceValue(String name, Object newValue)
throws NoSuchAttributeException
{
Attr attr = find(name); // lookup the attr
if (attr == null) // it isn't found
throw new NoSuchAttributeException(name, this);
attr.valueOf(newValue);
}
Lego Naming
Agglutination is a process in linguistic morphology
derivation in which complex words are formed by
stringing together morphemes, each with a single
grammatical or semantic meaning. Languages that
use agglutination widely are called agglutinative
languages.
http://en.wikipedia.org/wiki/Agglutination
pneumonoultramicroscopicsilicovolcanoconiosis
Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz
fylkestrafikksikkerhetsutvalgssekretariatslederfunksjonene
Proxy
validate
Service
Value
get
create
Manager
check
Controller
set
Factory
Object
doenable
process
disable
Exception
add
remove
public interface BookEntity ...
public interface BookEntityFactory ...
public interface ISBNValidator ...
public interface CatalogueRepository ...
public interface CatalogueRepositoryProvider ...
public abstract class AbstractBookEntity
implements BookEntity
public class BookEntityImpl
extends BookEntity ...
public class BookEntityFactoryImpl
implements BookEntityFactory ...
public class ISBNValidatorImpl
implements ISBNValidator ...
public class CatalogueRepositoryImpl
implements CatalogueRepository ...
public class CatalogueRepositoryProviderImpl
implements CatalogueRepositoryProvider ...
public interface Book ...
public final class ISBN ...
public interface Catalogue ...
public class DescriptionOfCatalogueImplementation
implements Catalogue ...
public interface ConditionChecker
{
boolean checkCondition();
}
public interface Condition
{
boolean isTrue();
}
public Connection createConnection(Provider...)
throws ConnectionFailureException
...
public Connection connectTo(Provider...)
throws ConnectionFailure
...
Omit needless words.
William Strunk and E B White
The Elements of Style
Underabstraction
http://fragmental.tw/2009/04/29/tag-clouds-see-how-noisy-your-code-is/
http://fragmental.tw/2009/04/29/tag-clouds-see-how-noisy-your-code-is/
if (portfolioIdsByTraderId.get(trader.getId())
.containsKey(portfolio.getId()))
{
...
}
Dan North, "Code in the Language of the Domain"
97 Things Every Programmer Should Know
if (trader.canView(portfolio))
{
...
}
Dan North, "Code in the Language of the Domain"
97 Things Every Programmer Should Know
parser.processNodes(text, false);
Gregor Hohpe, "Convenience Is Not an -ility"
97 Things Every Programmer Should Know
If you have a procedure with
ten parameters, you probably
missed some.
Alan Perlis
Unencapsulated State
encapsulate enclose (something) in or as if in a capsule.
 express the essential feature of (someone or something)
succinctly.
 enclose (a message or signal) in a set of codes which
allow use by or transfer through different computer
systems or networks.
 provide an interface for (a piece of software or hardware)
to allow or simplify access for the user.
The New Oxford Dictionary of English
An affordance is a quality of an
object, or an environment, which
allows an individual to perform an
action. For example, a knob affords
twisting, and perhaps pushing,
while a cord affords pulling.
http://en.wikipedia.org/wiki/Affordance
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public List<String> getList()
{
return items;
}
public void add(String newItem)
{
if(newItem == null)
throw new IllegalArgumentException();
items.remove(newItem);
items.add(0, newItem);
}
...
}
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public List<String> getList()
{
return items;
}
public void add(String newItem)
{
if(newItem == null)
throw new IllegalArgumentException();
items.remove(newItem);
items.add(0, newItem);
}
...
}
Don't ever invite a
vampire into your
house, you silly boy.
It renders you
powerless.
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public int isEmpty()
{
return items.isEmpty()
}
public int size()
{
return items.size();
}
public String get(int index)
{
return items.get(index);
}
public void add(String newItem)
{
if(newItem == null)
throw new IllegalArgumentException();
items.remove(newItem);
items.add(0, newItem);
}
...
}
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public int isEmpty()
{
return items.isEmpty()
}
public int size()
{
return items.size();
}
public String get(int index)
{
return items.get(index);
}
public void add(String newItem)
{
if(newItem == null)
throw new IllegalArgumentException();
items.remove(newItem);
items.add(0, newItem);
}
...
}
public class RecentlyUsedList
{
private List<String> items = new ArrayList<String>();
public int isEmpty()
{
return items.isEmpty()
}
public int size()
{
return items.size();
}
public String get(int index)
{
return items.get(size() – index - 1);
}
public void add(String newItem)
{
if(newItem == null)
throw new IllegalArgumentException();
items.remove(newItem);
items.add(newItem);
}
...
}
Do not use
new in
constructors.?
It’s OK to use
new in
constructors.
Do not use
throw in
constructors.?
It’s OK to use
throw in
constructors.
Getters and Setters
public final class Date implements ...
{
...
public int getYear() ...
public int getMonth() ...
public int getDayInMonth() ...
public void setYear(int newYear) ...
public void setMonth(int newMonth) ...
public void setDayInMonth(int newDayInMonth) ...
...
}
public final class Date implements ...
{
...
public int getYear() ...
public int getMonth() ...
public int getWeekInYear() ...
public int getDayInYear() ...
public int getDayInMonth() ...
public int getDayInWeek() ...
public void setYear(int newYear) ...
public void setMonth(int newMonth) ...
public void setWeekInYear(int newWeek) ...
public void setDayInYear(int newDayInYear) ...
public void setDayInMonth(int newDayInMonth) ...
public void setDayInWeek(int newDayInWeek) ...
...
}
When it is not
necessary to
change, it is
necessary not to
change.
Lucius Cary
public final class Date implements ...
{
...
public int getYear() ...
public int getMonth() ...
public int getWeekInYear() ...
public int getDayInYear() ...
public int getDayInMonth() ...
public int getDayInWeek() ...
...
}
public final class Date implements ...
{
...
public int year() ...
public int month() ...
public int weekInYear() ...
public int dayInYear() ...
public int dayInMonth() ...
public int dayInWeek() ...
...
}
public final class Date implements ...
{
...
public int year() ...
public Month month() ...
public int weekInYear() ...
public int dayInYear() ...
public int dayInMonth() ...
public DayInWeek dayInWeek() ...
...
}
Some people, when confronted with a
problem, think, "I know, I'll use threads,"
and then two they hav erpoblesms.
Ned Batchelder
https://twitter.com/#!/nedbat/status/194873829825327104
Mutable
Immutable
Unshared Shared
Unshared mutable
data needs no
synchronisation
Unshared immutable
data needs no
synchronisation
Shared mutable
data needs
synchronisation
Shared immutable
data needs no
synchronisation
Synchronisation
Quadrant
Uncohesive Tests
Everybody knows that TDD stands for Test Driven
Development. However, people too often concentrate
on the words "Test" and "Development" and don't
consider what the word "Driven" really implies. For
tests to drive development they must do more than
just test that code performs its required functionality:
they must clearly express that required functionality
to the reader. That is, they must be clear specifications
of the required functionality. Tests that are not written
with their role as specifications in mind can be very
confusing to read.
Nat Pryce and Steve Freeman
"Are Your Tests Really Driving Your Development?"
public class RecentlyUsedList
{
...
public RecentlyUsedList() ...
public boolean isEmpty() ...
public int size() ...
public String get(int index) ...
public void add(String newItem) ...
...
}
public class RecentlyUsedListTest
{
@Test
public void testConstructor() ...
@Test
public void testIsEmpty() ...
@Test
public void testSize() ...
@Test
public void testGet() ...
@Test
public void testAdd() ...
...
}
public class RecentlyUsedList_spec
{
public static class A_new_list
{
@Test public void is_empty() 
}
public static class An_empty_list
{
@Test public void retains_a_single_addition() 
@Test public void retains_unique_additions_in_stack_order() 
}
public static class A_non_empty_list
{
@Test public void is_unchanged_when_head_item_is_readded() 
@Test public void moves_non_head_item_to_head_when_it_is_readded() 
}
public static class Any_list
{
@Test public void rejects_addition_of_null_items() 
@Test public void rejects_indexing_past_its_end() 
@Test public void rejects_negative_indexing() 
}
}
public class RecentlyUsedList_spec
{
public static class A_new_list
{
@Test public void is_empty() 
}
public static class An_empty_list
{
@Test public void retains_a_single_addition() 
@Test public void retains_unique_additions_in_stack_order() 
}
public static class A_non_empty_list
{
@Test public void is_unchanged_when_head_item_is_readded() 
@Test public void moves_non_head_item_to_head_when_it_is_readded() 
}
public static class Any_list
{
@Test public void rejects_addition_of_null_items() 
@Test public void rejects_indexing_past_its_end() 
@Test public void rejects_negative_indexing() 
}
}
A test case should
be just that: it
should correspond
to a single case.
Style is the art of
getting yourself out
of the way, not
putting yourself in it.
David Hare

More Related Content

Similar to Seven Ineffective Coding Habits of Many Java Programmers

Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersKevlin Henney
 
The Role Of Ontology In Modern Expert Systems Dallas 2008
The Role Of Ontology In Modern Expert Systems   Dallas   2008The Role Of Ontology In Modern Expert Systems   Dallas   2008
The Role Of Ontology In Modern Expert Systems Dallas 2008Jason Morris
 
KevlinHenney_PuttingThereIntoArchitecture
KevlinHenney_PuttingThereIntoArchitectureKevlinHenney_PuttingThereIntoArchitecture
KevlinHenney_PuttingThereIntoArchitectureKostas Mavridis
 
Experiments in Reasoning
Experiments in ReasoningExperiments in Reasoning
Experiments in ReasoningAslam Khan
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-InKevlin Henney
 
Object? You Keep Using that Word
Object? You Keep Using that WordObject? You Keep Using that Word
Object? You Keep Using that WordKevlin Henney
 
Using binary classifiers
Using binary classifiersUsing binary classifiers
Using binary classifiersbutest
 
Dcn 20170823 yjy
Dcn 20170823 yjyDcn 20170823 yjy
Dcn 20170823 yjy재연 윤
 
Using Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigDataUsing Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigDataAnalyticsWeek
 
Computational decision making
Computational decision makingComputational decision making
Computational decision makingBoris Adryan
 
The Art of Identifying Vulnerabilities - CascadiaFest 2015
The Art of Identifying Vulnerabilities  - CascadiaFest 2015The Art of Identifying Vulnerabilities  - CascadiaFest 2015
The Art of Identifying Vulnerabilities - CascadiaFest 2015Adam Baldwin
 
What's next in Julia
What's next in JuliaWhat's next in Julia
What's next in JuliaJiahao Chen
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrongJulien Wetterwald
 
2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datos2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datosPHP Conference Argentina
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docxjeanettehully
 
Maintainable code
Maintainable codeMaintainable code
Maintainable codeRiverGlide
 

Similar to Seven Ineffective Coding Habits of Many Java Programmers (20)

Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many Programmers
 
Design Patterns: Back to Basics
Design Patterns: Back to BasicsDesign Patterns: Back to Basics
Design Patterns: Back to Basics
 
The Role Of Ontology In Modern Expert Systems Dallas 2008
The Role Of Ontology In Modern Expert Systems   Dallas   2008The Role Of Ontology In Modern Expert Systems   Dallas   2008
The Role Of Ontology In Modern Expert Systems Dallas 2008
 
KevlinHenney_PuttingThereIntoArchitecture
KevlinHenney_PuttingThereIntoArchitectureKevlinHenney_PuttingThereIntoArchitecture
KevlinHenney_PuttingThereIntoArchitecture
 
Experiments in Reasoning
Experiments in ReasoningExperiments in Reasoning
Experiments in Reasoning
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-In
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Object? You Keep Using that Word
Object? You Keep Using that WordObject? You Keep Using that Word
Object? You Keep Using that Word
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Using binary classifiers
Using binary classifiersUsing binary classifiers
Using binary classifiers
 
Designing Testable Software
Designing Testable SoftwareDesigning Testable Software
Designing Testable Software
 
Dcn 20170823 yjy
Dcn 20170823 yjyDcn 20170823 yjy
Dcn 20170823 yjy
 
Using Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigDataUsing Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigData
 
Computational decision making
Computational decision makingComputational decision making
Computational decision making
 
The Art of Identifying Vulnerabilities - CascadiaFest 2015
The Art of Identifying Vulnerabilities  - CascadiaFest 2015The Art of Identifying Vulnerabilities  - CascadiaFest 2015
The Art of Identifying Vulnerabilities - CascadiaFest 2015
 
What's next in Julia
What's next in JuliaWhat's next in Julia
What's next in Julia
 
A well-typed program never goes wrong
A well-typed program never goes wrongA well-typed program never goes wrong
A well-typed program never goes wrong
 
2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datos2013 - Andrei Zmievski: Machine learning para datos
2013 - Andrei Zmievski: Machine learning para datos
 
Running Head Discussion Board .docx
Running Head Discussion Board                                  .docxRunning Head Discussion Board                                  .docx
Running Head Discussion Board .docx
 
Maintainable code
Maintainable codeMaintainable code
Maintainable code
 

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
 
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
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our WaysKevlin Henney
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID DeconstructionKevlin Henney
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeKevlin 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
 
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...
 
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
 
The Error of Our Ways
The Error of Our WaysThe Error of Our Ways
The Error of Our Ways
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
Declarative Thinking, Declarative Practice
Declarative Thinking, Declarative PracticeDeclarative Thinking, Declarative Practice
Declarative Thinking, Declarative Practice
 

Recently uploaded

Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 

Recently uploaded (20)

Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 

Seven Ineffective Coding Habits of Many Java Programmers

  • 1. Seven Ineffective Coding Habits of Many Java Programmers @KevlinHenney
  • 2.
  • 3.
  • 4. It turns out that style matters in programming for the same reason that it matters in writing. It makes for better reading. Douglas Crockford JavaScript: The Good Parts
  • 5.
  • 7. Signal-to-noise ratio (often abbreviated SNR or S/N) is a measure used in science and engineering that compares the level of a desired signal to the level of background noise. Signal-to-noise ratio is sometimes used informally to refer to the ratio of useful information to false or irrelevant data in a conversation or exchange. http://en.wikipedia.org/wiki/Signal_to_noise_ratio
  • 8. To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? William Shakespeare Hamlet
  • 9. Continuing existence or cessation of existence: those are the scenarios. Is it more empowering mentally to work towards an accommodation of the downsizings and negative outcomes of adversarial circumstance, or would it be a greater enhancement of the bottom line to move forwards to a challenge to our current difficulties, and, by making a commitment to opposition, to effect their demise? Tom Burton Long Words Bother Me
  • 10. public class RecentlyUsedList { private List<String> items; public RecentlyUsedList() { items = new ArrayList<String>(); } public void add(String newItem) { if (items.contains(newItem)) { int position = items.indexOf(newItem); String existingItem = items.get(position); items.remove(position); items.add(0, existingItem); } else { items.add(0, newItem); } } public int size() { int result = items.size(); return result; } public String get(int index) { int position = 0; for (String item : items) { if (position == index) return item; ++position; } throw new IndexOutOfBoundsException(); } }
  • 11. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public void add(String newItem) { items.remove(newItem); items.add(newItem); } public int size() { return items.size(); } public String get(int index) { return items.get(size() - index – 1); } } public class RecentlyUsedList { private List<String> items; public RecentlyUsedList() { items = new ArrayList<String>(); } public void add(String newItem) { if (items.contains(newItem)) { int position = items.indexOf(newItem); String existingItem = items.get(position); items.remove(position); items.add(0, existingItem); } else { items.add(0, newItem); } } public int size() { int result = items.size(); return result; } public String get(int index) { int position = 0; for (String item : items) { if (position == index) return item; ++position; } throw new IndexOutOfBoundsException(); } }
  • 12.
  • 13.
  • 14. Comments A delicate matter, requiring taste and judgement. I tend to err on the side of eliminating comments, for several reasons. First, if the code is clear, and uses good type names and variable names, it should explain itself. Second, comments aren't checked by the compiler, so there is no guarantee they're right, especially after the code is modified. A misleading comment can be very confusing. Third, the issue of typography: comments clutter code. Rob Pike, "Notes on Programming in C"
  • 15. There is a famously bad comment style: i=i+1; /* Add one to i */ and there are worse ways to do it: /********************************** * * * Add one to i * * * **********************************/ i=i+1; Don't laugh now, wait until you see it in real life. Rob Pike, "Notes on Programming in C"
  • 16. A common fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments. Kevlin Henney https://twitter.com/KevlinHenney/status/381021802941906944
  • 17.
  • 19. To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take arms against a sea of troubles, And by opposing end them? William Shakespeare Hamlet
  • 20. Continuing existence or cessation of existence: those are the scenarios. Is it more empowering mentally to work towards an accommodation of the downsizings and negative outcomes of adversarial circumstance, or would it be a greater enhancement of the bottom line to move forwards to a challenge to our current difficulties, and, by making a commitment to opposition, to effect their demise? Tom Burton Long Words Bother Me
  • 21. Continuing existence or cessation of existence: those are the more empowe to work towa accommodati downsizings outcomes of circumstance a greater enh the bottom li forwards to a our current d by making a opposition, t demise?
  • 22. The way many programmers lay out their code Column 80
  • 24. To answer the question "What is clean design?" most succinctly: a clean design is one that supports visual thinking so people can meet their informational needs with a minimum of conscious effort. Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
  • 25. You convey information by the way you arrange a design's elements in relation to each other. This information is understood immediately, if not consciously, by the people viewing your designs. Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
  • 26. This is great if the visual relationships are obvious and accurate, but if they're not, your audience is going to get confused. They'll have to examine your work carefully, going back and forth between the different parts to make sure they understand. Daniel Higginbotham ∙ "Clean Up Your Mess — A Guide to Visual Design for Everyone ∙ http://www.visualmess.com/
  • 27. public int howNotToLayoutAMethodHeader(int firstArgument, String secondArgument) public int ensureArgumentsAreAlignedLikeThis( int firstArgument, String secondArgument) public int orEnsureArgumentsAreGroupedLikeThis( int firstArgument, String secondArgument) public int butNotAlignedLikeThis(int firstArgument, String secondArgument)
  • 28. int doNotFormat = likeThis(someArgumentOrExpression, anotherArgumentOrExpression); int insteadFormat = somethingLikeThis( someArgumentOrExpression, anotherArgumentOrExpression); int orFormat = somethingLikeThis( someArgumentOrExpression, anotherArgumentOrExpression);
  • 29. int asItIs = unstable(someArgumentOrExpression, anotherArgumentOrExpression); int butThisIs = stable( someArgumentOrExpression, anotherArgumentOrExpression); int andThisIs = stable( someArgumentOrExpression, anotherArgumentOrExpression);
  • 30. public ResultType arbitraryMethodName(FirstArgumentType firs SecondArgumentType sec ThirdArgumentType thir LocalVariableType localVariable = method(firstArgument, secondArgument) if (localVariable.isSomething(thirdArgument, SOME_SHOUTY_CONSTANT)) { doSomethingWith(localVariable); } return localVariable.getSomething(); }
  • 31. public ResultType arbitraryMethodName( FirstArgumentType firstArgument, SecondArgumentType secondArgument, ThirdArgumentType thirdArgument) { LocalVariableType localVariable = method(firstArgument, secondArgument); if (localVariable.isSomething( thirdArgument, SOME_SHOUTY_CONSTANT)) { doSomething(localVariable); } return localVariable.getSomething(); }
  • 32. XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX XX XXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
  • 33. public ResultType arbitraryMethodName( FirstArgumentType firstArgument, SecondArgumentType secondArgument, ThirdArgumentType thirdArgument) { LocalVariableType localVariable = method(firstArgument, secondArgument); if (localVariable.isSomething( thirdArgument, SOME_SHOUTY_CONSTANT)) { doSomething(localVariable); } return localVariable.getSomething(); }
  • 34. XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX XX XXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
  • 35. public ResultType arbitraryMethodName( FirstArgumentType firstArgument, SecondArgumentType secondArgument, ThirdArgumentType thirdArgument) { LocalVariableType localVariable = method(firstArgument, secondArgument); if (localVariable.isSomething( thirdArgument, SOME_SHOUTY_CONSTANT)) { doSomething(localVariable); } return localVariable.getSomething(); }
  • 36. XXXXXX XXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXX XX XXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXX XXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX
  • 37. class AttributedBody extends Body implements Attributed { AttributedImpl attrImpl = new AttributedImpl(); ... }
  • 38. public static String quotedString( String from, char start, char end) { int startPos = from.indexOf(start); int endPos = from.lastIndexOf(end); if (startPos == -1) // no start found return null; else if (endPos == -1) // no end found return from.substring(startPos); else // both start and end found return from.substring(startPos, endPos + 1); }
  • 39. public void replaceValue(String name, Object newValue) throws NoSuchAttributeException { Attr attr = find(name); // lookup the attr if (attr == null) // it isn't found throw new NoSuchAttributeException(name, this); attr.valueOf(newValue); }
  • 40.
  • 41.
  • 43. Agglutination is a process in linguistic morphology derivation in which complex words are formed by stringing together morphemes, each with a single grammatical or semantic meaning. Languages that use agglutination widely are called agglutinative languages. http://en.wikipedia.org/wiki/Agglutination
  • 45.
  • 47. public interface BookEntity ... public interface BookEntityFactory ... public interface ISBNValidator ... public interface CatalogueRepository ... public interface CatalogueRepositoryProvider ... public abstract class AbstractBookEntity implements BookEntity public class BookEntityImpl extends BookEntity ... public class BookEntityFactoryImpl implements BookEntityFactory ... public class ISBNValidatorImpl implements ISBNValidator ... public class CatalogueRepositoryImpl implements CatalogueRepository ... public class CatalogueRepositoryProviderImpl implements CatalogueRepositoryProvider ...
  • 48. public interface Book ... public final class ISBN ... public interface Catalogue ... public class DescriptionOfCatalogueImplementation implements Catalogue ...
  • 53. Omit needless words. William Strunk and E B White The Elements of Style
  • 54.
  • 58. if (portfolioIdsByTraderId.get(trader.getId()) .containsKey(portfolio.getId())) { ... } Dan North, "Code in the Language of the Domain" 97 Things Every Programmer Should Know
  • 59. if (trader.canView(portfolio)) { ... } Dan North, "Code in the Language of the Domain" 97 Things Every Programmer Should Know
  • 60. parser.processNodes(text, false); Gregor Hohpe, "Convenience Is Not an -ility" 97 Things Every Programmer Should Know
  • 61. If you have a procedure with ten parameters, you probably missed some. Alan Perlis
  • 62.
  • 64. encapsulate enclose (something) in or as if in a capsule.  express the essential feature of (someone or something) succinctly.  enclose (a message or signal) in a set of codes which allow use by or transfer through different computer systems or networks.  provide an interface for (a piece of software or hardware) to allow or simplify access for the user. The New Oxford Dictionary of English
  • 65. An affordance is a quality of an object, or an environment, which allows an individual to perform an action. For example, a knob affords twisting, and perhaps pushing, while a cord affords pulling. http://en.wikipedia.org/wiki/Affordance
  • 66. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public List<String> getList() { return items; } public void add(String newItem) { if(newItem == null) throw new IllegalArgumentException(); items.remove(newItem); items.add(0, newItem); } ... }
  • 67. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public List<String> getList() { return items; } public void add(String newItem) { if(newItem == null) throw new IllegalArgumentException(); items.remove(newItem); items.add(0, newItem); } ... }
  • 68. Don't ever invite a vampire into your house, you silly boy. It renders you powerless.
  • 69. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public int isEmpty() { return items.isEmpty() } public int size() { return items.size(); } public String get(int index) { return items.get(index); } public void add(String newItem) { if(newItem == null) throw new IllegalArgumentException(); items.remove(newItem); items.add(0, newItem); } ... }
  • 70. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public int isEmpty() { return items.isEmpty() } public int size() { return items.size(); } public String get(int index) { return items.get(index); } public void add(String newItem) { if(newItem == null) throw new IllegalArgumentException(); items.remove(newItem); items.add(0, newItem); } ... }
  • 71. public class RecentlyUsedList { private List<String> items = new ArrayList<String>(); public int isEmpty() { return items.isEmpty() } public int size() { return items.size(); } public String get(int index) { return items.get(size() – index - 1); } public void add(String newItem) { if(newItem == null) throw new IllegalArgumentException(); items.remove(newItem); items.add(newItem); } ... }
  • 72.
  • 73. Do not use new in constructors.?
  • 74. It’s OK to use new in constructors.
  • 75. Do not use throw in constructors.?
  • 76. It’s OK to use throw in constructors.
  • 77.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83. public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getDayInMonth() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setDayInMonth(int newDayInMonth) ... ... }
  • 84. public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setWeekInYear(int newWeek) ... public void setDayInYear(int newDayInYear) ... public void setDayInMonth(int newDayInMonth) ... public void setDayInWeek(int newDayInWeek) ... ... }
  • 85. When it is not necessary to change, it is necessary not to change. Lucius Cary
  • 86. public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... ... }
  • 87. public final class Date implements ... { ... public int year() ... public int month() ... public int weekInYear() ... public int dayInYear() ... public int dayInMonth() ... public int dayInWeek() ... ... }
  • 88. public final class Date implements ... { ... public int year() ... public Month month() ... public int weekInYear() ... public int dayInYear() ... public int dayInMonth() ... public DayInWeek dayInWeek() ... ... }
  • 89. Some people, when confronted with a problem, think, "I know, I'll use threads," and then two they hav erpoblesms. Ned Batchelder https://twitter.com/#!/nedbat/status/194873829825327104
  • 90. Mutable Immutable Unshared Shared Unshared mutable data needs no synchronisation Unshared immutable data needs no synchronisation Shared mutable data needs synchronisation Shared immutable data needs no synchronisation Synchronisation Quadrant
  • 91.
  • 93. Everybody knows that TDD stands for Test Driven Development. However, people too often concentrate on the words "Test" and "Development" and don't consider what the word "Driven" really implies. For tests to drive development they must do more than just test that code performs its required functionality: they must clearly express that required functionality to the reader. That is, they must be clear specifications of the required functionality. Tests that are not written with their role as specifications in mind can be very confusing to read. Nat Pryce and Steve Freeman "Are Your Tests Really Driving Your Development?"
  • 94.
  • 95. public class RecentlyUsedList { ... public RecentlyUsedList() ... public boolean isEmpty() ... public int size() ... public String get(int index) ... public void add(String newItem) ... ... }
  • 96. public class RecentlyUsedListTest { @Test public void testConstructor() ... @Test public void testIsEmpty() ... @Test public void testSize() ... @Test public void testGet() ... @Test public void testAdd() ... ... }
  • 97. public class RecentlyUsedList_spec { public static class A_new_list { @Test public void is_empty()  } public static class An_empty_list { @Test public void retains_a_single_addition()  @Test public void retains_unique_additions_in_stack_order()  } public static class A_non_empty_list { @Test public void is_unchanged_when_head_item_is_readded()  @Test public void moves_non_head_item_to_head_when_it_is_readded()  } public static class Any_list { @Test public void rejects_addition_of_null_items()  @Test public void rejects_indexing_past_its_end()  @Test public void rejects_negative_indexing()  } }
  • 98. public class RecentlyUsedList_spec { public static class A_new_list { @Test public void is_empty()  } public static class An_empty_list { @Test public void retains_a_single_addition()  @Test public void retains_unique_additions_in_stack_order()  } public static class A_non_empty_list { @Test public void is_unchanged_when_head_item_is_readded()  @Test public void moves_non_head_item_to_head_when_it_is_readded()  } public static class Any_list { @Test public void rejects_addition_of_null_items()  @Test public void rejects_indexing_past_its_end()  @Test public void rejects_negative_indexing()  } }
  • 99. A test case should be just that: it should correspond to a single case.
  • 100. Style is the art of getting yourself out of the way, not putting yourself in it. David Hare