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
206
votes
23 answers

Advantages of std::for_each over for loop

Are there any advantages of std::for_each over for loop? To me, std::for_each only seems to hinder the readability of code. Why do then some coding standards recommend its use?
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
202
votes
7 answers

Why do some scripts omit the closing PHP tag, '?>'?

In some scripts I see that they omit writing a closing tag ?> for the script. Why is it and should I do this as well? (I'm sure they have not forgotten it.)
Naughty.Coder
  • 3,922
  • 7
  • 32
  • 41
201
votes
9 answers

Internal typedefs in C++ - good style or bad style?

Something I have found myself doing often lately is declaring typedefs relevant to a particular class inside that class, i.e. class Lorem { typedef boost::shared_ptr ptr; typedef std::vector vector; // //…
Will Baker
  • 2,747
  • 4
  • 20
  • 17
200
votes
14 answers

Checking for NULL pointer in C/C++

In a recent code review, a contributor is trying to enforce that all NULL checks on pointers be performed in the following manner: int * some_ptr; // ... if (some_ptr == NULL) { // Handle null-pointer error } else { // Proceed } instead…
Bryan Marble
  • 3,467
  • 5
  • 26
  • 29
199
votes
11 answers

How can we force naming of parameters when calling a function?

In Python you may have a function definition: def info(obj, spacing=10, collapse=1) which could be called in any of the following ways: info(odbchelper) info(odbchelper, 12) info(odbchelper, collapse=0) …
Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
197
votes
15 answers

Should enums in C# have their own file?

I have a class which uses an enumeration, the enum is currently in its own file which seems wasteful. What is the general opinion on enums being placed within the namespace of a file that they are consumed in? Or should the enum really live in its…
Finglas
  • 15,518
  • 10
  • 56
  • 89
192
votes
8 answers

pythonic way to do something N times without an index variable?

I have some code like: for i in range(N): do_something() I want to do something N times. The code inside the loop doesn't depend on the value of i. Is it possible to do this simple task without creating a useless index variable, or in an…
Manuel Araoz
  • 15,962
  • 24
  • 71
  • 95
192
votes
23 answers

Setting variable to NULL after free

In my company there is a coding rule that says, after freeing any memory, reset the variable to NULL. For example ... void some_func () { int *nPtr; nPtr = malloc (100); free (nPtr); nPtr = NULL; return; } I feel that, in…
Alphaneo
  • 12,079
  • 22
  • 71
  • 89
189
votes
5 answers

Relational table naming convention

I'm starting a new project and would like to get my table and column names right. For example I've always used plural in table names but recently learned singular is correct. So if I have a table user and I have products that only the user will…
189
votes
5 answers

In Intellij, how do I toggle between camel case and underscore spaced?

At my company we have two different style guides for java vs sql. In java I have a field named historyOfPresentIllness and when i write the sql, I want to name it history_of_present_illness. Is there a keyboard shortcut to switch from one to the…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
188
votes
10 answers

How to name factory like methods?

I guess that most factory-like methods start with create. But why are they called "create"? Why not "make", "produce", "build", "generate" or something else? Is it only a matter of taste? A convention? Or is there a special meaning in…
deamon
  • 89,107
  • 111
  • 320
  • 448
187
votes
52 answers

Why is it considered a bad practice to omit curly braces?

Why does everyone tell me writing code like this is a bad practice? if (foo) Bar(); //or for(int i = 0 i < count; i++) Bar(i); My biggest argument for omitting the curly braces is that it can sometimes be twice as many lines with them.…
Bob
  • 97,670
  • 29
  • 122
  • 130
184
votes
32 answers

String output: format or concat in C#?

Let's say that you want to output or concat strings. Which of the following styles do you prefer? var p = new { FirstName = "Bill", LastName = "Gates" }; Console.WriteLine("{0} {1}", p.FirstName, p.LastName); Console.WriteLine(p.FirstName + " " +…
Philippe
  • 3,945
  • 3
  • 38
  • 56
182
votes
9 answers

How to break a line of chained methods in Python?

I have a line of the following code (don't blame for naming conventions, they are not mine): subkeyword = Session.query( Subkeyword.subkeyword_id, Subkeyword.subkeyword_word ).filter_by( subkeyword_company_id=self.e_company_id ).filter_by( …
Juliusz Gonera
  • 4,658
  • 5
  • 32
  • 35
182
votes
32 answers

Is there a valid reason for enforcing a maximum width of 80 characters in a code file, this day and age?

Seriously. On a 22" monitor, it only covers maybe a quarter of the screen. I need some ammo to axe down this rule. I'm not saying that there shouldn't be a limit; I'm just saying, 80 characters is very small.
TraumaPony
  • 10,742
  • 12
  • 54
  • 74