简体   繁体   中英

Is there a better solution than using an if/then statement for this?

I have this extension detection function, but it relies heavily on numerous lines of if/then statements. Is there a different method I could use to get the same results? (all of the variables are defined at a higher level). I want to deal with less code, and increase performance speeds.

if (extT == 'app') { docType = 'app'; docDesc = 'Application'; }
        else if (extT == 'exe') { docType = 'exe'; docDesc = 'Executable File'; }
        else if (extT == 'vwx') { docType = 'vwx'; docDesc = 'Vectorworks File'; }
        else if (extT == 'torrent') { docType = 'torrent'; docDesc = 'BitTorrent Document'; }
        else if (extT == 'ipsw') { docType = 'ipsw'; docDesc = 'Apple Device Software Update File'; }
        else if (extT == 'mpkg') { docType = 'mpkg'; docDesc = 'Installer package'; }
        else if (extT == 'prefPane') { docType = 'prefPane'; docDesc = 'Mac OS X Preference Pane'; }
        else if (extT == 'ogg') { docType = 'ogg_audio'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'ogv') { docType = 'ogv'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'flac') { docType = 'flac_audio'; docDesc = 'FLAC Audio File'; }
        else if (extT == 'ttf') { docType = 'ttf'; docDesc = 'TrueType Font'; }
        else if (extT == 'otf') { docType = 'otf'; docDesc = 'OpenType Font'; }
        else if (extT == 'air') { docType = 'air'; docDesc = 'Rich Internet Application'; }
        else if (extT == 'csproj') { docType = 'csproj'; docDesc = '.NET Source Code'; }
        else if (extT == 'download') { docType = 'download'; docDesc = 'File Download'; }
        else if (extT == 'log') { docType = 'log'; docDesc = 'Log File'; }
        else if (extT == 'conf') { docType = 'conf'; docDesc = 'IRC Related Configuration File'; }
        else if (extT == 'command') { docType = 'command'; docDesc = 'Terminal Shell Script'; }
        else if (extT == 'plist') { docType = 'plist'; docDesc = 'Property List XML File'; }
        else if (extT == 'mobileprovision') { docType = 'Developer Provisioning Profile'; docDesc = ''; }
        else if (extT == 'ds_store') { docType = 'DS_Store'; docDesc = 'Desktop Services Store'; }
        else if (extT == 'asp') { docType = 'asp'; docDesc = 'Active Server Pages'; }
        else if (extT == 'safariextz') { docType = 'safariextz'; docDesc = 'Safari Extension'; }
        else if (extT == 'qlab2license') { docType = 'QLab2License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlablicense') { docType = 'QLabLicense'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab1license') { docType = 'QLab1License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab3license') { docType = 'QLab3License'; docDesc = 'QLab License File'; }
        else if (extT == 'saver') { docType = 'saver'; docDesc = 'Mac OS X Preference Screen Saver'; }
        else if (extT == 'pkg') { docType = 'pkg'; docDesc = 'Installer Package'; }
        else if (extT == 'w3x') { docType = 'w3x'; docDesc = 'Warcraft III: The Frozen Throne'; }
        else if (extT == 'fcp') { docType = 'fcp'; docDesc = 'Final Cut Project File'; }
        else if (extT == 'pages') { docType = 'pages'; docDesc = 'Pages Publication'; }
        else if (extT == 'numbers') { docType = 'numbers'; docDesc = 'Numbers Document'; }
        else if (extT == 'key') { docType = 'key'; docDesc = 'Keynote Document'; }
        else if (extT == 'wwf') { docType = 'wwf'; docDesc = 'WWF Non-printable Document'; }
        else if (extT == 'swd') { docType = 'swd'; docDesc = 'Shockwave Document'; }
        else if (extT == 'rtfd') { docType = 'rtfd'; docDesc = 'NeXT RTF-Packet'; }
        else if (extT == 'wtpl') { docType = 'wtpl'; docDesc = 'WireTap Studio Library'; }
        else if (extT == 'abbu') { docType = 'abbu'; docDesc = 'Address Book Archive File'; }
        else if (extT == 'thllibrary') { docType = 'thllibrary'; docDesc = 'The Hit List Library'; }
        else if (extT == 'logic') { docType = 'logic'; docDesc = 'Logic Project File'; }
        else if (extT == 'pat') { docType = 'pat'; docDesc = 'Adobe Photoshop Patterns File'; }
        else if (extT == 'dwg') { docType = 'dwg'; docDesc = 'AutoCAD Image'; }
        else if (extT == 'ctb') { docType = 'ctb'; docDesc = 'AutoCAD Color-Based Plot Style File'; }
        else if (extT == 'stb') { docType = 'stb'; docDesc = 'AutoCAD Plot Style Table File'; }
        else if (extT == 'lck') { docType = 'lck'; docDesc = 'Lock File'; }
        else if (extT == 'aep') { docType = 'aep'; docDesc = 'After Effects Project'; }
        else if (extT == 'prtl') { docType = 'prtl'; docDesc = 'Adobe Permiere Title File'; }
        else if (extT == 'prproj') { docType = 'prproj'; docDesc = 'Adobe Premiere Project'; }
        else if (extT == 'band') { docType = 'band'; docDesc = 'GarageBand Project File'; }
        else if (extT == 'h') { docType = 'h'; docDesc = 'Header File'; }
        else if (extT == 'c') { docType = 'c'; docDesc = 'C/C++ Program File'; }
        else if (extT == 'r') { docType = 'r'; docDesc = 'Rez Source Code File'; }
        else if (extT == 'xcodeproj') { docType = 'xcodeproj'; docDesc = 'XCode Project'; }
        else if (extT == 'xcconfig') { docType = 'xcconfig'; docDesc = 'Xcode Configuration Settings'; }
        else if (extT == 'rcproject') { docType = 'rcproject'; docDesc = 'iMovie Project'; }
        else if (extT == 'cache') { docType = 'cache'; docDesc = 'Cache File'; }
        else if (extT == 'exs') { docType = 'exs'; docDesc = 'EXS24 instrument file'; }
        else if (extT == 'sdir') { docType = 'SDIR'; docDesc = 'Space Designer Impulse Response'; }
        else if (extT == 'ovw') { docType = 'ovw'; docDesc = 'Cool Edit Pro Overview File'; }
        else if (extT == 'lwp') { docType = 'lwp'; docDesc = 'LightWright Project'; }
        else if (extT == 'bin') { docType = 'BIN'; docDesc = 'Binary File'; }
        else if (extT == 'ini') { docType = 'INI'; docDesc = 'Initialization/Configuration File'; }
        else if (extT == 'ex_') { docType = 'EX_'; docDesc = 'Compressed EXE File'; }
        else if (extT == 'bat') { docType = 'bat'; docDesc = 'Microsoft Batch File'; }
        else if (extT == 'edml') { docType = 'edml'; docDesc = 'Adobe Extension Data Markup Language (EDML) Document'; }
        else if (extT == 'mak') { docType = 'MAK'; docDesc = 'MakeFile'; }
        else if (extT == 'cfm') { docType = 'cfm'; docDesc = 'Adobe ColdFusion Document'; }
        else if (extT == 'indd') { docType = 'indd'; docDesc = 'Adobe InDesign Document'; }
        else if (extT == 'indt') { docType = 'indt'; docDesc = 'Adobe InDesign Template'; }
        else if (extT == 'asnd') { docType = 'asnd'; docDesc = 'Adobe Sound Document'; }
        else if (extT == 'cpp') { docType = 'cpp'; docDesc = 'C++ Source'; }
        else if (extT == 'aac') { docType = 'aac'; docDesc = 'MPEG-2 Advanced Audio Coding File'; }
        else if (extT == 'm2v') { docType = 'm2v'; docDesc = 'MPEG-2 Blu-ray File'; }
        else if (extT == 'olproj') { docType = 'olproj'; docDesc = 'Adobe OnLocation Project'; }
        else if (extT == 'ipa') { docType = 'ipa'; docDesc = 'iOS Application'; }
        else if (extT == 'apk') { docType = 'apk'; docDesc = 'Android Application'; }
        else if (extT == 'scpt') { docType = 'scpt'; docDesc = 'Script File'; }
        else if (extT == 'applescript') { docType = 'AppleScript'; docDesc = 'AppleScript File'; }
        else if (extT == 'emlxpart') { docType = 'emlxpart'; docDesc = 'Mail Message Attachment'; }
        else if (extT == 'emlx') { docType = 'emlx'; docDesc = 'Mail Message'; }
        else if (extT == 'webbookmark') { docType = 'webbookmark'; docDesc = 'Safari Bookmark'; }
        else if (extT == 'jsx') { docType = 'jsx'; docDesc = 'Adobe JavaScript File'; }
        else if (extT == 'icns') { docType = 'icns'; docDesc = 'Apple Icon Image'; }
        else if (extT == 'caf') { docType = 'caf_audio'; docDesc = 'Apple Loop'; }
        else if (extT == 'cues') { docType = 'cues'; docDesc = 'QLab Workspace'; }
        else { docType = '--'; }

Use an object as a map.

var map = {
    "exe": {type:"exe",desc:"Executable File"},

    "vwx": {type:"vwx",desc:"Vectorworks File"}
};

var extT = "exe";

var docType = map[extT].type;
var docDesc = map[extT].desc;

http://jsfiddle.net/ZfbHf/1/

Advantages

  • Lists the data as actual data, and then contains only 2 lines of assignment
  • data could be loaded from a JSON file, removing it from the code entirely and making it configurable without changing code
  • clear structure to data
  • clear logic thats easy to follow (no risk of somebody later adding a random line of code in one of those if statements making it hard to debug)

It won't look like less code, but if you use an object to create a map, it should be more efficient:

var types = {
    'app': { 'docType': 'app', 'docDesc': 'Application' }, 
    'exe': { 'docType': 'exe', 'docDesc': 'Executable File' }, 
    ...
};

Then use it as a lookup:

var curType = types['app'];

Basically all that @ben336 said - use his answer

You could use a case/switch statement, but you're essentially going to be in the same boat, and (to my knowledge) there is really no performance difference between the two. Then again, I'm basing that last statement on my C# knowledge. Maybe preload this info into an array of some sort, pull it from a database or something? That way you could just make a call on it based on a parameter you pass to it, and simply return the value you need.

It's not clear what platform you're using other than javascript, so can't really go too much further in giving an answer. My 2 cents worth, at least....

Note Looking at Ben336 and dc5's answer, that also looks like viable options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM