Thread: esbo's data sharing example

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    I originally wanted to close tommy_chai's thread, but I figured the fellow deserved better treatment of his thread for what was not his fault.


    You are asking a mathematical proof for what, exactly?

    That global variables hinder extensibility.

    Holds breath.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That global variables hinder extensibility.
    Oh, I see. Kindly provide a mathematical proof that global variables help extensibility.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Esbo:

    A function must always return something, as Elysia already pointed out. In your functions, the correct type would be void, this is common knowledge to anyone who uses C, if a return-type is omitted, the default is integer, but then you would still have to atleast "return 0;" or something, anything else is just poor style and will be frowned upon by other programmers.

    If you do not believe me you can refer to virtually any tutorial on C functions, this is stuff that i learnt during my first week of programming. So i guess that's gonna go in the change log for your un-improvable program eh?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is it really so hard to understand that these functions are only useful

    - if you have one and only one array that you wish to save and read like that (or you'll have to duplicate the functions with modifications),

    - if the only file anyone could possible want to save to is called "data.doc". If you need a file with a different name, you'll need to modify the functions,

    - if the desired error response is close-down. What if in your program there was both data that is vital to the program and data that you could do without? What if failing to save some data was not a big problem? What if there were more sensible ways to deal with the problems, such as asking the user to pick another file, clear a read-only attribute, free disk space etc?

    What if you want to save the data to multiple files? Heaven forbids you should have multiple arrays which go into different files!

    On the other hand:
    1) if the file-name was passed as a parameter, you'd be able to save and write to a file with any name you like,
    2) if the data was passed as an argument as well - oh boy - you write code once and it works for different arrays!

    I have actually programmed in a language that doesn't support passing arrays to functions. Yes, that means if you have a sort routine, you can sort only one array in a program - without code reduplication - and basically for every program you'd have to waste time replacing the name and type of the array - rather than taking a ready-made function from the shelf and simply using it.

    Which is, what I believe reusability is all about. Your code, however, cannot be used under different requirements without modifications. That means - zero reusability.

    As to the data sharing ideas: if you have multiple processes reading and writing to the same file at the same time, wouldn't that cause problems: data corruption or random crashes if the file gets locked?

    ------------

    As to the general quality and extensibility of your code. Take a look at your poker routine and think for a minute what would it take to make it satisfy slightly different conditions. E.g more than one player? Variable or simply different constant hand-size?

    Now, wouldn't it be nice if you could make these changes by changing just the value of one or two variables / constants? Or perhaps by changing a value in command-line or ini-file without having to recompile?

    And guess what - it is possible to write code like that! Starting every project from scratch or doing massive amounts of tedious find-and-replace and copy-and-paste is not the only way to modify the behaviour of your programs.

    ----------------

    The bottom line is that I have seen no generality in your code. All your code is hardcoded with a very specific use case in mind. In fact your functions are misnamed, they should be properly called
    Code:
    void save_pname_to_data_doc_or_terminate();
    void read_pname_from_data_doc_or_terminate();
    to reflect properly what they do. That is, they do not read or write data, they read and write the contents of one specific array.

    Whereas if you coded properly, parts of the name would move into arguments, and the _or_terminate part would become the return value so that the caller can decide what is the most appropriate error response in the context of the call (which the function cannot know about). More importantly, this makes parts of the name variables which means that the same function can be used under much more versatile conditions without a single change.
    Last edited by anon; 01-08-2008 at 04:46 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Neo1 View Post
    Esbo:

    A function must always return something, as Elysia already pointed out. In your functions, the correct type would be void, this is common knowledge to anyone who uses C, if a return-type is omitted, the default is integer, but then you would still have to atleast "return 0;" or something, anything else is just poor style and will be frowned upon by other programmers.

    If you do not believe me you can refer to virtually any tutorial on C functions, this is stuff that i learnt during my first week of programming. So i guess that's gonna go in the change log for your un-improvable program eh?
    OK lets start with the first 6 words of that post.

    "A function must always return something."

    I can prove 'mathematically' that statement is incorrect
    as I can supply numerous programs which have function which do no return
    anything, I have even posted some here earlier.

    So...... can we establish whether you accept that statement is incorrect
    before moving on.?

    A yes or no answer would be most helpful.

    People frown upon lots of things but that in itself is meaningless.

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    Oh, I see. Kindly provide a mathematical proof that global variables help extensibility.
    I am not making that claim so I don't need to.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I can prove 'mathematically' that statement is incorrect
    as I can supply numerous programs which have function which do no return
    anything, I have even posted some here earlier.
    You are right. Neo1 is wrong to say that all functions must return some value. Indeed, Neo1 gave a counterexample in the same post: functions with a return type of void do not return a value. On the other hand, Neo1 also pointed out that your functions are supposed to return a value since you did not specify that their return type was void. Since you do not intend them to return values, you should declare them with a return type of void.

    I am not making that claim so I don't need to.
    I thought you might say that. Well, I am not making the claim so I do not need to either

    So, what claim would you like to make concerning global variables?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by esbo View Post
    I am not making that claim so I don't need to.
    But do you hinestly believe that it is possible to quantify conventional thinking, in order to prove it mathematically?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But do you hinestly believe that it is possible to quantify conventional thinking, in order to prove it mathematically?
    Shh... I'm trying to get esbo to prove something "mathematically" about a stylistic and good/bad practice issue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by esbo View Post
    OK lets start with the first 6 words of that post.

    "A function must always return something."

    I can prove 'mathematically' that statement is incorrect
    as I can supply numerous programs which have function which do no return
    anything, I have even posted some here earlier.

    So...... can we establish whether you accept that statement is incorrect
    before moving on.?

    A yes or no answer would be most helpful.

    People frown upon lots of things but that in itself is meaningless.
    The thing that you apparently don't realize is that if no return type is specified, the function defaults to returning int (and I'm not sure if it is standard any more, at least my compiler would issue a warning about that). That means that your functions can be used like that
    Code:
    int i;
    i = datasave();
    However, since you don't return an int, i would now contain some garbage value. (Again, if you turned on compiler warnings, you should see that it complains about reaching the end of a non-void function without returning something.

    OTOH, if the function specified that the return type is actually void, the above snippet wouldn't compile - you can't use the return value of a void function (because there is none).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #26
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anon View Post
    >Is it really so hard to understand that these functions are only useful

    >- if you have one and only one array that you wish to save and read like that (or you'll have to duplicate the functions with modifications),

    ?- if the only file anyone could possible want to save to is called "data.doc". If you need a file with a different name, you'll need to modify the functions,

    No you can just rename the file.

    >- if the desired error response is close-down. What if in your program there was both data that is vital to the program and data that you could do without? What if failing to save some data was not a big problem? What if there were more sensible ways to deal with the problems, such as asking the user to pick another file, clear a read-only attribute, free disk space etc?

    > What if the moon was made of green cheese?
    The vital data is what cannot be saved there is nothing you can do other than exit.

    >What if you want to save the data to multiple files? Heaven forbids you should have multiple arrays which go into different files!

    I only had one array to save so that what I did.

    >On the other hand:
    >1) if the file-name was passed as a parameter, you'd be able to save and write to a file with any name you like,
    >2) if the data was passed as an argument as well - oh boy - you write code once and it works for different arrays!

    Yes you could do that but I didn't need to do it so why make work for myself?




    >I have actually programmed in a language that doesn't support passing arrays to functions. Yes, that means if you have a sort routine, you can sort only one array in a program - without code reduplication - and basically for every program you'd have to waste time replacing the name and type of the array - rather than taking a ready-made function from the shelf and simply using it.

    >Which is, what I believe reusability is all about. Your code, however, cannot be used under different requirements without modifications. That means - zero reusability.

    The code was written to do a job and it did it very well.
    I could make it multi purpose but I have no need to.
    It is a trivial matter.

    >As to the data sharing ideas: if you have multiple processes reading and writing to the same file at the same time, wouldn't that cause problems: data corruption or random crashes if the file gets locked?

    However that is not the case so I need not be concerned about it.

    ------------

    >As to the general quality and extensibility of your code. Take a look at your poker routine and think for a minute what would it take to make it satisfy slightly different condition. E.g more than one player? Variable or simply different constant hand-size?

    It's a simple matter to modify the code for differnt hand size if required.


    >Now, wouldn't it be nice if you could make this changes by changing just the value of one or two variables / constants? And guess what - it is possible to write code like that! Starting every project from scratch or doing massive amounts of tedious find-and-replace and copy-and-paste is not the only way to modify the behaviour of your programs.

    There are numerous other modifications that could be specified, such as a certain two
    cards must be in the straight, but to think of every condiseration before writing a piece
    of code is a complete waste of time.
    You only need to give it the functinality it needs to work.
    If you need to modify it in the future you can, but you might need to so why waste your time?



    ----------------

    >The bottom line is that I have seen no generality in your code. All your code is meant for a very specific use case. In fact your functions are misnamed, they should be properly called
    Code:
    void save_pname_to_data_doc_or_terminate();
    void read_pname_from_data_doc_or_terminate();
    >to reflect properly what they do.


    I would say if you rely on the name of a function to know what it does you will be in for some surprises!!


    >Whereas if you coded properly, parts of the name would move into arguments, and the _or_fail part would become the interpretation of the returned value. More importantly, this makes parts of the name variables which means that the same function can be used under much more versatile conditions without a single change.

    It is coded properly becaue it works.
    It is a example of how to do something and it does it, that is all that was required.
    It is a good example of how to solve the problem faced.

  12. #27
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by anon View Post
    The thing that you apparently don't realize is that if no return type is specified, the function defaults to returning int (and I'm not sure if it is standard any more, at least my compiler would issue a warning about that). That means that your functions can be used like that
    Code:
    int i;
    i = datasave();
    However, since you don't return an int, i would now contain some garbage value. (Again, if you turned on compiler warnings, you should see that it complains about reaching the end of a non-void function without returning something.

    OTOH, if the function specified that the return type is actually void, the above snippet wouldn't compile - you can't use the return value of a void function (because there is none).
    Of course i = datasave();
    would give a problem you are using the program incorrectly.

    datasave(var1,var3); would also give a problem because you are using the function incorrectly.
    So the problem is not with the function but with you using it incorrectly.
    You can hardly blame other peoples mistakes on my function!!

  13. #28
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by citizen View Post
    But do you hinestly believe that it is possible to quantify conventional thinking, in order to prove it mathematically?

    If you are making a claim you need to be able to back it up.
    People use to think the world was flat.
    Think about it.
    There were people teaching that at the time.

  14. #29
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by esbo View Post
    The code is excellent. Of the highest order, infact I would go as far to say it is impossible to improve on it.
    As far as I can see, these headers are not required:
    Code:
    #include <io.h>
    #include <fcntl.h>
    I think it's hilarious that the Comeau compiler thinks you have a "catastrophic error" in your code! LOL!
    Quote Originally Posted by Comeau
    "ComeauTest.c", line 4: catastrophic error: could not open source file "io.h"
    Quote Originally Posted by esbo
    I can prove 'mathematically' that statement is incorrect
    Could you? I'd like to really see that.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No you can just rename the file.
    hmm... but you cannot, since anon specified that "you need a file with a different name".

    The code was written to do a job and it did it very well.
    I could make it multi purpose but I have no need to.
    ...
    You only need to give it the functinality it needs to work.
    If you need to modify it in the future you can, but you might need to so why waste your time?
    Normally, I would applaud such a statement. Indeed, a function should do one thing and do it well. Likewise, one should write just enough code to meet the requirements. On the other hand, what you have done is to cripple the flexibility of the code from the start.

    I think that hardcoding the filename as "data.doc" is fine. It was an example, so you do not need to be so flexible as you do not have to meet such requirements. However, by the use of global variables, your functions are now tightly coupled to the dataptr and pname variables. Sure, this does not necessarily hurt extensibility, but it does hurt reuse.

    You can argue that these functions will never be reused in this example program. That may be true, but unlike hardcoding a filename, this example is an example of poor design: there is no space for possible reuse, despite the fact that allowing for such reuse also accomplishes the goal of writing just enough code to meet the requirements. Why fix the code to use those two variables alone when it could be written to vary, and yet use those two variables?

    So the problem is not with the function but with you using it incorrectly.
    You can hardly blame other peoples mistakes on my function!!
    Actually, we can blame other people's mistake on your function: you stated that your function returns a type of int. Oh, you didn't? Well, you did, since its return type was not stated explicitly, so it defaulted to a return type of int (assuming this is still standard, and not just some undefined behaviour). It is your mistake, fair and square.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. COM data sharing
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2007, 02:48 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM