Вопрос

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.

Это было полезно?

Решение

At least in my opinion, if you need (or even want) this very much, you're probably not structuring your code very well.

An interest in hiding code tends to indicate that you may have too much code shoved together that might be better off being separated into more meaningful functions or (usually even better) generic algorithms.

I'd like to go into more detail and give more specific advice about how some code could be improved, but it's hard to do that when the question is so general, and the little code it contains lacks any context, so it's almost impossible to guess what it represents, which is the first thing you need to do to improve it much.

That said: I'd say as a general rule, adding a block without it being controlled by an flow control statement is perfectly reasonable -- but it's usually done to control object lifetimes -- if you want something created and destroyed, use an RAII object and put it in a block, so when execution exits the block, it'll be destroyed automatically.

Edit: At least to me, your sample looks ripe (overdue?) for some serious refactoring.

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

Right now, your else clause isn't really doing anything (puts something in tempString, but that's local, so it disappears upon exit from the block. Let's assume the comment is correct, so the else should have:

    msg.append(tempString.str());

before it exits. In this case, the two legs of the code are similar enough that they should probably be (mostly) merged:

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

Then, since the last part of that is executed unconditionally, we can merge the rest of the code with the preceding (and in the process, eliminate quite a bit so we end up with something like this:

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

From there, the question is whether it makes sense to turn that into a function or functor, so the calling code would end up something like:

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

Whether you want to do that, only you can probably say. Personally, I think it would depend on whether you were writing similar code in a number of places. If this is the only place with code like that, I'd probably leave it, but if you need the same code in two (or more) places, a function starts to make a lot more sense.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top