r/learnprogramming • • Jul 12 '13

What are some bad coding habits you would recommend a beginner avoid getting into?

Whether they're stylistic habits or program design habits. I'm learning java and want to be sure to write my code as efficiently as possible to avoid lengthy code and poor design choices.

249 Upvotes

215 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jul 12 '13

Any resources on how to remove breaks from a loop and where they're acceptable?

4

u/deuteros Jul 12 '13

One way breaks can be used effectively is when you're using a loop to look for a value that meets a certain condition. Once you've found that value there's no need to continue the loop so you break it.

1

u/vaelroth Jul 12 '13
boolean found = false;
while(!found)
{
    //do stuff
    if(search condition = true)
        found = true;
}

I'd much rather use this pattern than insert a break into a loop.

7

u/deuteros Jul 12 '13 edited Jul 12 '13

Except in this case you've just created an infinite loop if your search condition is never satisfied.

Here's an example of when a break statement would be appropriate:

public bool IsPalindrome(string word)
{
    int left = 0;
    int right = word.Length - 1;
    bool isPalindrome = true;

    while (left < right)
    {
        if (word[left] != word[right])
        {
            isPalindrome = false;
            break;
        }
        left++;
        right--;
    }
    return isPalindrome;
}

0

u/[deleted] Jul 12 '13

[deleted] — view deleted comment

1

u/false_tautology Jul 12 '13

Without a break it becomes more bug-prone and harder to read. Removing the break makes no sense. Why would you write worse code just to appeal to rigid dogma?

Break statements are a tool. One-in one-out can lead to bad design.

1

u/[deleted] Jul 12 '13

If you're going to do something like that, it can be better (sometimes) to not introduce a variable and just do something like:

// somewhat pseudocode
while(true) { // assuming for isn't appropriate
    var = getNextVar();
    
    if(!someCheapDesireableCondition(var)) {
        continue;
    }

    if(!somethingMoreExpensive(var)) {
        continue;
    }

    if(!thisCheckTakesForever(var)) {
        continue;
    }

    break;
}

1

u/Tynach Jul 12 '13

Pure speculation from a newb, but I'd say break is acceptable if an error occurs. Granted it's best to throw an exception, but some languages that don't have exceptions require the use of 'break'.

3

u/Shadowhawk109 Jul 12 '13

Or if you found what you're looking for (C# examples):

classType result = null;

foreach (var classInstance in myClassList)
{
     if (myArbitraryCondition(classInstance))
     {
          result = classInstance;
          break;
     }
}

if (result == null) 
   //throw exception, return false, whatever cool kids do

Note: you can also say that this is "poor" and can be easily achieved with LINQ:

 var result = myClassList.Where(c => myArbitraryCondition(c));
 if (result == null)
    //blah

But I'd argue that's not exactly noob-friendly; some people don't like trying to wrap their head around LINQ.

2

u/Tynach Jul 12 '13

Some would say this should result in a 'return' statement, not a 'break' statement.

1

u/[deleted] Jul 12 '13

How does LINQ do something without using a break statement itself?

For example, how could I search an array to see if it has an element, and break once an instance of that element is found?

1

u/Pyromine Jul 12 '13

Yeah, exactly I'm still very much a begginer programmer so I haven't worked my way up to LINQ yet so it is a lot easier to use break; than to look up some fancy way to do something with LINQ.

1

u/Shadowhawk109 Jul 12 '13

LINQ is just essentially "SQL for the STL". That is,

 var derp = FOR i IN mycontainer SELECT member WHERE condition;

which can be lambda'd into

var derp = mycontainer.select(f => f.member).Where(f => condition(f.member));

or something along those lines.

It's not hard, it's just new, and some people don't like new.

1

u/Pyromine Jul 12 '13

Oh, yeah I don't exactly expect it to be hard, it more comes down to I'm still learning SQL a bit right now, but to be honest that is incredibly easy to learn, but I'm more focused on learning to code right now than learning about LINQ, especially when most of my coding is actually in PHP so I don't really have a use for LINQ. I'm striving to be decent at web development type coding by the end of the year, but I'm only a high school student so I don't really know if I'll have the time.(Disclaimer, I would have no doubts about having the time but I'm taking summer classes right now and have a schedule with more credits than periods so I will unfortunately not get nearly as much coding time as I'd like)