Dealing with cross-cutting concerns - oop

In programming, cross-cutting concerns are a common concern (For lack of better terms).
For example, if you are modelling an aircraft and all its composite parts in classes, various devices will depend on the same type of part. E.g. fuel tank, water tank, etc.
I have the same issue to deal with in my design. Apart from AOP, what strategies are there to deal with this?
Thanks

Well, AOP is conceived to deal, by definition, with cross-cutting concerns. The answer to your question is, therefore, AOP. You don't need anything else apart from AOP. Why would you?

Related

Adapting design to be SOLID

I am working on a very large ASP.NET application. The problem is that there is not a lot of logic behind the design. The original developer chose about ten classes and that was it. There is high coupling and low cohesion. For example, clsPerson holds all the functionality for Person and breakes most of the rules of SOLID.
I have started to incorporate design patterns into my toolkit. My question is: what is the best way to incorporate badly designed classes into the better design. For example, if you had a class clsStudent that contained all the Student functionality to date and then created a class called clsUndergraduate then would you simply derive clsStudent? I realise that a lot of this depends on context but I am looking for general guidelines.
There is a lot of information online that talks about SOLID, but not a lot that talks about how to adapt an existing application to be SOLID.
It could be argued that you should only aim to implement SOLID principles as, and when the need arrives. However, I'm an advocate and believe that it's not difficult to add elements of SOLIDity to your design without too much overhead or heartache.
Trying to move an existing model to a more SOLID model can be difficult. I would suggest taking small, manageable parts and gradually refactoring. If you have the safety net of automated tests, this should be achievable with confidence. If not, make sure you fully understand the scope of the changes you are making. It's easy to introduce subtle bugs. Ultimately, serious changes will be likely to introduce new tests anyway.
Complying with the SRP is likely to be the easiest place to start. Try to define the main responsibility of each class. If they currently have more than one responsibility, note them and look at how these responsibilities can be moved out and elsewhere. For example, many 'God' classes will be managing persistence, validation, initialisation, etc. along with business logic. See if you can begin to take the persistence code out and put it into mappers/repositories/etc.
If you do this logically and sequentially, my experience is that, as you go, making lots of mistakes along the way, the relevance and importance of the other principles will emerge and make themselves, sort of, obvious.
I found that as I experimented with SOLID principles, by reading and re-reading (mainly Bob Martin and ObjectMentor) more clarity emerges as you have practical experience of implementation. Don't forget the opening principles defined in the Gang of Four, also. Concepts like 'favour composition over inheritance' go hand-in-hand with SOLID OO principles.
Bear in mind that SOLID code is generally more complex than non-SOLID code and can, therefore, be harder to maintain and debug by those not familiar with it. Skeptics might lay the accusation of 'over-engineering' at your door, which can be difficult to argue against with those who haven't wrestled with enhancing/fixing tightly coupled, incohesive code.
Good luck. Please feel free to ask more as I'd love to hear the input of others on this subject. It's scope is wide and varied.

Can good Object Orientated Design be formalised as good relational database design has been?

In the database world, we have normalisation. You can start with a design, crank the steps and end up with a normal form of the database. This is done on the basis of the semantics of the data and can be thought of as a series of design refactorings.
In object orientated design, we have the SOLID principals and various other adhoc guidelines towards good design.
Do you think it is possible to define the equivalent of normal forms for OO, such that a series of refactoring steps could move a procedural piece of code (or poorly factored OO design) into a correct (in some well-defined sense) formulation with the same functionality?
(NB. Happy to make this community wiki)
It is a possibility, but highly unlikely.
Context
First, in the days when the Relational Model came out, people who worked in IT were more educated and they esteemed standards. Computer resources were expensive, and people were always looking for the best way to use those resources. People like Codd and Date were giants in an industry where people were high tech.
Codd did not invent Normalisation, we were Normalising our non-relational databases long before Relational came along. Normalisation is a theory and practice, published as the Principle of Full Normalisation. We were Normalising our programs, we considered accidental duplication of a subrotine (method) a serious error. Nowadays it is known as Never Duplicate Anything or Don't Repeat Yourself, but the recent versions do not acknowledge the sound academic theory behind, and are therefore its power is unreallised.
What Codd did (among many things) was define formal Normal Forms specifically for Relational Databases. And these have progressed and been refined since then. But they have also been hijacked by non-academics for the purpose of selling their gear.
The database modelling that was invented by Codd and Chen, and finished by Brown had a solid grounding. In the last 25 years, its has achieved Standardisation and been further refined and progressed by many others who had solid grounding.
The World Before OO
Let's take the programming world before OO. We had many standards and conventions, for modelling our programs, as well as for language- and platform-specific implementation. Your question simply would not apply in those days. The entire industry understood deeply that database design and program design were two different sciences, and used different modelling methodologies for them, plus whatever standards applied. People did not discuss if they implemented standards, they discussed the extent to which they complied with standards; they did not discuss if they modelled their data and programs, they discussed the extent to which they modelled their data and programs. That is how we put men on the Moon, notably in 1969.
Dawn of OO
OO came along and presented itself as if no other programming language or design methodology existed before it. Instead of using existing methodologies and extending or changing them, it denied their existence. So, not surprisingly, it has taken 20 years to formulate the new methodologies from scratch and slowly progress them to the point of SOLID and Agile, which is not mature; the reason for your question. It is telling that more than twenty such methodologies have flashed up and died during that time.
Even UML, which could have been an outright winner, applicable to any programming language suffered the same disease. It tried to be everything to everyone while denying that mature methodologies existed.
Demise of the Industry
With the advent of MS, the attitude of "anyone can do anything" (implication: you do not need formal education or qualifications), that quality and pride of profession has been lost. People now invent things from scratch as if no one on the planet has ever done it before. The IT industry today is very low tech. You kow, but most people reading these pages do not know, that there is one Relational Modelling methodology, and one Standard. They do not model, the implement. Then re-implement. And re-implement. Re-factoring as you say.
OO Proponents
The problem was that the people who came up with these OO methods were not giants among professionals; they were simply the most vocal of an un-academic lot. Famous due to publishing books, not due to peer acknowledgement. Unskilled and unaware. They had One Hammer in their toolkit, and every problem looked like a nail. Since they were not formally educated they did not know that actually database design and program design are two different sciences; that database design was quite mature, had strongly established methodologies and standards, and they simply applied their shiny new hammer to every problem, including databases.
Therefore, since they were ignoring both programming methodologies and database methodologies, inventing the wheel from scratch, those new methodologies have progressed very slowly. And with assistance from a similar crowd, without sound academic basis.
Programs today have hundreds of methods that are not used. We now have programs to detect that. Whereas with the mature methodologies, we prevent that. Thin client was not a goal to be achieved, we had a science that produced it. We now have programs to detect "dirty" data and to "clean" it. Whereas in the upper end of the database market, we simply do not allow "dirty" data into the database in the first place.
I accept that you see database design as a series of re-factorings, I understand what you mean. To me it is a science (methodology, standards) that eliminates ever having to re-factor. Even the acceptance of re-factoring is loud signal that the older programming methodologies are unknown; that the current OO methodologies are immature. The danger, what makes it annoying to work with OO people, is that the methodology itself fosters a confidence in the One Hammer mentality, and when the code breaks, they have not one leg to stand on; when the system breaks, the whole system breaks, it is not one small piece that can be repaired or replaced.
Take Scott Ambler and Agile. Ambler spend 20 years publicly and vociferously arguing with the giants of the database industry, against Normalisation. Now he has Agile, which although immature, has promise. But the secret behind it it Normalisation. He has switched tracks. And because of his past wars, he cannot come out and declare that honestly, and give others due credit, so it remains a secret, and you are left to figure out Agile without its fundaments being declared.
Prognosis
That is why I say, given the evidenced small progress in the OO world over the last 20 years; the 20 or so OO methodologies that have failed; the shallowness of the approach, it is highly unlikely that the current OO methodologies will achieve the maturity and acceptance of the (singular) database design methodology. It will take at least another 10 years, more likely 20, and it will be over taken by some replacement for OO.
For it to be a possibility two things need to happen:
The OO proponents need formal tertiary education. A good grounding in the science of programming. Sure, anyone can do anything, but to do great things, we need a great grounding. That will lead to the understanding that re-factoring is not necessary, that it can be eliminated by science.
They need to break their denial of other programming methodologies and standards. That will open the door to either building OO on top of that, or taking the fundaments of that and merging it into OO. That will lead to a solid and complete OO methodology.
Real World OO
Obviously I speak from experience. On our large projects we use the mature analysis and design methodologies, one for database and another for function. When we get to the code-cutting stage, we let the OO team use whatever they like, for their objects only, which usually means UML. No problems with architecture or structure or performance or bloatware or One Hammer or hundreds of unused objects, because all that was taken care of outside OO. And later, during UAT, no problems with finding the source of bugs or making the required changes quickly, because the entire structure, has documented structure; the blocks can be changed.
I think this is an interesting question, because it presumes that Codd's Normal Forms are actually the definition of "correct" design. Not trying to start a flame war with that statement, but I guess my point is that there are very good reasons that many DB's aren't fully normalized (e.g. join performance) leads me to think that the real-world equivalent of normalization in OO space is probably design patterns or (as you said) SOLID. In both cases you're talking about idealized guidelines that have to be applied with a suitably critical eye, rather than slavishly followed as dogma.
Not only do I fully agree with Paul, but I will go a step further.
Models are just that - only models. The Normalization models used by Relational Databases are only one approach to storing and managing data. In fact, note that while RDBMS's are common for Data Manipulation operations (the standard CRUD), we have now evolved the DataWarehouse for consolidation, analysis, and reporting. And it most definitely does NOT adhere to the normalization models found in DML land.
Now we also have Google with their BigTable architecture, and Apache with Hadoop. These newer modeling systems reflect a change in the landscape, driven by the idea of the DISTRIBUTED database. Normalization need not apply for this club either.
We can apply a successful model ony to the point at which it becomes not-so-successful, or is supplanted by an model which better suits the needs of the designer. Note the many ways we humans have modelled our universe through physics/Astronomy what have you. Modelling attmpts to describe a system in discreet terms, but as the system, or the needs of the system change, so must the model.
OOP is and has been a very, very successfulk way to model computer applications. However, the needs of the application designer are different from thos eof Database designers. MOST of the time, there is a point at which the designer of an application must consider that his program will be interacted with by humans. Unlike the database designer, whose work will (mostly) be expected to interact with other code, the programmer's job is to take the machine and make it accessible to a much more random human-being. This art does not map quite so well to such standards like normalization.
All that said, n-tier, MVC, MVVC, and other paradims DO establish some guidelines. But in the end, the problem-space of application design is usually not as easy to fit into such discrete modelling steps as a relational databse.
Wow. Apologies for the length. If this is a breach of ettiquette here, do let me know . . .

Do Design patterns in Object Oriented Programming signal systemic problems of the paradigm of OO? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Are design patterns really language weaknesses?
After spending years pouring over books on OOP and the techniques of OOP, and recently getting involved more and more in Functional Styles of programming, would it be fair to extrapolate that design patterns are pointers to systemic problems with Object Oriented programming as a whole. Is there a fundamental flaw in Object Oriented Programming (not to be confused with Design), in that in the treatment of state through encapsulation, has led to more and more patterns to resolve the problems with such a paradigm.
I have not come to any conclusions on this, but my "gut" feeling is that there might be something more seriously wrong with the paradigm of OOP.
Is the very idea of encapsulation causing more problems than they solve.
A very good question and something that I have thought about some time ago. This is my conclusion \ opinion:
The idea of object oriented programming is not without flaws, but does provide the most complete design paradigm. If the problem domain is expressed properly, clearly defined object, who knows their responsibilities, can interact in a fairly elegant way, that closely resembles the real world interaction of the objects. (or ideas).
To make some of the more abstract concepts, specific, OOP makes some assertive statements. (Like encapsulation, not expose more than you have to and object responsibility).
Like all generic assumptions, there would be exceptions, when what normally would be a good idea, may not fit a particular problem in hand. It is also not helped by the fact that OOP, covers almost all problem conceived ( unlike AOP or even the more complex semantic modeling, that caters to a specific kind of problem).
So in situations, when you need to make exceptions and move away from OOP assertions, the designers needed a way to keep in bounds of good design, so that they do not stray far too much from accepted design practices.
So design patterns, for me is just case studies of problems, that will not be served by some of the core assertion of OOP. Apart from collaboration and collation of solution, design pattern also helps augment OOP. (especially for newbie designers).
Note: Most of the time, design patterns are not needed. There needs to be, clear justification for using patterns. I know, some greenhorns, trying to implement some design pattern, just because they know them ( and sometime not so greenhorns ;)). Its square peg, round hole problem
Good question, I started wondering about this my self a few weeks ago whilst getting more into Python and Scala.
I think yes and no. There are definitely some intrinsic problems with OOP and the encapsulation of state, but it's not to say that OOP itself is inherently a bad way of doing things. I think the problem is that when all you have is a hammer everything becomes a nail. OOP is great for some things, GUIs come to mind first but functional programming has very clear benefits as well.
It's worth noting that the newer functional programming languages like Scala haven't thrown objects away.
I haven't thought about the issue in great detail but I certainly agree that OOP has some issues that I haven't seen addressed, other than in the form of design patters, which really are addressing the symptoms rather than the disease.
No. Although you see slightly different design patterns, you certainly still see design patterns in functional code as well. The basic difference has little (if anything) to do with lack of state. Rather, it stems primarily from (most) functional languages providing enough more versatility in creating functions that what would be a "design pattern" in another language simply becomes a function in a functional language.
If you provide a (roughly) similar level of versatility in a language that has state, you can get the same effect. Just for example, most of the introduction to Modern C++ Design is defending the position that a design pattern can be encoded as a template (and most of the book is design patterns implemented as templates).
I think there will invariably be problems when you try to apply a single programming paradigm to a problem. That's why I like C++: it's multi-paradigm; it doesn't force you into a single set of solutions.
I am repeating a basic theory of mine, but models are just that - only models. The model defined by OOP is a very effective way to structure a program, and for many application programming domains, is entirely appropriate. For some problem spaces, the model may become decreasingly effective (or less efficient, or both).
A potential metaphore exists with physics. For many, many years Newtonian physics did (and in fact, still does) a remarkable job of modelling the laws of motion, time, and space (with some help from euclidian and sperical geometry). But when science began probing into the micro-and macro aspects of the problem space, Newtonian physics (AND euclidian/Spherical geometry) begin to break down. Hence we now have Relativity and quantumn mechanics. These do a fantastic job of modelling the universe at the macro and micro levels respectively, but are overly convoluted for use as descriptors of every-day, human-scale events.
OOP is very effective for application programming in a lot of cases, when considered in the context of the complexity involved with modelling real-world problems and human interactions for consumption and processing by a linear machine. As someone above observed, there are no silver bullets. And my impression (having never used C++) is that languages which attempt to be multi-paradigm also become more complex, and not necessarily as efficient for smaller problems more easily handled with a higher-level, more targeted language. Much like Quantumn mechanics and/or relativity theories (I mean, really, is anyone interested in the relationship between mass and velocity when travelling at 60 MPH on the freeway? OR the probability of Los angeles being where you expect it to be when you arrive?).
In my impression, adherence to qa specific model is important, so long as the model is suited to the problem space. At the point when this stops being true, the model may need to evolve, and there will be resistence to this. There will be attempts to force the problem space into a model not suited (review the history of physics again, or check into the evolution of the helio-centric model of the solar system, and include the key word "epicycles").
All of the above is simply MY best understanding of the state of things, and if I have missed the mark somewhere, I am happy to hear some contrary news.

What are your real world uses for Aspect Oriented Programming?

I'm stumped as to why adoption of AO has been so slow. There are plenty of rich implementations out there for the predominant languages. My guess is that, like OO in it's day, it is enough of a paradigm shift that people don't recognize the places where it could be helping them.
So, beyond non-invasive logging, what are some of the ways that you have used, or plan to use AO, that reduces complexity, improves maintenance, enhances system "ilities"?
I am currently using AOP via EntLib / Unity in production for:
logging
caching
security
exception reporting
performance counters
Take a look at http://www.agileatwork.com/unit-of-work-with-unity-and-aspnet-mvc/ for an implementation of the unit of work pattern with AOP
[UnitOfWork]
public void Process(Job job)
{
...
}
Transaction management. I know it's a canonical use of AOP, but it really does shine when used for that.
And while I haven't had an opportunity to use it in a real-world situation, I see "around-advice" as being INCREDIBLY powerful, in particular for the value it adds to simplify the complexity of code by removing the need for many checks for rare conditions.
I agree for Spring AOP.
AOSD (we no longer speak about AOP I don't exactly why) is really useful for middleware/service oriented architecture where you already have, by design, some modularity.
I've used it in this context for telephony services with some really limited billing service.
I've also used it to build a kind of modular interpreter/compiler in order to perform some analysis around some code.
To my mind, one problem are the pointcut languages that could be sometimes tricky to describe exactly where you want to apply your advice. Another problem is composition, I don't know if it's been solved but it could be difficult to understand when you order your advices....
AOP is common, except people rarely call it AOP. Look at all the places in .NET programming where attributes are used. Attributes are essentially cross-cutting behaviors that can apply across many classes/methods/parameters.
More recently, the ASP.NET MVC platform has adopted heavy use of attributes, for a wide range of cross-cutting components such as security, data binding, and exception handling.
In my experience, with Spring AOP seems to be pretty common.
I think the difficulty is that people are just not used to thinking in terms of aspects, and weaving code in, even at compile time, can be somewhat scary, as it is harder to see what is actually affecting each method, for example, esp if you use a mixture of weaving at compile-time and run-time.
I have used it in situations where I have one controller, and I add in whether it is a servlet or webservice, for example. I have also used it to abstract out the database, so the database connections and database-optimized queries, could be woven into the application.
The e hardware verification language uses AOP.

Database Guy Asks: Object-Oriented Design Theory?

I've worked with designing databases for a loooong time, and these days I'm working in C# too. OO makes sense to me, but I don't feel that I have a good grounding in the deep theory of OO design.
In database land, there's a lot of theory around how to design the structure of a database, the main notion being normalisation. Normalisation directly steers the structure of a database and to some extent dictates how to arrange entities in a database.
Are there any similar concepts behind how to design the structure of an Object-Oriented program?
What I'm reaching for is one or more underlying theoretical principles which naturally guide the developer into the "correct" design for the solution to a given problem.
Where can I look to find out more?
Is there a go-to work I should read?
Update:
Thanks to everyone for their answers.
What I'm reading seems to say that there is no "Grand Theory of OO Design", but there are a bunch of important principles - which are largely exemplified by design patterns.
Thanks again for your answers :)
Be careful some of the design patterns literature.
There are are several broad species of class definitions. Classes for persistent objects (which are like rows in relational tables) and collections (which are like the tables themselves) are one thing.
Some of the "Gang of Four" design patterns are more applicable to active, application objects, and less applicable to persistent objects. While you wrestle through something like Abstract Factory, you'll be missing some key points of OO design as it applies to persistent objects.
The Object Mentor What is Object-Oriented Design? page has mich of you really need to know to transition from relational design to OO design.
Normalization, BTW, isn't a blanket design principle that always applies to relational databases. Normalization applies when you have update transactions, to prevent update anomalies. It's a hack because relational databases are passive things; you either have to add processing (like methods in a class) or you have to pass a bunch of rules (normalization). In the data warehouse world, where updates are rare (or non-existent) that standard normalization rules aren't as relevant.
Consequently, there's no "normalize like this" for object data models.
In OO Design, perhaps the most important rule for designing persistent objects is the Single Responsibility Principle.
If you design your classes to have good fidelity to real-world objects, and you allocate responsibilities to those classes in a very focused way, you'll be happy with your object model. You'll be able to map it to a relational database with relatively few complexities.
Turns out, that when you look at things from a responsibility point of view, you find that 2NF and 3NF rules fit with sound responsibility assignment. Unique keys still matter. And derived data becomes the responsibility of a method function, not a persistent attribute.
The book "Design Patterns" is your next step.
http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612
But, you don't have to use an OO approach to everything. Don't be religious about it. If a more procedural approach feels more straitforward, then go with that. People new to OO tend to overdue it for a while.
I think Agile Software Development, Principles, Patterns, and Practices is quite good.
It provides a lot of in-depth disccusion of OO principles listed here:
The principles of Object Oriented Design and Dependency Management
SRP — The Single Responsibility Principle
OCP — The Open Closed Principle
LSP — The Liskov Substitution Principle
DIP — The Dependency Inversion Principle
ISP — The Interface Segregation Principle
REP — The Reuse Release Equivalency Principle
CCP — The Common Closure Principle
CRP — The Common Reuse Principle
ADP — The Acyclic Dependencies Principle
SDP — The Stable Dependencies Principle
SAP — The Stable Abstractions Principle
If you're used to building normalized databases, then Object Oriented design should come naturally to you. Your class structures will end up looking a lot like your data structure, with the obvious exception that association tables turn into lists and lookup tables turn into enums within your classes.
All together, I'd say you're a lot better off coming into OO design with a background in Relational Databases than you would be going the other direction.
If you want to really get to grips with O-O, go play with Smalltalk. ST is a pure OO language, and quite in-your-face about it. Once you get over the paradigm hump you've learned OO as you can't really do Smalltalk without it. This is how I first learned OO.
Check the results of this. Learn from each question.
I really liked Head First Design Patterns, which is very approachable, and the excellent Object oriented Design Heuristics by Arthur J. Riel
This site lists 101 title... design patterns, refactoring and other... Have a look at it.. It will be a good starting point...
Go for Object Thinking by David West. An interesting read..
You're from the dark side though.. as per the book;) Database thinking has been the curse of OO programmers all over. They're opposite ends of a spectrum. For instance
Database thinking values the data attribues over everything else.. normalization and creating types based on how they fit into the DB Schema OR the ER diagram.. OO thinking creates types based on behavior and collaboration and does not recognize the data attributes as all important.
Databases come from the scientific people who value formalization and method over everything else. OO comes from the people who use heuristics and rules of thumb and value individuality and social interaction over a hard and fast process.
The point being a COBOL programmer can write COBOL programs even after moving onto a OO Language. Check out any book like Thinking in Java for the first section which invariably details out the tenets of OO (Apprentice).. Follow it up with Object Thinking (journeyman) and in due time.. a master.
Model your objects by keeping real world objects in mind.
We are currently developing automation software for machines. One of those machines has two load ports for feeding it raw material, while all others have only one. In all modules so far, we had the information of the ports (current settings, lot number currently assigned to it etc) as members in the class representing the machine.
We decided to create a new class that holds the information of the ports, and add two LoadPort members to this MachineXY class. If we had thought about it before, we would have done the same for all those single port machines...
You should look at UML, which is an entire process given to OOD.
I'd recommend getting a book (or a couple), because the theory is quite large, most people pick and choose the techniques most appropriate for the project at hand.
Start reading about design patters, from say Martin Fowler. :)
They are the most practical use of OOP.
I am guess you mean OO in the database world.
Object-oriented databases which store objects never did really catch one so you are currently looking mapping objects to relational database. ORM or Object-relational mapping is the term used to describe the software that does this mapping. Ideally this gives you the best of both worlds where developers can internact with the objects and in the database everything is stored in relational tables where standard tuning can take place.
in DBA slang: object-oriented design is nothing else but properly normalized data behind safe operation interfaces, safe meaning, look at the operations, not the data directly