Questions tagged [coding-style]
Coding style is a set of guidelines that helps readability and understanding of the source code.
1,064 questions
-1
votes
1
answer
137
views
How to organize struct-based "methods" in C similar to object-oriented style? [closed]
So I was coding in C for a while now, getting used to language syntax and different styles.
Implemented a couple of simple data structures, algorithms and tried my skills in making Minesweeper clone.
...
2
votes
4
answers
489
views
How to combine multiple functions into a single template based function
Threre are two functions FindMaxDistanceVector and FindMaxDistanceList whose implementation is almost same except some debug information added in FindMaxDistanceList. Note that these two functions are ...
0
votes
2
answers
364
views
Which is better: a chain of OR statements or IF in a loop? (Java)
Which code style is preferred in Java?
final boolean result = isCausedBy(e, ExceptionType1.class)
|| isCausedBy(e, ExceptionType2.class)
|| isCausedBy(e, ExceptionType3.class)
|...
1
vote
3
answers
308
views
Considering IDE and text editor features when choosing a coding style
Recently, I had a debate with one of my friends on using type inference in C# (var keyword). His argument was that we should stick to using the explicit type names because "even with the ...
2
votes
3
answers
241
views
Is it still "feature envy" if the state to make decision or the action to take after asking the state involves other classes to participate?
According to https://softwareengineering.stackexchange.com/a/212130/432039, if a class asks another class for the state, and then call methods of that class, it is called "feature envy", eg:
...
10
votes
4
answers
2k
views
Is the inability to find code by searching for a class name a reason to avoid using auto in c++ variable declarations?
According to https://softwareengineering.stackexchange.com/a/180616/432039 suggested, I know the answer advised "auto" should be used instead of the actual type when declaring variables.
...
1
vote
5
answers
253
views
End method in normal flow versus exception flow [duplicate]
Consider the two following examples:
public Something fetchSomething(String key) {
if(somethingsMap.containsKey(key)) {
return somethingsMap.get(key);
}
throw new ...
6
votes
4
answers
3k
views
How do you honour the principle to only do "one thing" in a method in reactive streams?
Uncle Bob mentions in his book "Clean Code" that "[f]unctions should do one thing [...] only" and advocates that a method should stick to one abstraction level (I'd call it flight ...
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
436
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
355
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
278
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
1k
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.:
...
2
votes
2
answers
633
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
3k
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
59
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
279
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
10k
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
151
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
439
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
9k
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
217
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
356
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
5k
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
216
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
88
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
642
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
340
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
314
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
307
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
179
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
222
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 && $...
17
votes
6
answers
7k
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
2k
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
161
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
179
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
217
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 ...
40
votes
6
answers
14k
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
722
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
672
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
2k
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
170
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
1k
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
405
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
262
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 ...