Coding style is a set of guidelines that helps readability and understanding of the source code.
57
votes
39answers
19k views
Should curly braces appear on their own line? [closed]
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())
{
...
72
votes
22answers
15k views
Tabs versus spaces—what is the proper indentation character for everything, in every situation, ever? [closed]
The coding standards for the code hosted in drupal.org suggest to use two spaces to indent the code; other sites suggest to use tabs to indent the code.
What is the proper indentation character for ...
34
votes
11answers
3k views
Is the 80 character limit still relevant in times of widescreen monitors? [closed]
on a widescreen monitor one can easily see more than 80 characters at a time, without scrollbars. even linus torvalds sees the 80 character limit as outdated.
so, is the 80 character limit still ...
41
votes
19answers
5k views
Should I return from a function early or use an if statement? [duplicate]
Possible Duplicate:
Where did the notion of “one return only” come from?
I've often written this sort of function in both formats, and I was wondering if one format is preferred ...
60
votes
24answers
9k views
Where do you declare variables? The top of a method or when you need them?
I am in sort of a dilemma (in a geekish way of course).
I love to declare variables at the beginning of my methods, and usually order them in some logical way.
The problem is, when the list gets ...
16
votes
9answers
1k views
Style and recommendations of commenting code
I want to hear from you any advice and experience of writing comments in your code. How do you write them in the most easy and informative way? What habits do you have when commenting parts of code? ...
26
votes
10answers
2k views
Doesn't “if (0 == value) …” do more harm than good? [closed]
This is one of the things that I hate most when I see it in someone else's code. I know what it means and why some people do it this way ("what if I accidentally put '=' instead?"). For me it's very ...
15
votes
12answers
2k views
int* i; or int *i; or int * i; [closed]
What is your favorite method to declare a pointer?
int* i;
or
int *i;
or
int * i;
or
int*i;
Please explain why.
see also: http://www.stroustrup.com/bs_faq2.html#whitespace
50
votes
14answers
5k views
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, ...
31
votes
17answers
8k views
Single statement if block - braces or no?
Which is better/more generally accepted?
This:
if(condition)
{
statement;
}
Or:
if(condition)
statement;
I tend to prefer the first one, because I think it makes it easier to tell what ...
39
votes
11answers
4k views
Should I reuse variables?
Should I reuse variables?
I know that many best practice say you should not do it, however later when different developer is debugging the code and have 3 variables that look a like and only ...
8
votes
2answers
254 views
Use an else after exception (or not)
Consider this bit of code:
if (x == 1)
{
throw "no good; aborting" ;
}
[... more code ...]
Now consider this code:
if (x == 1)
{
throw "no good; aborting" ;
}
else
{
[... more code ...]
}
...
3
votes
3answers
130 views
Style for control flow with validation checks
I find myself writing a lot of code like this:
int myFunction(Person* person) {
int personIsValid = !(person==NULL);
if (personIsValid) {
// do some stuff; might be lengthy
int myresult ...
72
votes
56answers
10k views
Worst coding standard you've ever had to follow? [closed]
Have you ever had to work to coding standards that:
Greatly decreased your productivity?
Were originally included for good reasons but were kept long after the original concern became irrelevant?
...
68
votes
17answers
13k views
What is the benefit of not using Hungarian notation?
One of the things I struggle with is not using Hungarian notation. I don't want to have to go to the variable definition just to see what type it is. When a project gets extensive, it's nice to be ...
81
votes
20answers
3k views
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 = ...
27
votes
13answers
1k views
Handling my antiquated coworker
I'm a fairly young programmer, and I work at a medium sized company's IT department. I have a coworker, and he is a really good Visual Basic 6 programmer. And I mean really good. Honestly. He can ...
19
votes
8answers
2k views
What is the ideal length of a method?
In object-oriented programming, there is no exact rule on the maximum length of a method , but I still found these two qutes somewhat contradicting each other, so I would like to hear what you think.
...
19
votes
10answers
5k views
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. ...
26
votes
15answers
2k views
Space around operators? a=b/2; or a = b / 2; What are the arguments?
Personally I prefer space around operators. For me it makes the code much more readable. What are the arguments not have space around them? Line length?
An example from the Python style guide (I ...
4
votes
3answers
130 views
What could be some pitfalls in introducing a style guide and documentation generating software in a development team?
I'm considering using appledoc, a flavor of Doxygen for use in generating Objective-C code documentation, in order to create documentation for my company's iOS apps. The idea is that a server will ...
21
votes
12answers
2k views
Does anyone prefer proportional fonts? [closed]
I was reading the wikipedia article on programming style and noticed something in an argument against vertically aligned code:
Reliance on mono-spaced font; tabular
formatting assumes that the ...
8
votes
9answers
1k views
What C++ coding standard do you use? [closed]
For some time now, I've been unable to settle on a coding standard and use it concistently between projects. When starting a new project, I tend to change some things around (add a space there, remove ...
12
votes
6answers
3k views
What should my “code sample” look like?
I've just had quite a good phone interview (for a CakePHP-related position, not that it's especially important to the question). The interviewer seemed to be impressed with my resume and personality. ...
12
votes
4answers
2k views
In Java, should I use “final” for parameters and locals even when I don't have to?
Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an ...
58
votes
17answers
3k views
Recommendations for teaching junior programmers good coding style
I am a big fan of good coding style, producing clean, clear code that runs well and is easy to use and integrate into larger systems. I believe that we programmers are essentially craftspeople who ...
36
votes
11answers
3k views
Is it bad habit not using interfaces?
I use interfaces rarely and find them common in others code.
Also I create sub and super classes (while creating my own classes) rarely in my code.
Is it a bad thing?
Would you suggest changing ...
31
votes
12answers
2k views
Would a cut and paste coder ever get past a job interview?
As a long time cut and paste coder I never committed much of the syntax of a language to memory. Even worse, I now use google to solve many of the coding problems which are of the type typically used ...
33
votes
15answers
9k views
If you need more than 3 levels of indentation, you're screwed?
Per the Linux kernel coding style document:
The answer to that is that if you need more than 3 levels of
indentation, you're screwed anyway, and should fix your program.
What can I deduce ...
32
votes
8answers
1k views
When using method chaining, do I reuse the object or create one?
When using method chaining like:
var car = new Car().OfBrand(Brand.Ford).OfModel(12345).PaintedIn(Color.Silver).Create();
there may be two approaches:
Reuse the same object, like this:
public ...