Programmatical Habits

Alexander Nicholi

what do you know about computing?
  • 5,485
    Posts
    15
    Years
    When writing code for developing applications, most everyone has a little quirk or two that they do when programming, or habits they've formed over time. Well, what are some of yours?

    For me, I'll just go ahead and say I use chars and shorts... a lot. I hate overallocating space for integers in my C(++) programs.

    Another thing, specifically with plain C: I have to add alias types such as byte, int8, int16, int32, and int64 into my programs. I guess it's just weird for me to use a char as a number. >_>
     
    I find myself needing to constantly put my thoughts in my code. Things like, "This might need work," or "Could remove above section if needed."
     
    I prefer to play it safe when it comes to C++ programs and my code; if I think I'm going to need to do so, I almost always include the necessary libraries and use whatever is provided. I tend to also assume that the variables I'm going to use are going to need to be as flexible as they could be for a given type. I hate unexpected surprises. If I'm over-allocating, so be it. It's probably better to waste a little bit of memory if it means I don't have to think too much about writing future changes and extensions that need reworking later.

    Also, I'm kind of OCD when it comes to tab spacing and general prettiness.
     
    I don't comment enough, actually I don't use them at all in any of the programming languages I know XD Thankfully I'm not coding for anyone else, but myself so there's no need to make it understandable for others.
     
    I avoid using breaks as it's difficult to debug later when you have a break in the middle of a thousand line project.
     
    tbh I've never used a break ever. I do know what they are, and I can agree it'd be a pain in the rear if it did that.

    Edit: I just played around with it a tad, and I now have a much easier way of debugging rather than always using step-in debugging. I just have to place a breakpoint, and from there I can step in and debug only what I have to. But accidentally forgetting to remove them would be painful.

    Exactly.

    It's okay when you use it for debugging and such and remove it later, but I personally think it's a bad programming practice because it's easy to get thrown off when you have thousands of lines of code, especially if one small break is an important component of your code. Same with continue. It's really a personal opinion kind of thing, though.
     
    Exactly.

    It's okay when you use it for debugging and such and remove it later, but I personally think it's a bad programming practice because it's easy to get thrown off when you have thousands of lines of code, especially if one small break is an important component of your code. Same with continue. It's really a personal opinion kind of thing, though.

    First off, I want to say that if you prefer not to use break and continue statements, there's nothing wrong with that, but for any new programmers coming on the scene, I want to make sure that it's understood that the use of the break and continue statements are idiomatic in the majority of languages which support iterative looping. This includes C, C++, C#, Java, Python, and many others. When I say that it's idomatic, I mean that it's widely accepted by programmers, and the designers of the language, as a control-flow tool which is appropriate to use. It's not considered a bad programming practice to use them, and in many situations, it's necessary.

    Take for example, a switch statement in C++, how would you use one effectively without a break statement?
    Take this example as well which includes a short-circuit return, acting similarly to a break statement:
    Code:
    bool contains(int* myPntr, int size, int target){
      for(int i = 0; i < size; i++){
        if(myPntr[i] == target){
          return true; //If you had to do something else before returning, you could actually just break out here.
        }
      }
      return false;
    }
    Also, consider the following for continue situation:

    Code:
    int someFunc(bool a, bool b){
      int i = 0; //Doing work on this.
      int c = 0; //Counter.
      while(c < 50){
        c++;
        i+=1;
        if(a) continue;
        i += 2;
        if(b) continue;
        i += 3;
      }
     return i;
    }
    I realize that's a pretty contrived example, sorry, I'm kind of tired and couldn't think of anything off the top of my head. That said, there are times when you need to do one thing first, then if I certain condition is true, ignore the rest (using continue), but if the condition isn't true, you can carry on and do the rest. It's like a conditional fall-through. Sometimes you're going to need the second part of that while loop, sometimes you're not. Rather than writing a redundant piece of code, you can just conditionally use continue.

    Like I said, if you don't want to use it, that's cool. It totally depends on what you're doing, and yeah, you can get around the use of break in most cases, but sometimes it leads to really complicated conditions in while loops, and sometimes it can detract from readability for that reason. I rarely use continue, I'll be honest, I have used it, but it's rare. I do often use break though, and I don't think it makes my code any harder to follow. I usually don't have massively long loops with a huge number of break statements, but it's not uncommon to see a while(true) loop in my code with a couple of breaks thrown in, depending on the application. I really don't think it detracts from readability, since breaks are only one way, right? They break out of the function downwards. It's not like a goto statement (although, in C++ I think it is actually a wrapper for goto, but it's obviously restricted to jumping out of the loop), which can literally go pretty much anywhere. You see a break, you know it's breaking out of the loop. Keep in mind, this is just my opinion.

    I just wanted to be clear that using break and continue is not considered bad programming practice paradigmatically, and is in fact idiomatic, in most languages which support the statements.

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

    As far as quirky stuff I do while coding, probably having conversations with other developers through comments. At the company I'm working for now, we're notorious for having conversations, discussions, and occasionally arguments at the top of our code files. We always clean it up though. :)
     
    Last edited:

    Of course, using breaks in switch statements is necessary, I forgot to account for that as well. But I was mainly talking about

    Yes, my opinion is not objective, in fact I have stressed that it's my opinion by saying "I personally think." It is a valid programming construct, and there are times when it's absolutely necessary (i.e. switch statements) - However outside those situations I never used break. I also think it also depends on what you are using them for inside loops, but that's whole another debate that I don't want to get into.

    Thank you for your insight, though.
     
    Of course, using breaks in switch statements is necessary, I forgot to account for that as well. But I was mainly talking about

    Yes, my opinion is not objective, in fact I have stressed that it's my opinion by saying "I personally think." It is a valid programming construct, and there are times when it's absolutely necessary (i.e. switch statements) - However outside those situations I never used break. I also think it also depends on what you are using them for inside loops, but that's whole another debate that I don't want to get into.

    Thank you for your insight, though.

    Just so we're clear, I have absolutely no problems with your original post. ^_^ I totally appreciate that your views are entirely subjective. Like I said, it's fine to avoid breaks and continues and other control flow tools if you don't want to use them; that's totally, totally fine. Everybody has their own style, and I'm sure that yours is very readable and awesome! I just didn't want people who were new to programming who might see this thread to think that using breaks and continues were considered bad practice by the general community, when that is certainly not the case. ^_^
     
    Last edited:
    I make sure to indent even if it isn't necessary for the program, and comment as well. Even if the comment is just a joke. One of my programs I used for honours had a comment along the lines of 'I heard you like compares so I put compares in your compares so the program can compare the compares'. It was also actually relevant...kinda. :V

    I use continues at times, but usually for testing. I also tend to have messages display when I'm bug checking ('yay' if something works, 'nay' if not).
     
    I'd also like to mention that in Notepad++, I write all of my code and right before compile time I use the replace macro to convert all of the tabs into quadruple spaces. If I want to continue editing, I do the converse.

    Tab-based nesting is very important to me, which is why I love using NP++. I also have all of my syntax highlighting color-coded in NP++; C uses reds, C++ uses yellows, C# uses greens, VB uses soft blues, PHP uses teals, Java uses oranges and golds, Python uses hard blues, Perl uses pinks and magentas, and so on. It really personifies the languages for me. :)

    BTW, when I use braces in C-based languages, I always have the opening brace on the same line as the statement. It gives it a more uniform feel, plus an entire line isn't being wasted just for a brace like in Visual Studio. I do give closing braces their own lines, on the contrary. :P
     
    Just so we're clear, I have absolutely no problems with your original post. ^_^ I totally appreciate that your views are entirely subjective. Like I said, it's fine to avoid breaks and continues and other control flow tools if you don't want to use them; that's totally, totally fine. Everybody has their own style, and I'm sure that yours is very readable and awesome! I just didn't want people who were new to programming who might see this thread to think that using breaks and continues were considered bad practice by the general community, when that is certainly not the case. ^_^

    I know what you're trying to say. It would've been better for me to have said to use them only when necessary.

    I make sure to indent even if it isn't necessary for the program, and comment as well. Even if the comment is just a joke. One of my programs I used for honours had a comment along the lines of 'I heard you like compares so I put compares in your compares so the program can compare the compares'. It was also actually relevant...kinda. :V

    I use continues at times, but usually for testing. I also tend to have messages display when I'm bug checking ('yay' if something works, 'nay' if not).

    Indenting and commenting to me is second nature. Unindented and uncommented programs are very difficult to read. Indentation is required for some programming languages like Python as well.
     
    Back
    Top