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.

1039 questions
1330
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…
580
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…
Gary
  • 24,420
  • 9
  • 63
  • 108
322
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 { …
Tamara Wijsman
  • 8,259
  • 14
  • 58
  • 94
302
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
  • 23,979
  • 16
  • 91
  • 159
294
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,051
  • 3
  • 10
  • 5
259
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…
194
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,…
dash-tom-bang
  • 2,543
  • 2
  • 16
  • 11
185
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,627
  • 2
  • 9
  • 10
174
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,747
  • 5
  • 33
  • 41
172
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 Young
  • 1,637
  • 3
  • 11
  • 21
161
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,141
  • 2
  • 16
  • 15
150
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,733
  • 2
  • 12
  • 10
146
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
  • 921
  • 2
  • 7
  • 10
140
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,773
  • 4
  • 35
  • 34
135
votes
10 answers

Why do most of us use 'i' as a loop counter variable?

Has anyone thought about why so many of us repeat this same pattern using the same variable names? for (int i = 0; i < foo; i++) { // ... } It seems most code I've ever looked at uses i, j, k and so on as iteration variables. I suppose I picked…
kprobst
  • 641
  • 2
  • 6
  • 9
1
2 3
69 70