0

Often times, I find myself creating two objects like these:

String pathString = "/foo/bar";
File path = new File(pathString);

Though variable naming is a fairly subjective issue, what's the most common naming convention for File objects and their corresponding absolute file path stored in a String?

1
  • I don't think there's a universally accepted convention, though certain projects may have their own. In this particular case I'd either call the first variable pathName or get rid of it altogether. Commented Oct 6, 2014 at 11:01

4 Answers 4

1

I guess there is no naming convention. I would either take a look at the constructor argument of File and name it after that, e.g.

String pathname = "/foo/bar";
File file = new File(pathname);

Normally the file has a meaning in your application. So I would choose a name that describes it. E.g.

String errorLogFilepath = "/var/log/error.log";
File errorLogFile = new File(errorLogFilepath);
Sign up to request clarification or add additional context in comments.

Comments

0

Do you need to idependently identify the path?

If not, Id suggest just using:

File path = new File("/foo/bar");

Similarly, pass around File objects, not string paths.

Comments

0

If you are not reusing this path for further reference in the code , suggest you to initialize the file by passing an arguement. eg

File file = new File("/foo/bar");

This will prevent the creation of an extra string variable.

Comments

0

I always use double backslash and I've never have problem with that:

File outputfile = new File("c:\\Selenium\\workspace\\....\\input.txt");

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.