Your Coding Style?

This is a discussion on Your Coding Style? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Code: B.) void Function() { //myCode } Code: A.) int *myPointer This is because in: int *myPointer, myPointer2; myPointer2 wouldn't ...

  1. #16
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Code:
    B.)
    void Function()
    {
       //myCode
    }
    Code:
    A.)
    int *myPointer
    This is because in:
    int *myPointer, myPointer2;
    myPointer2 wouldn't be a pointer to an int, it would just be an int, so having the * closer to the variable name is clearer.
    Code:
    A.)
    if (false == condition)
    I like the false first, because condition might be very very long and I'd rather not scroll horizontally.
    Code:
    A.)
    const char msg[] = "Welcome to Utah.";
    Code:
    A.)
    void Function(int& x){
         x = 25;
    }
    
    int main(void){
        int x=0;
        Function(x);
        return 0;
    }

  2. #17
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    > 2. B (the * is a part of the type name)
    int* p,i;
    What type is i?
    If you're thinking i is a pointer, think again.
    If, as I, you only declare one variable per line, then I can see his point, although it is not as I do it. int* p declares an "integer pointer" so it is associated with the type.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #18
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Most C++ programmers seem to prefer the int* var style. Most C programmers seem to prefer the int *var style. Only numbnuts use int * var. As for braces, I always line them up in columns. Its just so much easier to spot a missing one than k&r style bracing. I hate looking at code that has k&r style bracing and reformat it as a matter of urgency. I find its easier to read that way.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #19
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    agreed. k&r I believe was done that way to make it take less lines back when terminal windows and editors were limited. Lined up on the left I believe is called Allman. If you indent the braces and keep them lined up it's called whitesmith.

    I prefer Allman. Thus, I guess my answer to the original question is:

    1. B
    2. A
    3. B
    4. B
    5. poor question
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #20
    Registered User
    Join Date
    May 2005
    Posts
    5
    1. A
    2. c
    3. a
    4. a
    5. b

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    1 - B
    2 - A
    3 - A
    4 - B
    5 - For that example A (assuming C++), but there are functional differences between pointers and references that would make a general answer impossible.

    >I hate looking at code that has k&r style bracing and reformat it as a matter of urgency.
    I appologize for any code I may have forced you to read with my evil K&R bracing style.
    My best code is written with the delete key.

  7. #22
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by Salem
    > 2. B (the * is a part of the type name)
    int* p,i;
    What type is i?
    If you're thinking i is a pointer, think again.
    Umm, yes I'm aware of the basic functionality of the language.


    More info:
    http://www.research.att.com/~bs/bs_faq2.html#whitespace
    Last edited by Sang-drax; 05-31-2005 at 02:31 PM.

  8. #23
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    1. B
    2. A
    3. A
    4. A
    5. B

  9. #24
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    1.) B
    2.) A
    3.) A
    4.) A
    5.) A since B wouldn't work as he has it

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    1) Mixed. (Linux Kernel Style)
    Code:
    function
    {
    
      if(cond) {
      }
    
      for(;;) {
      }
    
      if(really
          long
          condition)
      {
      }
    
      switch(value)
      {
      case :
        {
        statements;
        }
      }
    
    }
    (And I NEVER not place braces except on case marks.)

    2) int *p1;
    int *p2;
    (I try to avoid having more than one pointer declaration on one line.)

    3) if(!condition)

    4) const char *const REAL_STRING_CONSTANT = "...";
    std::string initialized_string("...");
    (The two given methods are different: one can be edited, the other takes up less space.)

    5) Depends. I prefer references. For arrays, I pass std::vector references. For optional parameters, I like Boost.Optional.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    1 - B
    2 - B
    3 - B (w/o the space in before the opening paren: "if(!cond)")
    4 - B (w/ * attached to "char")
    5 - Depends on the situation... in that one, A
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #27
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    1.B
    2.B
    3.B (extracting the space before the opening parenthesis)
    4.depends (probably B but usually neither)
    5.depends (B most of the time excepting that i would deference the pointer in the function)

    >
    Most C++ programmers seem to prefer the int* var style. Most C programmers seem to prefer the int *var style. Only numbnuts use int * var.
    <

    i would have thought the opposite(i would generally be a C programmer, though i use C++ where it fits), though i agree with the third part...
    Last edited by no-one; 05-31-2005 at 06:11 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #28
    FOX
    Join Date
    May 2005
    Posts
    188
    1. B
    2. A
    3. A
    4. B
    5. Depends


    I have another question for you. Those of you who never code beyond 80 characters per line, how to you handle such occasions when it's impossible to not do it.

    Code:
    static char *long_function_name(const char *dest, const char *src, const char *options, size_t n)
    I can't seem to indent the code with tabs properly if I put a newline after any of the parameters. Using VIM and tabs that take up 8 spaces on the screen.

  14. #29
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Those of you who never code beyond 80 characters per line
    ...obviously haven't done much STL programming in C++.

    >how to you handle such occasions when it's impossible to not do it.
    When is it impossible to not do it?
    Code:
    static char *long_function_name(
      const char *dest, const char *src, 
      const char *options, size_t n)
    It doesn't have to be pretty (though saying that chafes a LOT), it just has to work. You do what you gotta do.
    My best code is written with the delete key.

  15. #30
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I've had no problem using VIM. And you should consider reducing the tab size.

    I would go with something like:
    Code:
    static char*
    long_function_name(
    	const char *dest, 
    	const char *src, 
    	const char *options, 
    	size_t n)
    {
    }
    Or something similar

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it bad style of coding ??
    By noobcpp in forum C++ Programming
    Replies: 15
    Last Post: 11-06-2008, 10:39 AM
  2. Coding style?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 10-22-2003, 12:26 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM
  4. comment on my coding style and if i use functions well
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-09-2002, 07:24 AM
  5. coding style
    By ActionMan in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21