Coding style is a set of guidelines that helps readability and understanding of the source code.

learn more… | top users | synonyms (1)

96
votes
37answers
50k 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()) { ...
5
votes
3answers
483 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 ...
45
votes
11answers
6k views

Is the 80 character limit still relevant in times of widescreen monitors?

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 ...
69
votes
22answers
16k views

Where do you declare variables? The top of a method or when you need them? [closed]

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 ...
64
votes
19answers
12k 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 ...
93
votes
22answers
6k 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 = ...
25
votes
3answers
3k views

if ('constant' == $variable) vs. if ($variable == 'constant')

Lately, I've been working a lot in PHP and specifically within the WordPress framework. I'm noticing a lot of code in the form of: if ( 1 == $options['postlink'] ) Where I would have expected to ...
18
votes
9answers
3k 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? ...
30
votes
10answers
3k 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 ...
42
votes
11answers
6k 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 ...
16
votes
12answers
5k 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
43
votes
11answers
10k 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. ...
70
votes
10answers
13k 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, ...
62
votes
18answers
20k views

Are `break` and `continue` bad programming practices?

My boss keeps mentioning nonchalantly that bad programmers use break and continue in loops. I use them all the time because they make sense; let me show you the inspiration: function verify(object) ...
22
votes
4answers
7k 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 ...
74
votes
16answers
18k 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 ...
51
votes
15answers
10k 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. ...
4
votes
6answers
2k views

Clarification of “avoid if-else” advice [duplicate]

The experts in clean code advise not to use if/else since it's creating an unreadable code. They suggest rather using IF and not to wait till the end of a method without real need. Now, this if/else ...
6
votes
6answers
2k views

Best practice in setting return value (use else or?)

Whenever you want to return a value from a method, but whatever you return depends on some other value, you typically use branching: int calculateSomething() { if (a == b) { return x; } else ...
9
votes
2answers
299 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 ...] } ...
61
votes
17answers
4k 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 ...
65
votes
19answers
12k 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 ...
39
votes
17answers
4k 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 ...
32
votes
11answers
4k views

Is it okay to make coding style changes on an open source project that doesn't follow best practices?

Recently, I came across a number of open source Ruby (or majority of it was Ruby) projects on GitHub that when checked with a code analyzing tool like Rubocop, create a lot of offenses. Now, most of ...
16
votes
14answers
2k views

Coding standards and coding style, is it that much important?

There are a lot of ways of typing code, but I can't understand what is the good reason to adopt a certain standard or style if it does not change the way the code can be read and understood. Everyone ...
27
votes
15answers
4k views

Space around operators? a=b/2; or a = b / 2; What are the arguments? [closed]

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 ...
13
votes
14answers
659 views

Should we encourage coding styles in favor of developer's autonomy, or discourage it in favor of consistency?

A developer writes if/else blocks with one-line code statements like: if (condition) // Do this one-line code else // Do this one-line code Another uses curly braces for all of them: if ...
5
votes
2answers
412 views

API/Technical Writers Guide [closed]

Is there any good book/tutorial/guide for programmers, which can help them to write good programming documentation/tutorials/guides?
4
votes
3answers
175 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 ...
23
votes
12answers
4k 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 ...
12
votes
8answers
2k views

What is the accepted style for using the `this` keyword in Java?

I come from languages like Python or Javascript (and others that are less object-oriented) and I am trying to improve my working knowledge of Java, which I know only in a superficial way. Is it ...
10
votes
5answers
1k views

What are the downsides of mixing tabs and spaces? [duplicate]

The advantage of using tabs for indentation is that people can configure their editor to use the tab width they are comfortable with. The only argument against this seems to be that people don't want ...
9
votes
7answers
2k views

Keep my classes and methods as small as possible?

A few days ago, I was talking to a Software Engineering PhD candidate and at some point she said to me: Keep your classes and methods as small as possible And I wonder if this is always a good ...
8
votes
9answers
2k 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 ...
14
votes
6answers
7k 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. ...
5
votes
4answers
347 views

In which order should I do comparisons? [duplicate]

I'm a strong proponent of writing if statements like this: variable == constant Because to me it just makes sense, it is more readable than the inverted: constant == variable Which seems to be ...
0
votes
4answers
320 views

Coding convention regarding the usage of underscores [closed]

There seems to be divided opinion on this subject, and I wanted to get people's insights on whether they've found using underscore prefixes and suffixes to be helpful with coding or not. You know, ...
73
votes
8answers
5k views

Should you always pass the bare minimum data needed into a function in cases like this

Let's say I have a function IsAdmin that checks whether a user is an admin. Let's also say that the admin checking is done by matching user id, name and password against some sort of rule (not ...
57
votes
16answers
4k 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 ...
49
votes
17answers
5k 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 ...
67
votes
11answers
4k views

Never use Strings in Java? [closed]

I stumbled upon a blog entry discouraging the use of Strings in Java for making your code lack semantics, suggesting that you should use thin wrapper classes instead. This is the before and after ...
61
votes
6answers
5k 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 ...
43
votes
6answers
18k 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 ...
36
votes
8answers
5k 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 ...
36
votes
11answers
3k views

Is it bad habit not using interfaces? [closed]

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 ...
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
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 ...
17
votes
7answers
999 views

Should a function use premature returns or wrap everything in if clauses? [duplicate]

Possible Duplicate: Where did the notion of “one return only” come from? Which is better? I see pros and cons for both, so I can't really decide on which one to stick to. Wrap ...
0
votes
2answers
266 views

How to enforce good coding style in team? [duplicate]

Even if I don't like enforcing people to do things (and I believe that it may decline the productivity and cause anger), I really want to enforce good coding style. Is there a way to set up ...
25
votes
9answers
1k 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 ...