problem about patterns-Collection of common programming errors


  • casperOne
    multithreading concurrency patterns
    I’m looking into educating our team on concurrency. What are the most common pitfalls developers fall into surrounding concurrency. For instance, in .Net the keyword static opens the door to a lot of concurrency issues.Are there other design patterns that are not thread safe?UpdateThere are so many great answers here it is difficult to select just one as the accepted answer. Be sure to scroll through them all for great tips.

  • mschonaker
    java android patterns
    Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it’s a good idea to use Singletons instead of single instances shared through global application state (subclassing android.os.Application and obtaining it through context.getApplication()).What advantages/drawbacks would have both mechanisms?To be honest, I expect the same answer in this post http://stackoverflow.com/questions/2709071

  • Dmitry Krasun
    oop validation design-patterns patterns
    I have dispute with my friend. He said me that this code: method SetBalance(balance) {if (balance > 0) {this.balance = balance;return true;}return false; }is better then this: method SetBalance(balance) {if (balance < 0) {throw new InvalidArgumentException(“Balance couldn’t be negative”)}this.balance = balance; }My question is: “Which approach is better for validation?” and why? Thank you.

  • Sisiutl
    c# patterns refactoring
    Martin Fowler’s Refactoring discusses creating Null Objects to avoid lots of if (myObject == null)tests. What is the right way to do this? My attempt violates the “virtual member call in constructor” rule. Here’s my attempt at it:public class Animal{public virtual string Name { get; set; }public virtual string Species { get; set; }public virtual bool IsNull { get { return false; }}}public sealed class NullAnimal : Animal{public override string Name{get{ return “NULL”; }set { ; }}public override

  • Marcal
    ios core-graphics patterns quartz-2d
    I have a Core graphics Pattern that works perfectly on the simulator, but it crashes on the device:void MyDrawColoredPattern4 (void *info, CGContextRef context) {UIColor* colorClar = [UIColor colorWithRed: 0.57 green: 0.57 blue: 0.57 alpha: 1];UIColor* colorFosc = [UIColor colorWithRed: 0.15 green: 0.15 blue: 0.15 alpha: 1];CGColorRef dotColor = colorFosc.CGColor;CGColorRef shadowColor = colorClar.CGColor;CGContextMoveToPoint(context, 5, -0.5);CGContextAddLineToPoint(context, 1.1, 1.75);CGContex

  • Thomas Owens
    anti-patterns patterns-and-practices patterns
    Imagine to have this code:class Foo:def __init__(self, active):self.active = activedef doAction(self):if not self.active:return# do somethingf=Foo(false) f.doAction() # does nothingThis is a nice code; I actually have (not in Python) a global active variable called “dosomething” and a routine called “something,” where the first thing happening inside the “something” routine is if not dosomething return.An alternative implementation would call to a routine that always performs an action, and the

  • Will
    python google-app-engine error-handling patterns
    I am developing an application on the Google App Engine using Python.I have a handler that can return a variety of outputs (html and json at the moment), I am testing for obvious errors in the system based on invalid parameters sent to the request handler.However what I am doing feels dirty (see below):class FeedHandler(webapp.RequestHandler): def get(self):app = self.request.get(“id”)name = self.request.get(“name”) output_type = self.request.get(“output”, default_value = “html”)pretty = self.re

  • gsharp
    c# wcf patterns
    I want to ensure that a WCF-ServiceClient State will be closed after using the service.I implemented the following Code to ensure that:public static class ServiceClientFactory {public static ServiceClientHost<T> CreateInstance<T>() where T : class, ICommunicationObject, new(){return new ServiceClientHost<T>();} }public class ServiceClientHost<T> : IDisposable where T : class, ICommunicationObject, new() {private bool disposed;public ServiceClientHost(){Client = new T();}~

  • AlexMorley-Finch
    php oop validation patterns strategy-pattern
    I need a php validator class that validates user inputs.I want it to be able to accept an assoc array of fields => values like:array(“username” => “Alex”,”email_address” => “@@#3423£[email protected]” );and then return an array of errors like this:array(“username” => “”,”email_address” => “Invalid Email Address” );But I’m really struggling on HOW the hell I’m going to do this!I’ve read countless pages on PHP validators and read that the best way to do this is with the strategy pattern

  • north
    javascript jquery performance patterns organization
    After doing some research on the subject, I’ve been experimenting a lot with patterns to organize my jQuery code (Rebecca Murphy did a presentation on this at the jQuery Conference for example).Yesterday I checked the (revealing) module pattern. The outcome looks a bit reminiscent of the YUI syntax I think://global namespace MyNameSpace if(typeof MNS==”undefined”||!MNS){var MNS={};}//obfuscate module, just serving as a very simple example MNS.obfuscate = function(){//function to create an email

  • Simo Endre
    javascript design module patterns cross-reference
    Is there a way to cross-reference an instance of a class or an instance variable from different namespaces, taking into account that it does matter in which order are the script files defined in the main html application. Actually I want to know if there is any possibility to cross reference two different class instances, one pointing to a reference defined in a different namespace, and another variable defined in the second class pointing back to the first one. Suppose i have a main.js file whe

  • Priyank
    javascript design-patterns patterns
    What is apply invocation pattern in Javascript in reference to function invocation patterns and how can I use it? What are the benefits of using this invocation pattern.

  • alnafie
    javascript function patterns
    What do you call these patterns? What is the difference between them? When would you use each? Are there any other similar patterns?(function() {console.log(this); // window })();(function x() {console.log(this); // window })();var y = (function() {console.log(this); // window })();var z = function() {console.log(this); // window }();EDIT: I just found two more seemingly redundant ways to do this by naming the functions in the last two cases…var a = (function foo() {console.log(this);

  • Owen
    javascript patterns
    Going to show my JS newbieness here, but I have a few questions about general JavaScript patterns. I’m looking at creating a widget that injects JavaScript into client pages similar to firebug lite. With that in mind the first thing I did was look at the FBL source and there are a few things happening there that I dont understand. Here goes:The author of firebug lite uses a few ways to mixin functionality to an object beyond prototypical inheritance. The first is the following:var append = funct

  • Rob
    javascript inheritance patterns
    I’m working on an ebook on GitHub on TDD JavaScript and I’m wondering if I’m missing any popular inheritance patterns. If you know of any additional patterns I’d love to see them. They should have the following:Time tested – used in real apps Source code should be supplied. Should be as straight forward and pedantic as possible. Of course be correct and working.The reason I’m doing this is that it seems that object inheritance in JavaScript has been quite difficult for many of us to grok. my Jav

  • WizzyBoom
    javascript jquery forms oop patterns
    I feel I should start by saying I’m new the patterning. I’m using the revealing prototype pattern to validate form inputs, etc. This is going to be a validation that is setup as sort of a “template” that will cover 95% of our forms but there will be requirements for certain forms to add additional custom validation down the road. That being said what I’m wanting to do is create a new form validation object then initialize it. On initialization all the evaluation magic happens and then will eit

  • WizzyBoom
    jquery jquery-ajax patterns javascript-objects prototype-oriented
    IntroductionI’m building world-wide conference room directory using a XML data set. I want to tuck the data in the this.formData so that I can reuse that info in multiple ways without having to call the XML again and again.this.formData is undefined as I step through the debuggerWhat am I doing wrong?The Codevar Directory = function () {this.formData; };Directory.prototype = function () {init = function () {getXMLforDropDown.call(this);buildDropDown.call(this);},getXMLforDropDown = function () {

  • DanC
    javascript patterns
    Possible Duplicate:JavaScript scope and closure What is this for?(function(){//The code to be executed })(); Also, has this anything to do with closures?

  • saille
    javascript patterns
    I’m trying to write OO javascript for an object that has an expensive initialization process that will callback a function when its done.The problem is that the caller needs to use the functions of that same object in the callback routine, and the object doesn’t exist yet:// ctor for foo object function foo(callback) {// do slow initialization here..// callback when donecallback(); };foo.prototype = function() {return {// doStuff methoddoStuff: function() {alert(‘stuff done’);}}; }();//

  • Mahesha999
    javascript for-loop global-variables variable-scope patterns
    I was trying out some simple JS code. I was aware that we should use var keyword to declare a loop variable inside the loop say for loop to avoid global variable declaration. However I realized that the loop variable exists after the execution of the loop too:var a = [1, 2, 3, 4, 5, 6]; for (var i = 0; i < a.length; i++)document.write(a[i]); //123456 document.write(i); //6This is not inline (in fact it does not need to be, I know) with how loop variable of for loop in Object Oriented concept

  • Namanyayg
    javascript patterns
    Here’s my code: var APP = {}APP.item = function() {var two = function() { return “three”; }return {two: two}; }console.log(APP.item.two);Now, from what I’ve read, shouldn’t the output be “three”? Rather, the result is undefined.Fiddle: http://jsfiddle.net/mhxpz/1/

  • Andy Hayden
    c++ oop patterns
    I’ll soon be posting an article on my blog, but I’d like to verify I haven’t missed anything first.Find an example I’ve missed, and I’ll cite you on my post.The topic is failed Singleton implementations: in what cases can you accidentally get multiple instances of a singleton?So far, I’ve come up with:Race Condition on first call to instance() Incorporation into multiple DLLs or DLL and executable Template definition of a singleton – actually separate classesAny other ways I’m missing – perh

  • AakashM
    javascript patterns iife
    Possible Duplicate:What does the exclamation mark do before the function? I’ve long used the following for self-executing, anonymous functions in JavaScript:(function () { /* magic happens */ })()Lately, I’ve started seeing more instances of the following pattern (e.g., in Bootstrap):!function () { /* presumably the same magic happens */ }()Anyone know what the advantage is of the second pattern? Or, is it just a stylistic preference?

  • Thomas Deutsch
    function knockout.js patterns undefined revealing-module-pattern
    I am using the Revealing Module Pattern to get some structure in my knockout.js code. It is a very simple Example Goal: return the value of the Name-Property of the Object. Problem: The function parameter x is undefined. http://jsfiddle.net/ThomasDeutsch/8hzhp/What exactly is the problem here? Help me fiddle this one out please.

  • Merc
    javascript variables patterns
    I have been programming Javascript for a little while now, and still am not quite sure if I am being too lazy or not. I have a lot of:if( typeof( something) === ‘undefined’ ){// .. }However, sometimes it just becomes too verbose. For example, now I am doing:var redirectURLs = hotplate.get(‘hotCoreAuth/redirectURLs/success’) || {};That’s because in the following lines I am treating redirectURLs as an object, although it might not be defined at all (the function might well return undefined).Is the

  • 7ogan
    c# design parsing types patterns
    I am dealing with values delimited by commas sent to me as a string. The strings come in many different structures (meaning different data types in different locations of the string as well as varying amounts of data). So while one string might be represented as:- common data,identifier,int,string,string,string.Another might be represented as:- common data,identifier,int,int,string,string,string.Design goals:Common parse method Common validation (i.e. int.TryParse() returns true) Readily able to

  • Shad
    javascript patterns google-closure-compiler jsdoc
    I’m getting a lot of “Unknown type” warnings when running a fairly large library through Closure Compiler, and they seem to occur when my types are declared in self-executing anonymous functions. There’s nothing exotic about this, but if I strip the self-executing functions out, the type declarations seem to work (at least in this simple test).I’m not sure if there’s something wrong with my code annotations or if there’s anything illegal in the code, but I think this is all kosher and the standa

Web site is in building

I discovery a place to host code、demo、 blog and websites.
Site access is fast but not money