我注意到C11不再允许您在循环结构中声明迭代器变量,例如,以下内容无效:
for (int i = 0; i < 10; ++i)
Run Code Online (Sandbox Code Playgroud)
但这没关系:
int i;
for (i = 0; i < 10; ++i)
Run Code Online (Sandbox Code Playgroud)
鉴于在C99之前甚至不可能在函数开头声明变量(块?C11是否允许函数中任何地方的变量声明?),我很好奇这与安全实践有什么关系.迭代器是应该在它们出现的函数或块的顶部声明,还是在它们被使用的第一个循环之上?我无法看到它在任何方面都有很大的不同,但前一种方法似乎对于移动代码更加健壮.就安全而言,我没有看到任何明显的影响.
另外,首先是C11上述变化的原因是什么?我更喜欢第一个例子中的语法.
编辑:对于我的上一个问题,我想一个问题是,如果多次使用迭代器,for (int i = 0; ...在一个块内移动就不会那么容易重构.
我大约一周参加Clojure和函数式编程 - 我的所有背景都是在OOP中.我想利用Clojure滔滔不绝的易读性和内在逻辑,但是现在我不知道我是否成功地做到了这一点并且没有完全包围我的想法,或者我是否真的在滥用语言以一种糟糕的方式.
例如:
(ns waterfall-quiz.response-parser
(:require [clojure.java.io :as io]))
(defn process-input
[input]
(finalize-input
(normalize-height
(map-input
(numberize-vector
(vectorize-input
(clean-input input)))))))
(defn clean-input
"Removes extraneous whitespace."
[input]
(clojure.string/replace input #"\s+" " "))
(defn vectorize-input
"Turns input into a vector."
[input]
(clojure.string/split input #"\s"))
(...)
Run Code Online (Sandbox Code Playgroud)
我非常怀疑process-input函数,它调用所有其他函数来格式化一些输入.它是引用透明的,但看起来很脆弱 - 是否有更智能的方法将所有功能链接在一起?
另一个例子:
(defn map-builder
"Transforms the vector of waterfalls into a map of waterfalls."
[vectorized-db]
(assoc waterfall-db
(keyword (str 'waterfall (first (re-seq #"[0-9]+" (str (first vectorized-db))))))
(subs (str (first vectorized-db)) (+ 2 (.indexOf (str (first …Run Code Online (Sandbox Code Playgroud) 我刚才在这个问题的评论中听说过PSR-2编码标准:在方法和成员变量名称之前有没有理由使用"public"关键字?
我对PSR-2标准中的一条规则有疑问:
必须从仅包含PHP的文件中省略关闭?>标记.
那是什么意思?
我一直在研究学习你一个Haskell和开始Haskell并且遇到了一个有趣的问题.作为前言,我通常是一名C++程序员,如果我不知道我在说什么,请原谅我.
Beginning Haskell的一个练习让我创建了一个类型的客户端,可以是政府组织,公司或个人.我决定为此尝试记录语法.
data Client = GovOrg { name :: String }
| Company { name :: String,
id :: Integer,
contact :: String,
position :: String
}
| Individual { fullName :: Person,
offers :: Bool
}
deriving Show
data Person = Person { firstName :: String,
lastName :: String,
gender :: Gender
}
deriving Show
data Gender = Male | Female | Unknown
deriving Show
Run Code Online (Sandbox Code Playgroud)
这用于给定客户列表的练习,我必须找到列表中每个性别的数量.我开始通过过滤得到一个只有个人的列表,因为只有他们有性别类型,但我的方法似乎是完全错误的:
listIndividuals :: [Client] -> [Client]
listIndividuals xs = filter (\x -> x …Run Code Online (Sandbox Code Playgroud) 我试图自学C ++类,但遇到了一个绊脚石,我似乎无法解决。我希望有人能够指出正确的方向。
我决定构造一个小类,Tree该类构造一个新的BST。我希望能够像这样在我的对象上调用某些方法:
int main() {
Tree<int> tree1;
tree1.insert(5);
int treeMin = tree1.minValue();
int treeMax = tree1.maxValue();
tree1.printTree();
}
Run Code Online (Sandbox Code Playgroud)
现在,为了调用这些函数,我同时定义了public和private函数,以便您不会以多余的方式调用函数。例如:
(我想避免的事情)
int main() {
Tree<int> tree1;
tree1.insert(tree1, 5);
int treeMin = tree1.minValue(tree1);
int treeMax = tree1.maxValue(tree1);
tree1.printTree(tree1);
}
Run Code Online (Sandbox Code Playgroud)
为了避免出现这种冗余,我定义了同一功能的公共和私有版本。这样,公共职能部门会调用其私人部门。
template<class T>
class Tree {
private:
treeNode<T>* root;
treeNode<T>* newNode(T data);
void insert(treeNode<T>*& root, T data);
int minValue(treeNode<T>*& root);
int maxValue(treeNode<T>*& root);
void printTree(treeNode<T>*& root);
public:
Tree();
~Tree();
void insert(T data);
int minValue();
int maxValue(); …Run Code Online (Sandbox Code Playgroud) Apple的Swift 语言文档提供了以下使用泛型函数和类型参数的示例(在"操作中的类型约束"部分下):
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
for (index, value) in enumerate(array) {
if value == valueToFind {
return index
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
这里有必要使用吗?是不是
func findIndex(array: [Equatable], valueToFind: Equatable) -> Int? {
for (index, value) in enumerate(array) {
if value == valueToFind {
return index
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
当量?如果没有,我错过了什么?如果是这样的话,有理由选择前者而不是后者或一般的经验法则来决定使用泛型函数吗?
Visual Studio有很多很棒的功能,其中一个功能使您的代码非常适合您.我喜欢它,除了一个小细节之外没有任何问题.
来自Java和PHP的Netbeans和Eclipse都没有这样做,并允许我开发自己的风格.(虽然肯定不是唯一的)
这就是我编写方法的方法:
void myMethod () {
}
Run Code Online (Sandbox Code Playgroud)
哪个更改为:
void myMethod()
{
}
Run Code Online (Sandbox Code Playgroud)
我对花括号的下一行完全没问题.
但是我真的不喜欢Visual Studio删除方法名称和括号之间的小空格.
有谁知道如何专门关闭它?
我曾经喜欢Python的可读性,但在转移到Pythion 3后,这似乎已经消失了.
考虑一下:
print([k for k in hist_full_g(img.scale(12,44))])
Run Code Online (Sandbox Code Playgroud)
也许它不是Python 3的问题,而是我的编码风格,但是没有.
看看所有的parens!
现在很明显我可以将它分成几个局部变量,但这样做会很宽松.
img_scaled = img.scale(12,44)
histogram = hist_full_g()
histogram_array = [k for k in ]
print(histogram_array)
Run Code Online (Sandbox Code Playgroud)
我还可以在parens周围添加空间 - 实际上我有时会这样做,但它有点难看,而且我已经读过它的不良做法.
print( [ k for k in hist_full_g( img.scale(12, 44) ) ] )
Run Code Online (Sandbox Code Playgroud)
我该如何写它是可读的和"质量好"?
我不是在谈论这个例子,我的意思是一般.我的Python通常看起来像Lisp,我不认为它应该.
在查看某人的代码时,我遇到了类似于下面的情况,其中错误(基本上是一些糟糕的编程习惯)不是直接可见的.根据所使用的编译器,i/i++可能是0或1.
int foo(int n) {
printf("Foo is %d\n", n);
return (0);
}
int bar(int n) {
printf("Bar is %d\n", n);
return (0);
}
int main(int argc, char *argv[]) {
int x = 0;
int(*(my_array[3]))();
int i = 1;
int y = i/++i;
printf("\ni/++i = %d, ", y);
my_array[1] = foo;
my_array[2] = bar;
(my_array[++x])(++x);
}
Run Code Online (Sandbox Code Playgroud)
因此,输出是Foo is 2或者Bar is 2.
我的问题可能被认为过于宽泛,但我想知道:
我正在阅读K&R的书,第1.9节.从给定的一组行打印最长行的程序给出为
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main()
{
int len; /* current line length */
int max; /* maximum length seen so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len; …Run Code Online (Sandbox Code Playgroud)