如何优化此代码?如何让它更优雅?(C#) [英] How to optimize this code? How to make it more elegant?(C#)

查看:68
本文介绍了如何优化此代码?如何让它更优雅?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a;
         int b;
         int c;
         int d;
         if (a > b)
         {
             c = a;
             d = b;
         }
         else
         {
             c = b;
             d = a;
         }

推荐答案

没什么可说的。最多,你可以争取的只是更具可读性。你所展示的内容需要大量扫描上下来发现当时和其他部分之间的差异。看看以下内容;只是将分配放在更近的位置可以使差异更明显。



There's not much to that. At most, all you can strive for is "more readable". What you show requires a lot of scanning up-and-down to spot the differences between the then and else parts. Have a look at the following; just putting the assignments in closer proximity can make the differences more apparent.

if (a > b) { c = a ; d = b ; }
else       { c = b ; d = a ; }


虽然您可能无法优化或使其更优雅,但您可以修复它(您正在使用未初始化的变量)。
While you probably can't either optimize or make it more elegant, you might fix it (you are using uninitialized variables).


在没有上下文的情况下优化代码是没有意义的,即使使用上下文,代码也太简单甚至无法理解。



如果它有效并且你理解然后离开它。
Optimization of the code make no sense without context and even with context the code is too simplistic to even bother.

If it works and you understand what it does then leave it.


这篇关于如何优化此代码?如何让它更优雅?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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