1

One thing I really like about IDEs is the ability to "minimize" sections of code, so that:

while(conditions){
  // Really long code...
}

Can become:

while(conditions){ // The rest is hidden

My question is whether or not something like this would be acceptable formatting

// Code
{
  // More code
}
// Code

I understand that anything done inside the brackets would have that limited scope, but I can also edit variables in the outer scope as well.

So, for a short, unnecessary example

int x = 1;
{ // Create new variable, add and output
  int y = 2;
  cout << x + y;
}

Would become:

int x = 1;
{ // Create new variable, add and output (The rest of the code is hidden)

So would this be acceptable, or shunned?

Here is a real example of something I would want to just hide.

/// 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());

Thanks.

4

1 回答 1

5

至少在我看来,如果你非常需要(甚至想要)这个,你可能没有很好地构建你的代码。

对隐藏代码的兴趣往往表明您可能将太多代码挤在一起,最好将它们分成更有意义的函数或(通常甚至更好)通用算法。

我想更详细地给出关于如何改进某些代码的更具体的建议,但是当问题如此笼统并且它包含的小代码缺乏任何上下文时,很难做到这一点,所以几乎不可能猜猜它代表什么,这是你需要做的第一件事来改进它。

那就是说:我想说作为一般规则,添加一个不受流控制语句控制的块是完全合理的——但通常这样做是为了控制对象的生命周期——如果你想要创建和销毁某些东西,请使用 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);
}

现在,您的else子句实际上并没有做任何事情(将某些内容放入tempString,但这是本地的,因此它会在退出块时消失。假设评论是正确的,所以 else 应该有:

    msg.append(tempString.str());

在它退出之前。在这种情况下,代码的两条腿足够相似,它们可能应该(大部分)合并:

stringstream tempString;
tempString << Message << validMesssages.length()+1;
if (uniqueSide && line == 1)
    tempString << " - Side " << numSide;
msg.append(tempString.str());

然后,由于最后一部分是无条件执行的,我们可以将其余的代码与前面的代码合并(在这个过程中,消除了很多,所以我们最终得到了这样的东西:

stringstream tempString;
if (line == 1) {
    tempString << "Message " << validMesssages.length()+1;
    if (uniqueSide) tempString << " - Side " << numSide;
}
tempString << setprecision(5) << " ( " << width << " ) " << tempInput;
msg.append(tempString.str());    

从那里开始,问题是把它变成一个函数或仿函数是否有意义,所以调用代码最终会是这样的:

msg.append(msg(line, validMessages, uniqueSide, numSide, width, tempInput));

你想不想那样做,大概只有你能说。就个人而言,我认为这取决于您是否在许多地方编写了类似的代码。如果这是唯一有类似代码的地方,我可能会离开它,但如果您需要在两个(或更多)地方使用相同的代码,那么函数开始变得更有意义。

于 2013-08-21T03:24:52.247 回答