我经常发现自己有一种Option[T]类型,T并希望测试选项的价值对某些价值.例如:
val opt = Some("oxbow")
if (opt.isDefined && opt.get == "lakes")
//do something
Run Code Online (Sandbox Code Playgroud)
以下代码是等效的,并删除了测试选项值是否存在的要求
if (opt.map(_ == "lakes").getOrElse(false))
//do something
Run Code Online (Sandbox Code Playgroud)
然而,这对我来说似乎不太可读.其他可能性是:
if (opt.filter(_ == "lakes").isDefined)
if (opt.find(_ == "lakes").isDefined) //uses implicit conversion to Iterable
Run Code Online (Sandbox Code Playgroud)
但我不认为这些明确表达的意图要么更好,因为:
if (opt.isDefinedAnd(_ == "lakes"))
Run Code Online (Sandbox Code Playgroud)
有没有人有更好的方法来做这个测试?
是否有一种方法/快捷方式/内置功能可以对齐操作数的代码,如'='符号.
例如,有一个XAlign for Xcode(https://github.com/qfish/XAlign),允许用户选择需要对齐的代码并使用快捷方式自动对齐.
Android Studio中有一个"字段组"功能,可以"对齐列",但它不适用于已编写的代码.
提前致谢!:)
我一直在审查代码,其中一些编码器一直在使用冗余三元运算符"以提高可读性."例如:
boolean val = (foo == bar && foo1 != bar) ? true : false;
Run Code Online (Sandbox Code Playgroud)
显然,将语句的结果分配给boolean变量会更好,但编译器是否关心?
考虑这一行:
if (object.getAttribute("someAttr").equals("true")) { // ....
Run Code Online (Sandbox Code Playgroud)
显然这条线是一个潜在的错误,属性可能是null,我们将得到一个NullPointerException.所以我们需要将它重构为以下两种选择之一:
第一种选择:
if ("true".equals(object.getAttribute("someAttr"))) { // ....
Run Code Online (Sandbox Code Playgroud)
第二种选择:
String attr = object.getAttribute("someAttr");
if (attr != null) {
if (attr.equals("true")) { // ....
Run Code Online (Sandbox Code Playgroud)
第一个选项很难读,但更简洁,而第二个选项在意图上是明确的,但是很冗长.
在可读性方面,您更喜欢哪个选项?
我正在尝试提供一个NotOfType具有可读调用语法的实现.NotOfType应该是补充,OfType<T>并因此产生所有非类型的元素T
我的目标是实现一个方法OfType<T>,就像在这个片段的最后一行一样调用它:
public abstract class Animal {}
public class Monkey : Animal {}
public class Giraffe : Animal {}
public class Lion : Animal {}
var monkey = new Monkey();
var giraffe = new Giraffe();
var lion = new Lion();
IEnumerable<Animal> animals = new Animal[] { monkey, giraffe, lion };
IEnumerable<Animal> fewerAnimals = animals.NotOfType<Giraffe>();
Run Code Online (Sandbox Code Playgroud)
但是,我无法想出一个支持特定调用语法的实现.
这是我到目前为止所尝试的:
public static class EnumerableExtensions
{
public static IEnumerable<T> NotOfType<T>(this IEnumerable<T> sequence, Type type) …Run Code Online (Sandbox Code Playgroud) 我现在试图将我的代码保持在80或更少,因为我认为它在大多数情况下看起来更美观.但有时,如果我必须在奇怪的地方放置换行符,代码最终会变得更糟.
有一件事我还没弄清楚如何处理非常好的是长串.例如:
#0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx
def foo():
if conditional():
logger.info("<Conditional's meaning> happened, so we're not setting up the interface.")
return
#.....
Run Code Online (Sandbox Code Playgroud)
结束了!将它放在下一行也无济于事:
#0.........1........2........3........4.........5.........6.........7.........8xxxxxxxxx9xxxxxx
def foo():
if conditional():
logger.info(
"<Conditional's meaning> happened, so we're not setting up the interface.")
return
#.....
Run Code Online (Sandbox Code Playgroud)
我可以使用换行但看起来很糟糕:
#0.........1........2........3........4.........5.........6.........7.........8
def foo():
if conditional():
logger.info(
"<Conditional's meaning> happened, so we're not setting \
up the interface.")
return
#.....
Run Code Online (Sandbox Code Playgroud)
该怎么办?缩短字符串是一种选择,但我不希望我的消息的可读性受到与该代码恰好有多少缩进级别一样任意的影响.
我正在用Python和C编写一个带有一些复杂物理模拟算法的科学程序.在实现算法之后,我发现有很多可能的优化来提高性能.常见的是预先计算值,使计算超出周期,用更复杂的替代简单矩阵算法等.但是出现了一个问题.未经优化的算法要慢得多,但其逻辑和与理论的联系看起来更加清晰和可读.此外,扩展和修改优化算法更加困难.
所以,问题是 - 我应该使用哪些技术来提高可读性,同时提高性能?现在我试图保持快速和清晰的分支并且并行开发它们,但也许有更好的方法?
对于函数/方法包含许多输入参数,如果以不同的顺序传入它会有所不同吗?如果是,在哪些方面(可读性,效率......)?我更好奇我应该如何为自己的功能/方法做些什么?
在我看来,这:
通过引用/指针传递的参数通常在通过值传递的参数之前.例如:
void* memset( void* dest, int ch, std::size_t count );
Run Code Online (Sandbox Code Playgroud)目标参数通常在源参数之前.例如:
void* memcpy( void* dest, const void* src, std::size_t count );
Run Code Online (Sandbox Code Playgroud)除了一些硬约束之外,即具有默认值的参数必须是最后的.例如:
size_type find( const basic_string& str, size_type pos = 0 ) const;
Run Code Online (Sandbox Code Playgroud)无论他们传递的是什么顺序,它们都是功能等同的(实现相同的目标).
实验研究对代码中的空白区域有何看法?让我具体一点:我所说的是认知研究,它可以比较人们阅读和掌握不同格式的视觉信息的速度和效果.
假设您正在设计一种新的计算机语言,并且必须做出一些影响源代码外观的决策.或者您只是为新语言编写风格指南,并希望提出建议.相关主题可能是标识符样式(snake_cased_identifiers与camelCaseIdentifiers/PascalCaseIdentifiers),水平缩进,文档样式或垂直间距.
我故意以这种方式提出这个问题,以避免以下建议:
我不希望支持不同方法的人之间发生火焰战争; 相反,我想知道实验研究对此事有何评论.(我不认为任何特定的研究必须完全'客观'或'中立'.)
为这个问题提供一个"更加软弱"的动机:人们在阅读文档和艺术(如听音乐)时欣赏代码中的空白.这些领域都非常强调空间的重要性.
所以,谢谢,我很高兴听到这些研究的内容.要明确的是,我并不排除风格和艺术的重要性 - 我实际上希望这些世界的智慧能够在实验研究中出现.
总之,如果可以,请触摸以下一项或多项:
最近我们讨论了局部变量对Java代码的性能与可读性的影响.我的一些同事的意见是这样的声明
new DoSomethingCmd(new SelectionContext(context, keys), infoStuff.getCurrentRole().getRole_id()).execute(getResultContainer());
Run Code Online (Sandbox Code Playgroud)
将为应用程序提供相当大的性能提升.他们愿意为此牺牲代码可读性.他们是否正确地声称这个?上述版本的性能是否比这个版本高得多?
final SelectionContext selectionContext = new SelectionContext(context, keys);
final String roleId = infoStuff.getCurrentRole().getRole_id();
final DeleteSomethingCmd deleteSomethingCmd = new DeleteSomethingCmd(selectionContext,roleId);
deleteSomethingCmd.execute(getResultContainer());
Run Code Online (Sandbox Code Playgroud)
我意识到第一个语句本身并不难以掌握,但是当大多数代码都是这样构造时,复杂性会相当快.
谢谢您的意见.
code-readability ×10
coding-style ×3
java ×3
performance ×3
python ×2
algorithm ×1
c ×1
c# ×1
c++ ×1
linq ×1
optimization ×1
option ×1
readability ×1
scala ×1
whitespace ×1