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 something is a "code smell" or not is too subjective.

Filter by
Sorted by
Tagged with
0 votes
1 answer
46 views

Better design for a REST import into web store

I have an import that needs to grab data from a REST service and import into an web store. It's basically an ETL type of service, but because the REST service can be slow and I don't want to call it ...
user avatar
0 votes
2 answers
358 views

Is "error first" better than "if-else", why? [duplicate]

I have an eternal discussion in my work about why "error first" is "worng". In order to ensure what I try to tell with error first is the following code pattern: if condition: ...
user avatar
  • 369
0 votes
1 answer
45 views

MVVM List<T> from Model to View

I am writing a control app for a 6-DOF robot. This app can send scripts to the robot, which in turn executes these and thus moves around. In the app a robotprogram can be created by adding 3D ...
user avatar
94 votes
9 answers
14k views

Is putting general-use functions in a "helpers" file an anti-pattern or code smell?

Is it an anti-pattern or code smell to put "general use" functions (examples below) into a catch-all file named "helpers" or "utils"? It's a pattern I've seen quite a lot ...
user avatar
  • 839
0 votes
4 answers
246 views

Refactoring two methods with similar for loops containing some unique logic

Please help me with the following code refactoring, I spent so much time researching it and there's still no clear answer on how to approach it. Any help is greatly appreciated! So, given 2 methods (C#...
user avatar
  • 11
28 votes
4 answers
4k views

Law of Demeter and over-wide interfaces

The Law of Demeter makes sense in some obvious cases. # better dog.walk() # worse dog.legs().front().left().move() dog.legs().back().right().move() # etc. But in other cases it seems to lead to an ...
user avatar
  • 1,045
0 votes
2 answers
447 views

What is Insider Trading Bad Smell?

In Martin Fowler's refactoring book, there is a code smell named "Insider Trading" which I don't understand: Software people like strong walls between their modules and complain bitterly ...
user avatar
6 votes
1 answer
573 views

Why would it be considered bad practice if a concrete class inherits from only one Mixin?

I'm referring to "Fluent Python" by Luciano Ramalho. In chapter 12, there's a section "Coping with Multiple Inheritance" where the author suggests a few best practices, such as: ...
user avatar
  • 476
1 vote
2 answers
354 views

How to avoid cascading dynamic casts?

So, in general casting and dynamic_cast in particular are to be avoided. But I don't see a proper alternative for this: List<DerivedA*> ListA; List<DerivedB*> ListB; Bool Add(Base* obj) { ...
user avatar
  • 21
-3 votes
3 answers
263 views

Is there a name for the code smell where we add different types of objects to a list or array instead of defining a new data structure? [closed]

I came across a question earlier where someone asked how to create an array list with multiple object types (Create an ArrayList with multiple object types?). The easiest solution is to create a list ...
user avatar
0 votes
2 answers
135 views

Avoiding sequential coupling while maintaining separation of concerns

Got a desktop application that interacts with some stateful third party web APIs. Its an interactive application, at some stages it has to halt execution and prompt for user input based on responses ...
user avatar
-2 votes
1 answer
159 views

Is frequent use of bind(this) in Javascript a code smell?

I am doing a project for a client where I am getting my first real heavy, hands-on exposure with Javascript. Since I have learned about adding .bind(this to callback functions, I find I am doing it ...
user avatar
3 votes
7 answers
335 views

Problem with runaway number of properties

Rewritten Question I appreciate the feedback and in response to that I'm re-writing my question. I can't give my specific situation (classes, etc), nor do I think that it would be helpful, as I work ...
user avatar
  • 149
1 vote
3 answers
379 views

Feature Envy vs. Single Responsibility Principle: Data class and a validator for it

Let's say I have a class that's a bit more than a pure Data object, i.e. it holds some data and some basic queries about that data. (In this case, it's a class representing a timetable) Now, I want ...
user avatar
  • 476
2 votes
1 answer
58 views

Should a Mover and Tracker be separate classes to maintain SRP?

I'm doing some Java code practice to better understand the Single Responsibility Principle. Currently, I'm working with a network of data structures, where each structure implements an interface ...
user avatar
2 votes
0 answers
111 views

Code quality balancing with need to perform scientific analysis on your own software

We have a project that is meant to do signal processing. If all we had to do with the software was that one job, we would have a pretty clean architecture. Unfortunately, we've run into an issue most ...
user avatar
  • 948
-1 votes
2 answers
65 views

How to set object parameters

I am rewriting a lexer class that has several "parameters" like whitespaceSplit. So it goes this way: lexer = new Lexer(); lexer.whitespaceSplit = true; lexer.otherParameter = ...; lexer.parse(file);...
user avatar
  • 722
31 votes
9 answers
28k views

Is there a better way to use C# dictionaries than TryGetValue?

I find myself often looking up questions online, and many solutions include dictionaries. However, whenever I try to implement them, I get this horrible reek in my code. For example every time I want ...
user avatar
  • 1,477
0 votes
1 answer
128 views

Is a "concrete function" over "generic function" a good implementation?

I want to know others opinion about this approach that I use in a lot of projects, for example, I can show the last time that I use it: public class DataStorageService { private enum Keys { ...
user avatar
  • 369
0 votes
1 answer
225 views

use always get and set methods is a bad practique, is call directly an attribute class a bad practique in OOP?

I have a doubt about if there are some recommendations for call directly an attribute in a class I think that in OOP you always should call an attribute by the get method. For example: On set ...
user avatar
  • 369
-1 votes
5 answers
263 views

How can I efficiently write a complex if or switch condition that depends on a combination of two (or more) variables? [duplicate]

In my current case, I've got two variables. Let's call them action and status for ease of use. They can have any value between 1 and 9. Depending on business logic, I've to either call subActionA() or ...
user avatar
10 votes
4 answers
2k views

is this way of calling a function a bad practice?

I have the following code: public void moveCameraTo(Location location){ moveCameraTo(location.getLatitude(), location.getLongitude()); } public void moveCameraTo(double latitude, double ...
user avatar
  • 369
0 votes
1 answer
520 views

Two reversed dictionaries or one dictionary with key and value lookup?

I have situation where in the Repository class I have a dictionary: Dictionary<TableName, Dictionary<EntityColumnName, SourceColumnName> map1 = new Dictionary<TableName, Dictionary<...
user avatar
  • 219
2 votes
4 answers
128 views

Date comparison with the last second of a day

Would you consider WHERE SomeDate <= '2018-01-01 23:59:59' instead of WHERE SomeDate < '2018-01-02' When the intention is that SomeDate goes no further then 2018-01-01 a code smell and if ...
user avatar
  • 129
0 votes
5 answers
1k views

Is using the Pair class a sign of primitive obsession code smell?

Let's say I use a Pair in this way: Pair<Long, Date> signup = getSignup(); System.out.println("User with ID " + signup.getLeft() + " signed up on " + signup.getRight()); Is it a form of ...
user avatar
  • 111
5 votes
2 answers
408 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 ...
user avatar
  • 948
3 votes
1 answer
817 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 ...
user avatar
-1 votes
1 answer
78 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 = ...
user avatar
  • 2,412
1 vote
1 answer
173 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 ...
user avatar
-1 votes
1 answer
130 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 ...
user avatar
2 votes
2 answers
124 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 ...
user avatar
0 votes
2 answers
350 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 ...
user avatar
  • 201
3 votes
5 answers
462 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 ...
user avatar
0 votes
2 answers
142 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, ...
user avatar
  • 1,317
16 votes
2 answers
12k 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() ...
user avatar
4 votes
1 answer
6k 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 ...
user avatar
  • 1,453
2 votes
4 answers
863 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 ...
user avatar
1 vote
1 answer
121 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 ...
user avatar
3 votes
4 answers
821 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 ...
user avatar
  • 149
4 votes
2 answers
560 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 ...
user avatar
  • 51
1 vote
2 answers
1k 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 ...
user avatar
  • 141
7 votes
1 answer
4k 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 ...
user avatar
  • 2,520
3 votes
3 answers
316 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 ...
user avatar
3 votes
1 answer
1k 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 ...
user avatar
  • 948
0 votes
4 answers
2k 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 ...
user avatar
-1 votes
1 answer
201 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 ...
user avatar
1 vote
1 answer
2k 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 ...
user avatar
  • 139
0 votes
2 answers
299 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): ...
user avatar
  • 267
9 votes
1 answer
6k 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 ...
user avatar
  • 253
6 votes
4 answers
8k 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 ...
user avatar
  • 63