4
\$\begingroup\$

The biggest question is if it's worth it to use a useless if statement to make the x and y variables local away from the main statement.

    //Unconventional part, make it so that x and y are local variables
if(true){
        int x,y;
        //set x and y to be the width + height of the terminal
        getmaxyx(stdscr,y,x);
        for(int i = 0; i < x; i++){
                //top border
                mvaddch(0,i,'#');
                //bottom border
                mvaddch(y - 1,i,'#');
        }
        for(int i = 0; i < y; i++){
                //left border
                mvaddch(i,0,'#');
                //right border
                mvaddch(i,x - 1,'#');
        }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You could just have a block; the if(true) could be left out \$\endgroup\$ Commented Nov 26, 2016 at 6:12
  • \$\begingroup\$ @qxz, he wants to make x and y local variables. He asks if it's worth it. I would say that he should just make it a function. \$\endgroup\$ Commented Nov 26, 2016 at 6:43
  • \$\begingroup\$ @Incomputable I was just making a comment that you can have a plain block without the if. I agree that I'd make it a function \$\endgroup\$ Commented Nov 26, 2016 at 6:44

2 Answers 2

3
\$\begingroup\$

You can create a block without an if statement. For example, these are equivalent:

{
    // some code
}

if (true) {
    // some code
}

But it's not a good practice to create blocks like this. It's a code smell, suggesting to wrap // some code in a dedicated function instead.

Anytime you think some neat trick is "unconventional", it's most probably a bad practice. Don't do it, fix it.

\$\endgroup\$
1
\$\begingroup\$

In addition to what @janos said:

Your formatting style looks weird. This is because in mvaddch(0,x - 1,'#') there is plenty of space around the - operator, but none around the ,. This commonly suggests that the , binds more tightly than the -, which is wrong.

So either use no space at all or use a space after the comma as well.

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.