code-smell

Is global:: a bad code smell in C#?

From what I understand, the global:: qualifier allows you to access a namespace that has been hidden by another with the same name. The MSDN page uses System as an example. If you create your own namespace System, you can reach the original with global::System. The first thing that came to mind is, why would anyone call their namespace S...

Is there a limit on the number of exceptions I should throw from a method?

this is my function declaration public init(){ try{ initApplication(); }catch(A1Exception){ }catch(A2Exception){ ... }catch(A5Exception){ } } private void initApplication() throws A1Exception, A2Exception, A3Exception, A4Exception, A5Exception { initApp1(); //throws A1, A2, A3 initApp2(); ...

Refactoring Studies

Hi, Our development shop is conducting a seminar in a local college to encourage clean code (the standards are from the eponymous book) and best practices. Now we need references for the time and effort saved. Has anybody read studies about it? Thanks, Jon ...

Simple, general-interest, code-analyzers based, Java questions

OK, after reviewing some code with PMD and FindBugs code analyzers, i was able to do great changes on the reviewed code. However, there are some things i don't know how to fix. I'll iterate them bellow, and (for better reference) i will give each question a number. Feel free to answer to any/all of them. Thanks for your patience. 1. Ev...

Code smell: Passing Javascript variable names and values from server script (ASP.NET)

What are some good ways to deal with this messy and unnecessary and not-really-dynamic generation of JavaScript: var <%# JavascriptId %> = new BusinessChart( '<%# JavascriptId %>',<%# CurrentUserId %>,'<%# ChartId %>' ,'<%# Helper.GetBaseUrl() %>','<%# ChartPath %>' ,'<%# Helper.ResolveUrl("~", true) %>' ); <%# JavascriptId...

Could someone explaining the reasoning behind some of these PMD rules?

DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'variable' (lines 'n1'-'n2'). DataflowAnomalyAnalysis: Found 'DU'-anomaly for variable 'variable' (lines 'n1'-'n2'). DD and DU sound familiar...I want to say in things like testing and analysis relating to weakest pre and post conditions, but I don't remember the ...

Extension 'Class': Good use of extension methods and increase code readability... or bad smell?

So I've been dealing with several APIs recently provided by different software vendors for their products. Sometimes things are lacking, sometimes I just want to make the code more readable, and I'm trying to avoid a ton of static methods where they don't belong to "get what I need" from the APIs. Thus, I've found myself writing quite a ...

linux threads and fopen() fclose() fgets()

I'm looking at some legacy Linux code which uses pthreads. In one thread a file is read via fgets(). The FILE variable is a global variable shared across all threads. (Hey, I didn't write this...) In another thread every now and again the FILE is closed and reopened with another filename. For several seconds after this has happened,...

Is it possible to avoid using type checking in this example?

Sorry for the poor title, can't think of a succinct way of putting this.. I'm thinking of having a list of objects that will all be of a specific interface. Each of these objects may then implement further interfaces, but there is no guarantee which object will implement which. However, in a single loop, I wish to be able to call the me...

Refactoring Two Levels of Switch Statements

I've inherited a code base that makes heavy use of switch statements (C#, FWIW) to drive some logic. It's a multi-tenant web app where one set of switch statements pertains to how content displays and the other pertains to links to features, at least in most cases. I have an opportunity to refactor, so I'm taking the content-related sw...

MVC in PHP without the "magic"

Let's say I have a PHP Model-View-Controller framework that maps an address like http://site.com/admin/posts/edit/5 to an action that looks like Posts_Controller::editAction($id) in file /admin/controllers/posts.php Now, many existing PHP frameworks that I've looked at would accomplish this by something similar to $module = Router::...

C# overloading operator== versus Equals()

I'm working on a C# project for which, until now, I've used immutable objects and factories to ensure that objects of type Foo can always be compared for equality with ==. Foo objects can't be changed once created, and the factory always returns the same object for a given set of arguments. This works great, and throughout the code base ...

Calling a specific constructor in c# with null

I have a class with several constructors and I want to call the "main" one from another - but using null. Using just this(null) results in a compile time error, so I cast null to the type of the other constructor. Compiles fine. MyClass { public MyClass(SomeType t) { } public MyClass(IList<FooType> l) : this((SomeType)null)...

Is there a less hacky way to do this in MySQL?

I need to get the last inserted product by a user. This is what I've came up with $query = 'SELECT id FROM products WHERE user_id = ? ORDER BY id DESC LIMIT 1'; It should work because the id is auto increment. To me though, it feels hacky. Is there a better wa...

Additional try statement in catch statement - code smell?

Situation: My application need to process the first step in the business rules (the initial try-catch statement). If an certain error occurs when the process calls the helper method during the step, I need to switch to a second process in the catch statement. The back up process uses the same helper method. If an same error occurs du...

None-Pure Functional Code Smells

What are some of the common signs your taking the wrong approach in a mixed functional / imperative / OO environment? ...

before and after, from bad code to good code, open source project examples, php

It's possible to download old source code versions from open source projects. I want to learn from history. Maybe, more people in Stack Overflow want to learn from history too. What open source projects do you know that made a transition from bad php code to good php code? How do you describe the transition (big, fast, easy...)? What c...

Should clean code always obey these rules?

Regarding the book "Clean Code - A Handbook of Agile Software Craftsmanship": it says that functions should do one thing. Should else be avoided? If so, should return true and return false that use to come together really be avoided too? it says that functions should have one level of abstraction. Is high-level like ruby or like less i...

Write-only accessors in Actionscript 3: Bad Practice?

I just can across some code where they've got an implicit setter, but no getter eg: public class Person { //no getter! public function set food(value:Food):void { // do something with food. this.processFood(value); this.dispatchEvent(new Event(FoodEvent.EATEN)); } } This smells bad to me. Hacky. W...

Code Smell: Configuration Nightmare

How would you identify and fix the following code smell: I've got a small scientific computing app that I'm writing that has to be able to handle lots of variations on the same theme. The inner workings of it are well-factored, mostly using the template method pattern and some higher-order functions. However, specifying how all these ...