Questions tagged [coding-style]

Coding style is a set of guidelines that helps readability and understanding of the source code.

Coding style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.

1064 questions
1352
votes
14 answers

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 confusing." When they show me solutions with a single…
585
votes
1 answer

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, is this a better way to resolve potential…
328
votes
36 answers

Should curly braces appear on their own line?

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()) { you.postAnswer(); } else { …
310
votes
2 answers

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 of each, should the filenames be all lower case…
darkace
  • 3,211
301
votes
19 answers

Should I return from a function early or use an if statement?

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) { // Do Something } } or public…
Rachel
  • 24,037
268
votes
21 answers

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) { if (object->value < 0) return false; if…
202
votes
14 answers

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. Once I got through with my pitch, my coworker asked,…
186
votes
15 answers

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 if statements that I find rather…
Patsuan
  • 1,637
176
votes
24 answers

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 = OpenFile(file); // <-- prevents inclusion in if …
Benjol
  • 3,737
174
votes
23 answers

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 functions, or to simulate something - "scientific…
auden
  • 1,657
168
votes
6 answers

Should the variable be named Id or ID?

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 than the other? Why? I've seen this done very…
Adam
  • 2,211
153
votes
14 answers

What is the ideal length of a method for you?

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 you think. In Clean Code: A Handbook of Agile…
Spring
  • 1,763
147
votes
6 answers

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 in its own private method. This would make the…
Jordak
  • 931
139
votes
18 answers

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 documentation as to what most of them do, so I have…
KChaloux
  • 5,824
137
votes
5 answers

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 entire class - are meant to be immutable. On the…
Oak
  • 5,315
1
2 3
70 71