background preloader

Don't repeat yourself

Don't repeat yourself
Applying DRY[edit] DRY vs WET solutions[edit] Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice" or "we enjoy typing".[2][3] See also[edit] References[edit] External links[edit]

12 common programming mistakes to avoid | Geek Files Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many… and I would like to share with you the mistakes that I have made over my journey with development. These are some of the most common programming mistakes made by developers (including me) and how to avoid them (not listed in any specific order) Can you imagine a program without comments. {*style:<b>2. </b>*}Once you get that big ‘AAha’ feeling looking at your code after you have completed it, it’s best to review your code immediately and try finding issues if any. What do we all do when we have no one from the business side to help us with Business Logic… we assume. {*style:<b>4. </b>*}A silly question to ask, but here it is. Completing your code and getting ready for integration is the good part, but not unit testing your code bounces back on you when you start getting a series of integration errors. {*style:<b>6. {*style:<b>8. {*style:<b>10. {*style:<b>12.

Lazy Loading with External PHP Iterators The fun side of outer iterators (at least when using an OOP approach) is that their implementation is (in most cases) a straightforward process that can be mastered in a snap. Of course, claming this without concrete proof would be rather pointless. In line with this idea, in previous installments of this series I went through the development of a couple of concrete examples that demonstrated how to use an external iterator for traversing the protected fields of a class whose primary use was to model generic entities. In the first tutorial, the whole traversing task was performed via a custom array collection, while the second how-to achieved the same process through the native ArrayIterator class bundled with the SPL. It goes without saying that external iterators can be used for many different purposes, aside from those described above. Traversing Array Elements: Building a Custom Array Collection Pretty simple right?

How to think about OO by Miško Hevery Everyone seems to think that they are writing OO after all they are using OO languages such as Java, Python or Ruby. But if you exam the code it is often procedural in nature. Static Methods Static methods are procedural in nature and they have no place in OO world. This means that for a static method to do something interesting it needs to have arguments. But most justifications for static methods argue that they are "utility methods". In the end I am sometimes (handful of times per year) forced to write static methods due to limitation of the language. Instance Methods So you got rid of all of your static methods but your codes still is procedural. The problem here is the method may as well be static! The funny thing about the getter methods is that it usually means that the code where the data is processed is outside of the class which has the data. Now this begs the question: should the User know about the Ldap? Should User have a field reference to Ldap?

Lazy Loading An object that doesn't contain all of the data you need but knows how to get it. For a full description see P of EAA page 200 For loading data from a database into memory it's handy to design things so that as you load an object of interest you also load the objects that are related to it. However, if you take this to its logical conclusion, you reach the point where loading one object can have the effect of loading a huge number of related objects - something that hurts performance when only a few of the objects are actually needed. A Lazy Load interrupts this loading process for the moment, leaving a marker in the object structure so that if the data is needed it can be loaded only when it is used. There are four main varieties of lazy load. These approaches vary somewhat subtly and have various trade-offs.

Flaw: Brittle Global State & Singletons Accessing global state statically doesn’t clarify those shared dependencies to readers of the constructors and methods that use the Global State. Global State and Singletons make APIs lie about their true dependencies. To really understand the dependencies, developers must read every line of code. It causes Spooky Action at a Distance: when running test suites, global state mutated in one test can cause a subsequent or parallel test to fail unexpectedly. Break the static dependency using manual or Guice dependency injection. NOTE: When we say “Singleton” or “JVM Singleton” in this document, we mean the classic Gang of Four singleton. Why this is a Flaw Global State Dirties your Design The root problem with global state is that it is globally accessible. In other words, if I instantiate two objects A and B, and I never pass a reference from A to B, then neither A nor B can get hold of the other or modify the other’s state. Global State enables Spooky Action at a Distance int timeout = 30;

CRUD: creating, reading, updating, deleting CRUD is een afkorting uit de informatica die staat voor de vier basisoperaties die op duurzame gegevens (meestal een database) uitgevoerd kunnen worden. Deze zijn: Create (of insert): Toevoegen van nieuwe gegevens.Read (of select): Opvragen van gegevens.Update: Wijzigen van gegevens.Delete: Verwijderen van gegevens. Vrijwel elk softwareprogramma maakt gebruik van de CRUD-functionaliteit. CRUD-matrix[bewerken] Een CRUD-matrix legt de relaties vast tussen data-objecten (tabellen in een relationele database) en systeemfuncties. CRUD-matrices (of tabellen) kunnen gebruikt worden als een hulpmiddel voor de compleetheidscontrole van een logisch informatie- of datamodel. Gegevenscyclustest[bewerken] Een CRUD-matrix is ook nodig als er een gegevenscyclustest moet worden uitgevoerd. Geschiedenis[bewerken]

Static Methods are Death to Testability by Miško Hevery Recently many of you, after reading Guide to Testability, wrote to telling me there is nothing wrong with static methods. After all what can be easier to test than Math.abs()! And Math.abs() is static method! If abs() was on instance method, one would have to instantiate the object first, and that may prove to be a problem. (See how to think about the new operator, and class does real work) The basic issue with static methods is they are procedural code. Here is another way of thinking about it. Lets do a mental exercise. We have already covered that global state is bad and how it makes your application hard to understand. Sometimes a static methods is a factory for other objects. “So leaf methods are ok to be static but other methods should not be?”

RESTful ? How to think about OO Everyone seems to think that they are writing OO after all they are using OO languages such as Java, Python or Ruby. But if you exam the code it is often procedural in nature. Static Methods Static methods are procedural in nature and they have no place in OO world. This means that for a static method to do something interesting it needs to have arguments. But most justifications for static methods argue that they are “utility methods”. In the end I am sometimes (handful of times per year) forced to write static methods due to limitation of the language. Instance Methods So you got rid of all of your static methods but your codes still is procedural. The problem here is the method may as well be static! The funny thing about the getter methods is that it usually means that the code where the data is processed is outside of the class which has the data. Now I don’t know if this code is well written or not, but I do know that the login() method has a very high affinity to user.

Data transfer object Data transfer object (DTO)[1][2] is an object that carries data between processes. The motivation for its use has to do with the fact that communication between processes is usually done resorting to remote interfaces (e.g. web services), where each call is an expensive operation.[2] Because the majority of the cost of each call is related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by one call only.[2] See also[edit] This pattern is often incorrectly used outside of remote interfaces. See also[edit] Value object Note: A Value object is not a DTO. References[edit] External links[edit]

Breaking the Law of Demeter is Like Looking for a Needle in the Haystack July 18th, 2008 · 20 Comments · by Miško Hevery Every time I see Law of Demeter violation I imagine a haystack where the code is desperately trying to locate the needle. class Mechanic { Engine engine; Mechanic(Context context) { this.engine = context.getEngine(); } } The Mechanic does not care for the Context. Most applications tend to have some sort of Context object which is the kitchen sink and which can get you just about any other object in the system aka the service locator.If you want to reuse this code at a different project, the compiler will not only need Mechanic and Engine but also the Context. But here is the real killer! Every time I have to write a test I have to create a graph of objects (the haystack) which no one really needs or cares for, and into this haystack I have to carefully place the needles (the real object of interests) so that the code under test can go and look for them. class Mechanic { Engine engine; Mechanic(Engine engine) { this.engine = engine; } }

ValueObject domain driven design · API design tags: In P of EAA I described Value Object as a small object such as a Money or date range object. Their key property is that they follow value semantics rather than reference semantics. You can usually tell them because their notion of equality isn't based on identity, instead two value objects are equal if all their fields are equal. A general heuristic is that value objects should be entirely immutable. Early J2EE literature used the term value object to describe a different notion, what I call a Data Transfer Object. You can find some more good material on value objects on the wiki and by Dirk Riehle.

Procedural Language Eliminated GOTOs; OO Eliminated IFs August 14th, 2008 · 7 Comments · by Miško Hevery Procedural languages allowed us to remove GOTOs in our code. I would like to think that OO languages allow us to remove IFs (conditionals). I know you can’t remove of all the IFs, but it is interesting just how many IFs you can remove in an application. You can’t remove these IFs: Comparing relative sizes (>, < operators)Comparing primitives Recently I came across a very interesting and clever way to get rid of IFs when doing lazy initialization. For extra credit you can do thread safe initializer as well class abstract ThreadSafeLazyInitializer implements Getter { private Getter getter = new Getter() { public synchronize T get() { return synchronizedGetter.get(); } }; private Getter synchornizedGetter = new Getter() { public T get() { final T value = initialize(); gettter = new Getter(){ T get() { return value; } } synchronizedGetter = gettter; } }; protected abstract T initialize(); public T get() { return getter.get(); } }

Related: