The document discusses object-oriented programming (OOP) and functional programming (FP). It introduces the presenter, Uberto Barbini, as a software artisan and agile enthusiast. Barbini questions if OOP was wrong and suggests OOP is only a tool that needs to be kept sharp by learning other tools, like FP. The presentation later discusses state as the foundation of behavior but also being hidden from the outside and needing protection. Functional programming is said to expel state from functions.
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Boost your-oop-with-fp
1. How to boost your
OOP with FP
Uberto Barbini
@ramtop
Saturday, November 19, 11
2. About me
Uberto Barbini
Software artisan
Agile enthusiast.
Hobby:
photography and the game of Go.
http://www.flickr.com/photos/uberto
Team leader and Architect for Vodafone
editorial and backend products.
Saturday, November 19, 11
3. What if OOP was wrong?
http://www.flickr.com/photos/mbshane
Saturday, November 19, 11
4. What if OOP was wrong?
OOP is only a tool,
we need to keep it sharp
we need to learn other tools
http://www.flickr.com/photos/mbshane
Saturday, November 19, 11
5. What if OOP was wrong?
OOP is only a tool,
we need to keep it sharp
we need to learn other tools
Caution, this presentation can
http://www.flickr.com/photos/mbshane
contain trace of Philosophy
Saturday, November 19, 11
6. I would like to
pay my tribute to
for their contribution to
Software Engineering
growing-object-oriented-software@googlegroups.com
Saturday, November 19, 11
7. Bugs
http://www.flickr.com/photos/staflo/
Saturday, November 19, 11
8. Bugs
• cause of delays and frustration
• many are easy to fix
• some cause big problems
• the worst ones are caused by...
http://www.flickr.com/photos/staflo/
Saturday, November 19, 11
9. State
• Foundation of
behavior
• Hidden from outside
• Should be defended
like a castle
Saturday, November 19, 11
10. Where is the
state?
Easy to
understand what
it does.
Hard to
understand why
it does it.
http://fractalforge.cvs.sourceforge.net/viewvc/fractalforge/fractalforge/Mandelbrot.pas?view=markup
Saturday, November 19, 11
11. The new procedural
public class NameAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = new String("success");
if (form != null) {
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm) form;
String name = nameForm.getName();
}
// if no mane supplied Set the target to failure
if (name == null) {
target = new String("failure");
} else {
request.setAttribute("NAME", name);
}
return (mapping.findForward(target));
}
}
Saturday, November 19, 11
12. The new procedural
public class NameAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = new String("success");
if (form != null) {
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm) form;
String name = nameForm.getName();
}
// if no mane supplied Set the target to failure
if (name == null) {
target = new String("failure");
} else {
request.setAttribute("NAME", name);
}
return (mapping.findForward(target));
}
}
Frameworks make easy to keep writing procedural
code in OO fashion
Saturday, November 19, 11
25. Alan Kay
• The best way to predict the future is to invent it.
• Possibly the only real object-oriented system in working order. (About Internet)
• The greatest single programming language ever designed. (About Lisp programming
language)
Saturday, November 19, 11
28. What is OOP?
Basically, an object type looks like a
record type with additional fields
including procedure fields and also
optional keywords which indicate the
scope of the fields.
Saturday, November 19, 11
29. What is OOP?
Basically, an object type looks like a
record type with additional fields
including procedure fields and also
optional keywords which indicate the
scope of the fields.
Lazarus (Object Pascal) wiki
Saturday, November 19, 11
30. What is OOP?
Object-oriented programming (OOP) is a programming
paradigm using "objects" – data structures consisting of
data fields and methods together with their interactions – to
design applications and computer programs. Programming
techniques may include features such as data abstraction,
encapsulation, messaging, modularity, polymorphism, and
inheritance.
Saturday, November 19, 11
32. Edsger W.
Dijkstra
It is practically
impossible to teach
good programming to
students that have had a
prior exposure to
BASIC: as potential
programmers they are
mentally mutilated
beyond hope of
regeneration. - 1975
Saturday, November 19, 11
34. Edsger W.
Dijkstra
Object-oriented
programming is an
exceptionally bad idea
which could only have
originated in California.
Saturday, November 19, 11
35. Edsger W.
Dijkstra
Object-oriented
programming is an
exceptionally bad idea
which could only have
originated in California.
I don't know how many of you have ever met Dijkstra,
but you probably know that arrogance in computer science
is measured in nano-Dijkstras. Alan Kay.
Saturday, November 19, 11
36. Alan Kay
OOP to me means only messaging, local retention and
protection and hiding of state-process, and extreme late-
binding of all things. It can be done in Smalltalk and in LISP.
There are possibly other systems in which this is possible, but
I'm not aware of them. (email 23/07/2003)
http://www.flickr.com/photos/marcospiller
Saturday, November 19, 11
37. Alan Kay
OOP to me means only messaging, local retention and
protection and hiding of state-process, and extreme late-
binding of all things. It can be done in Smalltalk and in LISP.
There are possibly other systems in which this is possible, but
I'm not aware of them. (email 23/07/2003)
I thought of objects being like biological cells and/or individual
computers on a network, only able to communicate with
messages (so messaging came at the very beginning -- it took
a while to see how to do messaging in a programming
language efficiently enough to be useful). http://www.flickr.com/photos/marcospiller
Saturday, November 19, 11
38. The venerable master Qc Na was walking with his student, Anton. Hoping to
prompt the master into a discussion, Anton said "Master, I have heard that
objects are a very good thing - is this true?" Qc Na looked pityingly at
his student and replied, "Foolish pupil - objects are merely a poor man's
closures."
Chastised, Anton took his leave from his master and returned to his cell,
intent on studying closures. He carefully read the entire "Lambda: The
Ultimate..." series of papers and its cousins, and implemented a small
Scheme interpreter with a closure-based object system. He learned much, and
looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by
saying "Master, I have diligently studied the matter, and now understand
that objects are truly a poor man's closures." Qc Na responded by hitting
Anton with his stick, saying "When will you learn? Closures are a poor man's
object." At that moment, Anton became enlightened.
Saturday, November 19, 11
39. The venerable master Qc Na was walking with his student, Anton. Hoping to
prompt the master into a discussion, Anton said "Master, I have heard that
objects are a very good thing - is this true?" Qc Na looked pityingly at
his student and replied, "Foolish pupil - objects are merely a poor man's
closures."
Chastised, Anton took his leave from his master and returned to his cell,
intent on studying closures. He carefully read the entire "Lambda: The
Ultimate..." series of papers and its cousins, and implemented a small
Scheme interpreter with a closure-based object system. He learned much, and
looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by
saying "Master, I have diligently studied the matter, and now understand
that objects are truly a poor man's closures." Qc Na responded by hitting
Anton with his stick, saying "When will you learn? Closures are a poor man's
object." At that moment, Anton became enlightened.
Anton van Straaten 4 June 2003
Saturday, November 19, 11
41. Yun Tung Lao - The Art of Objects - Addison Wesley
Saturday, November 19, 11
42. Bruce Eckel - Thinking in Java
The object-oriented approach goes a step further by providing tools
for the programmer to represent elements in the problem space.
This representation is general enough that the programmer is not
constrained to any particular type of problem. We refer to the
elements in the problem space and their representations in
the solution space as “objects.”
(You will also need other objects that don’t have problem-space
analogs.) The idea is that the program is allowed to adapt itself to
the lingo of the problem by adding new types of objects, so when you
read the code describing the solution, you’re reading words that also
express the problem. This is a more flexible and powerful language
abstraction than what we’ve had before.
Thus, OOP allows you to describe the problem in terms of
the problem, rather than in terms of the computer where the
solution will run.
Saturday, November 19, 11
43. An object has state, behavior,
and identity - Grady Booch
Saturday, November 19, 11
44. An object has state, behavior,
and identity - Grady Booch
OOP fundamentals:
Ignorance Apathy Selfishness
Kevlin Henney (Will The Real Oop Please Stand Up)
Saturday, November 19, 11
45. Half presentation check
Summary so far:
1. The worst bugs are related to state
2. Procedural paradigm doesn’t care much
3. Functional paradigm expel state from
functions
4. OOP (the real one) encapsule state in
Objects
Saturday, November 19, 11
46. Do I smell spaghetti?
Saturday, November 19, 11
49. Why singleton?
Strategy without DI
is pointless
Saturday, November 19, 11
50. Why singleton?
Strategy without DI
is pointless
This is worse than procedural!
Saturday, November 19, 11
51. the real
problem is
that many
Why singleton? people
believe this is
Strategy without DIOOP
good
is pointless because
there are
many
objects,
interfaces,
patterns.
This is worse than procedural!
Saturday, November 19, 11
52. DDD
Eric Evans
• Many objects are not fundamentally defined by their attributes but
rather by a thread of continuity and identity An object defined
primarily by its identity is called an ENTITY. p.91
• When you care only about the attributes of an element of the model,
classify it as a value object. Threat the VALUE OBJECT as
immutable. Don’t give it any identity and avoid the design
complexities necessary to maintain ENTITIES. p.97
• Place as much of the logic of the program as possible in functions,
operation that returns results with no observable side effects. p. 251
Saturday, November 19, 11
54. Mark Needham
I’m beginning to think that the combination of
functional and object oriented programming
actually results in code which I think is more
expressive and easy to work with than code
written only with an object oriented approach
in mind.
The functional mindset seems to be more about
considering the problem as a whole and then
working out how we can simplify that problem
from the outside in which is a bit of a paradigm
shift. I don’t think I’ve completely made but it can
certainly lead to solutions which are much easier
to understand. - blog April 2009
Saturday, November 19, 11
56. The Functional eye
http://www.flickr.com/photos/dirtyf
Saturday, November 19, 11
57. The Functional eye
• Immutable structured values (messages)
• Never unready or null objects (a keyword)
• Use of pure functions (no side effect)
• Closures to decouple (or inner classes)
http://www.flickr.com/photos/dirtyf
Saturday, November 19, 11
60. The ObjectOriented eye
• Dependency Injection
• No getters for mutable state
• Interfaces for collaborators (Liskov)
• Simpler aggregates (Demeter)
• Meaningful name convention
Saturday, November 19, 11
61. Just an example:
protected void processRequest(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws IOException {
log.info("Processing request " + httpServletRequest.getRequestURI());
HttpCallClock clock = new HttpCallClock();
MyRequest request = createRequest(httpServletRequest);
MyResponse response = prepareResponse(getContentService(), request);
translateResponse(response, httpServletResponse);
long elapsed = clock.getElapsedTime();
log.info("Processed in " + elapsed + " ms. " +
httpServletRequest.getRequestURI());
accessLog.info(AccessLog.getRequestLog(httpServletRequest, elapsed,
response.getStatus(), response.getResponseContentLength()));
}
Saturday, November 19, 11
62. Another example:
protected MyResponse prepareResponse(MyRequestConfiguration myConfiguration) {
String opco = myConfiguration.getOpco();
Device device = VIRTUAL_DEVICE;
if (myConfiguration.isTacPresent()) {
DeviceResponse devicesResponse = getDeviceResponse(opco,
myConfiguration.getPublishType());
if (devicesResponse.isErrorResponse()) {
return new ResponseWithError(devicesResponse.getHttpStatusCode(),
devicesResponse.getMessage());
}
if (!devicesResponse.isDevicePresentByTac(myConfiguration.getTac())) {
return new ResponseWithError(SC_NOT_FOUND, "device with tac code: " +
myConfiguration.getTac() + " not found");
}
device = devicesResponse.getDeviceByTac();
}
DatastoreResponse responseFromDatastore = getDashboards(device,
myConfiguration.getPublishType(), opco);
if (responseFromDatastore.isErrorResponse())
{
return new ResponseWithError(responseFromDatastore.getHttpStatusCode(),
responseFromDatastore.getMessage());
}
return retrieveResponseAccordingUrl(myConfiguration,
responseFromDatastore.getDashboards());
}
Saturday, November 19, 11
63. Object Design Rebecca Wirfs-Brock
Control
• Centralized
• Dispersed
• Delegated
Saturday, November 19, 11
65. Centralized
Invoice
Customer
Invoice
Martin Fowler: (http://martinfowler.com/bliki/
AnemicDomainModel.html)
The basic symptom of an Anemic Domain Model is that Printer
at first blush it looks like the real thing. [...] The catch
Manager
comes when you look at the behavior, and you realize
that there is hardly any behavior on these objects,
making them little more than bags of getters and setters.
[...] The key point here is that the Service Layer is thin -
all the key logic lies in the domain layer.
Saturday, November 19, 11
66. Dispersed
Customer Items
Invoice
Printer
Popular with Zombie objects
(aka Hibernated beans)
Saturday, November 19, 11
67. Logic
Delegated
Entity
Identity
CustomerFetcher
Value
InvoiceFetcher Immutable
Customer No Id
Invoice
Printer
InvoicePrinter
Saturday, November 19, 11
68. The valley
of comfort
http://www.flickr.com/photos/lviggiano
Saturday, November 19, 11
69. The valley
Elegance and familiarity are orthogonal
of comfort
Rich Hickey on #Clojure
http://www.flickr.com/photos/lviggiano
Saturday, November 19, 11
70. The valley
Elegance and familiarity are orthogonal
of comfort
Rich Hickey on #Clojure
A Conversation with Ward Cunningham, Part III (5 January 2004)
The Accidental Architecture
I like the notion of working the program, like an artist works a lump of
clay. An artist wants to make a sculpture, but before she makes the
sculpture, she just massages the clay.
[...] In a sense we get the architecture without really trying. All the
decisions in the context of the other decisions simply gel into an
architecture.
http://www.flickr.com/photos/lviggiano
Saturday, November 19, 11
71. The valley
Elegance and familiarity are orthogonal
of comfort
Rich Hickey on #Clojure
There are two ways of constructing a
software design.
One way is to make it so simple that there are
obviously no deficiencies.
And the other way is to make it so
A Conversation with Ward Cunningham, Part III (5 January 2004)
complicated that there are no obvious
deficiencies.
The Accidental Architecture
[C.A.R. Hoare]
I like the notion of working the program, like an artist works a lump of
clay. An artist wants to make a sculpture, but before she makes the
sculpture, she just massages the clay.
[...] In a sense we get the architecture without really trying. All the
decisions in the context of the other decisions simply gel into an
architecture.
http://www.flickr.com/photos/lviggiano
Saturday, November 19, 11
72. The journey
to the valley
• What about domain
• What about tests
• What about mocks
• What about
refactoring
http://www.flickr.com/photos/bobcatnorth
Saturday, November 19, 11
73. Open questions
• Do we need a yet another new language?
• Can we have Architects?
• How to teach design?
Saturday, November 19, 11