我有时会在getter的属性中看到缩写.例如这两种类型:
public int Number { get; } = 0
public int Number => 0;
Run Code Online (Sandbox Code Playgroud)
如果这两者之间有任何差异,有人可以告诉我.他们的表现如何?它们都是只读的吗?
在查看各种PHP库时,我注意到很多人选择使用单个下划线为某些类方法添加前缀,例如
public function _foo()
Run Code Online (Sandbox Code Playgroud)
...代替...
public function foo()
Run Code Online (Sandbox Code Playgroud)
我意识到这最终归结为个人偏好,但我想知道是否有人对这种习惯的来源有所了解.
我的想法是,它可能是从PHP 4继承的,在类方法可以被标记为受保护或私有之前,作为暗示"不要从类外调用此方法"的一种方式.然而,我也想到它可能起源于我不熟悉的某个地方(一种语言),或者背后可能有很好的推理,我会从中获益.
任何想法,见解和/或意见将不胜感激.
来自Python背景,在风格方面总是有"正确的方法"("Pythonic"方式),我想知道Ruby是否存在相同的情况.我一直在使用自己的风格指南,但我正在考虑发布我的源代码,并且我希望它遵守可能存在的任何不成文的规则.
return在方法中明确键入是"Ruby方式" 吗?我已经看到它有没有完成,但有没有正确的方法呢?是否有合适的时机去做?例如:
def some_func(arg1, arg2, etc)
# Do some stuff...
return value # <-- Is the 'return' needed here?
end
Run Code Online (Sandbox Code Playgroud) 可能重复:
ReSharper和var
在我安装了ReSharper后,它要求(通过警告)我尽可能使用var,例如
UnhandledExceptionEventArgs ue = (UnhandledExceptionEventArgs) t;
Run Code Online (Sandbox Code Playgroud)
ReSharper想把它变成
var ue = (UnhandledExceptionEventArgs) t;
Run Code Online (Sandbox Code Playgroud)
我更喜欢第一个版本,有没有理由更喜欢var?更好的性能?什么?或者它只是一种代码风格?
看起来很容易找到这样的Java工具(Checkstyle,JCSC),但我似乎找不到一个用于C/C++的工具.我不是在寻找类似于lint的静态代码分析器,我只想检查编码标准,如变量命名,大小写,间距,标识,括号放置等.
在尝试遵守python样式规则时,我将编辑器设置为最多79列.
在PEP中,它建议在括号,括号和括号内使用python隐含的延续.但是,当我达到col限制时处理字符串时,它会有点奇怪.
例如,尝试使用多行
mystr = """Why, hello there
wonderful stackoverflow people!"""
Run Code Online (Sandbox Code Playgroud)
将返回
"Why, hello there\nwonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
这有效:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
因为它返回:
"Why, hello there wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
但是,当语句缩进几个块时,这看起来很奇怪:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
如果您尝试缩进第二行:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
Run Code Online (Sandbox Code Playgroud)
你的字符串最终为:
"Why, hello there wonderful stackoverflow …Run Code Online (Sandbox Code Playgroud) 该蟒蛇风格指南建议对进口组这样的:
应按以下顺序对导入进行分组:
- 标准库导入
- 相关的第三方进口
- 本地应用程序/库特定导入
但是,它没有提到应该如何规划两种不同的进口方式:
from foo import bar
import foo
Run Code Online (Sandbox Code Playgroud)
有多种方法可以对它们进行排序(让我们假设所有这些导入都属于同一个组):
首先from..import,然后import
from g import gg
from x import xx
import abc
import def
import x
Run Code Online (Sandbox Code Playgroud)首先import,然后from..import
import abc
import def
import x
from g import gg
from x import xx
Run Code Online (Sandbox Code Playgroud)按模块名称的字母顺序,忽略导入的类型
import abc
import def
from g import gg
import x
from xx import xx
Run Code Online (Sandbox Code Playgroud)PEP8没有提到这个的首选顺序和"清理导入"功能,一些IDE可能只是做该功能的开发人员所喜欢的任何东西.
我正在寻找另一个PEP澄清这个或来自BDFL(或其他Python核心开发人员)的相关评论/电子邮件.请不要发表陈述您自己偏好的主观答案.
假设我有一个ArrayList
ArrayList<MyClass> myList;
Run Code Online (Sandbox Code Playgroud)
我想打电话给阿瑞,是否有使用性能的原因
MyClass[] arr = myList.toArray(new MyClass[myList.size()]);
Run Code Online (Sandbox Code Playgroud)
过度
MyClass[] arr = myList.toArray(new MyClass[0]);
Run Code Online (Sandbox Code Playgroud)
?
我更喜欢第二种风格,因为它不那么冗长,我认为编译器会确保空数组不会真正被创建,但我一直想知道这是不是真的.
当然,在99%的情况下,它不会以某种方式产生影响,但我希望在我的普通代码和优化的内部循环之间保持一致的风格......
在我的公司中,有一个编码规则,在释放任何内存后,将变量重置为NULL.例如 ...
void some_func ()
{
int *nPtr;
nPtr = malloc (100);
free (nPtr);
nPtr = NULL;
return;
}
Run Code Online (Sandbox Code Playgroud)
我觉得,在上面显示的代码中,设置为NULL没有任何意义.或者我错过了什么?
如果在这种情况下没有任何意义,我将采用"质量团队"来删除此编码规则.请指教.
每天我都越来越喜欢python.
今天,我写了一些代码,如:
for i in xrange(N):
do_something()
Run Code Online (Sandbox Code Playgroud)
我不得不做N次.但每次都不依赖于i(索引变量)的值.我意识到我正在创建一个我从未使用过的变量(i),并且我认为"在没有这个无用的索引变量的情况下,确实存在更多的pythonic方式."
所以......问题是:你知道如何以更多(pythonic)美丽的方式完成这个简单的任务吗?
coding-style ×10
python ×3
c ×2
c# ×2
.net ×1
c++ ×1
for-loop ×1
free ×1
getter ×1
heap-memory ×1
java ×1
malloc ×1
pep8 ×1
performance ×1
php ×1
resharper ×1
return-value ×1
ruby ×1
shorthand ×1
var ×1