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
277
votes
14 answers

Iterate through a C++ Vector using a 'for' loop

I am new to the C++ language. I have been starting to use vectors, and have noticed that in all of the code I see to iterate though a vector via indices, the first parameter of the for loop is always something based on the vector. In Java I might do…
Flynn
  • 5,903
  • 7
  • 38
  • 55
261
votes
6 answers

What's the correct way to sort Python `import x` and `from x import y` statements?

The python style guide suggests to group imports like this: Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports However, it does not mention anything…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
260
votes
17 answers

Is it a good practice to place C++ definitions in header files?

My personal style with C++ has always been to put class declarations in an include file and definitions in a .cpp file, very much like stipulated in Loki's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style…
T.E.D.
  • 44,016
  • 10
  • 73
  • 134
255
votes
5 answers

Creating an empty list in Python

What is the best way to create a new empty list in Python? l = [] or l = list() I am asking this because of two reasons: Technical reasons, as to which is faster. (creating a class causes overhead?) Code readability - which one is the standard…
user225312
  • 126,773
  • 69
  • 172
  • 181
248
votes
31 answers

When do you use the "this" keyword?

I was curious about how other people use the this keyword. I tend to use it in constructors, but I may also use it throughout the class in other methods. Some examples: In a constructor: public Light(Vector v) { this.dir = new…
ftdysa
  • 1,242
  • 6
  • 17
  • 26
235
votes
7 answers

Why are dashes preferred for CSS selectors / HTML attributes?

In the past I've always used underscores for defining class and id attributes in HTML. Over the last few years I changed over to dashes, mostly to align myself with the trend in the community, not necessarily because it made sense to me. I've always…
Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
235
votes
9 answers

What does 'foo' really mean?

I hope this qualifies as a programming question, as in any programming tutorial, you eventually come across 'foo' in the code examples. (yeah, right?) what does 'foo' really mean? If it is meant to mean nothing, when did it begin to be used so?
prakash
  • 58,901
  • 25
  • 93
  • 115
234
votes
11 answers

For a boolean field, what is the naming convention for its getter/setter?

Eg. boolean isCurrent = false; What do you name its getter and setter?
user496949
  • 83,087
  • 147
  • 309
  • 426
233
votes
15 answers

What are the advantages of using getters and setters instead of functions or simply public fields in PHP?

I'm not a PHP developer, so I'm wondering what the advantages and disadvantages are in PHP to using explicit getter/setters, in a pure OOP style, with private fields (the way I like): class MyClass { private $firstField; private…
Mark
  • 67,098
  • 47
  • 117
  • 162
230
votes
19 answers

Using "super" in C++

My style of coding includes the following idiom: class Derived : public Base { public : typedef Base super; // note that it could be hidden in // protected/private section, instead // Etc. } ; This…
paercebal
  • 81,378
  • 38
  • 130
  • 159
226
votes
8 answers

return statement vs exit() in main()

Should I use exit() or just return statements in main()? Personally I favor the return statements because I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to…
Srikanth
  • 11,780
  • 23
  • 72
  • 92
224
votes
14 answers

Check if a string contains an element from a list (of strings)

For the following block of code: For I = 0 To listOfStrings.Count - 1 If myString.Contains(lstOfStrings.Item(I)) Then Return True End If Next Return False The output is: Case 1: myString: C:\Files\myfile.doc listOfString: C:\Files\,…
user57175
  • 3,284
  • 9
  • 32
  • 26
217
votes
38 answers

How do you tell someone they're writing bad code?

I've been working with a small group of people on a coding project for fun. It's an organized and fairly cohesive group. The people I work with all have various skill sets related to programming, but some of them use older or outright wrong…
Maximillian
  • 743
  • 2
  • 22
  • 41
215
votes
8 answers

.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?

Assuming I have an ArrayList ArrayList myList; And I want to call toArray, is there a performance reason to use MyClass[] arr = myList.toArray(new MyClass[myList.size()]); over MyClass[] arr = myList.toArray(new MyClass[0]); ? I prefer…
itsadok
  • 28,822
  • 30
  • 126
  • 171
214
votes
5 answers

codestyle; put javadoc before or after annotation?

I know that it isn't the most vital of issues, but I just realised that I can put the javadoc comment block before or after the annotation. What would we want to adopt as a coding standard? /** * This is a javadoc comment before the annotation …
Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120