Linked Questions

20
votes
7answers
3k 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 in if ...
1
vote
2answers
8k views

Python - only one return per method? [duplicate]

I'm trying to sort out whether this is just a personal preference thing or whether it's actually bad practice to do this one way or another. I did try to reference PEP8 first, and I did not see an ...
2
votes
1answer
591 views

What are the pros and cons of temporary variables vs multiple returns [duplicate]

Take the following examples: public static String returnOnce() { String resultString = null; if (someCondition) { resultString = "condition is true"; } else { resultString ...
4
votes
1answer
369 views

Should methods always return from one place? [duplicate]

Possible Duplicate: Where did the notion of “one return only” come from? I got into a little argument with a coworker about whether a method should be written like this: public Thing getThing(...
305
votes
19answers
137k views

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

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) ...
56
votes
11answers
42k views

What characteristics or features make code maintainable? [duplicate]

I used to think I knew what this was, until I really started thinking about it... "maintainable"... what exactly makes code maintainable? To me, if code must be maintainable that means we can expect ...
23
votes
7answers
47k views

Avoid too complex method - Cyclomatic Complexity

Not sure how to go about this method to reduce Cyclomatic Complexity. Sonar reports 13 whereas 10 is expected. I am sure nothing harm in leaving this method as it is, however, just challenging me how ...
18
votes
7answers
1k views

Should “else” be used in situations where control flow renders it redundant?

I sometimes stumble upon code similar to the following example (what this function does exactly is out of the scope of this question): function doSomething(value) { if (check1(value)) { return -...
25
votes
5answers
1k views

Are there any scientifically rigorous studies of coding style principles? [closed]

Is a coding style principle - e.g. the single-exit principle - really a good thing? Always, or just sometimes? How much difference does it really make? Whatever your opinions, these are obviously ...
2
votes
6answers
962 views

Stacking keywords on top of each other - poor style? [closed]

I have always wondered about this, especially in C/Java style languages. For example, consider the first 3 lines of this C# code: lock (serviceLock) using (var client = new ServiceClient()) try { ...
2
votes
4answers
220 views

function exit condition on parameter consistency check [duplicate]

When checking for parameter consistency a the top of a function body, what is the best strategy? This one: protected void function(Object parameter) if (parameter == null) return; ...
4
votes
2answers
182 views

Return type declaration and checks

I've been mainly programming PHP, and I recently started with C++. In PHP the return of a function can be of any type, so you can do checks like this: public function doSomething() { if (! this-&...
-1
votes
2answers
252 views

Single exit of function uses goto

Apropos of What kind of bugs do "goto" statements lead to? Are there any historically significant examples? I am not that learned in C, and to me the puzzle is that a single exit of a ...
2
votes
4answers
295 views

If a function contains only a switch, is it bad practise to replace the break; statements with return; statements?

Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code): bool executeSomeAction(...