如何删除通用杂乱(例如多个Console.WriteLine) [英] How do I remove generic clutter (such as multiple Console.WriteLine)

查看:47
本文介绍了如何删除通用杂乱(例如多个Console.WriteLine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我有关于删除重复命令的问题,例如 Console.WriteLine StringBuilder.Append 写了很多东西。



我们都知道我们不应该重复自己(DRY)而且可能更有效 - 不那么混乱的方式来实现相同的Consoel输出,文件输出或其他东西,并且mabye甚至改进了诸如可维护性之类的东西。

我不想发垃圾邮件,所以我在这里放了一个secreenshot而不是代码示例

(我知道这写得太可怕,但它告诉我们我遇到的问题)



这会占用屏幕上的空间,因此会使代码更加混乱。我无法想到一种排除它的方法,可能在资源文件中,然后使用替换[InsertDateHere]之后的标签,然后用 DateTime.UtcNow 替换String.Replace()



请告诉我是否有一种普遍接受的方式,因为我认为这是糟糕的编程和我想要支持它。



感谢所有帮助,祝你有愉快的一天。



真诚地:

~Jonas

Hi
I have a question about removing repetetive Commands, such as Console.WriteLine or StringBuilder.Append when writing many things.

We all know that we shouldn't repeat ourself (DRY) and maybe there's a more effient - less cluttering way to archieve the same Consoel Output, File Output or other things, and mabye even improve such things like Maintainability.
I dont want to spam, so I put a secreenshot here instead of a code example
(I know that it's horrible written, but it shows us the Problem I have)

This takes away space on my screen, and so it makes the code more confusing. I can't really think of a way of excluding it, perhaps in an resourcefile and then replace tags like [InsertDateHere] later with DateTime.UtcNow using String.Replace()

Please tell me if there's a common accepted way of doing so, because I think that this is bad programming and I want to impreove it.

Thanks for all your help guys, have a nice day.

Sincerely:
~Jonas

推荐答案

这里很难给出一些建议,取决于上下文和抽象的需要。

查看代码5秒钟后的第一个想法:

创建一个表示报告/文档的格式字符串(无论你怎么称呼它),输入所有参数。但如果这段代码会更好(可维护),我不知道...



另外我不明白为什么这应该是糟糕的编程,你只是没有报告/文档的抽象。在我的意见中,如果再次使用相同的代码,则只会违反DRY原则,而不是如果您使用不同的参数连续调用相同的Routine 1000行....
Hard to give some advice here, depends on the context and the need for abstraction.
First idea after looking 5 seconds to your code:
Create a format-string that represents your Report/document (whatever you call it), feed in all parameters. But if this code will be any better (maintainable), I don't know...

Also I don't see why this should be bad programming, you just don't have an abstraction for your Report/document. In my opinition DRY priniciple is only violated if you use the same code again, not if you call the same Routine 1000 lines in succession with different parameters....


首先,您的代码示例对我来说似乎不是坏代码。即使你的代码示例有一百个'Console.WriteLine语句,对我来说也不会坏。



编辑#1:并且,所有对StringBuilder.Append的调用对我来说都不是邪恶的:我更愿意看到我可以直观地了解它正在做的代码,而不是看到一个难以理解的巨大陈述。当您确信代码已准备就绪时,您总是可以使用一些简单的搜索和替换来合并这样的语句。并且,当代码生产就绪时,您可以对字符串内容的位置做出其他选择(在资源中,加密,压缩等)。在任何情况下使用StringBuilder几乎总是一件好事!



我的评估将取决于我查看代码的上下文:如果你的代码是在开发中,我看到了一百个'Console.WriteLines,我可能会问你为什么没有采用另一种调试/测试策略,比如使用一些单元测试框架的正式单元测试。并且,您的有效回复可能类似于:您正在查看原型;当我们达到概念验证时,我们将转向正式的单元测试。 编辑#2:如果这是一个控制台应用程序,Console.WriteLine就可以了:为什么要穿着燕尾服装扮一个chicen?



还有其他方法可以用来装饰程序,以便某些代码仅在满足某些条件时执行。其中一种方法是预处理器指令,它可能非常复杂或非常简单:[ ^ ]。这里有关于这些的CodeProject教程:[ ^ ]。



编辑#3L 恢复消失代码示例:使用预处理器指令很容易推出自己的替代方案;例如:
First, your code example doesn't seem like "bad code" to me. Even if your code sample had one-hundred 'Console.WriteLine statements, it would not seem bad to me.

edit #1: And, all those calls to StringBuilder.Append don't look "evil," to me, either: I'd much rather see code I can follow along visually with what it's doing, than see one giant statement that's unreadable. You can always use some simple search-and-replaces to consolidate statements like that when you are confident the code is production ready. And, when code is production ready you might make other choices about where the string content goes (in a Resource, encrypted, compressed, etc.). In any case using StringBuilder is almost always a very good thing !

My evaluation would depend on the context in which I viewed your code: if your code was in development, and I saw one-hundred 'Console.WriteLines, I might ask you why you had not adopted another debugging/testing strategy like formal unit-tests using some unit-testing framework. And, your valid reply might be something like: "you're looking at a prototype; we'll move to formal unit-testing when we reach proof-of-concept." edit #2: If this is a Console App, Console.WriteLine is just fine: why dress-up a chicen in a tuxedo ?

There are other ways you can go about decorating your program so that certain code is only executed when certain conditions are met. One of these ways is "Preprocessor Directives" which can be quite complex, or very simple: [^]. There's a CodeProject tutorial on these here: [^].

edit #3L Restoration of "disappeard" code example: it's easy to roll your own alternative to using Preprocessor Directives; for example:
public partial class Form1 : Form
{
    private static bool IsDebug = true;

    // in some method
    private protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if(IsDebug) Console.WriteLine("KeyData is: {0}", keyData);
    }
}

编辑#4:最后,你可以使用'Console.Out方法,向它传递一个类的实例的引用继承.NET的Writer对象之一,基本上覆盖Console.WriteLine的标准行为;这是一个高级主题,我正准备一篇关于在WinForms应用程序中使用它的文章,其中包含(我相信此时)一些原创想法。我将不得不在控制台应用程序中测试它,虽然我很确定它会起作用。

edit #4:Finally, you can use the 'Console.Out method, passing it a reference to an instance a Class that inherits form one of .NET's "Writer" objects, to essentially override the standard behavior of Console.WriteLine; that's an advanced topic, and I am preparing an article for CP on using that in a WinForms Application that contains (I believe at this time) some original ideas. I'll have to test that in a Console App, although I am pretty sure it will work.


这篇关于如何删除通用杂乱(例如多个Console.WriteLine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆