Questions tagged [code-quality]

Questions for best practices for writing high quality code.

Filter by
Sorted by
Tagged with
-2
votes
0answers
32 views

Using DTO in service works fine with APIs but it does not get along when using on another service or component

I was thinking about good design to follow when doing service layer and found a lot of talks and recommendation about encapsulating the domain in the service layer and use DTO objects instead for ...
4
votes
2answers
386 views

Am I Over-Engineering if I add some code for the unknown future? [duplicate]

Look at this code: Console.WriteLine(user.name); Console.WriteLine("**********"); // 10 stars //... Tomorrow, I needed to use 20 stars instead 10 stars. Okay I would change it: Console.WriteLine(...
0
votes
3answers
307 views

Code Design: Repeatedly doing the same Boolean check with Stream Writer

I have several areas in a program where I am doing the following check on the same two booleans, but each spot has different text being written to a file via Stream Writer based on the value of the ...
0
votes
1answer
83 views

What's the better way to organize retrieval of difference in props

I have a system, which stores different objects in DB. Each such object has properties. Some of them are relevant for all objects (like Name, Description), but some are specific to each object and ...
1
vote
2answers
158 views

Game Engine Architecture: what am I doing wrong?

I'm working on a Game Engine (more like a Graphics Engine for Physics simulations) in C++. I know a bit of Unity and I like the concept of GameObject and Component Unity has and I wanted to use that ...
2
votes
1answer
124 views

Law of Demeter helper utilities

Do helper utilities like the one below violate the spirit of the Law of Demeter? It doesn't technically violate the law, since the doOperate method doesn't invoke C's methods itself. If so is there a ...
41
votes
6answers
6k views

What's the best way to handle refactoring a big file?

I'm currently working on a bigger project which unfortunately has some files where software quality guidelines where not always followed. This includes big files (read 2000-4000 lines) which clearly ...
0
votes
1answer
101 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 { ...
3
votes
2answers
141 views

How to improve code quality in database driven applications

I'm working for a company with a big ass legacy codebase. Changing existing code is like a guarantee to break things. Most functions are a few thousand lines, using some global flags, having different ...
0
votes
0answers
113 views

How to handle error responses in a chain of CompletableFutures?

I have a long chain of completable futures in my project, with each step calling a backend API, which can give multiple error responses and one success response. Now, after parsing the response, I ...
1
vote
2answers
201 views

How to find already existing code | How to arrange code in a way it can be found again [closed]

I'm refactoring the framework of our company, trying to fix the issues we had in the past. We're a team of 6 developers, and we have various needs and issues in regards to tidying up our framework. ...
1
vote
2answers
216 views

Are CCQ (Continuous Code Quality) tools like SonarQube expected to deny version control changes? [closed]

I am getting into SonarQube and everything looks quite and simple so far, but I am not sure what is the final purpose of CCQ overall. Yes it gives you a lot of tips about whats going on inside your ...
-3
votes
1answer
115 views

Is throwing exceptions in Debug mode a bad idea? [closed]

I am working in an iOS project which have two schemes enabled from configurations, Release Debug As you know Debug scheme is used for developers while developing features and testing etc. However, ...
0
votes
4answers
466 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 ...
1
vote
1answer
206 views

How to remove code duplication in Web API versioning

I'm working with ASP.NET core web API project, where I have 8 controllers, multiple DTOs being used for request and response and mappers to map models(also used in the web app) with DTOs. Recently, ...
0
votes
1answer
81 views

Is it better to initialize semaphores in shared memory or give them as command line arguments? (C++)

I am on a project for which I have to spawn many different processes, all in different executables, and there will be some shared memory segments and some semaphores to synchronize them. I use an "...
2
votes
3answers
298 views

Workaround for no methods in namespace in C#

I am new to C# and have a difficulty with the code structure. My application has: GUI with button Show my Ethernet IP. When user clicks this button PC's IP address is displayed. Class ...
3
votes
3answers
145 views

One try/catch for each error, or one global try catch and switch error?

I have multiple methods that could fail, let's say : readFile() parseFile() compute() Each of them could fail (e.g file not found, wrong file type, wrong data), should I put each of them in their ...
33
votes
7answers
6k views

Should you use a library when you can do the task without it? [closed]

I am in a situation where I can use an open source JavaScript plugin to fulfill a task. But when I tried to use it,I found myself I have to redesign lot of things of what I already have done,and it ...
14
votes
6answers
2k views

Clean Code - Should I change the literal 1 to a constant?

To avoid magic numbers, we often hear that we should give a literal a meaningful name. Such as: //THIS CODE COMES FROM THE CLEAN CODE BOOK for (int j = 0; j < 34; j++) { s += (t[j] * 4) / 5; } ...
0
votes
3answers
192 views

Too much logic in a nested catch block

My lead dev complained that I have too much logic in a nested catch block. My code goes something like this: try { // some setup and a network call } catch (CustomEx ex) { try { ...
0
votes
1answer
66 views

Doing a membership check on a container vs having multiple ifs?

What is better? if ("a".equals(x) || "b".equals(x)) { //do something } vs private static List<String> validAlphabets = ImmutableList.of("a", "b"); if (validAlphabets.contains(x)) { //...
2
votes
1answer
225 views

Alternative for commented out code that will be used later

I had to implement a module that after completed, the client suddenly asked that for a while it would not need to be displayed, so I was told to comment it out (since we probably will use it later) ...
2
votes
6answers
2k views

How to avoid code duplication in if else condition?

How can I avoid duplicate use of doSomethingCommon() in the following block of code? doSomething(); if (conditionA) { doSomethingSpecificToConditionA(); doSomethingCommon(); } else if (...
1
vote
3answers
240 views

Automated test “all changes are feature-flagged”, possible?

Let's say in an effort to improve quality, your team agrees on the following policy: all commits to the master branch (direct or via pull request) have to be categorized as either bugfixes or ...
13
votes
3answers
1k views

What are the barriers that prevent widespread adoption of formal methods? [closed]

Formal methods can be used to specify, prove and generate code for an application. This is less prone to errors - thus mostly used in safety/critical programs. Why don't we use it more often for ...
13
votes
5answers
1k views

Getting ALL developers do code reviews

I'm a software developer in a 7-8 developers team. We've been doing code reviews for quite some time now and code quality has improved over time. However I recently noticed that some developers are ...
80
votes
9answers
17k views

How do huge open source libraries get maintained while having code far from “clean code” practices?

I'm still inexperienced to write high quality code, so I read books addressing the issue such as Clean Code by Robert C. Martin, and keep checking code of well-known libraries to improve my skills. ...
1
vote
0answers
32 views

Applying an overloaded/templated function depending on the type of a QVariant

I'm trying to take a QVariant (in this case from a QProperty generated on a class) and based on the type, return a QWidget that allows for it to be edited appropriately. a signature for the ...
1
vote
4answers
490 views

Code Coverage and Defensive Programming (in private functions)

Assuming you want to build automated tests for the following (very simple, if odd) class. // Calculates the distance between neighboring values in a vector // and provides functions to return the ...
6
votes
4answers
1k views

Is this using macros to define classes that fit a pattern in C++ a sound idea?

I've got a set of classes that all inherit from a base class that are responsible for different functions (sort of like a group of "operators") They all work on the same input and output the same ...
5
votes
5answers
257 views

How does one resolve low level types dependent on high level types?

I frequently come across this problem when developing games and simulations. As an example, I'm currently writing an implementation of chess. I'm using it as a vehicle to experiment with entity-...
3
votes
1answer
207 views

Reducing Redundant Calculations

I am experiencing a dilemma. Let me explain it with two functions: # Takes vector.magnitude(), vector.x, vector.y and uses it somewhere def func1(vector, {other parameters 1}): pass # Takes ...
10
votes
4answers
656 views

Good code style to introduce data checks everywhere?

I have a project that is sufficiently large in size that I can't keep every aspect in my head any more. I'm dealing with a number of classes and functions in it, and I'm passing data around. With ...
4
votes
1answer
174 views

Split ORM module without creating cyclic imports or side-effects

Preface I have an ORM library for a relational real estate database. The used framework is peewee. The library contains ~60 models, each representing a distinct table. The models represent a (pre-...
2
votes
0answers
138 views

What is the best way to manage a class with lots of properties?

We have one table meta where there is key and value so each of the row represents one component or service and then we have api system which manages the metas database. And it was going pretty well ...
4
votes
5answers
449 views

Is it a bad practice to use conditionals with functions that change program's state?

The title might be a bit vague, so let me explain. Let's assume we have a function that does something (changes state of the program), for example a function that creates a file. That function returns ...
1
vote
3answers
447 views

Does code coverage make sense when running end-to-end tests?

I have a situation where I am running mostly end-to-end tests on my framework: I start of with simple fixtures (which are CSV-files). These are imported by my application (which I consider to be a ...
-1
votes
3answers
133 views

How to build your class in a readable way [closed]

When developping a project I follow the OOP principles and break my code into classes and most of the time I go for one file = one class. Now I dont know how to organize my code into the class and ...
68
votes
12answers
19k views

Prevent deprecated code from compiling after reaching a deadline [closed]

In my team we have been cleaning a lot of old stuff in a big monolithic project (whole classes, methods, etc.). During that cleaning tasks I was wondering if there is a kind of annotation or library ...
0
votes
2answers
2k views

changing the value of String object in java

I have to set the value of a string object "result" depending on the results of different methods and different if/else conditions. In the end, there would be one (last value set) in the string that i ...
3
votes
3answers
199 views

Best practice to avoid partial branches

I do a lot of coding in python and got a lot of if conditions without an else statement so to say partial branches. E.g.: # if a certain kwarg was passed to a function call if kwargs.get('a_option'):...
2
votes
3answers
138 views

Using methods that are not under test within a unit test for a different method?

I'm building a Sudoku generator. I have a board class with a number of methods: public class Board { public Board() { /* Creates an empty board */ } public bool ValidateRow(int row) { /* ...
6
votes
1answer
1k 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 ...
4
votes
4answers
780 views

Should I assert the preconditions of functions in a public API?

I am writing a library for some data structures in C that will be used in embedded systems. I have had issues designing and coming up with a solid error handling plan. This API is only subject to ...
26
votes
7answers
4k views

Split the calculation of return value and the return statement in one-line methods?

I have had a discussion with a coworker about breaking a return statement and the statement that calculates the return value in two lines. For example private string GetFormattedValue() { var ...
25
votes
4answers
1k views

How to handle a TODO in a pull request?

When I review the changes in a pull request, I sometimes stumble upon a comment with a "TODO" note which may be there for different reasons, in our case mostly because of: the solution used to solve ...
3
votes
1answer
246 views

What motivates syntactic preference regarding pointer declaration in C/C++? [duplicate]

Both of the following are valid pointer declarations in C/C++: int *x; int* x; The former seems to be preferred by seasoned C/C++ programmers. I personally find the latter to be easier to understand ...
2
votes
2answers
273 views

Highlighting importance of order when using short-circuited conditions

I was working on a piece of code when I noticed that an if statement could work or crash depending on the order used for the parts connected with and. You can replicate the problem like this: ...
4
votes
1answer
151 views

Object design principle which describes interface symmetry

Someone made a comment about some of my code which I thought was valuable, and I wanted to look up the design pattern to learn more, but I'm having a hard time identifying which design principle it ...