Linked Questions

21 votes
7 answers

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 ...
JoJo's user avatar
  • 1,495
2 votes
2 answers

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 ...
user avatar
2 votes
1 answer

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 ...
datguy's user avatar
  • 121
4 votes
1 answer

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(...
ConditionRacer's user avatar
302 votes
19 answers

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) ...
59 votes
11 answers

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 ...
void.pointer's user avatar
  • 5,073
23 votes
7 answers

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 ...
asyncwait's user avatar
  • 407
20 votes
7 answers

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 -...
rhino's user avatar
  • 357
10 votes
6 answers

Why are people coding "C-style C++"?

In discussions about whether to cast the result of malloc or not when coding C, one common argument is that if you cast the result then you can compile your C code with a C++ compiler. Why would one ...
klutt's user avatar
  • 1,438
2 votes
6 answers

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 { ...
user74130's user avatar
  • 173
3 votes
4 answers

Alternative to nested-if on single return functions

I'd like to see if there has been any precedent on alternatives to nested-ifs--particularly for error-code returns. My workplace requires one return per function, so I cannot early exit. Here is some ...
Saxpy's user avatar
  • 41
1 vote
2 answers

What is the cleanest way of writing a function with conditional statement with many returning options?

Maybe that's simple, but I'm a little confused. I have such a Ruby code: def my_function(found_objects) # ... if found_objects.second return CoreObjectFactory.get_object(found_objects.second,...
Karol Selak's user avatar
2 votes
4 answers

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; ...
Seraphim's host's user avatar
1 vote
1 answer

Which statements can be considered as exit points?

There are many discussions related to whether it is better to have only one or multiple exit points per function, but it is not clear to me which statements can be considered as exit points: only ...
user90726's user avatar
  • 205
3 votes
4 answers

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(...
Kevin's user avatar
  • 844

15 30 50 per page