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.

7804 questions
1135
votes
6 answers

What are the most common Python docstring formats?

I have seen a few different styles of writing docstrings in Python, what are the most popular styles?
Noah McIlraith
  • 14,122
  • 7
  • 31
  • 35
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
  • 14,047
  • 24
  • 80
  • 101
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
  • 343,444
  • 107
  • 203
  • 205
355
votes
11 answers

Acronyms in CamelCase

I have a doubt about CamelCase. Suppose you have this acronym: Unesco = United Nations Educational, Scientific and Cultural Organization. You should write: unitedNationsEducationalScientificAndCulturalOrganization But what if you need to write the…
gal007
  • 6,911
  • 8
  • 47
  • 70
349
votes
9 answers

Why is it recommended to have empty line in the end of a source file?

Some code style tools recommend this and I remember seeing some unix command line tools warning about missing empty line. What is the reasoning for having an extra empty line?
Petteri H
  • 11,779
  • 12
  • 64
  • 94
340
votes
24 answers

What's the purpose of using braces (i.e. {}) for a single-line if or loop?

I'm reading some lecture notes of my C++ lecturer and he wrote the following: Use Indentation // OK Never rely on operator precedence - Always use parentheses // OK Always use a { } block - even for a single line // not OK, why ??? Const object…
JAN
  • 21,236
  • 66
  • 181
  • 318
328
votes
7 answers

Coding Conventions - Naming Enums

Is there a convention for naming enumerations in Java? My preference is that an enum is a type. So, for instance, you have an enum Fruit{Apple,Orange,Banana,Pear, ... } NetworkConnectionType{LAN,Data_3g,Data_4g, ... } I am opposed to naming…
Walter White
327
votes
10 answers

Which comment style should I use in batch files?

I've been writing some batch files, and I ran into this user guide, which has been quite informative. One thing it showed me was that lines can be commented not just with REM, but also with ::. It says: Comments in batch code can be made by using a…
MikeFHay
  • 8,562
  • 4
  • 31
  • 52
323
votes
12 answers

Should methods in a Java interface be declared with or without a public access modifier?

Should methods in a Java interface be declared with or without the public access modifier? Technically it doesn't matter, of course. A class method that implements an interface is always public. But what is a better convention? Java itself is not…
Benno Richters
  • 15,378
  • 14
  • 42
  • 45
317
votes
9 answers

Why does PEP-8 specify a maximum line length of 79 characters?

Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters? Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the…
pcorcoran
  • 7,894
  • 6
  • 28
  • 26
308
votes
2 answers

80-characters / right margin line in Sublime Text 3

You can have 80-characters / right margin line in Netbeans, Text Mate and probably many, many more other IDEs. Is it possible to have it in Sublime Text 3 as well? Any option, plugin etc.?
trejder
  • 17,148
  • 27
  • 124
  • 216
301
votes
27 answers

When is JavaScript's eval() not evil?

I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result. However, I've always shied away from using…
Richard Turner
  • 12,506
  • 6
  • 36
  • 37
299
votes
10 answers

Dictionaries and default values

Assuming connectionDetails is a Python dictionary, what's the best, most elegant, most "pythonic" way of refactoring code like this? if "host" in connectionDetails: host = connectionDetails["host"] else: host = someDefaultValue
mnowotka
  • 16,430
  • 18
  • 88
  • 134
299
votes
28 answers

How can I set multiple CSS styles in JavaScript?

I have the following JavaScript variables: var fontsize = "12px" var left= "200px" var top= "100px" I know that I can set them to my element iteratively like…
Mircea
  • 11,373
  • 26
  • 64
  • 95
287
votes
13 answers

Best way to check for nullable bool in a condition expression (if ...)

I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools. Is the following good or bad coding style? Is there a way to express the condition better/more cleanly? bool? nullableBool = true; if…
FireSnake
  • 2,983
  • 2
  • 16
  • 9
1
2 3
99 100