Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question of optimization

 
Pablo Olmos
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Recently a question came up with a coworker. What is faster and more robust to do?:



or



The question came up because the original code was:



It is unknown if the path string contains a trailing slash ("/"). So if a slash isn't concatenated between the path and file name, the created File object would not be a valid representation of the file.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that the proper directory separator is not necessarily a '/', the first one is simply wrong. There's a static variable in the File class that contains the right characters; but personally I'd use #2. Don't worry about efficiency at this micro-level -- worry about using efficient algorithms.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Ernest. #2 is more robust, and that trumps speed per default.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic