Thread: C++ and C#

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    C++ and C#

    I try to learn C# and it said that it is a good practice to limit 25 lines of code in one function. If it is more than that, you have to break into another function. What about C++? Do we have such advice? What is the limit to put lines of code in one function in C++?

    In C#, code like this:
    Code:
    int main() {
    
         Detroit_Pistons() {
       
         }
    
    }
    is bad. The good one is like this:
    Code:
    int main()
    {
    
          Detroit_Pistons()
          {
    
          }
    
    
    }
    But if I were not mistaken, in C++ both style are good. So I just wonder why C# developers are such weird people?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Correct me if I'm mistaken, but you have a function inside another function... isn't that illegal?

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Sorry it should be:
    Code:
    int main() {
    
    }
    
    Detroit_Pistrons() {
    
    }
    and:

    Code:
    int main()
    {
        
    }
    
    Detroit_Pistrons() 
    {
    
    }
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm pretty sure it's a matter of taste which way you do it. Personally I prefer this way:
    Code:
    int main()
    {
        return 0;
    }
    As opposed to this way:
    Code:
    int
    main() {
        return 0;
    }
    But other people are different.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What about C++? Do we have such advice? What is the limit to put lines of code in one function in C++?
    I don't think any language limits it as such - the limit is imposed by programmer discipline in terms of producing readable code.

    It basically boils down to this - if you can't see the whole function on one screen / page, then it may be too complicated.
    There are however exceptions - for example a command decoder may be many hundreds of lines long, yet perfectly clear to any reader.

    The compiler won't care whether a function is 10 or 1000 lines long, but your maintainer might.
    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.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I agree with Benny, I also prefer:
    Code:
    int main()
    {
        
    }
    
    Detroit_Pistrons() 
    {
    
    }
    I'll think you'll find code that follows that bracing style is not considered "bad", it's just not generally done. If you look through the .NET SDK developers reference you'll see most of their examples are done this way. It's a personal preference thing, the same as whether or not to use hungarian notation.

Popular pages Recent additions subscribe to a feed