CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    General Improvments

    Hello.
    I have been coding for a while but i have pathetic conventions and style.
    Here is a sample piece of code that i ahve and i was wondering if anyone got any ideas on how i could improve.Any critisims+ideas are welcome.
    Code:
     
    #ifndef sys_console
    #define sys_console
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    static const WORD fgBlack=     0; 
    static const WORD fgLoRed=     FOREGROUND_RED; 
    static const WORD fgLoGreen=   FOREGROUND_GREEN; 
    static const WORD fgLoBlue=    FOREGROUND_BLUE; 
    static const WORD fgLoCyan=    fgLoGreen|fgLoBlue; 
    static const WORD fgLoMagenta= fgLoRed|fgLoBlue; 
    static const WORD fgLoYellow=  fgLoRed|fgLoGreen; 
    static const WORD fgLoWhite=   fgLoRed|fgLoGreen|fgLoBlue; 
    static const WORD fgGray=      fgBlack|FOREGROUND_INTENSITY; 
    static const WORD fgHiWhite=   fgLoWhite|FOREGROUND_INTENSITY; 
    static const WORD fgHiBlue=    fgLoBlue|FOREGROUND_INTENSITY; 
    static const WORD fgHiGreen=   fgLoGreen| FOREGROUND_INTENSITY; 
    static const WORD fgHiRed=     fgLoRed|FOREGROUND_INTENSITY; 
    static const WORD fgHiCyan=    fgLoCyan|FOREGROUND_INTENSITY; 
    static const WORD fgHiMagenta= fgLoMagenta|FOREGROUND_INTENSITY; 
    static const WORD fgHiYellow=  fgLoYellow|FOREGROUND_INTENSITY;
    static const WORD bgBlack=     0; 
    static const WORD bgLoRed=     BACKGROUND_RED; 
    static const WORD bgLoGreen=   BACKGROUND_GREEN; 
    static const WORD bgLoBlue=    BACKGROUND_BLUE; 
    static const WORD bgLoCyan=    bgLoGreen|bgLoBlue; 
    static const WORD bgLoMagenta= bgLoRed|bgLoBlue; 
    static const WORD bgLoYellow=  bgLoRed|bgLoGreen; 
    static const WORD bgLoWhite=   bgLoRed|bgLoGreen|bgLoBlue; 
    static const WORD bgGray=      bgBlack|BACKGROUND_INTENSITY; 
    static const WORD bgHiWhite=   bgLoWhite|BACKGROUND_INTENSITY; 
    static const WORD bgHiBlue=    bgLoBlue|BACKGROUND_INTENSITY; 
    static const WORD bgHiGreen=   bgLoGreen|BACKGROUND_INTENSITY; 
    static const WORD bgHiRed=     bgLoRed|BACKGROUND_INTENSITY; 
    static const WORD bgHiCyan=    bgLoCyan|BACKGROUND_INTENSITY; 
    static const WORD bgHiMagenta= bgLoMagenta|BACKGROUND_INTENSITY; 
    static const WORD bgHiYellow=  bgLoYellow|BACKGROUND_INTENSITY;
    class csys_console{
        public:
            csys_console();
            ~csys_console();
            void cls();
            void color(WORD wColor);
            void pause(); 
            void cursor(DWORD dwSize,bool bVisible);       
    };
    #endif
    Code:
    #include "sys_console.h"
    csys_console::csys_console(){
    }
    csys_console::~csys_console(){
    }
    void csys_console::cls(){
        COORD coordScreen = {0,0};
        DWORD cCharsWritten=0;
        CONSOLE_SCREEN_BUFFER_INFO csbiWindow; 
        DWORD dwConSize=0;
        if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiWindow))std::cout<<"Error getting screen buffer info."<<std::endl;
        dwConSize = csbiWindow.dwSize.X * csbiWindow.dwSize.Y;
        if(!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
        if(!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), csbiWindow.wAttributes,dwConSize, coordScreen, &cCharsWritten ))std::cout<<"Error clearing the console."<<std::endl;
        if(!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen ))std::cout<<"Error reseting the cursor position."<<std::endl;
    }
    void csys_console::color(WORD wColor){
         if(!SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),wColor))std::cout<<"Error setting new color."<<std::endl;
    }
    void csys_console::pause(){
         std::cout<<"Press any key to continue."<<std::endl;
         while(!_kbhit()){
             Sleep(100);
        }
         _getch();
    }
    void csys_console::cursor(DWORD dwSize,bool bVisible){
        _CONSOLE_CURSOR_INFO cCursor;
        cCursor.dwSize=dwSize;
        cCursor.bVisible=bVisible;
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cCursor);
        
    }
    Any ideas how i could improve the code?
    Note that the current code works.
    Thx alot.

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: General Improvments

    Well, as for advices (if the code really serves your purpose):
    1. I would not use static const.. rather I would group all the constants into a seperate namespace called CommonConstants.. or whatever sounds nicer.
    2. Keep the constants into a different file seperate from the class definition.
    3. Would write if-else-.. statements like this:
    Code:
    if (condition){
        //set of statements
    else{
        //another set of statements
    }
    rather than putting all in one line (even when there is no else).
    4. Put a using std::cout and using std::endl at the top so that I don't need to write std::blah all the time.
    5. Some inlined comments about what some part of the code do..
    6. Most importantly - there is no exception handling in your code. I would not let it go that easy without it.

    Hope this helps. Regards.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: General Improvments

    Use blank lines to isolate "atomic" sections of code. As exterminator says, put control statements on their own line, and the controlled code on lines following them (indented), and avoid "statics" - if they're meant to be local to the file, put them in an unnamed namespace.

    DITCH HUNGARIAN NOTATION - it adds nothing and often ends up lying; rather prefer to give variables and functions names that clearly express their purpose.

    Let's have a look at the function cls(). First, reformatted with the hungarian warts removed:
    Code:
    void csys_console::cls()
    {
    	COORD Screen = {0,0};	  // "Screen"?
    	DWORD CharsWritten=0;
    	CONSOLE_SCREEN_BUFFER_INFO Window;   // "Window"?
    	DWORD ConSize=0;	 // "ConSize"?
    
    	if(!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &Window))
    		std::cout<<"Error getting screen buffer info."<<std::endl;
    
    	ConSize = Window.dwSize.X * Window.dwSize.Y;
    
    	if (!FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE),
    		(TCHAR) ' ', ConSize, Screen, &CharsWritten ))
    		std::cout<<"Error clearing the console."<<std::endl;
    
    	if (!FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
    		Window.wAttributes, ConSize, Screen, &CharsWritten))
    		std::cout<<"Error clearing the console."<<std::endl;
    
    	if (!SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Screen ))
    		std::cout<<"Error reseting the cursor position."<<std::endl;
    }
    (For some reason, the "code" formatting adds extra space to the indents - I had intended for each indent to be four spaces.)

    Notice the declarations where I've queried the name of the variable. Ask yourself: does the name really tell you what the purpose of that variable is? If someone else had written this, and you were picking it up for the first time, would you be thanking the original author for making your life easier?

    Finally (and again pointed out by exterminator) you are printing an error message and then carrying on with the processing. Surely some mistake?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Aug 2003
    Posts
    938

    Re: General Improvments

    yeah, exception handling needs improvment.
    thx alot for that stuff.
    If anyone gets any ohter ideas plz post

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: General Improvments

    Remember that your functions are called by other functions. Functions can't see cout statements output by your functions so would have no idea that an error has occurred.

    You might consider using enums for constant integral values.

    Don't put unnecessary includes in header files. Some of your constants might come from windows.h so you need that (WORD you can do away with) but you didn't need the conio.h or iostream in that header.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured