1

Which of the following is best practice according to Java coding standards

public void function1(){
 boolean valid = false;
 //many lines of code
 valid = validateInputs();
 //many lines of code
}

or

public void function1(){
 //many lines of code
 boolean valid = validateInputs();
 //many lines of code
}

Here 'valid' will not be for returning. Its scope is internal to the function only. Sometimes only in one if condition

I usually code similar to the second case. It seems my superior does not like this and modifies the code when I put it for review. Is there some specific reason that my approach is not correct?

The disadvantage I see for the first approach is that it is very difficult to refactor the method to multiple methods at a later point.

7
  • 2
    If this is a completely representative case, then no, there's no reason for your superior to alter it. Commented Jun 11, 2013 at 12:13
  • I think it's a C-ism rather than better or worse practice (ISO C90 forbids mixed declarations & code) Commented Jun 11, 2013 at 12:14
  • 5
    I would say the problem is the "many lines of code." If your methods are so long that this really matters, then you have other problems. Commented Jun 11, 2013 at 12:16
  • stackoverflow.com/q/8144890/591801 Commented Jun 11, 2013 at 12:17
  • 1
    i'd suggest migrating this to code review... Commented Jun 11, 2013 at 12:23

4 Answers 4

5

I would go for the second approach - not much a matter of Java coding standards here, but a matter of clean and readable code. Also, you assign the value false to valid in the first case, but that's not really correct as valid shouldn't have any value at that point.

On a side note, I won't expect a method called validateInputs() to return a boolean. There's no parameter passed, and the name is not giving an hint that the method would return something. What about refactoring your code to something like boolean validInput = isValid(input)?

Sign up to request clarification or add additional context in comments.

3 Comments

The first version might be flagged by tools like FindBugs as "unread value" (between initialisation and call) yielding to the third style boolean valid;.
what i meant is any method call and any return variable, not just a validate method and a boolean return value. A validate function was the easiest to put out as an example.
@afxgx, that's ok - I just wanted to point out that readable code is one topic we should worry a lot, as the world is already full of crap code and we don't want to make it worst.
2

I would prefer to only declare variables within the scope they are used. This avoid accidentally using it when you shouldn't and means you can see both the declaration and the usage together, instead of having to jump to the start of your code to find it.

In the C days, you had to use the first form, because the compilers were not very smart. But the second form was added as it made the code easier to understand AFAIK.

Comments

2

Whichever is better is a matter of personal taste. Every place has its own standards, so you should follow it at work.

That's one more reason I think every programmer should have their own personal projects. That way you can also code in your own style at home, so you don't get your mind stuck with just one style.

5 Comments

It's not really a matter of personal taste. The first is more error prone, as @Peter said.
Without knowing more context, I wouldn't call it error prone. False might be a default value for returning, should the validateInputs method never be called due to the rest of the logic.
@Renan 'valid' will not be for returning. Its scope is internal to the function only. Sometimes only in one if condition
@afxgx You knew that beforehand, but nobody else here did.
@Renan I have added it to the question
2

There should always be reasoning behind a decision.

The second example is better because it is good to initialize values in the declaration.

Google has a good set of standards that is applicable to many C-type languages. The example you are referring to is shown in the 'local variables' section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.