我对我的代码的美学有一种特别愚蠢的不安全感......坦率地说,我对空格的使用很尴尬。我的代码看起来像极客跳舞;不是很吓人,但很尴尬,以至于你盯着看会感觉很糟糕,但又无法移开视线。
我只是不确定何时应该留下空白行或使用行尾注释而不是上面的行注释。我更喜欢在我的代码上方发表评论,但有时为了三个字的评论打断流程似乎很奇怪。有时在一段代码之前和之后加入一个空行就像在一段平滑的代码中放一个减速带。例如,在一个嵌套循环中,在中间分隔三行或四行代码块几乎抵消了缩进的视觉效果(我注意到 K&R 括号比 Allman/BSD/GNU 样式更不容易出现这个问题)。
我个人的偏好是密集的代码,除了函数/方法/注释块之间,几乎没有“减速带”。对于棘手的代码部分,我喜欢留下一个大的注释块,告诉你我将要做什么以及为什么,然后在该代码部分中添加一些“标记”注释。不幸的是,我发现其他一些人通常喜欢大方的垂直空白。一方面,我可以拥有更高的信息密度,而其他人认为流动性不是很好,另一方面,我可以以更低的信噪比为代价拥有更好的流动代码库。
我知道这是一件微不足道、愚蠢的事情,但这是我真正想要努力的事情,因为我提高了我的其他技能。
有人愿意提供一些提示吗?你认为什么是流畅的代码,在哪里使用垂直空白是合适的?对两三个字评论的行尾评论有什么想法吗?
谢谢!
PS 这是我一直在研究的代码库中的一种方法。不是我最好的,但不是我迄今为止最差的。
/**
* TODO Clean this up a bit. Nothing glaringly wrong, just a little messy.
* Packs all of the Options, correctly ordered, in a CommandThread for executing.
*/
public CommandThread[] generateCommands() throws Exception
{
OptionConstants[] notRegular = {OptionConstants.bucket, OptionConstants.fileLocation, OptionConstants.test, OptionConstants.executable, OptionConstants.mountLocation};
ArrayList<Option> nonRegularOptions = new ArrayList<Option>();
CommandLine cLine = new CommandLine(getValue(OptionConstants.executable));
for (OptionConstants constant : notRegular)
nonRegularOptions.add(getOption(constant));
// --test must be first
cLine.addOption(getOption(OptionConstants.test));
// and the regular options...
Option option;
for (OptionBox optionBox : optionBoxes.values())
{
option = optionBox.getOption();
if (!nonRegularOptions.contains(option))
cLine.addOption(option);
}
// bucket and fileLocation must be last
cLine.addOption(getOption(OptionConstants.bucket));
cLine.addOption(getOption(OptionConstants.fileLocation));
// Create, setup and deploy the CommandThread
GUIInteractiveCommand command = new GUIInteractiveCommand(cLine, console);
command.addComponentsToEnable(enableOnConnect);
command.addComponentsToDisable(disableOnConnect);
if (!getValue(OptionConstants.mountLocation).equals(""))
command.addComponentToEnable(mountButton);
// Piggy-back a Thread to start a StatReader if the call succeeds.
class PiggyBack extends Command
{
Configuration config = new Configuration("piggyBack");
OptionConstants fileLocation = OptionConstants.fileLocation;
OptionConstants statsFilename = OptionConstants.statsFilename;
OptionConstants mountLocation = OptionConstants.mountLocation;
PiggyBack()
{
config.put(OptionConstants.fileLocation, getOption(fileLocation));
config.put(OptionConstants.statsFilename, getOption(statsFilename));
}
@Override
public void doPostRunWork()
{
if (retVal == 0)
{
// TODO move this to the s3fronterSet or mounts or something. Take advantage of PiggyBack's scope.
connected = true;
statReader = new StatReader(eventHandler, config);
if (getValue(mountLocation).equals(""))
{
OptionBox optBox = getOptionBox(mountLocation);
optBox.getOption().setRequired(true);
optBox.requestFocusInWindow();
}
// UGLY HACK... Send a 'ps aux' to grab the parent PID.
setNextLink(new PSCommand(getValue(fileLocation), null));
fireNextLink();
}
}
}
PiggyBack piggyBack = new PiggyBack();
piggyBack.setConsole(console);
command.setNextLink(piggyBack);
return new CommandThread[]{command};
}