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.
Model–view–controller Model–view–controller (MVC) is a software pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.[1][2] The central component, the model, consists of application data, business rules, logic and functions. A view can be any output representation of information, such as a chart or a diagram. Component interactions[edit] A typical collaboration of the MVC components In addition to dividing the application into three kinds of components, the Model–view–controller (MVC) design defines the interactions between them.[4] Use in web applications[edit] Although originally developed for desktop computing, model-view-controller has been widely adopted as an architecture for World Wide Web applications in all major programming languages. History[edit] See also[edit] References[edit] External links[edit]
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?
Convention over configuration Convention over configuration (also known as coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility. The phrase essentially means a developer only needs to specify unconventional aspects of the application. For example, if there's a class Sale in the model, the corresponding table in the database is called “sales” by default. It is only if one deviates from this convention, such as calling the table “sale”, that one needs to write code regarding these names. When the convention implemented by the tool matches the desired behavior, it behaves as expected without having to write configuration files. Only when the desired behavior deviates from the implemented convention is explicit configuration required. Motivation[edit] Some frameworks need multiple configuration files, each with many settings. Usage[edit] See also[edit] References[edit] External links[edit]
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;
Active record pattern This pattern is commonly used by object persistence tools, and in object-relational mapping (ORM). Typically, foreign key relationships will be exposed as an object instance of the appropriate type via a property. Implementations[edit] Implementations of the concept can be found in various frameworks for many programming environments. For example, if in a database there is a table parts with columns name (string type) and price (number type), and the Active Record pattern is implemented in the class Part, the pseudo-code part = new Part() part.name = "Sample part" part.price = 123.45 part.save() will create a new row in the parts table with the given values, and is roughly equivalent to the SQL command INSERT INTO parts (name, price) VALUES ('Sample part', 123.45); Conversely, the class can be used to query the database: b = Part.find_first("name", "gearbox") This will find a new Part object based on the first matching row from the parts table whose name column has the value "gearbox".
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?”
rails/rails 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.
Solution stack In computing, a solution stack is a set of software subsystems or components needed to perform a task without further external dependencies. For example, to develop a web application, the designer needs to use an operating system, web server, database, and programming language. Another version of a solution stack is operating system, middleware, database, and applications.[1] Linux-based solution stacks[edit] Linux (the operating system) Apache (the web server) MySQL or MariaDB (the database management systems) Linux (operating system) Apache (web server) Seaside (web framework) Smalltalk (programming language) The cloud stack of LEAP for: AppScale (Cloud computing-framework and free and open-source alternative to Google App Engine), OpenStack Linux – OpenStack controller nodes run exclusively on Linux OpenStack – providing an infrastructure as a service (IaaS) Ganeti Xen or KVM (hypervisor) Distributed Replicated Block Device (storage replication) Ganeti (virtual machine cluster management tool) Node.js
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; } }
Ruby on Rails Ruby on Rails emphasizes the use of well-known software engineering patterns and principles, such as active record pattern, convention over configuration (CoC), don't repeat yourself (DRY), and model–view–controller (MVC). History[edit] On December 23, 2008, Merb, another web application framework, was launched, and Ruby on Rails announced it would work with the Merb project to bring "the best ideas of Merb" into Rails 3, ending the "unnecessary duplication" across both communities.[7] Merb was merged with Rails as part of the Rails 3.0 release.[8][9] Rails 3.2 was released on January 20, 2012 with a faster development mode and routing engine (also known as Journey engine), Automatic Query Explain and Tagged Logging.[11] Rails 3.2.x is the last version that supports Ruby 1.8.7.[12] Rails 3.2.12 supports Ruby 2.0[13] Technical overview[edit] Like many web frameworks, Ruby on Rails uses the model–view–controller (MVC) pattern to organize application programming. Framework structure[edit]
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(); } }