HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Creating a border using ncurses

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
creatingncursesusingborder

Problem

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,'#');
        }
}

Solution

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.

Code Snippets

{
    // some code
}

if (true) {
    // some code
}

Context

StackExchange Code Review Q#148135, answer score: 3

Revisions (0)

No revisions yet.