Coding style is a set of guidelines that helps readability and understanding of the source code.
93
votes
16answers
19k views
Is there an excuse for short variable names?
This has become a large frustration with the codebase I'm currently working in; many of our variable names are short and undescriptive. I'm the only developer left on the project, and there isn't ...
81
votes
20answers
4k 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 = ...
74
votes
22answers
16k 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 ...
73
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 ...
67
votes
15answers
3k views
Is defining a variable to name a method argument a good practice?
For the sake of readability I often find myself defining temporary variables while calling functions, such as the following code
var preventUndo = true;
doSomething(preventUndo);
The shorter ...
61
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 ...
61
votes
20answers
4k views
How forgiving do you need to be on new employees?
Recently we have a new developer in our team. We are getting him up to speed and he is picking it all up quite fast, but a new developer means new (foreign) coding styles and ways to solve things.
It ...
61
votes
10answers
3k views
Never use Strings in Java?
I stumbled upon a blog entry discouraging the use of Strings in Java for making your code to lack semantics, suggesting that you should use thin wrapper classes instead. This is the before and after ...
60
votes
39answers
20k 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())
{
...
59
votes
6answers
3k views
Should the variable be named Id or ID?
This is a bit pedantic, but I've seen some people use Id as in:
private int userId;
public int getUserId();
and others use:
private int userID;
public int getUserID();
Is one of these a better ...
59
votes
19answers
10k views
How important is it to reduce the number of lines in code?
I am a Software developer who works on J2SE (core java).
Often during our code reviews we are asked to reduce the number of lines in our code.
It's not about removing redundant code, it's about ...
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 ...
55
votes
11answers
2k views
What are the drawbacks of elastic tabstops?
Look here: a typical holy war on tabs vs spaces.
Now look here: elastic tabstops. All problems solved, and a bunch of very useful new behaviours added.
Are elastic tabstops even mentioned in that ...
52
votes
14answers
3k views
Is inconsistent formatting a sign of a sloppy programmer?
I understand that everyone has their own style of programming and that you should be able to read other people's styles and accept it for what it is. However, would one be considered a sloppy ...
50
votes
14answers
6k 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, ...
46
votes
15answers
3k views
Working on someone else's code
I have hardly a year's experience in coding. After I started working, most of the time I would be working on someone else's code. Either adding new features over the existing ones or modifying the ...
43
votes
16answers
3k views
Strictness in programming methods among Stack Overflow users [closed]
I've been a member of Stack Overflow for a couple of weeks now and have answered questions and read others answers, mostly in C/C++. True, I have learned about some things. For example, undefined ...
42
votes
12answers
2k views
Coding style (do more, then simplify) [duplicate]
I'm a CS student and I have been coding for a few months shy of a year now, and I seem to have developed what I think may be a "bad" habit and I'm wondering if anyone does the same (or whether it's a ...
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 ...
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 ...
36
votes
17answers
3k views
Why Use !boolean_variable Over boolean_variable == false
A comment on this question: Calling A Method that returns a boolean value inside a conditional statement says that you should use !boolean instead of boolean == false when testing conditions. Why? To ...
36
votes
14answers
2k views
Should I encourage junior developers to use explicit or implicit typing?
I am looking to solicit the community's opinion on whether or not it would be advisable to teach junior developers to code with implicit typing (using var) or should I encourage the use of explicit ...
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 ...
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 ...
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
15answers
3k views
Pointless Code In Your Source
I've heard stories of this from senior coders and I've seen some of it myself. It seems that there are more than a few instances of programmers writing pointless code. I will see things like:
...
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 ...
31
votes
17answers
9k 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 ...
31
votes
9answers
6k views
Does the usage of LINQ and Lambda Expressions lead to less readable code?
I'm having a discussion with a co-worker on Linq, I'll copy here:
Co-Worker: Lets be honest here. Linq
syntax sucks. It's confusing and
non-intuitive.
Me: oh come on, more
confusing ...
31
votes
16answers
3k views
Should I continue my self-taught coding practice or learn how to do coding professionally? [closed]
Lately I've been getting professional work, hanging out with other programmers, and making friends in the industry. The only thing is I'm 100% self-taught. It's caused my style to extremely deviate ...
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 ...
28
votes
8answers
3k views
How can I learn to effectively write Pythonic code?
Doing a google search for "pythonic" reveals a wide range of interpretations. The wikipedia page says:
A common neologism in the Python community is pythonic, which can have a wide range of ...
27
votes
10answers
3k views
I can't program because the code I am using uses old coding styles. Is this normal to programmers?
I have my first real job as programmer, but I can't solve any problems because of the coding style used. The code here:
Does not have comments
Does not have functions (50, 100, 200, 300 or more ...
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 ...
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 ...
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 ...
25
votes
11answers
2k views
Maintenance wise, is `else while` without intervening braces considered safe?
Is else while without intervening braces considered "safe" maintenance wise?
Writing if-else code without braces like below...
if (blah)
foo();
else
bar();
...carries a risk because the ...
25
votes
9answers
954 views
Scientific evidence that supports using long variable names instead of abbreviations?
Is there any scientific evidence that the human brain can read and understand fully written variable names better/faster than abbreviated ones?
Like
PersistenceManager persistenceManager;
in ...
24
votes
21answers
2k views
What are your programming idiosyncrasies? [closed]
I noticed that I have a peculiar habit of finishing every line with a space. It carries over from my prose writing where a paragraph can have multiple sentences and so it is very common to follow a ...
24
votes
9answers
2k views
Simple vs Complex (but performance efficient) solution - which one to choose and when?
I have been programming for a couple of years and have often found myself at a dilemma.
There are two solutions -
one is simple one i.e. simple approach, easier to understand and maintain. It ...
23
votes
8answers
5k views
Should I write HTML or CSS first?
There are a lot of analogies for HTML/CSS development; which can be a bit confuse for a beginner.
HTML = foundations/house
CSS = walls/blueprint/wallpaper
Is there any best practice here? ...
23
votes
6answers
3k views
Why is trailing whitespace a big deal?
Trailing whitespace is enough of a problem for programmers that editors like Emacs have special functions that highlight it or get rid of it automatically, and many coding standards require you to ...
22
votes
9answers
862 views
Intentional misspellings to avoid reserved words
I often see code that include intentional misspellings of common words that for better or worse have become reserved words:
klass or clazz for class: Class clazz = ThisClass.class
kount for count in ...
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 ...
21
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. ...
21
votes
12answers
1k views
When does the “Do One Thing” paradigm become harmful?
For the sake of argument here's a sample function that prints contents of a given file line-by-line.
Version 1:
void printFile(const string & filePath) {
fstream file(filePath, ios::in);
...
20
votes
8answers
1k views
Should comments say WHY the program is doing what it is doing? (opinion on a dictum by the inventor of Forth)
The often provocative Chuck Moore (inventor of the Forth language) gave the following advice (paraphrasing):
"Use comments sparingly. Programs are self-documenting, with a
modicum of help from ...
20
votes
10answers
1k views
Are nullable types preferable to magic numbers?
I have been having a little bit of a debate with a coworker lately. We are specifically using C#, but this could apply to any language with nullable types. Say for example you have a value that ...
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.
...