Questions tagged [coding-style]
Coding style is a set of guidelines that helps readability and understanding of the source code.
1,056
questions
14
votes
5
answers
5k
views
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
According to Explanation on how "Tell, Don't Ask" is considered good OO, I know the following is bad:
if(a.isX){
a.doY();
}
public class A{
public boolean isX;
public void ...
0
votes
4
answers
331
views
Code quality: expressiveness vs. conciseness
I've been wondering about code expressiveness vs. conciseness lately. What I mean by that is that a lot of modern programming languages offer features to express statements in a very short manner, ...
1
vote
2
answers
152
views
What is an appropriate length for function names? [closed]
I'm writing a C++ class, CBookSpellDef which has the following methods:
int GetIndexOf(const char *psz);
char* GetNameAtIndex(int index) { return m_spellTypes[index].szName; }
char* ...
24
votes
11
answers
8k
views
Is it logical to not use inheritance because the function is critical?
Our codebase has a typical base-class with a ton of sub-classes. The base-class already has many default functions for the sub-classes.
However, one particular function has the same verbatim ...
0
votes
1
answer
133
views
How to deal with very complex codebase? [duplicate]
I recently changed my job to a big MNC and the code I am exposed to is highly complicated, difficult to read and understand. Although it is divided in microservices and runs on local I have to keep ...
17
votes
8
answers
7k
views
How to be (more) critical during Code Reviews? [closed]
I'm a Software Engineer who sometimes need to review the code of my fellow team members. I often look at the source code and think; this looks fine. I'm having a hard time to think critical about it. ...
-2
votes
3
answers
394
views
Common practice where to place "is" word while naming predicate function: at the beginning or in the middle?
There's a lot of free or member predicate-like functions (that returns boolean value) in different programming languages and popular libraries/frameworks that have "is" as a prefix, e.g.:
...
3
votes
2
answers
274
views
When to prefer print over logging?
Generally print statements are frowned upon in favor to logging. But are there any situations where I should prefer using print statements?
In a interactive command line application, if I ask for user ...
13
votes
3
answers
2k
views
Using `any` to indicate a wildcard value
I'm writing a validator class to validate certain request objects against a known format. Rule declarations and the validator will both be written entirely in Python, and I don't need to store the ...
0
votes
0
answers
52
views
Are type, constant/static/global, and pointer prefixes still necessary in C coding standards, considering the capabilities of modern IDEs? [duplicate]
Our coding standards for C include various prefixes for data types, constants, static/global variables, and pointers. These prefixes were originally introduced for code review purposes, but with the ...
2
votes
1
answer
116
views
Declaring code style settings in a Java project
In a Java open source project built with Maven, is there a standard way to declare code style settings (such as indentation settings and import order)? I would like that people who import the project ...
21
votes
4
answers
8k
views
In C, if I am passing a file descriptor to a function, should I close it in the function or in the place where I called the function?
I have a situation like the following:
int fds[2];
pipe(fds);
// Write lots of stuff into fds[1]
close(fds[1]);
do_stuff(fds[0]);
// Should I close fds[0] here or at the end of do_stuff?
Which one ...
0
votes
1
answer
120
views
Is this way of programming related to DDD?
In my new job, I'm getting a hard time understanding how they want to model things... they are using Domain Driven Design. For example, I come across this kind of code:
$userRepo = new UserRepository($...
0
votes
4
answers
289
views
Are "easier to search for the interface" and "avoid yo-yo to interface to find implementations to fix bugs" reasons to add prefix "I" on interfaces?
According to Should interface names begin with an "I" prefix?, I should not add prefix "I" to interfaces. However, I think prefix "I" in interface may be useful sometimes....
24
votes
6
answers
8k
views
Best practice for redundant conditions in if-elif-else statements
What is considered better practice?
Case 1:
if n == 0:
doThis()
elif n < 0:
doThat()
elif n > 0:
doSomethingElse()
Case 2:
if n == 0:
doThis()
elif n < 0:
doThat()
else:
...
0
votes
2
answers
193
views
For non-container classes, are "better naming" and "ready for commented codes" good reasons not to declare the most abstract type?
According to Why define a Java object using interface (e.g. Map) rather than implementation (HashMap), I know I should declare the most abstract type possible, but the question is about template class ...
5
votes
4
answers
3k
views
Are "need to call objects in parent object" and "avoid circular dependency" reasons to avoid "Tell, don't ask"?
According to Explanation on how "Tell, Don't Ask" is considered good OO, I know I should avoid get the state of an object and then decides the actions to take to that object, eg:
Bad:
...
2
votes
3
answers
202
views
Is it necessary or "class obsession" (opposite to primitive obsession) to create classes for non-business fields?
I know there are some posts talk about primitive obsession : When is primitive obsession not a code smell?, Is "avoid the yo-yo problem" a valid reason to allow the "primitive obsession&...
18
votes
6
answers
4k
views
Is setRGBColor(32,128,192) a valid use of magic number?
According to When is a number a magic number?, I know magic number should not exists, so I know the following code about creating a Label from some UI framework and set the color RGB needs to be ...
1
vote
1
answer
189
views
Should I separate special cases from for loop deliberately, or let a for loop handles special cases naturally?
Suppose I need to find the last selected index of options according to last selected contents, eg: options : A,B,C,D,E ,last selected option: C, so the last selected index is 2.
There is a special ...
6
votes
7
answers
7k
views
How to avoid repeating "a==b" when comparing "condition a" and then "condition b" and then...?
For example, I have an object:
public class Obj{
public int a;
public float b;
public String c;
}
I need to find best "Obj": largest a and then largest b and then longest c:
int ...
0
votes
1
answer
73
views
Practical Advice for rapidly changing code maintainability [closed]
This question may get closed quickly, but I'll appreciate any advice I can get and all the resources I can find online about this topic aren't quite relevant to my case.
I am a Math PhD student doing ...
1
vote
3
answers
587
views
Is "avoid misuse in other languages" a valid reason to avoid myString=="abc" in c++?
For example, I know in c++, I can use
myString=="abc"
to check if 2 strings are equal. However, in Java, it is comparing if 2 objects are the same object. Also in other language, for ...
4
votes
1
answer
295
views
Dealing with global variables required by badly-written library
I am working with a library that is somewhat poorly written. In order to function, it requires several global variables to be declared and sometimes even maintained by my own code. I really don't ...
1
vote
3
answers
200
views
To enforce column limits on long strings? [closed]
We're trying to update our style guide (using google's guide as a starting point) and I'm currently in the middle of a debate with my colleagues about column limits. I believe we're all in agreement ...
1
vote
1
answer
104
views
Is module scoped initialisation considered a bad practice?
A module app.js includes another module service.js. The service.js module/file contains some functions but also does some module scoped initialisations(registry variable).
// service.js
const ...
0
votes
2
answers
98
views
Is usage of "with" prefix acceptable for filter methods that are specific(not generic) [closed]
I am not a native english speaker so I have the following question related to naming I have a class that has generic filter method of type:
filter(Predicate predicate)
In the same class I have some ...
0
votes
4
answers
198
views
Should we create a new function for every multiple conditions?
I have the existing code provided below:
if (!$existing_records && !$has_required_field) {
return 'Skip Update, no records found';
} elseif (!$existing_records && $...
18
votes
6
answers
6k
views
How do you manage your own comments on a foreign codebase?
A common scenario I have is this:
I download a new codebase. In order to have me understand the code, I need to litter it with my own comments about what each section of code does.
It seems ...
15
votes
6
answers
6k
views
Which language's style guidelines should be used when writing code that is supposed to be called from another language?
This is just a simple code-style question. Imagine you're writing a function in language 1 that is supposed to be called from language 2. Should the function follow language 1's style guidelines or ...
10
votes
8
answers
1k
views
When are try/exceptions not an anti-pattern?
I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:
resp = fetch(URL)
if resp.status_code != 200:
return ...
1
vote
1
answer
135
views
Silently re-using data vs requiring the user to pass a "re-use" argument?
Context
After creating a (bash) CLI interface that creates self-signed SSL certificates (for onion domains), the user can choose to re-use some of the data that is created in past runs. For example, a ...
0
votes
2
answers
149
views
Where to put the code that searches objects in a complex hierarchy?
Given the following hierarchy of objects: a keyed collection of ClassA objects, where each ClassA object contains a keyed collection of ClassB objects and each ClassB object contains a keyed ...
-1
votes
1
answer
165
views
What is point of having certain microservices making another API call in the same service
I was going through an old, largely untouched part in my company’s codebase and found one API. It does the following things.
A POST API with path host/entities/trigger which fetches a list of entity ...
39
votes
6
answers
10k
views
What's the correct way to do pair programming?
We've been utilizing pair programming (or something like it) for a few years. As a senior engineer on the team - I find that pairing actually negatively impacts the team's throughput.
The common ...
0
votes
2
answers
301
views
Is there some easy way to refactor deeply coupled python code
I recently took on a long ago python project which has some weird code style that I can't pinpoint. e.g.
# this is a params and value package?
opts={
infile="xxx",
outfile="xxx"
}
...
2
votes
1
answer
396
views
Does "happy path to the left edge" break Python conventions?
I found the short article Align the happy path to the left edge quite helpful in improving readability of functions. Briefly, reading down the left edge of the function should step you through the ...
8
votes
4
answers
815
views
Are there advantages to using an interface, rather than a class, for a Java application?
Java is often (rightly IMHO criticized) for overusing the class keyword, as it can denote:
a factory to instantiate objects (traditional classes)
a collection of global methods (when all methods are ...
3
votes
0
answers
150
views
Forgetting about the utils [closed]
Let's say my language's standard library does not include TrickyFunction(). Since implementation seems quite trivial for me, I decide to create utils in the project and add such function, for others ...
4
votes
5
answers
714
views
Why are vertical and horizontal spacing heavily used in coding styles?
Why would so many repositories use additional — apparently unnecessary — vertical and horizontal space in coding styles?
I almost unanimously see this:
public function getName() {
...
0
votes
2
answers
311
views
C# coding style, functional approach
I have thought of this for a while and I want to know what you think about this.
This is more of a way to structure the code that might not be 100% object oriented and that is not the purpose. I would ...
2
votes
2
answers
224
views
Should I abstract function calls with duplicate arguments?
I have an intuition, that I'd like to read what others have talked about. To me, it seems fairly intuitive that when you have a function which is called multiple times with the same arguments, it ...
3
votes
4
answers
630
views
Is the combination of state machine and pure function a programming style?
As a university student who just have been learning programming for a year. After I learned about the concept of state machine and the pure function in functional programming, I suddenly got an idea ...
0
votes
2
answers
799
views
Reassign parameter to local variable
On Stack Overflow I frequently see questions with code in the following style:
function funcName(parameter) {
let variable = parameter;
// rest of function uses variable rather than parameter
}
...
-1
votes
2
answers
133
views
Any established convention on symbols to use as multi-level separators? [closed]
Let’s say that we want to encode recipes or dishes for x days in the form of a string. There can be an arbitrary number of days. Each day, we can have 0–3 of the following: breakfast, lunch, dinner. ...
36
votes
11
answers
9k
views
Best practices: Clarity vs. confidence in code behavior
When reviewing code, I sometimes see blocks like this (I happen to be using a JavaScript since it is widely understood):
if (!myVar || myVar === null || myVar === undefined) {
...
}
Those who know ...
8
votes
5
answers
3k
views
Should an optional boolean function argument rather default to true or false?
In many cases I can formulate some boolean parameter equally well positively as negatively, e.g. isOn or isOff. Which one should I pick then, in case I want the argument to have a default value? Is ...
4
votes
5
answers
2k
views
Alternatives to if-else on data reading
I have some code on Arduino (so, written in C++) that receives a String through the UART terminal, reads the String, then decides what String to print back and how many things to print depending on ...
0
votes
1
answer
105
views
Should a class be an attribute for another class if all I need is the data from it?
Title sucks, I know, but I'm having trouble describing my question.
Say I have a class Three_Eyed_Robot that sees the world in RGB through three eyes. Lets call them Red_Eye, Green_Eye, Blue_Eye, each ...
1
vote
5
answers
125
views
Should docs of extended methods include documentation of the base method?
Let's say some method of a parent class is reimplemented in a child class.
This child method is intended to do the same that the parent method, with a minor change.
In this case, in the documentation ...