Questions tagged [coding-style]
Coding style is a set of guidelines that helps readability and understanding of the source code.
1,064 questions
26
votes
7
answers
22k
views
In C/C++, should I use 'const' in parameters and local variables when possible?
This question is inspired by a question about final in
java.
In C/C++, should I use const whenever possible?
I know there is already a related question about using const in parameters. ...
174
votes
24
answers
27k
views
Programming cleanly when writing scientific code
I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code.
My code is primarily "scripting" type stuff - things to test mathematical ...
9
votes
4
answers
10k
views
When are chained assignments (i.e. a=b=c) bad form? [closed]
I'm working on a VB.Net WinForms project and found myself writing code like this:
this.Fizz.Enabled = this.Buzz.Enabled = someCondition;
I couldn't decide whether that was bad code or not. Are there ...
-1
votes
1
answer
144
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.
...
9
votes
7
answers
10k
views
Is it bad practice to use short-circuit evaluation instead of an if clause? [duplicate]
I'm working on an installation script, and especially there I have many things that need to be checked and actions that only need to be performed in case of related outcomes. For example, a folder ...
61
votes
3
answers
82k
views
What is the difference between K&R and One True Brace Style (1TBS) styles?
I have read the Wikipedia article on Indent Styles, but I still don't understand. What is the difference between K&R and 1TBS?
2
votes
4
answers
490
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 ...
31
votes
4
answers
10k
views
Style for control flow with validation checks
I find myself writing a lot of code like this:
int myFunction(Person* person) {
int personIsValid = !(person==NULL);
if (personIsValid) {
// do some stuff; might be lengthy
int myresult ...
310
votes
2
answers
550k
views
Python file naming convention?
I've seen this part of PEP-8 https://www.python.org/dev/peps/pep-0008/#package-and-module-names
I'm not clear on whether this refers to the file name of a module/class/package.
If I had one example ...
106
votes
11
answers
17k
views
Has / can anyone challenge Uncle Bob on his love of removing "useless braces"?
I hate referencing paywalled content, but this video shows exactly what I'm talking about. Precisely 12 minutes in Robert Martin looks at this:
And says "One of my favorite things to do is getting rid ...
0
votes
2
answers
365
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)
|...
14
votes
2
answers
10k
views
Which is a better pattern (coding style) for validating arguments - hurdle (barrier) or fence? [duplicate]
I don't know if there are any accepted names for these patterns (or anti-patterns), but I like to call them what I call them here. Actually, that would be Question 1: What are accepted names for these ...
26
votes
6
answers
7k
views
Why use Either over (checked) Exception?
Not too long ago I started using Scala instead of Java. Part of the "conversion" process between the languages for me was learning to use Eithers instead of (checked) Exceptions. I've been coding this ...
0
votes
2
answers
1k
views
Should I wrap all codes into runOnUiThread even some code doesn't need to run on UI thread?
In android, some codes, such as updating UI buttons, needs to be called in UI thread, however, before UI update, some task, such as getting response from internet, need to be done on another thread, ...
35
votes
5
answers
56k
views
Throwing an exception inside finally
Static code analyzers like Fortify "complain" when an exception might be thrown inside a finally block, saying that Using a throw statement inside a finally block breaks the logical progression ...