Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [code-smell]

Determining what is and is not a "code smell" is subjective, and varies by language, developer and development methodology. Before you ask if some technique is a "code smell," ask yourself what the consequences to your specific project would be, if you used the technique. Simply asking whether ...

-3
votes
0answers
64 views

Encoding information in object names

I work with a system that has some lists of objects that can have arbitrary, user defined names, and each object has some properties that can be configured. In the IDE for the system the objects are ...
7
votes
2answers
359 views

Class that returns multiple unrelated values, are there better way to accomplish this?

I have a "tracker" class. This class tracks objects from a given input image. But in addition to this, there is another parameter that corresponds to a value used in generating the image. The ...
2
votes
1answer
183 views

How do I use polymorphism instead of instanceof?

I'm trying to make an abstract board game. In the game, a player can choose to make multiple actions within one turn such as placing, moving, or rotating a piece. I'm not sure if whether or not my ...
-1
votes
1answer
62 views

My action creators and reducers all mostly look the same - is this a code smell?

Most of my redux actions are 'fetch some data from the database, here's the authentication token', and they for the most part look the same, like this: action creator export const fetchDashboard = ...
1
vote
1answer
122 views

Why is using an internal attribute to filter objects considered OK, but when I use a map to filter based on attributes, it's considered a code smell?

A long time ago I asked about using an enum to essentially use as a poor version of instanceof to make decisions about an object. As stated in this answer: When your weapon types enum just mirrors ...
-1
votes
1answer
107 views

Have an object communicate with another object

I asked similar question earlier this morning, and for whatever reason, I'm not getting a complete answer to my question. I'll ask it a different way. I was attempting to understand Eric Lippert's ...
2
votes
2answers
87 views

Using a Collection with Attributes to Check and Make Decisions

I was reading on this SESE page about using a variable to indicate the object type, more specifically, an enum. The accepted answer states: When your weapon types enum just mirrors the class ...
0
votes
3answers
208 views

Formatting a string by converting to a number then back to a string

I'm not sure whether this qualifies as code smell, or if there may be some better way of performing the same task, but, basically, I want to format a 10 digit string, using C#, and have the following ...
3
votes
5answers
313 views

Using a variable to indicate object type

The use of instanceof or getClass() is largely considered code smell. Is using a variable to indicate the type of object you're using also considered code smell? Suppose if I had an enum called ...
0
votes
2answers
137 views

Building dynamic code that can still handle many one-offs?

So we're having a rather heated debate at work as to what would be the best practice going forward while refactoring our reporting system. Most of us have some strong opinions one way or the other, ...
4
votes
2answers
1k views

Should a trait refer to parent methods?

Is it a code smell if the methods in my trait refer to parent::methods or to methods that are assumed to be in the utilising class? A random (senseless) example trait foo { public function bar() ...
1
vote
0answers
1k views

How to improve a pattern 'enum class with methods'

Over many years, I always find myself reconsidering this design, so I wanted to get some feedback on my solution to it. Problem: I need a limited amount of objects = instances from a class, and I ...
2
votes
4answers
294 views

Use of static methods to implement logic within a class

I've recently caught an odd behaviour in my own coding: At some point in the last year I began creating protected static methods to implement the logic of my public methods, ensuring that the ...
1
vote
1answer
101 views

Is a Presentation potentially a god object?

I'm working on a project with a MVP architecture. My program revolve around forms that give rights to people. There are X differents forms depending on profiles ( so there are X differents profiles) I ...
2
votes
4answers
308 views

code smell: passing through variables [closed]

Consider this simple example: def a(val_x, val_y): return 5 + b(val_x, val_y) def b(val_x, val_y): return 1 + c(val_x, val_y) def c(val_x, val_y): return val_x * val_y Passing the ...
3
votes
2answers
274 views

Is using spacing effectively equivalent to the long method code smell?

There's a common code smell involving long methods with the most common answer being that methods should be really small, less than 50 lines per say (or 20). I understand why this is because it ...
1
vote
2answers
422 views

MVVM. Is it a code smell when view model has properties with names show/hide/display that semantically belong to view?

I've lately been involved with WPF and looked into MVVM. I understand that view model shouldn't be aware of view. However, sometimes I come across situation where my initial instinct is to write ...
5
votes
1answer
683 views

How do I refactor a Data Class to not be one?

We recently upgrade to PMD 6.0.0 and are getting several classes flagged as "Data Classes"? It argues that it breaks encapsulation and makes for brittle design (I understand that this site has a ...
3
votes
3answers
284 views

Is there an elegant way to write code with differing paths having a common middle segment?

I started out with the following, simple routine: void originalCode(){ boolean status = getStatus(); function2(status); if(status){ return; } function3(); } But ...
3
votes
1answer
384 views

Hash Table with iterators as the keys, is this poor design and can I do this better?

I'm developing a program where twice I've found the solution to a problem was to use hash tables with iterators as keys and some other arbitrary type as the value. I found my self using this pattern ...
-1
votes
4answers
850 views

Can factory pattern avoid code smell?

With two existing implementations, UFOEnemyShip & RocketEnemyShip, To introduce every new implementation implementing EnemyShip interface, shown below, changes are needed in factory class ...
-1
votes
1answer
174 views

Police selection without hard coding an if statement

I'm trying to model the security of my project using DDD but I ended up in a dead end. I have the User, UserA, and UserB classes that were modeled as follows. What changes in practice is that UserA ...
0
votes
1answer
565 views

Clear examples for code smells [closed]

does anybody know how the code smells "rigidity", "fragility", "immobility" and "viscosity" can be explained in short code examples? I've found a lot of text but never a source code example. Thanks ...
0
votes
2answers
179 views

Is implementations not being interchangeable and having to cast often a code smell?

I'm building a small game similar to chess. I'd like to be able to reuse the structure for another version of checkers too. I'm modeling the games with interfaces (showing only relevant ones): ...
3
votes
1answer
1k views

Definition of brittle unit tests

I've seen the terms brittle and fragile used to describe poorly written unit tests. But I haven't been able to find a clear definition for what brittle or fragile actually mean in this context. Is it ...
5
votes
4answers
2k views

Are empty interfaces (but not marker interfaces) a bad programming practice?

I'm designing an architecture in which I have: public interface IObjectReader{ public Object read(); } public class ConcreteObjectReader implements IObjectReader{ @Override public ...
3
votes
4answers
305 views

Sugar Classes as code smell

Over the years I've been switching back and forward on the idea of creating Sugar Classes. Sooner or later I find myself repeating the same operation over an existing type. Like string manipulation, ...
10
votes
4answers
950 views

Reworking a function returning an integer code that represents many different statuses

I've inherited some awful code that I've included a short sample of below. Is there a name for this particular anti-pattern? What are some recommendations for refactoring this? // 0=Need to log in / ...
-3
votes
2answers
930 views

What is the difference between Code Smells and Anti Patterns?

I am trying to understand the difference between these two terms. In some contexts that these two terms have been used interchangeably. What is the exact difference between these two terms ?
15
votes
3answers
1k views

When is it okay to use Parallel Arrays?

I've been running into code (new code) that uses what I call 'Parallel Arrays' or Lists. Meaning there are 2 arrays that contain related data and are linked by their position (index) in the array. I ...
5
votes
4answers
2k views

Multiple properties/methods giving the same result

I'm trying to ascertain whether the use of multiple references to the same property is code smell / an anti-pattern, based on the needs of the organisation. As an example, consider: abstract class ...
1
vote
1answer
104 views

Refactoring code dependent on external class

I have a class similar to ServletFilter which has multiple validation on the input request class TokenHandler implements SomeHandler{ Response handle(Request request){ if(paramXMissing(request))...
5
votes
3answers
748 views

DependencyInjection - Constructor over-injection smell vs service locator - where is the proper approach?

I am trying to improve my MVC projects and I read a few articles about DI, IoC containers, Constructor Injection and Service locators. I wanted to go with Ninject to help with the dependencies, but ...
0
votes
2answers
345 views

Does this code have only one level of abstraction?

I'm trying to apply some good practices from Clean Code in my code, but I'm stuck trying to figure out if my code has a [G34] code smell, which tells that functions should descend only one level of ...
4
votes
5answers
1k views

Should constructors ever be used only for side-effects?

Summary: Why is it wrong to design a constructor only for its side effects, and then to use the constructor without ever assigning its return value to a variable? I'm working on a project that ...
2
votes
3answers
417 views

Refactoring Atrocious Java Code [duplicate]

So I recently started a new job for a small software company. They've got some old technology which I was aware of during the interview process. However, I've been digging into their code-base, and I'...
2
votes
0answers
235 views

Does using returnTrue() or returnFalse() functions a code-smell?

I was trying to understand how jQuery's cross-browser events work, when I noticed these two functions in the source code: function returnTrue() { return true; } function returnFalse() { ...
15
votes
3answers
6k views

Mocking introduces handling in production code

Assuming an IReader interface, an implementation of the IReader interface ReaderImplementation, and a class ReaderConsumer that consumes and processes data from the reader. public interface IReader { ...
4
votes
1answer
1k views

Swift-like extensions in Java using inheritance

After picking up some Swift skills with Java as my strongest language, one feature of Swift that I really like is the ability to add extensions to a class. In Java, a pattern I see very often is ...
12
votes
2answers
2k views

Why are data classes considered a code smell?

This article claims that a data class is a "code smell". The reason: It's a normal thing when a newly created class contains only a few public fields (and maybe even a handful of getters/setters)....
14
votes
3answers
843 views

I'm losing track of the flow of my PHP web app, it's becoming hard to work with

I've been programming for a few years, and have become very familiar with C# and JavaScript over time. I have some larger C# and JavaScript projects that I have no trouble navigating around. I ...
13
votes
9answers
9k views

Are init() methods a code smell?

Is there any purpose for declaring an init() method for a type? I'm not asking whether we should prefer init() over a constructor or how to avoid declaring init(). I'm asking if there is any ...
4
votes
1answer
825 views

Testing: why expect_any_instance_of is considered a design smell?

In this question, I'll use a ruby example, but I think it is a general question. According to the most popular Ruby's test framework (RSpec), mocking any instance of a class (allow_any_instance_of) ...
4
votes
1answer
203 views

Compiled javascript code smell: good reasons for large classes?

I've been looking at a javascript library for drawing graphs, Dygraph, source on github. It has several enormous classes, such as here and here. Personally, I don't think justifications for large ...
2
votes
3answers
186 views

Data duplication, can it be an unavoidable practice in this example?

Say I have different employees of type Employee stored in a list inside a class SubCase. public class SubCase{ protected ArrayList<Employee> employees; ... } SubCase represents a part ...
3
votes
2answers
94 views

Is it ok to inherit certain exceptions and implement them only through their base types?

So, I was thinking about writing custom exceptions today, and considered the invalid operation exception. This exception can mean many many things, and in some actions, the operations might be invalid ...
0
votes
0answers
109 views

Excessive LINQ chaining - Is it considered a code smell, and why? [duplicate]

I am in a current iteration between design and implementation, and ended up with the following "one-liner": IEnumerable<Channel> ActiveChannels = Receiver.AcquisitionInfo ...
0
votes
0answers
111 views

Is this an example of Data Envy? How do I solve it?

I have a scenario where I'm using an external service which requires a big hash of parameters like so: def external_service_params(user, invoice) { name: user.name, email: user.email, ...
9
votes
3answers
3k views

Cleanest way to write logically procedural software in an OO language

I'm an electrical engineer and I don't know what the hell I'm doing. Please save the future maintainers of my code. Recently I've been working on a number of smaller programs (in C#) whose ...
5
votes
2answers
314 views

Is there a name for the anti-pattern of having low-level components controlling higher-level ones?

For example, suppose I had a pageControl UI class and a searcher business logic class with the following : pageControl.js function searchButtonClicked() { // call business logic class method when ...