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

learn more… | top users | synonyms (1)

-2
votes
2answers
92 views

which is a better practice one method that does everything or a series of different methods?

I'm working with asp.net and c# Lets say I have a bunch of drop-downs and I want to bind data from a database, is it better to make a master-bind method that loops each one and gets the parameters to ...
12
votes
2answers
884 views

Are there well-known PowerShell coding conventions?

Are there any well-defined conventions when programming in PowerShell? For example, in scripts which are to be maintained long-term, do we need to: Use the real cmdlet name or alias? Specify the ...
76
votes
19answers
17k views

Should I return from a function early or use an if statement?

I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why. public void SomeFunction(bool someCondition) { if (someCondition) ...
1
vote
2answers
121 views

Is it better to perform a calculation in the field's setter or have a different method?

I'm implementing a simple Quota object which determines a usage percentage based on the maximum and the used. private int maximum; private int used; public Quota(int used, int maximum) { ...
95
votes
14answers
15k views

Is it always a best practice to write a function for anything that needs to repeat twice?

For myself, I can't wait to write a function when I need to do something more than twice. But when it comes to the things that only appear twice, it's a bit more tricky. For code that needs more than ...
8
votes
5answers
793 views

How to structure a loop that repeats until success and handles failures

I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another ...
50
votes
4answers
8k views

Should I encourage junior developers to use explicit or implicit typing? [closed]

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 ...
2
votes
4answers
249 views

How does a developer code in anticipation of change? [closed]

I ask this question based on the fact that currently my environment is under constant change due to the type of work we do. We do not always work on a project bases we often have smaller changes that ...
3
votes
4answers
252 views

int * vs int [N] vs int (*)[N] in functions parameters. Which one do you think is better?

When programming in C (or C++) there are three different ways to specify the parameter in a function that takes an array. Here is an example (implementing std::accumulate from C++ in C) that shows ...
11
votes
4answers
387 views

When should a private method take the public route to access private data?

When should a private method take the public route to access private data? For example, if I had this immutable 'multiplier' class (a bit contrived, I know): class Multiplier { public: ...
13
votes
7answers
2k views

What are the pros and cons of a language using whitespace vs. {}'s to indicate scope? [closed]

There seems to be a conflict over whether its better to use whitespace or tokens like brackets to indicate scope. I've seen many praise python's solution to the inconsistent indentation problem, but ...
0
votes
2answers
118 views

Using the optional 'self' reference in instance methods in Swift as a matter of style

Swift allows optional prefixing of method calls and property references to the current object instance via self. Removing these prefixes can declutter code but, depending on the length of the method ...
1
vote
3answers
90 views

Nullable enumeration values vs. “NoValue” or “Undefined”, etc

I often write code which translates entities in the database to domain objects. These entities often have fields which are constrained and translate to enumerations in the domain objects. In some ...
67
votes
11answers
5k 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 ...
12
votes
5answers
757 views

Evolution in coding standards, how do you deal with them?

How do you deal with evolution in the coding standards / style guide in a project for the existing code base? Let's say someone on your team discovered a better way of object instantiation in the ...
0
votes
2answers
30 views

Are there established guidelines for code formatting in included files with references to other files?

When developing for the web you will inevitably end up referencing a lot of files that eventually all get included and combined into a finished product. I specifically develop a lot of Wordpress sites ...
1
vote
1answer
139 views

Where should I place a typedef when used in method signatures in C++?

I'm using an Optional class quite similar to that of boost. For semantic reasons, I switched an attribute of the same (structured) type in some class definitions (and therefore also in method ...
11
votes
4answers
1k views

If you favor “T *var”, do you ever write “T*”? [duplicate]

Possible Duplicate: int* i; or int *i; or int * i; Thinking about where we place our asterisks; how do those that prefer to keep the "pointerness" away from the type and with the identifier ...
2
votes
1answer
171 views

Redundant ElseIf-Else Blocks [duplicate]

These types of if-elseif-else blocks appear all over the place, and in no small number (so the less the better). Every time I have to think and decide: Do I want the simpler or the more thorough of ...
2
votes
2answers
314 views

Should we refactor our existing codebase to use functional programming, especially streams? [closed]

We have a large project written in PHP. It almost exclusively uses imperative operations, as in example 1. Should we refactor our existing code to use functional operations? Do you think that the code ...
1
vote
3answers
155 views

Coding convention question about AS3 duplicate variable definition

AS3's got some awkward rules about variable scope, due to its use of hoisting. I don't like pointlessly leaving a bunch of compiler warnings lying around, but it's more important to me for my code to ...
8
votes
3answers
886 views

Reason for placing function type and method name on different lines in C

I just started at a company and one of the style comments at my first code review was that the return type and method name should be on different lines. For example, this void foo() { } should be ...
0
votes
2answers
414 views

“positively formulated” vs. “reads as english sentence” style logic [duplicate]

Sorry, I'd be happy to have someone improve the title of this question. What are the benefits and downsides of "positively formulated" logic vs. having it "english readable". I'm looking for a ...
0
votes
2answers
75 views

PHP conditional test func call against two values

I was wondering if there's a one liner for PHP that would allow me test multiple values against a function call. As example, say I want to test if foo() returns either 1 or 2, (in pseudo code) if( ...
13
votes
7answers
2k views

A defense for boilerplate?

To me, boilerplate code is obviously bad. However I've met a developer who displays resistance in any attempt to reduce boilerplate. I realized I didn't have a readily formed, well thought out ...
17
votes
15answers
2k views

I hate one of our coding standards and it drives me insane, how to process it? [closed]

Disclaimer: Not as exaggerated as the title suggests, but it still makes me uncomfortable. I'm just going to express honestly, so take it with a grain of salt. Just pretend that I'm talking about that ...
21
votes
6answers
3k views

Why the recent shift to removing/omitting semicolons from Javascript?

It seems to be fashionable recently to omit semicolons from Javascript. There was a blog post a few years ago emphasising that in Javascript, semicolons are optional and the gist of the post seemed to ...
-1
votes
3answers
167 views

Cleaner C# without unneeded indents [closed]

In OO languages, at least C#, everything has to be in a class. Sometimes, everything is in a namespace as well. Just about literally all the code in one class is going to be automatically indented ...
5
votes
1answer
175 views

Why is the use of JavaScript in HREF attributes discouraged?

Disclaimer: I came to Programmers.SE to ask this question because I understand this is the place to ask this type of question, and not necessarily stackoverflow. If I am wrong, please close the ...
3
votes
5answers
2k views

What is “using namespace” pollution?

I was looking at the google coding guide [here] and they do not recommend that one use the using namespace ornamespace:function` - if I did not misinterpret it. Does this apply to std as well? ...
1
vote
1answer
942 views

How to break these long C++ lines in a neat way? [closed]

I'm on my first bigger C++ project and find that I have some really long lines. My goal is to break them to 79 columns, but I do not really know how to do this in a neat way. Are there some guidelines ...
0
votes
0answers
43 views

type name for state machine state (as opposed to other state variables)

I need to create a typedef for a state machine state, e.g. enum ToasterStateMachineState { TSM_READY, TSM_TOASTING, TSM_DONE }; But ToasterStateMachineState seems verbose and redundant; is ...
5
votes
3answers
812 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 ...
59
votes
15answers
13k 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. ...
-1
votes
7answers
265 views

Single line comments for multiple indented lines of code [closed]

After many years of coding, trying various programming styles, weeding out unreadable or impractical stuff, I still can't figure out one thing: what is the best way to single-line-comment multiple ...
3
votes
1answer
113 views

Should a class explicitly implement interface if its superclass also implements it?

Given an interface interface I { one(); two(); } An abstract base class partially implementing I abstract class A { @Override void one() { //something } } And lastly ...
5
votes
4answers
2k views

Should you ever use private on fields and methods in C#?

I am somewhat new to C# and just found out that: in C# all of the fields and methods in a class are default private. Meaning that this: class MyClass { string myString } is the same as: class ...
7
votes
5answers
742 views

Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?

I'm having a bit of a debate with my friend over whether these two practices are merely two sides of the same coin, or whether one is genuinely better. We have a function which takes a parameter, ...
0
votes
2answers
92 views

Print Statements Inside Function Or Before Calling It? [closed]

I have a script which different people may use. I have print statements so people can follow along what the script is doing, and if it breaks where it went wrong, and if a certain step takes a long ...
2
votes
2answers
200 views

At what point should you collapse many parameters into (e.g.) struct to improve readability in function headers?

While making a struct creates some overhead at run-time, packaging a bunch of frequently-used-together variables can dramatically increase code readability. How do you balance the two? I was just ...
0
votes
0answers
76 views

Improving the speed of coding in Fortran [duplicate]

I've got about 1.5 years of experience in writing Fortran 2003 codes for scientific applications (duh), and I'm finding myself now in the situation in which I need to write working code as fast as ...
2
votes
3answers
412 views

Should one value simpler code over performance when returning multiple values?

I'm too often facing situations where I need to get several types of information from a method. I usually think long and hard to circumvent these situations but I'm thinking it's pointless work that ...
26
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 ...
72
votes
20answers
13k 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 ...
26
votes
4answers
20k views

Why is #include <iostream.h> bad?

I was reading another thread where a guy asked about C++ books for beginners, and one of the programmers answering wrote this: Some warnings: avoid all books that present an "hello world" stating ...
8
votes
7answers
318 views

How does a variable introduce state?

I was reading the "C++ Coding Standards" and this line was there: Variables introduce state, and you should have to deal with as little state as possible, with lifetimes as short as possible. ...
15
votes
16answers
1k views

Do else blocks increase code complexity? [closed]

Here is a very simplified example. This isn't necessarily a language-specific question, and I ask that you ignore the many other ways the function can be written, and changes that can be made to it.. ...
107
votes
17answers
22k 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 ...
6
votes
2answers
515 views

Which is a better pattern (coding style) for validating arguments - hurdle (barrier) or fence? [duplicate]

I don't know if there are any accepted names for these patterns (or anti-patterns), but I like to call them what I call them here. Actually, that would be Question 1: What are accepted names for these ...
2
votes
1answer
123 views

RefactorException: Good idea or bad idea?

When I'm doing large scale refactors I'm often commenting out the contents of methods and using NotImplementedExceptions for stuff that I still need to refactor. Problem is that this is interfering ...