Dav*_*vid 1 c++ code-formatting
我真正喜欢IDE的一个方面是能够"最小化"代码段,以便:
while(conditions){
// Really long code...
}
Run Code Online (Sandbox Code Playgroud)
可以变成:
while(conditions){ // The rest is hidden
Run Code Online (Sandbox Code Playgroud)
我的问题是这样的东西是否可以接受格式化
// Code
{
// More code
}
// Code
Run Code Online (Sandbox Code Playgroud)
我知道在括号内完成的任何事情都有限制范围,但我也可以在外部范围内编辑变量.
所以,对于一个简短的,不必要的例子
int x = 1;
{ // Create new variable, add and output
int y = 2;
cout << x + y;
}
Run Code Online (Sandbox Code Playgroud)
会成为:
int x = 1;
{ // Create new variable, add and output (The rest of the code is hidden)
Run Code Online (Sandbox Code Playgroud)
这样可以接受,还是避开?
这是我想隐藏的一个真实例子.
/// Add line to msg vector
// Check to see if each side X is needed
if(uniqueSide && line == 1){
// Create stringstream to hold string and int (e.g. "Message " + 1 + " - Side " + 1
stringstream tempString;
// Add full line to stringstream. validMessages.length() + 1 will make Message X be incremental
tempString << "Message " << (validMessages.length() + 1) << " - Side " << numSide;
// Then append full string (e.g. "Message 1 - Side 1") to msg
msg.append(tempString.str());
}
// Else just add Message X using same method as above
else if(line == 1){
stringstream tempString;
tempString << "Message " << (validMessages.length() + 1);
}
// Add each line to msg vector with double space indent and (width) before each line
stringstream tempString;
tempString << setprecision(5) // Makes width be output as 10.325 or 100.33
<< " (" << width << ") " << tempInput
msg.append(tempString.str());
Run Code Online (Sandbox Code Playgroud)
谢谢.
至少在我看来,如果你需要(甚至想要)这么多,你可能不会很好地构建你的代码.
隐藏代码的兴趣往往表明你可能有太多的代码被挤在一起,最好分成更有意义的函数或(通常更好)通用算法.
我想详细介绍一些有关如何改进代码的更具体的建议,但是当问题如此笼统,并且它包含的小代码缺少任何上下文时,很难做到这一点,所以几乎不可能猜猜它代表什么,这是你需要做的第一件事来改善它.
这就是说:作为一般规则,添加一个不受流控制语句控制的块是完全合理的 - 但通常是为了控制对象的生命周期 - 如果你想要创建和销毁的东西,使用RAII对象并将其放在一个块中,因此当执行退出块时,它将被自动销毁.
编辑:至少对我来说,你的样本看起来成熟(过期?)进行一些严肃的重构.
/// Add line to msg vector
// Check to see if each side X is needed
if(uniqueSide && line == 1){
// Create stringstream to hold string and int (e.g. "Message " + 1 + " - Side " + 1
stringstream tempString;
// Add full line to stringstream. validMessages.length() + 1 will make Message X be incremental
tempString << "Message " << (validMessages.length() + 1) << " - Side " << numSide;
// Then append full string (e.g. "Message 1 - Side 1") to msg
msg.append(tempString.str());
}
// Else just add Message X using same method as above
else if(line == 1){
stringstream tempString;
tempString << "Message " << (validMessages.length() + 1);
}
Run Code Online (Sandbox Code Playgroud)
现在,你的else子句并没有真正做任何事情(把东西放进去tempString,但那是本地的,所以当它从块中退出时就消失了.让我们假设评论是正确的,所以其他应该有:
msg.append(tempString.str());
Run Code Online (Sandbox Code Playgroud)
在它退出之前.在这种情况下,代码的两条腿足够相似,它们应该(大部分)合并:
stringstream tempString;
tempString << Message << validMesssages.length()+1;
if (uniqueSide && line == 1)
tempString << " - Side " << numSide;
msg.append(tempString.str());
Run Code Online (Sandbox Code Playgroud)
然后,由于其最后一部分是无条件执行的,我们可以将其余的代码与前面的代码合并(并且在此过程中,消除了相当多的代码,因此我们最终会得到类似的结果:
stringstream tempString;
if (line == 1) {
tempString << "Message " << validMesssages.length()+1;
if (uniqueSide) tempString << " - Side " << numSide;
}
tempString << setprecision(5) << " ( " << width << " ) " << tempInput;
msg.append(tempString.str());
Run Code Online (Sandbox Code Playgroud)
从那里,问题是将它转换为函数或函子是否有意义,因此调用代码将最终类似于:
msg.append(msg(line, validMessages, uniqueSide, numSide, width, tempInput));
Run Code Online (Sandbox Code Playgroud)
无论你想要这样做,只有你可以说.就个人而言,我认为这取决于你是否在许多地方编写类似的代码.如果这是唯一一个像这样的代码的地方,我可能会离开它,但如果你需要两个(或更多)位置的相同代码,一个函数开始变得更有意义.