我喜欢函数式编程的思想,即每个函数仅用于一个目的,等等...
尽管如此,但我不知道如何通过排序功能使代码易于阅读。
// writeParagraph.js
const writeWord = (wordArgs) => {
// do stuff with wordArgs
return word
}
const writeSentence = (sentenceArgs) => {
const sentence = sentenceArgs.map(writeWords)
return sentence
}
export const writeParagraph = (paragraphArgs) => {
const paragraph = paragraphArgs.map(writeSentence)
return paragraph
}
每当我打开writeParagraph.js
来探索其中的内容时,我都会寻找export const writeParagraph
部分。要阅读该书,我必须去上方的writeSentence
,而不是下方的{{1}}。作为一个人,我习惯于自上而下地阅读。有没有办法构造功能代码以从上至下阅读。
答案 0 :(得分:1)
如果您希望使用其他订单,则可以更改它:
export const writeParagraph = (paragraphArgs) => {
const paragraph = paragraphArgs.map(writeSentence)
return paragraph
}
const writeSentence = (sentenceArgs) => {
const sentence = sentenceArgs.map(writeWords)
return sentence
}
const writeWord = (wordArgs) => {
// do stuff with wordArgs
return word
}
之所以可行,是因为所有功能都在同一作用域中声明,并且可以互相访问。
如果您的短毛猫现在抱怨潜在的使用前定义(即使您没有这样做),则可以禁用该规则(如果您知道自己在做什么),或者改用适当的声明:
export function writeParagraph(paragraphArgs) {
const paragraph = paragraphArgs.map(writeSentence)
return paragraph
}
function writeSentence(sentenceArgs) {
const sentence = sentenceArgs.map(writeWords)
return sentence
}
function writeWord(wordArgs) {
// do stuff with wordArgs
return word
}
答案 1 :(得分:-1)
代码顺序可能是主观的。
某些文化从书的背面读到右侧。 有些人从右到左阅读。
我认为这就是发明注释的原因。
在顶部放置一个注释块,以解释代码的顺序。