Questions for best practices for writing high quality code.
29
votes
8answers
4k views
How to argue against lowering quality standards for legacy codebase? [closed]
We have here a large legacy code base with bad code you can't imagine.
We defined now some quality standards and want to get those fulfilled in either completely new codebase, but also if you touch ...
2
votes
3answers
216 views
Is Professional Code Review/Mentoring Offered? [closed]
I'm a newer developer who has worked on some personal projects as well as non-profit/charity projects. However, I seem to be the most "senior" developer in my circle, meaning, most guys come to me for ...
91
votes
14answers
10k views
At what point is brevity no longer a virtue?
A recent bug fix required me to go over code written by other team members, where I found this (it's C#):
return (decimal)CostIn > 0 && CostOut > 0 ? (((decimal)CostOut - (decimal)...
9
votes
2answers
264 views
Application service layer calling database functions. Bad architecture?
Scenario:
Stack: Java, Spring, Hibernate.
Model: Client-Server Application.
Pattern: Model-View-Controller (MVC).
The Service Layer classes has three behaviors:
Some services have the business ...
6
votes
6answers
789 views
Calling the next method from a previous one… Why is this bad design?
If createWorld() is really long and I need to split it, I can split it to createLight(), createEarth(), createPlants(), and createAnimals().
So, naturally I do:
function createLight(){
//work 1
...
3
votes
2answers
132 views
How to establish set of rules when several teams managing a single code base
We have several teams in our section of the organization and each will be give features of the system to develop and deploy.
A feature includes back-end work as well as front-end work. All the teams ...
2
votes
1answer
236 views
How to reduce “mapping” code?
I have two components, both are a parser/builder pair. So component A can parse A and build A, component B can parse B and build B.
A and B both contain different entities, which are extracted by the ...
12
votes
6answers
2k views
Does Code Coverage improve code quality? [duplicate]
I am curious whether there are metrics on whether code coverage actually improves code quality? Any research studies?
If so, at what percent does it become a case of diminishing returns?
If not, why ...
5
votes
5answers
178 views
In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?
In my day to day programming, I tend to use very few pointers, not only because I want to keep my code simple and error free, but because I assume that the programming that I do does not have any ...
4
votes
5answers
273 views
Should a Match keep a list of Players or should a Player keep a reference to its Match?
In the game I am working on there is a Match object and a Player object.
The game is divided into different Matches (basically a lobby). Every Player needs to be in a Match but cannot be in several ...
6
votes
9answers
888 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 ...
3
votes
1answer
343 views
Why are pure functions easier to reason about?
In computer programming (I code in c#), why are pure functions easier to reason about?
From my own experience, I find that pure functions are easier to reason about because they lack side effects, ...
3
votes
1answer
69 views
Pass Objects or values as parameters to functions
I'm working with JEE standard. I have the following layers: JPA (Eclipse Link), Data Access, Business Logic, and JSF (Primefaces). Primefaces uses MVC design pattern, so the the presentation layer ...
0
votes
2answers
145 views
SonarQube is complaining about: “Use isEmpty() to check whether the collection is empty or not.”
So as my the title says, SonarQube is complaining whenever you use
list.size() == 0
or
list.size > 0
However I started changing to isEmpty() and !is.Empty() and noticed the code becomes way ...
18
votes
7answers
736 views
How do you know if software is good or bad based upon empirical metrics?
I'm currently being asked to look at a project that has finished core development five months ago, but still has a high level of defects. What transpires is for around every 10 defects fixed, we raise ...
1
vote
0answers
42 views
What are the boundaries of the term 'Consistency' in the context of system's design?
In many books on code style or design of the software systems authors often stress out the importance of being consistent, yet they do not give a precise and clear definition for it.
Now I need that ...
2
votes
3answers
193 views
Prefer self documenting code over cyclomatic complexity?
I'm interested in which approach to prefer. Consider some code which retrieves a translation for some text. It has to respect these constraints:
Return translation only if the text will be used on a ...
-3
votes
2answers
214 views
What is well written code? [closed]
Is code that runs fast but written with a bad and hard to understand syntax, good code?
Is code that runs slowly but written with a good and easy to understand syntax, good code?
2
votes
1answer
145 views
Do extracting piece of logic to function improve or reduce code readability?
If there is some code that needs to be implemented across multiple controllers (lets say, 10), e.g.:
// Inside a controller function
var myField = null;
var response = service.callBackend(function(){...
0
votes
1answer
87 views
Should I always use prefix private methods with an underscore in Python?
Prefixing methods and members with an underscore indicates internal use. For simple classes, I sometimes find the easier reading and typing of self.foo outweighing the indent of self._foo. Especially ...
2
votes
2answers
85 views
How to avoid code duplication or cascaded changes and major refactoring in business logic layer?
Let's say, I have created a business logic layer service (or a handler in Command/Query pattern) with a method DoSomething which processes a bunch of entities and stores them in database.
Later on, ...
5
votes
2answers
152 views
Create a new variable to shorten code
I have often wondered about the implications of shortening code by assigning temporary variables with shorter names to data accesses with long names. It's best illustrated by an example (in Ruby, but ...
19
votes
7answers
838 views
Peer / Code Review Frustrations
I wouldn't call myself a superstar dev, but a relatively experienced one. I try to keep code quality to a high level, and am always looking to make improvements to my coding style, try to make code ...
167
votes
20answers
21k views
A large part of my code has a major design flaw. Finish it off or fix it now?
I am a high school student working on a C# project with a friend of mine with about the same skill level as me. So far, we have written roughly 3,000 lines of code and 250 lines of test code in a span ...
5
votes
3answers
422 views
Is nesting try-catch statements still a code smell if nested within a loop?
I have heard that nesting try-catch statements can often be a code smell, so I'm wondering whether this situation is an exception. If not, what would be some good ways to refactor?
My code looks like ...
0
votes
1answer
47 views
Static analysis criteria different for various parts of solution
We have quite a large project where and tend to apply risk based approach to stringency of unit tests and code reviews. E.g. components classified as A need to have higher coverage than components ...
-2
votes
1answer
166 views
Duplicate code to avoid performance decrease due to function calls? [closed]
I have a simple list in Java that I need to search using one of two methods. The first method simply returns the position in the list of the first matching element. The second filters out any elements ...
7
votes
4answers
253 views
Code design: Duplicate code or good implementation
I'm working on application in C#, where I need to serialize and deserialize some classes into/from XML. This operations will be implemented in Class Library. I chose XMLSerialization class to ...
1
vote
2answers
363 views
Get variable with accessor method or just use dot notation?
So, I recently noticed something on some code I was writing. I could get a variable for a different class/object using dot notation to get the variable:
object.someVarable or I could do it the way I ...
1
vote
1answer
159 views
how to measure defects per KLOC
I have been reading on the internet about the metric "number of bugs per 1000 lines of code" and what would be a good number.
However, I wonder how someone would compute such a metric? The reason a ...
0
votes
0answers
39 views
What is the common practice of calculating average length of identifiers
Quoted from Ian Sommerville's Software Engineering(9th ed), page 673:
This is a measure of the average length of identifiers (names for
variables, classes, methods, etc.) in a program. The longer ...
-2
votes
1answer
292 views
Is this unprofessional when “Wappalyzer” sees all my technology? [closed]
After I installed "Wappalyzer", extension wich display technology, wich site using. I checked many sites and in most of cool projects, like "Youtube", "Github","stackoverflow" etc, wappalyzer display ...
4
votes
1answer
272 views
Help in writing more generic code
I'm doing a php MVC project using code igniter. I have two models, a and b.
Each class contains four functions (insert, delete, update and view) and their implementations are almost the same with ...
6
votes
4answers
509 views
Is using nested function calls a bad thing?
In a recent homework assignment I ended up calling my functions in an ugly way uglyReceipt(cashParser(cashInput())) the program itself worked perfectly but I still felt like I was doing something ...
58
votes
5answers
7k views
Why are large amounts of magic numbers acceptable in CSS and SVGs?
Often times I see questions on the Hot Network Questions list like this that basically ask "how do I draw this arbitrary shape in CSS". Invariably the answer is a couple of blocks of CSS or SVG data ...
5
votes
2answers
281 views
Define “Refactoring”
Everyone has heard this term thrown around, and most people, I would imagine, have a conceptual idea of the meaning. I, myself, am in that latter group. However, I feel that the definition Google/...
104
votes
16answers
19k views
Should I add redundant code now just in case it may be needed in the future?
Rightly or wrongly, I'm currently of the belief that I should always try to make my code as robust as possible, even if this means adding in redundant code / checks that I know won't be of any use ...
2
votes
3answers
135 views
What is the difference between Static code analysis and code quality?
I need to know whether Static code analysis and code Quality are same or not because, I need to find out the tools for code quality and each time I google for it, I'm getting response for the tools ...
5
votes
2answers
608 views
The concept of “quality gates” in software testing
We are using SonarQube for code quality testing. It tests the quality of code, and not the function of code. It has the concept of quality gates, so you can set for instance a 90% quality gate, ...
2
votes
1answer
225 views
How to respond to bizarre requests in code reviews? [closed]
I think that code reviews are great and very helpful for everyone. With that being said, from time to time I've received feedback on a pull request (leading to the PR being delayed for at least as ...
3
votes
1answer
73 views
What makes design by contract a tool?
Design by contract is based on Hoare logic where a proof of correctness of a program is established by reasoning over pre/post conditions and invariants.
What are the means and prerequisites of a ...
1
vote
1answer
198 views
Is defining only one method against S in S.O.L.I.D
The first version of pseudo code I consider a code smell because of (I think) the Command Query Separation principle and/or the S in S.O.L.I.D. What I like to see is in the 2nd version.
Should I keep ...
3
votes
1answer
344 views
Bad c++ code design? [closed]
This is bothering me a long time. I feel like I am doing mistakes on the code design relative to the performance. I never had any teacher to indicate my mistakes so its hard to me to make it right in ...
6
votes
3answers
1k views
Is break a code smell?
I'm asking in terms of a loop, obviously break is important in switch statements. Whether or not switch statements themselves are code smells is a separate issue.
So consider the following use cases ...
1
vote
2answers
119 views
Does an interface including several methods that return instances of Object make sense?
I am in the process of writing my first true API. In the process, I am defining an interface for mapping complex data structures onto other complex data structures.
At the moment, the interface ...
28
votes
8answers
6k views
Is it acceptable to copy and paste long but straightforward code instead of wrapping them into a class or function?
Suppose I have a segment of code to connect to internet and show connection results like it:
HttpRequest* httpRequest=new HttpRequest();
httpRequest->setUrl("(some domain .com)");
httpRequest->...
98
votes
13answers
17k views
Should we avoid language features that C++ has but Java doesn't?
Suppose I am limited to use C++ by the environment in the project. Is it good to prevent the use of some language features that C++ has but Java doesn't have (e.g.: multiple inheritance, operator ...
1
vote
0answers
389 views
Can this be used to implement Post Redirect Get pattern?
I am trying to implement proper a Post Redirect Get on a PHP site (question is language agnostic in nature however). I thought about it, and realized that running this code on every request seems to ...
0
votes
3answers
255 views
Best practices in exposing interface
Let's assume I have a class that downloads data from API, cleans it and saves to database. What methods should I expose?
class ApiConnector1
{
public string GetDataFromApi()
{
// ...
...
2
votes
1answer
177 views
WMC Calculation in CK Metrics Suite
I have two classes C1 and C2. C1 has 2 methods and C2 has 3 methods each of complexity value 1. C2 inherits from C1. So, I know C2 has 2+3=5 methods in all. The question is, should I take C2 to have 5 ...