#40 Improve const-correctness

general
open
general (9)
5
2008-02-03
2008-01-27
Markus Elfring
No

Would you like to apply the advices from the article "http://en.wikipedia.org/wiki/Const-correctness" to more places in your sources?
I suggest to add the key word "const" to the type specifiers for parameters like the following.
- http://xvidcap.svn.sourceforge.net/viewvc/xvidcap/trunk/src/app_data.h?revision=289&view=markup
sopts (function "xvc_captypeoptions_copy")
filename (function "xvc_is_filename_mutable")
command, file (function "xvc_command_execute")
http://cvs.sourceforge.net/viewcvs.py/sox/sox/src/FFT.h?rev=1.1&view=markup

- http://xvidcap.svn.sourceforge.net/viewvc/xvidcap/trunk/src/codecs.h?revision=293&view=markup
fps_string (function "xvc_read_fps_from_string")

Discussion

  • Logged In: YES
    user_id=782084
    Originator: NO

    Correct, pointers appreciated and code cleanup is certainly necessary.
    A pactch would speed this up, though

     
    • assigned_to: nobody --> charly4711
     
  • Markus Elfring
    Markus Elfring
    2008-02-02

    changes from 9 files

     
    Attachments
  • Markus Elfring
    Markus Elfring
    2008-02-02

    Logged In: YES
    user_id=572001
    Originator: YES

    File Added: 1880627.diff

     
  • Logged In: NO

    Thanks for the patch. Didn't apply cleanly, but I'm applying it manually right now.
    Perhaps, while we're at this you could clarify some things I have never understood about const so I can look over this again and see if there is more to optimize/clean up. (And by now you will have noticed, I'm not a trained C programmer at all ;) )

    You were (a) mainly looking at pointer arguments to functions. And there (b) making the pointee constant, not the pointer. I take it that is because C passes primitive arguments by copy (like Java) and thus you don't have to worry about constness of primitives or the pointer itself, right?
    In other words const for primitve types and the pointer itself is only really a case for variable declarations.

     
  • Logged In: YES
    user_id=782084
    Originator: NO

    Yes, I was just wondering (since you looked at several files) why you haven't changed things like:
    void xvc_error_write_msg (int code, int print_action_or_not);

    Was that an oversight? Or would you say it's not worth the while because those arguments are passed by value anyway and the function cannot change the originals?

    Otherwise, I have applied the patch.

     
    • status: open --> pending
     
  • Markus Elfring
    Markus Elfring
    2008-02-03

    Logged In: YES
    user_id=572001
    Originator: YES

    I don't want to appear pedantic in this special use case you ask for. I could not see that we might have achieved consensus on such an API detail already.
    It is a matter If you would like to express design aspects in a very strict way.

    Will it be more appropriate to introduce safer data types like "bool" or "size_t" at some places?

     
  • Markus Elfring
    Markus Elfring
    2008-02-03

    • status: pending --> open