Results 1 to 8 of 8

Thread: A question of style and design

  1. #1
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default A question of style and design

    Hi,
    I'd like to hear some opinions. Usually I only use Linux and the gcc. And I always make sure that it compiles cleanly. So does the code below. Now I wanted to try to compile my program with Visual Studio 2005. The compiler gave me the following warning:

    'ImageScaler::scaleFactor' : not all control paths return a value

    In principle the compiler is right, but I wonder if this counts as bad design. The control path Visual Studio complained about can never be executed. Of course, when I compile as release, the assertion will go away and this might open the undefined path.

    I just wonder why this piece of code is ok for the gcc.

    Qt Code:
    1. double ImageScaler::scaleFactor(RubberBands::lineType type) const{
    2. switch(type){
    3. case RubberBands::horizontal:
    4. return _scaleFactorX;
    5. case RubberBands::vertical:
    6. return _scaleFactorY;
    7. default:
    8. assert(false);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A question of style and design

    but I wonder if this counts as bad design.
    I would not go as far as calling it bad design.
    You can add the following, which should apease both compilers:
    Qt Code:
    1. deefalut: assert(false);
    2. return 0.0; //or -1.0 or what have you that means error
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: A question of style and design

    Quote Originally Posted by high_flyer View Post
    I would not go as far as calling it bad design.
    You can add the following, which should apease both compilers:
    Qt Code:
    1. deefalut: assert(false);
    2. return 0.0; //or -1.0 or what have you that means error
    To copy to clipboard, switch view to plain text mode 
    True, thought of that. However, if I 'invent' a dummy error value good design would demand that I test for it, but I just tested what happens when I change the assertion to 'assert(true)'. When I did this I got the same warning the Visual Studio compiler gave me. So the gcc really seems to see that this path cannot be executed. In that case it probably would remove the dead code. I'd say it is pointless to add code, which the compiler removes and test for an impossible return value.

    Too bad I have to do it anyways. It does not compile as release (-Werror) with NDEBUG defined.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A question of style and design

    I'd say it is pointless to add code, which the compiler removes and test for an impossible return value.
    Why impossible?
    Its very possible to call ImageScaler::scaleFactor(RubberBands::lineType type) with a type that is not handled in the any of the 'case' segments.
    And the error return value doesn't have to be a dummy one, just defind it as such - you don't have to use it - but it'll never the less be defined

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A question of style and design

    If you only want to distinguish between horizontal and vertical, you can use the following:
    Qt Code:
    1. double ImageScaler::scaleFactor(RubberBands::lineType type) const{
    2. return type == RubberBands::horizontal ? _scaleFactorX : _scaleFactorY;
    3. }
    To copy to clipboard, switch view to plain text mode 

    Without any compiler warnings

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: A question of style and design

    True, but this would not work anymore should there ever appear the need for a third value, e.g. if I go 3 dimensional.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A question of style and design

    You can always modify the function later. You can achieve the same by returning _scaleFactorY from a "default" case in your switch statement.

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: A question of style and design

    Quote Originally Posted by wysota View Post
    You can always modify the function later.
    This of course is always true.

    Quote Originally Posted by wysota View Post
    You can achieve the same by returning _scaleFactorY from a "default" case in your switch statement.
    This would not help at all. I am 100% sure that this method won't be called with a wrong parameter in my code. However, blind faith like this is always an invitation to disaster and I have been wrong before (one or two times ). If I am willing to accept the risk, I would not have to do anything. If I am not willing to risk it, your code is probably worse than mine, because it potentially obfuscates bugs. A wrong parameter would translate to _scaleFactorY, which probably would be within a plausible range, but wrong. I would not want to find the random bugs, which could result from that.

    The way I see it now it that if I want to be 100% correct I'd have to remove the assertion and replace it with a permanent runtime check.

Similar Threads

  1. config question
    By nightwalker in forum Qt Tools
    Replies: 2
    Last Post: 31st March 2006, 18:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.