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-quality]

Questions for best practices for writing high quality code.

30
votes
7answers
5k views

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

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 ...
8
votes
5answers
854 views

Clean Code - Should I change the variable 1 to constant?

To avoid magic numbers, we often hear that we should give a meaningful name for a variable. Such as: //THIS CODE COMES FROM THE CLEAN CODE BOOK for (int j = 0; j < 34; j++) { s += (t[j] * 4) / ...
0
votes
3answers
145 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
37 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
135 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
578 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
165 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 ...
4
votes
3answers
253 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
745 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 ...
78
votes
9answers
16k 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
25 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 ...
0
votes
2answers
173 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 ...
5
votes
4answers
386 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
217 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
151 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
620 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 ...
5
votes
1answer
108 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-...
1
vote
0answers
101 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
437 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
188 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
122 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 ...
67
votes
12answers
18k 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
592 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
150 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
129 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) { /* ...
4
votes
1answer
514 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
4answers
549 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 ...
23
votes
4answers
931 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
233 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
270 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
127 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 ...
110
votes
14answers
18k views

How can I maintain code quality without SCM?

I am working in a governmental institution. The technology being used here and the methods for developing software are quite old fashioned. They have tons of storage space but no appropriate space ...
0
votes
1answer
246 views

Shall I use low quality insecure software if I'm asked to?

I work for a finance startup managing the technical stuff. The COO decided to create a new web site using a contract developer and the owner of the company paid for it ($9k). I'm asked to use the new ...
1
vote
3answers
3k views

Are protected properties evil? [duplicate]

Introductory piece of code: class BaseClass { protected Foo MyFoo { get; } } class ChildClass : BaseClass { void SomeMethod() { MyFoo.DoStuff(); //Here, I have no idea ...
25
votes
9answers
5k views

How to communicate that insertion order matters in a map?

I'm fetching a set of tuples from the database, and putting it into a map. The database query is costly. There is no obvious natural ordering of the elements in the map, but insertion order matters ...
0
votes
1answer
72 views

Do all Get/SetAccess, Access, and Dependent property attribute combinations have valid use cases?

If You are Unfamiliar with OOP Matlab For those not familiar with matlab, matlab provides a variety of access attributes when creating member variables. All member variables are essentially C# ...
-1
votes
4answers
745 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 ...
54
votes
10answers
12k views

In what cases is less code not better? [closed]

I have refactored some code at work lately, and I thought I did a good job. I dropped 980 lines of code to 450 and halved the number of classes. When showing this to my colleagues some did not agree ...
2
votes
3answers
351 views

Is there a way to have four possible outcomes without repeating the two conditions? [duplicate]

In a generic sense, my question goes something like this: if (p) { if (q) { a(); } else { b(); } } else { if (q) { c(); } else { d(); } } ...
1
vote
1answer
150 views

How to write Readable Comment when we have to deal with IDs?

How to write Readable Comment when we have to deal with IDs? I will explain this with example. +---------------+-----------------+ | permission_id | permission_name | +---------------+--------------...
3
votes
1answer
407 views

Are there widely-accepted guidelines to write safe C? How do people learn that? [closed]

C often seems to be taught in terms of contracted examples without much regard to code safety, such as buffer overflows etc. I have often wondered if there are proven, totally safe ways of writing C ...
37
votes
8answers
7k views

Inverting an IF statement

So I've been programming for a few years now and recently have started using ReSharper more. One thing that ReSharper always suggests to me is to "invert 'if' statement to reduce nesting". Let's say ...
2
votes
2answers
207 views

Object Oriented Re-Design/Re-Write

Let me begin by stating I have already reviewed the following related questions, and just wanted to showcase the software architecture which I have inherited to see how opinions may vary. When is a ...
-3
votes
3answers
357 views

Writing quality code [closed]

I know there are many books on writing clean, modular, readable and whatever else you can say but frankly speaking I find it very hard to remember every advice given on those books. Frankly speaking ...
1
vote
1answer
88 views

Benefits for the developer of separating pull requests by logical change?

I am currently responsible for reviewing one of my team mate's code for a front end web app. We have established a workflow which involves creating a feature branch in git for each feature we define. ...
4
votes
1answer
201 views

How to make sure that each field's setter executes a specific code?

I am working with Java 1.7, IDE is Eclipse Indigo. I have a base class which has id as an int. It also has a boolean variable onlyIdInitialized; I am setting it to true when an object is created with ...
15
votes
5answers
669 views

What is a helpful mindset when conducting a formal code review

Our team has recently started conducting code reviews against each checkin. As the team lead I'm trying to find a balance between providing too many suggestions, annoying developers and decreasing ...
10
votes
4answers
944 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 / ...
5
votes
3answers
262 views

How to avoid falling into the trap of not commenting code? [duplicate]

I've been working on a project for about a month now. I am extremely familiar with the code and understand it to the extent where I feel that it is so easy to understand, that most methods don't need ...