4

I've been following along with the Intro to Android Apps MOOC on Udacity and have been learning a lot about good coding practices just by looking at the sample code from that course. However, today I found something a little bit puzzling:

// Third Step: Insert ContentValues into database and get a row ID back
    long locationRowId;
    locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);

Why was locationRowId declared on a separate line? It seems like it would be much cleaner to change the code above to

long locationRowId = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, testValues);

Is there a particular reason for including the extra step? Or can I safely use the single-line version.

Thanks

4
  • there is no difference in both of them Commented Jul 30, 2015 at 6:06
  • well in my opinion it is up to you. there is neither a point for or against it. do it as you like, but so it in a consistent way! Commented Jul 30, 2015 at 6:07
  • No difference... But the way how you can make it look better is to take all the needed variables at one place in... and use them afterwards... in this way code remains clean Commented Jul 30, 2015 at 6:09
  • Its only for the sake of clarity, if you happen to revisit this code after months certain practices will really be worth it. Commented Jul 30, 2015 at 6:11

4 Answers 4

2

Yes you can safely use the single line version! As commented above in my opinion it is just your preference. There is neither a point for nor against doing it. BUT choose one option and use it consequently and do not switch between them to get a clean code ;-)

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

Comments

2

There is no difference between the two. The best practice that I can think of here is - Don't declare local variables before use.

If you declare local variables before it is required, it will increase the scope of the variable and it will also likely to increase the chances of errors.

There are cases where you have to declare the variables before it is used. The below is one such example.

String line = null;
while (( line = br.readLine()) != null) {
    buff.append(line);
}

It is up to the place where you are declaring your variable but there are no hard and fast rule to be followed.

Comments

0

It does not matter, becouse you are doing theese same operations. Both expressions are correct, but better looks the second one. Of course it depends on it what do you want and where. If you want to use some of them, use one. Do not mix them

Comments

0

If you are declaring global variables then declare it in a different line. Which means Using same variable in different methods.If you are using a variable inside of the methods then no need to initiate in different line.

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.