更好的简单任务技术

huangapple go评论74阅读模式
英文:

Better techniques for simple tasks

问题

我试图编写一个相当大的应用程序,这是我第一次尝试。它接受大量输入,我需要清理这些输入,这似乎相当简单,但我似乎陷入了“好吧”的编码循环中。我想要一些帮助或指导,如何更有效地完成我的任务。例如,在这里,需要检查输入是否满足3个条件,代码可以正常工作,但看起来就像是一个学前班的孩子写的,我似乎卡住了,因为我甚至不知道从哪里开始寻找另一种方法。我可能需要检查多达40个元素,而40个if语句似乎太荒谬了。

要使我的编码风格更高效,我应该研究哪些技术?我真的怀疑这不是专业人士在查看我的作品集时所期望的。我想要改进。

编辑:这只是一个模拟,我知道最后的if语句可以压缩成一个语句。我更多地是指if语句,检查条件。有没有一种可能检查30-40个条件而不需要30多个if语句的方法。

英文:

So I'm trying to write a fairly large application, one of my first. It accepts quite a lot of input which I need to clean which was fairly simple but I seem to be stuck in a loop of "okay" coding. I would like some help or directions on how I get better at making my tasks more efficient. For example here, an input needs to be checked for 3 things and the code works fine but it just seems like something a pre-schooler would write and I seem stuck doing it as I don't even know where to begin searching for another way. I may need to check upto 40 elements and 40 if statements just seems ridiculous.

 bool[] CheckList = new bool[3];

            if (!input.Contains("a"))
            {
                CheckList[0] = true;
            }
            if (!input.Contains("b"))
            {
                CheckList[1] = true;
            }
            if (!input.Contains("c"))
            {
                CheckList[2] = true;
            }

            if (CheckList[0] == true && CheckList[1] == true && CheckList[2] == true)
            {
                return "SUCCESS";
            }
            else
            {
                return "FAILED";
            }

What techniques should I look into to adapt my coding style to a more efficient approach, I really doubt this is what professionals are looking for when they are viewing my portfolio. I want to improve.

EDIT: This was just a mockup, the if statement at the end I know can be compacted into one statement. I'm referring more to the if statements, checking conditions. A way of plausibly checking 30-40 conditions without 30+ if statements.

答案1

得分: 1

将您要检查的字符放入List中并进行操作:

var chars = new List<char> { 'a', 'b', 'c' };
return chars.All(g => !input.Contains(g))
    ? "SUCCESS"
    : "FAILED";
英文:

Place your chars you want to check into List and work with it:

var chars = new List&lt;char&gt; { &#39;a&#39;, &#39;b&#39;, &#39;c&#39; };
return chars.All(g =&gt; !input.Contains(g))
    ? &quot;SUCCESS&quot;
    : &quot;FAILED&quot;;

huangapple
  • 本文由 发表于 2020年1月6日 02:46:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定