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
140
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.
...
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)
|...
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:
...
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 ...
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 ...
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* ...
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 ...
-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.:
...
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 ...
2
votes
1
answer
280
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 ...
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($...
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:
...
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:
...
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 ...
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 ...
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 ...
1
vote
3
answers
315
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
2
votes
1
answer
674
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 ...
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 ...
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 ...