Thread: Is c++ Inherently Broken ?

  1. #1
    Registered User
    Join Date
    Oct 2023
    Posts
    18

    Is c++ Inherently Broken ?

    Code:
    namespace util
      {
      void print_array(int array[])
        {
        int count = sizeof( array ) / sizeof( array[0] );
        for (int i = 0; i <= count; i++) cout << array[i];
        }
      }
    class Util
      {
      public:
        static void print_array(int array[])
          {
          int count = sizeof(array);
          for (int i = 0; i <= count; i++) cout << array[i];
          }
      };
    
    
    util::print_array()  // Namespace
    Util::print_array()  // Class
    C++ - Namespace vs. Static Functions - Stack Overflow


    Consider the example above from stackoverflow.

    The static method is called from a class, similar to that of a namespace.
    They are both valid when called although namespace is frowned upon because it is an extra language feature,
    but the fundamental question is, which is which ?
    The second question is, what to use ?

    Furthermore, the static keyword in c hides the method; the method cannot be used outside that header, in that sense, a one-dimensional c programmer would be confused ?

  2. #2
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    Is c++ Inherently Broken ?

    The correct word would be flawed.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I guess given a sufficiently rich set of tools to play with, people will always find ways to mis-use them and then blame the tool.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by upload
    They are both valid when called although namespace is frowned upon because it is an extra language feature
    You are mistaken: the use of a namespace is not "frowned upon because it is an extra language feature". In fact, for grouping a bunch of functions so loosely related that their group name is "util" or "Util", a namespace would be more appropriate than a class of static non-member functions. This answers your question of "what to use ?" for this case.

    Quote Originally Posted by upload
    but the fundamental question is, which is which ?
    You would look at the prefixed name (as in util or Util) and then figure out what it refers to based on the context (declarations earlier in the code, in included header files, etc). In some IDEs, some kind of intellisense tool could shortcut that and show it to you.

    Quote Originally Posted by upload
    Furthermore, the static keyword in c hides the method; the method cannot be used outside that header, in that sense, a one-dimensional c programmer would be confused ?
    Don't hire one-dimensional C programmers to write C++ code then. Actually, don't hire them at all. After all, they might be confused by the multiple uses of the static keyword even when writing C programs!
    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

  5. #5
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    If you use namespaces, you will frequently hit a language defect that functions which call each other must be listed in a specific order, because C++ can't see definitions further down in the file.


    If you use classes, this defect does not occur.


    It can be easier and cleaner to wrap implementation functions in a class than to maintain declarations for them all or put them in an unnatural order to make it compile.
    Also refer to :
    c++ - Namespace + functions versus static methods on a class - Stack Overflow

    The post is not untrue, I have reviewed code where there is an excessive use of namespace and the whole thing ends up messy.


    You are mistaken: the use of a namespace is not "frowned upon because it is an extra language feature". In fact, for grouping a bunch of functions so loosely related that their group name is "util" or "Util", a namespace would be more appropriate than a class of static non-member functions. This answers your question of "what to use ?" for this case.
    Actually the idea is to use basic syntax and not overuse the additional features that comes with every new c++ standards, there are certain projects that are hard to read and almost illegible for that reason. I incorrectly assumed that there could be some general rules that may require these features.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    It can be easier and cleaner to wrap implementation functions in a class than to maintain declarations for them all or put them in an unnatural order to make it compile.
    But why would you put functions in a namespace if they're not shared with other source files? (That is, if they're all static (file-local) functions then why do they need to be in a namespace?)

    And if they are shared with other source files, then why wouldn't you put declarations for all functions in a header file? And even if they're not in a namespace you would run into the same issue with regard to using a function before it's declared. So namespaces are not the real issue here.

    Edit: I missed the "implementation functions" part, but my previous logic still applies re: using a function before it's declared, whether it's in a namespace or not.

    Quote Originally Posted by upload View Post
    I have reviewed code where there is an excessive use of namespace and the whole thing ends up messy.
    Sure, excessive use of any language feature can make code messy.

    Quote Originally Posted by upload View Post
    Actually the idea is to use basic syntax and not overuse the additional features that comes with every new c++ standards,
    Namespaces were added to C++ in 1995. That was 28 years ago. That's the year Windows 95 came out. Movies like Toy Story and Apollo 13 came out that year too. Mac OS X hadn't come out yet. Let that sink in. 28 years is an eternity in computer time and far more than long enough to get used to a "new" or "additional" language feature.

    And the idea of using only the "basic syntax" of C++ sounds like a "one-dimensional C programmer" who can't understand the purpose of namespaces and what sort of problems they solve.

    Quote Originally Posted by upload View Post
    there are certain projects that are hard to read and almost illegible for that reason.
    Sure, blame the tool rather than the programmers who overuse it. See also "excessive use of any language feature can make code messy".

    Methinks you have a grudge against a language feature you don't quite understand (a feature that's been a part of the language since perhaps before most programmers in the workforce were even born, and almost surely before most programmers started learning the language) and want to blame it for poor programming practices.

  7. #7
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    Methinks you have a grudge against a language feature
    That's a wild assumption, that's like me thinking that you are a hardline liberal hooked on that kool-aid, who feels stuck in a red state and who remembers only Toy story and Apollo13.

    Ramblings aside, I could care less about language or features, at some point in time I will be done with this whole thing.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by upload View Post
    I could care less about language or features
    Clearly you could care less as you do care at least a little about them. (If you cared not a whit about them then that means you couldn't care less about them, and you wouldn't be posting about them in the first place.)

    What is the point of this thread anyway, besides rambling about a basic language feature that's been part of the language from practically the beginning, one that you don't really understand but want to complain about for some reason? Do you have an actual question?

    (C++ had existed for 16 years by the time namespaces were added, but it's now been 28 years since namespaces were added, so they were added closer to the beginning than to today, so I think it's fair to say they've been part of the language from "practically the beginning".)

  9. #9
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    complain about for some reason?
    Believe what you want to believe.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by upload View Post
    Also refer to :
    c++ - Namespace + functions versus static methods on a class - Stack Overflow

    The post is not untrue, I have reviewed code where there is an excessive use of namespace and the whole thing ends up messy.
    Surely you read the top voted answer to that Stack Overflow question? It rightly concludes:
    Namespaces are for namespaces. Classes are for classes.

    C++ was designed so each concept is different, and is used differently, in different cases, as a solution to different problems.

    Don't use classes when you need namespaces.

    And in your case, you need namespaces.
    As for "excessive use": then don't use it excessively. Duh. I mean, have you seen three star C programmers? Would you conclude then that pointers in C should not be used, merely because an excessive number of levels of indirection makes code difficult to understand? (Actually, some would agree, that's why they don't program in C, or even in C++!)

    Quote Originally Posted by upload
    Actually the idea is to use basic syntax and not overuse the additional features that comes with every new c++ standards, there are certain projects that are hard to read and almost illegible for that reason. I incorrectly assumed that there could be some general rules that may require these features.
    Well, if overuse of namespaces is causing code to be "hard to read and almost illegible for that reason", then you need to figure out how to restructure the code to address that problem. Merely replacing namespaces with classes consisting entirely of static member functions does not fix the problem, and likewise replacing namespaces with name prefixes does not fix the problem. In fact, they likely would make the problem worse, since the option to use using declarations and using directives disappears (but those can be overused too! At some point you just need better training and judgement on the use of tools).
    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

  11. #11
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    judgement on the use of tools).
    I don't understand why you have to pull a judgement card, what if I tell you that the whole idea of namespace is flawed and that it is you who have poor judgement.

    Would you conclude then that pointers in C should not be used ?
    Now you are pulling my leg, that's no different to someone who takes a wild guess, and who assumes that you are a pro-choice apologist who screams, "all lives matter" ?

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by upload View Post
    I don't understand why you have to pull a judgement card
    ???

    I'm thinking there might be a language barrier here. "Judgment", as laserlight used the word, means (from a quick Google search on its definition) "the ability to make considered decisions or come to sensible conclusions", which is a very useful skill as a programmer and is one that can be learned through experience. Surely you're not arguing that having good judgment as a programmer is bad?

    what if I tell you that the whole idea of namespace is flawed
    You'd be arguing against a whole lot of existing computer science: Namespace. The namespace feature in C++ simply applies that concept. Do you actually have any evidence or arguments that the concept of namespaces is flawed? Just repeatedly saying it's flawed is not an argument.

    Now you are pulling my leg
    I think it's a good analogy.

    By your logic, because excessive use of namespaces makes code messy, namespaces shouldn't be used (or is a flawed concept). Am I correct so far?

    If we applied the same logic to pointers, we could conclude that pointers shouldn't be used either.

    You can apply the same logic to many other useful language features:

    • Function pointers
    • return statements
    • Classes
    • Conditional operator
    • Function parameters
    • Nested loops
    • Recursion
    • Variables
    • Parentheses
    • Whitespace
    • Etc.


    Should we say the whole idea of function pointers is flawed? Should we get rid of the "return" statement? Are classes flawed too? And let's not forget that flawed conditional operator!

    I hope you can see the silliness of that logic.

    Edit to add: Java also has namespaces. The String class, for example, is in the java.lang namespace. Are namespaces in Java flawed too?
    Last edited by christop; 11-01-2023 at 10:56 AM.

  13. #13
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    By your logic, because excessive use of namespaces makes code messy, namespaces shouldn't be used (or is a flawed concept). Am I correct so far?


    If we applied the same logic to pointers, we could conclude that pointers shouldn't be used either.


    You can apply the same logic to many other useful language features:


    Function pointers
    return statements
    Classes
    Conditional operator
    Function parameters
    Nested loops
    Recursion
    Variables
    Parentheses
    Whitespace
    Etc.




    Should we say the whole idea of function pointers is flawed? Should we get rid of the "return" statement? Are classes flawed too? And let's not forget that flawed conditional operator!


    I hope you can see the silliness of that logic.
    That's your logic.

    Java also has namespaces.

    Packages in Java and namespaces in C++ are two different things.


    With logic like yours, maybe you could explain to us why liberals dye their hair green, pink and violet ?

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by upload View Post
    Packages in Java and namespaces in C++ are two different things.
    Packages in Java and the namespace keyword in C++ both implement the "idea" of namespaces, which you implied is flawed:

    Quote Originally Posted by upload View Post
    what if I tell you that the whole idea of namespace is flawed
    Or do you mean something else?

    If you only mean that "namespace" in C++ is flawed, then why is it flawed yet packages in Java are not flawed? They're really not that much different except in syntax (Java has a line like "package foo;", while C++ has "namespace foo" with the contents inside braces).

    But in any case, how is it flawed?

  15. #15
    Registered User
    Join Date
    Oct 2023
    Posts
    18
    But in any case, how is it flawed?
    How about using a regular class.

    Why eat soy food when you can eat ground beef and drink logger life ?
    Libtardism I guess..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Broken for loop in c++
    By Stark_Barksalt in forum C++ Programming
    Replies: 9
    Last Post: 10-17-2013, 03:46 AM
  2. Is my C++ compiler broken?
    By @nthony in forum C++ Programming
    Replies: 57
    Last Post: 10-12-2008, 02:50 PM
  3. QInttValidator may be broken.
    By curlious in forum Linux Programming
    Replies: 0
    Last Post: 09-20-2005, 11:00 AM
  4. This CD Drive is broken?
    By alphaoide in forum Tech Board
    Replies: 3
    Last Post: 06-04-2005, 02:59 PM
  5. Rep Broken?
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 02-04-2005, 07:00 AM

Tags for this Thread