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
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Questions for best practices for writing high quality code.

-2
votes
0answers
22 views

PyLint/PyFlakes from WITHIN a program? [on hold]

Odd situation is that I can't shell out to run a linter. Yet, I'd love to run one. So, I'm left with trying to invoke the linter from within the script I want to lint. Something like this: import ...
1
vote
6answers
478 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
155 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
242 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
722 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
155 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
370 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
205 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
141 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
612 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
0answers
82 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
98 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
434 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
143 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
119 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
17k 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
211 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
147 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
126 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
379 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
506 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
895 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
228 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
269 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
121 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
244 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
56 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
655 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
346 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
327 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
203 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
339 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
83 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
199 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
643 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
940 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
260 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 ...
4
votes
4answers
4k views

A DTO class having an ArrayList of its own type - is this considered a good design?

I came across a DTO class like the below one. class PersonDTO { private String firstName; private String middleName; private String lastName; private String dob; // some 50 fields ...
3
votes
3answers
659 views

Should OOP languages eliminate keyword 'protected' in order to force programmers to write clean and high quality codes?

I've seen some posts which has strong reason to avoid using protected: Why have private fields, isn't protected enough? Why is Clean Code suggesting avoiding protected variables? And there are ...
92
votes
13answers
15k views

How to deal with 'almost good' code from a junior developer? [closed]

I got a question about team managing. Right now I'm dealing with a junior developer who's working remotely from a coding factory. The guy is open to criticism and willing to learn, but I got some ...
2
votes
6answers
572 views

What are the techniques for keeping the code quality high when requirements change?

Whenever I write a piece of functionality from scratch, it's a good and robust code. But often times my assumptions about how the code should work evolve over time. After I reflect those changes in ...