Questions tagged [coding-style]

**DO NOT USE! This tag refers to an entirely opinionated subject and is therefore no longer on-topic.** Questions that follow coding style and conventions.

DO NOT USE! This tag refers to an entirely opinionated subject and is therefore no longer on-topic.

Questions that follow coding style and conventions.

Coding-style or Programming 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.

Questions relating to coding style: stylistic issues like braces and indentation or larger issues like refactoring, size of functions, and so forth.

Where can I ask these questions?

  • Process-level questions about coding conventions or style guides may be on-topic for Software Engineering. That also covers design-level questions

  • If you want to improve your style, you can ask for a Code Review.

  • Questions that ask for the “best style” or for advice in a specific situation are too subjective and/or too broad.

7652 questions
1147
votes
14 answers

Best way to return multiple values from a function?

The canonical way to return multiple values in languages that support it is often tupling. Option: Using a tuple Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0, y1, y2) However, this quickly gets…
saffsd
  • 21,564
  • 16
  • 57
  • 65
1020
votes
7 answers

What is the standard Python docstring format?

I have seen a few different styles of writing docstrings in Python, is there an official or "agreed-upon" style?
Noah McIlraith
  • 12,932
  • 7
  • 24
  • 35
836
votes
9 answers

Python `if x is not None` or `if not x is None`?

I've always thought of the if not x is None version to be more clear, but Google's style guide and PEP-8 both use if x is not None. Is there any minor performance difference (I'm assuming not), and is there any case where one really doesn't fit…
orokusaki
  • 48,267
  • 47
  • 159
  • 244
780
votes
50 answers

Should a function have only one return statement?

Are there good reasons why it's a better practice to have only one return statement in a function? Or is it okay to return from a function as soon as it is logically correct to do so, meaning there may be many return statements in the function?
David
  • 13,789
  • 24
  • 77
  • 99
749
votes
30 answers

Styling multi-line conditions in 'if' statements?

Sometimes I break long conditions in ifs onto several lines. The most obvious way to do this is: if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something Isn't very very appealing visually,…
Eli Bendersky
  • 231,995
  • 78
  • 333
  • 394
718
votes
19 answers

Single quotes vs. double quotes in Python

According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
readonly
  • 306,152
  • 101
  • 198
  • 201
700
votes
15 answers

Order of items in classes: Fields, Properties, Constructors, Methods

Is there an official C# guideline for the order of items in terms of class structure? Does it go: Public Fields Private Fields Properties Constructors Methods ? I'm curious if there is a hard and fast rule about the order of items? I'm kind of all…
mmcdole
  • 86,293
  • 60
  • 181
  • 221
615
votes
8 answers

What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported. (function() { ... code ... })(); What is the reason for doing this rather than a…
Andrew Kou
  • 6,775
  • 4
  • 19
  • 15
606
votes
12 answers

"std::endl" vs "\n"

Many C++ books contain example code like this... std::cout << "Test line" << std::endl; ...so I've always done that too. But I've seen a lot of code from working developers like this instead: std::cout << "Test line\n"; Is there a technical reason…
Head Geek
  • 33,955
  • 20
  • 72
  • 83
600
votes
23 answers

What's the best way to convert a number to a string in JavaScript?

What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ? Some examples: String(n) n.toString() ""+n n+""
Pacerier
  • 76,400
  • 86
  • 326
  • 602
589
votes
5 answers

Why use def main()?

I've seen some code samples and tutorials that use def main(): # my code here if __name__ == "__main__": main() But why? Is there any reason not do define your functions at the top of the file, then just write code under it? ie def…
Wizzard
  • 11,849
  • 19
  • 60
  • 97
528
votes
28 answers

Are PHP short tags acceptable to use?

Here's the information according to the official documentation: There are four different pairs of opening and closing tags which can be used in PHP. Two of those, and , are always available. The…
MDCore
  • 15,705
  • 8
  • 38
  • 48
511
votes
11 answers

typeof !== "undefined" vs. != null

I often see JavaScript code which checks for undefined parameters etc. this way: if (typeof input !== "undefined") { // do stuff } This seems kind of wasteful, since it involves both a type lookup and a string comparison, not to mention its…
Derek Thurn
  • 13,971
  • 9
  • 35
  • 54
486
votes
10 answers

What is the most effective way to get the index of an iterator of an std::vector?

I'm iterating over a vector and need the index the iterator is currently pointing at. AFAIK this can be done in two ways: it - vec.begin() std::distance(vec.begin(), it) What are the pros and cons of these methods?
cairol
  • 7,723
  • 8
  • 25
  • 25
476
votes
31 answers

Is there a better way of writing v = (v == 0 ? 1 : 0);

I want to toggle a variable between 0 and 1. If it's 0 I want to set it to 1, else if it's 1 I want to set it to 0. This is such a fundamental operation that I write so often I'd like to investigate the shortest, clearest possible way of doing it.…
Ollie Glass
  • 17,505
  • 18
  • 65
  • 98
1
2 3
99 100