Questions tagged [coding-style]
Coding style is a set of guidelines that helps readability and understanding of the source code.
1,064 questions
1353
votes
14
answers
279k
views
Where did the notion of "one return only" come from?
I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's ...
585
votes
1
answer
76k
views
Is the use of "utf8=✓" preferable to "utf8=true"?
I have recently seen a few URIs containing the query parameter "utf8=✓". My first impression (after thinking "mmm, looks cool") was that this could be used to detect a broken character encoding.
So, ...
329
votes
36
answers
397k
views
Should curly braces appear on their own line? [closed]
Should curly braces be on their own line or not? What do you think about it?
if (you.hasAnswer()) {
you.postAnswer();
} else {
you.doSomething();
}
or should it be
if (you.hasAnswer())
{
...
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 ...
301
votes
19
answers
246k
views
Should I return from a function early or use an if statement? [closed]
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.
public void SomeFunction(bool someCondition)
{
if (someCondition)
...
271
votes
21
answers
237k
views
Are `break` and `continue` bad programming practices?
My boss keeps mentioning nonchalantly that bad programmers use break and continue in loops.
I use them all the time because they make sense; let me show you the inspiration:
function verify(object) {
...
202
votes
14
answers
93k
views
What's wrong with circular references?
I was involved in a programming discussion today where I made some statements that basically assumed axiomatically that circular references (between modules, classes, whatever) are generally bad. ...
187
votes
15
answers
79k
views
Developer insists if statements shouldn't have negated conditions, and should always have an else block
I have an acquaintance, a more seasoned developer than me.
We were talking about programming practices and I was taken aback by his approach on 'if' statements.
He insists on some practices regarding ...
175
votes
24
answers
118k
views
Elegant ways to handle if(if else) else
This is a minor niggle, but every time I have to code something like this, the repetition bothers me, but I'm not sure that any of the solutions aren't worse.
if(FileExists(file))
{
contents = ...
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 ...
168
votes
6
answers
69k
views
Should the variable be named Id or ID? [closed]
This is a bit pedantic, but I've seen some people use Id as in:
private int userId;
public int getUserId();
and others use:
private int userID;
public int getUserID();
Is one of these a better name ...
153
votes
14
answers
125k
views
What is the ideal length of a method for you? [closed]
In object-oriented programming, there is of course no exact rule on the maximum length of a method , but I still found these two quotes somewhat contradicting each other, so I would like to hear what ...
147
votes
6
answers
18k
views
Are private methods with a single reference bad style?
Generally I use private methods to encapsulate functionality that is reused in multiple places in the class. But sometimes I have a large public method that could be broken up into smaller steps, each ...
141
votes
5
answers
88k
views
In Java, should I use "final" for parameters and locals even when I don't have to?
Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an ...
138
votes
18
answers
54k
views
Is there an excuse for short variable names?
This has become a large frustration with the codebase I'm currently working in; many of our variable names are short and undescriptive. I'm the only developer left on the project, and there isn't ...