简体   繁体   English

是否有比使用if / then语句更好的解决方案?

[英]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. 我具有此扩展名检测功能,但它严重依赖于if / then语句的许多行。 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/ 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 可以从JSON文件加载数据,将其从代码中完全删除,并使其可配置而不更改代码
  • 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) 易于遵循的清晰逻辑(没有风险,以后有人在其中的if语句之一中添加随机代码行使其难以调试)

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 基本上所有@ ben336所说的-使用他的答案

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. 可以使用case / switch语句,但实际上您将处于同一条船上,并且(据我所知)两者之间确实没有性能差异。 Then again, I'm basing that last statement on my C# knowledge. 再说一次,我将最后的陈述基于我的C#知识。 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. 目前尚不清楚您使用的是JavaScript以外的平台,因此在给出答案方面并不能走得太远。 My 2 cents worth, at least.... 我至少要花2美分。

Note Looking at Ben336 and dc5's answer, that also looks like viable options. 注意查看Ben336和dc5的答案,这似乎也是可行的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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