problem about code-smell-Collection of common programming errors


  • Jason
    asp.net error-handling code-smell
    This seems like a pretty efficient way to do error handling, but I want to know how to do it correctly if this is smelly:Class Widget…Public Function IsValid() As BooleanIf (some condition isnt met) ThenThrow New ApplicationException(“Error message”)ElseIf (some other condition isnt met) ThenThrow New ApplicationException(“Another error message”)End IfReturn TrueEnd Function… End Class… (somewhere else)… Public Function DoAwesomeStuff(id As Integer) As StringDim w As Widget() = Widget.Ge

  • Paul Stovell
    code-quality code-smell coupling
    Suppose you have this code in a class:private DataContext _context;public Customer[] GetCustomers() {GetContext();return _context.Customers.ToArray(); }public Order[] GetOrders() {GetContext();return _context.Customers.ToArray(); }// For the sake of this example, a new DataContext is *required* // for every public method call private void GetContext() {if (_context != null) {_context.Dispose();}_context = new DataContext(); }This code isn’t thread-safe – if two calls to GetOrders/GetCustomers

  • James South
    c# thread-safety code-smell
    The following code is intended to implement a LinFu DynamicProxy interceptor to lazy load given virtual properties from an Umbraco datastore.My concerns:Thread safety, Have I covered all the bases, Am I storing variables correctly? Duplication – Getting the child type, am I safe to store a variable via my Lazy to use later? Efficiency, If I have everything else correct, am I being wasteful? The code:/// <summary> /// The content interceptor for intercepting virtual content properties. ///

  • Joshua Enfield
    .net design domain-driven-design code-smell
    I am noticing occasions where I need to add dependencies to parent classes, and their parents and so on, only because a child class needs it. Is this a code smell? Is it reasonable for a parent class to need information only because a child class needs it?In particular in our [slightly anemic] flavor of DDD this seems to occur with Application Services (Domain boundary) needing information from our application layer to pass on to Domain Services. It is quite painful to add dependencies to app se

  • user128807
    c# code-smell
    Possible Duplicate:What static analysis tools are available for C#? Is there a code smell checker for C#?

  • BenV
    java design-patterns code-smell
    In general, what is the maximum number of parameters a class constructor should accept? I’m developing a class that requires a lot of initialization data (currently 10 parameters). However, a constructor with 10 parameters doesn’t feel right. That leads me to believe I should create a getter/setter for each piece of data. Unfortunately, the getter/setter pattern doesn’t force the user to enter the data and without it characterization of the object is incomplete and therefore useless. Thoughts?

  • rwong
    performance exceptions code-smell complexity workaround
    The focus of this question: Some software performs “extra work” in order to increase the chance of a “eventually successful/satisfactory” outcome, despite one or more internal errors in the software, which requires a longer execution time when those errors happen. All these happen without the user’s knowledge if the outcome was successful.Definition of complex software:Contains code written by (contributed from) more than 10 developers over its lifetime, and not written in the same time frame De

  • AlexMorley-Finch
    php object-oriented programming-practices code-quality code-smell
    Basically, I develop websites, some large with many crud operations, etc…However I’ve gotten into the habit of storing re-usable data as a constant in my PHP applicationsI currently have 44 constants defined in one application.These range from:Database configHome page for admin and guesturl slug namesdeclaring development modedeclaring maintenance modea few directory shortcuts ( instead of writing /admin/templates/images ill use const ADM_IMAGES )session namescookie namesrandom things like MAX

  • Buttons840
    unit-testing code-quality code-smell
    Usually I just throw my unit tests together using copy and paste and all kind of other bad practices. The unit tests usually end up looking quite ugly, they’re full of “code smell,” but does this really matter? I always tell myself as long as the “real” code is “good” that’s all that matters. Plus, unit testing usually requires various “smelly hacks” like stubbing functions.How concerned should I be over poorly designed (“smelly”) unit tests?

  • Erik Dietrich
    design unit-testing code-smell polymorphism
    I was reading some blog posts this morning, and stumbled across this one:If the only class that ever implements the Customer interface isCustomerImpl, you don’t really have polymorphism and substitutabilitybecause there is nothing in practice to substitute at runtime. It’sfake generality.That makes sense to me, as implementing an interface adds complexity and, if there is only ever one implementation, one might argue that it adds needless complexity. Writing code that is more abstract than it n

  • Bascy
    code-smell
    In our Delphi 2007 application we are using a lot of the following constructsFdmBasic:=TdmBasicData(FindOwnerClass(AOwner,TdmBasicData));The FindOwnerClass travels the Owner hierarchy of the current component upwards to find a specific class (in the example TdmBasicData). The resulting object is stored in the Field variable FdmBasic. We use this primarily to pass datamodules along.Example: When generating a report, the resulting data is compressed and stored in a Blob field of a table accessed t

  • user16764
    programming-languages code-smell
    Many projects combine languages, for example on the web with the ubiquitous SQL + server-side language + markup du jour + JavaScript + CSS mix (often in a single function). Bash and other shell code is mixed with Perl and Python on the server side, evaled and sometimes even passed through sed before execution. Many languages support runtime execution of arbitrary code strings, and in some it seems to be fairly common practice. In addition to advice about security and separation of concerns,what

  • Arne
    java clean-code style code-smell
    I recently changed one of my methods and used this pattern (names are changed, a dose of humor applied for the exception):switch (myEnum) { case e1:// do stuffif (!alsoApplyCodeForE2) {break;} // else fallthrough case e2:// do some more stuffbreak; default:throw new NIHException(); }The // do stuff part uses too many variables to extract it into a method. For e1, in some conditions the code in e2 also has to be executed.Before I structured it this way, I had either duplicated code for the action

  • Alexey Sidash
    python parsing xml software-design code-smell
    What do you think of this class? Is this simple to understand how it works and what it does? Do you think it is good piece of code?class Field(object): “””Class which contains logic of field-processing “””def __init__(self, routes, callback=None, is_iterable=False,default=None):”””Arguments:- `routes`: sequence of xPath routes to the field, in order of descending priority.- `callback`: callable handler of field- `is_iterable`: if there may be a more then one field with a such route in the xml Do

  • Emil
    iphone ios nsdictionary nsmutabledictionary code-smell
    I have the following code fragment:NSString* value = @”value”; NSString* key = @”key”; NSMutableDictionary* foo = [NSMutableDictionary dictionary]; NSDictionary* bar = [NSDictionary dictionary]; NSAssert([bar isKindOfClass: [NSMutableDictionary class]], @””); // passes the assert as both are NSCFDictionary; NSAssert([bar respondsToSelector: @selector(setValue:forKey:)], @””); // passes the assert because it NSCFDictionary does respond to the selector [foo setValue:value forKey:key]; // no

  • user1022677
    c# design coding-style code-smell
    I’m coding a bunch of systems right now. They do not derive from a common interface.Some example systems: MusicSystem, PhysicsSystem, InputSystem, et cetera.Currently, MusicSystem loads a lot of audio files in its constructor and as a result, there may be some brief lag when the object is first created.Because of this, should this code loading all the audio files be placed in an Initialize() method instead? This allows the programmer to determine when he wants to load the audio files but then if

  • svick
    c# asp.net code-smell iis
    This question already has an answer here:Is catching general exceptions really a bad thing?9 answersWe have production code that is not easily deployed in a test area. We also have a new library of code that we would like to “plug in” to the production code. The production code would be making a few calls to the new code. Each of these calls are not mission critical (updating analytic information in a database). If I surrounded all the calls to the new code with “try{…} catch(exception)” to av

  • Theodor
    code-smell functional-testing
    I have recently been looking at some testscripts which looks a bit like… try {receiveSomething();// something was received even though it shouldn’tfailTest(); } catch (TimeoutException e) {// nothing should be receivedsuccedTest(); }The problem I have with these types of tests isThey are not deterministic. You don’t know if nothing was sent on purpose or if everything has crashed. Its very hard to simultaneously test something else, which might actually, in this case, send something.My thought

  • Robert Harvey
    code-smell anti-patterns parameters
    I am reading about common code smells in Martin Fowler’s Refactoring book. In that context, I was wondering about a pattern I am seeing in a code base, and wether one could objectively consider it an anti-pattern.The pattern is one where a object is passed as an argument to one or more methods, all of which change the object’s state, but none of which return the object. So it is relying on the pass by reference nature of (in this case) C#/.NET.var something = new Thing(); // … Foo(something);

  • jmort253
    code-quality code-smell maintainability
    I remember doing a couple of projects where I totally neglected using flags and ended up with better architecture/code; however, it is a common practice in other projects I work at, and when code grows and flags are added, IMHO code-spaghetti also grows.Would you say there are any cases where using flags is a good practice or even necessary?, or would you agree that using flags in code are… red flags and should be avoided/refactored; me, I just get by with doing functions/methods that check fo

  • gnat
    javascript refactoring code-smell
    In some javascript code I’m working on refactoring I’ve handled cases where I wanted to default an object property to true without having to go through the code-base and add the property to every instance of the object by doing something like this:if(typeof(thisBool) === ‘undefined’ || thisBool) {//do cool stuff here; }so for existing instances I only had to fix up the model in cases where I wanted to set thisBool to some value calculated somewhere else, or to set it to false; if it was unset th

  • Mawg
    php netbeans code-smell lint
    I’m not striving for perfection, but bang for the buck (in freeware). Most return for least effort, 80/20 sort of thing.I am using NetBeans for development. It is pretty good at telling me about possibly undefined variables (excep thatif ($x)$y = 5; else$y = 7;$z = $y; <==== Netbeans warns that $y many not be defined.It warns that some functions may not be defined when they are (I think it doesn’t follow nested include/require).If I don’t use PHP unit & code coverage I can’t see which fu

  • DBD
    iphone code-smell dealloc viewdidload
    I’m not going to go into the which one is called when and why. (There is a lot on that already)Since we can’t rely on viewDidUnload being called before dealloc I find myself with a lot of duplicated code between the methods.- (void)viewDidUnload {[super viewDidUnload];self.foo = nil;self.bar = nil; }- (void)dealloc {[super dealloc]; [foo release];[bar release];[abc release]; }Redundant code, ick. Anyone know issues in doing it more like this? – (void)viewDidUnload {[super viewDidUnload];[

  • JasonG
    clean-code scala code-smell
    I’m new to scala and I’m noticing I have some repeating code in my controllers.I’m wondering if anyone can give some hints for traits and parameterization for eliminating duplicate code for basic crud operations on the web services – I’m still a little confused about how to use scala ‘generics’/parameters.Don’t mind the json – I’ve fixed this horrible scrappy code now using the objects to render themselves as json.Code piecepackage controllersimport play.api.mvc._ import securesocial.core._ impo

Web site is in building