Good coding style?


  • I survived the hour long Uno hand

     Is there a resource somewhere where I can get an explination of what's "good form" in programming and, more importantly, why? While studying on my own I only ran across things that seemed to make sense, but now that I'm learning in an academic setting all I seem to be getting from my teacher is "that would work but don't ever do it becasue it's bad form." and no explination why. Is there some website or something where I can find better reasoning? 

     (an example of the kind of thing I mean is, my first thought upon getting an assignment to display a menu and do things based on the choice, with a final option to return to the menu or quit, is to use a do-while loop, while the teacher insists the only correct way is to print the menu, then use a while loop to do the calculations, ending with the menu again, or better yet, put the menu in a function and call it from two locations). 



  • The book Code Complete by Steve Mcconnell is a good resource.  It doesn't really address UI stuff, it is a very good resource on how to approach things from a pure code standpoint.


  • I survived the hour long Uno hand

     I should mention, free resources are preferable. That said, I've heard good things about that book before; hopefully either the school or public library has a copy I can borrow. 



  • CC is indeed a great resource, but what you're looking for (a "GAAP but for programmers") simply doesn't exist. We're getting close to seeing the makings of such principles (SOLID, for example is a good start), but aren't quite there yet. Check back in twenty years.

    Proper code is working and maintainable code; everything you do should stem from that. The only thing that will teach you "good form" is experience, mentors, and having to deal with "bad form" code.


  • I survived the hour long Uno hand

     @Alex Papadimoulis said:

    Proper code is working and maintainable code; everything you do should stem from that. The only thing that will teach you "good form" is experience, mentors, and having to deal with "bad form" code.



    OK. See, that's what I thought, but the teacher keeps going on with hard and fast rules, so I wondered if it was just her personal preference she was teaching as rules or if there was some, I don't know, giant civil war resulting in some sort of treaty in which programmers swore to never again use do-while loops or something like that.



  • @yamikuronue said:

    OK. See, that's what I thought, but the teacher keeps going on with hard and fast rules, so I wondered if it was just her personal preference she was teaching as rules
    What you'll find (if you go into programming as a profession) is that some places have their own 'as hard and fast' rules. And these rules will, in all probability, directly contradict what your teacher says.

    @yamikuronue said:

    or if there was some, I don't know, giant civil war resulting in some sort of treaty in which programmers swore to never again use do-while loops or something like that.
    About the only globally 'accepted' rule is 'goto's are (mainly) bad.' With that parentetical bit omitted in the cases of some less enlightened people.

    Everything else is generally opinion (except where forced to comply with local rules, like your teacher, or your boss,) or being 'sensible.'

    My advice - nod, say 'yes teacher,' comply with what she's asking, while being aware that her rules do not apply everywhere, but in some places you'll meet more people like her, and they too will have a say over how your code is written.



  • @yamikuronue said:

    but the teacher keeps going on with hard and fast rules

    The "hard and fast rules" are pretty common for new learners of all things... mostly b/c new learners lack the ability to judge and adequately perform more advanced scenarios. I guess I can see someone considering a do-while (as opposed to a while) being one of those techniques...



  • @yamikuronue said:

    OK. See, that's what I thought, but the teacher keeps going on with hard and fast rules, so I wondered if it was just her personal preference she was teaching as rules or if there was some, I don't know, giant civil war resulting in some sort of treaty in which programmers swore to never again use do-while loops or something like that.

    Nah, there's no real practical reason do-while loops are "poor form" per say. Like most coding tools there's good times to use them and bad times to use them like almost everything. It's coding patterns/structures that make bad code, not individual methods that should never be called. Only thing I've heard a general consensus as "poor form" are goto's, though even those have their places (Very limited places, but there is reasons for those to exist).

    I'd probably say a do while loop is fine for what you're talking about, but as people have said you're generally best to follow instructions. Same can apply in the workforce, you'll come across coding conventions that won't match what you normally do, it's simply a fact, and for the sake of everyone else at the place you work you'll have to change your style to suit the conventions.

    Most people I've heard object to do while have done so because they're used to the condition for a loop being at the top of the loop and find it less readable, but that's all personal preference and if the code inside the loop is split into functions correctly it shouldn't be that long of a loop body anyway ;)

    As far as good things to do, it's hard to get a definitive list cause there's no magic bullet. Might be worth considering giving [url]http://en.wikipedia.org/wiki/Anti-pattern[/url] a skim though as a place to start, it's a list of bad coding styles (As opposed to specific methods) to avoid and why each is bad. End of the day if it's efficient, readable and maintainable it's good code ;)



  • do while loops imho only have one application, when you want a piece of code to run one or more times. This scenerio however almost never appears, so it goes largly unused.

    At least that's my experience.



  • @stratos said:

    do while loops imho only have one application, when you want a piece of code to run one or more times. This scenerio however almost never appears, so it goes largly unused.

    At least that's my experience.

    Not much experience then?



  • As long as the code doesn't overly complicate things / is unnecessarily duplicated / simply looks terrible, it's good.



  • @HypocriteWorld said:

    As long as the code [b] [/b] is unnecessarily duplicated / simply looks terrible, it's good.

    Simplified that for you.



  • @PJH said:

    About the only globally 'accepted' rule is 'goto's are (mainly) bad.' With that parentetical bit omitted in the cases of some less enlightened people.

     

     There's an interesting pdf to read (if you don't know it already); it's  "Structured programming with go to Statements", written by Knuth.

    It's a bit outdated, but you can read some bits about the struggle for readability in the old times ... and you can see how advanced we're today (at least regarding loop control - in perl with do, while, next, last, continue).

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.