How to make fewer errors at the stage of code writing
1–10 of 24 posts
Re: How to make fewer errors at the stage of code writing
#2Yes. Makes code easier to read and makes it easier to refactor if you use an editor with column select/multi-line caret.
Re: How to make fewer errors at the stage of code writing
#3> Align everything you can in code Yes. Makes code easier to read and makes it easier to refactor if you use an editor with column select/multi-line caret.
Unsigned types, on the other hand, are just evil. Way too many people use an unsigned type because they think it represents an invariant that this value will never be negative, that it has some kind of documentation or safety aspect, but that's just wrong. Unsigned types don't act according to the intuitions of most average programmers, and things as simple as for-loops are incredibly easy to get wrong with them (e.g. think about the most common idiom for iterating backwards in C derivatives). If I had my druthers, mixing signed and unsigned ought to expand the expression to the next larger signed type that can represent the range of both (and error out if there is none), and there ought to be separate subtraction / decrement operators specifically for unsigned types. 'u += -2' where u is unsigned would be a type error - assigning a signed value to an unsigned location.
Re: How to make fewer errors at the stage of code writing
#4Get good with your tools (whether that be IDE, knowledge of the language, APIs etc)
Test your assumptions mercilessly
If you write code as though nobody will ever see it and nobody cares, the code will suck. If you write code as though for the best, most respected and most intelligent hacker(s) you know, the then code will be better. Write code as though you have an important audience that cares deeply about the result.
Yes, you can sprinkle the code with comments explaining to your important audience how awesome you are and what a clever piece of coding something is. And this is actually a good thing, since these become focal points for mercilessly testing your assumptions :D
Re: How to make fewer errors at the stage of code writing
#5> Align everything you can in code Yes. Makes code easier to read and makes it easier to refactor if you use an editor with column select/multi-line caret.
I personally detest people who do this, outside of things like tables (i.e. structured constant initializers) and comments on type definitions. If there's a high degree of regularity in the source that you're trying to expose, there's probably a better way of doing it - extracting out common variables, using a table lookup, adopting acronym conventions for regular parts along any given axis which have the same length…
... don't hate me because I'm beautiful ... :D
----
On a more serious note, something I have noticed working on large projects with other programmers, is that if each programmer has their own distinctive style and conventions, then you can tell straight away by looking at it who wrote it, and more importantly who modified it.
Thus if Billy (who is an idiot) modified something written by Sheila (who is like unto the goddess of inspired poetry in wisdom and understanding), and the whole is broken, you can pretty much bet that it is Billy's modification that has broken it, and you can spot his bits straight away, this makes debugging a lot easier.
I think this is one of the more unfortunate side-effects of things like checkstyle and other forms of overly restrictive style enforcement. The more we sand-paper everybody back to the same (low) level of code appearance, the more information is lost for what I like to call "forensic debugging".
Re: How to make fewer errors at the stage of code writing
#6> Align everything you can in code Yes. Makes code easier to read and makes it easier to refactor if you use an editor with column select/multi-line caret.
I personally detest people who do this, outside of things like tables (i.e. structured constant initializers) and comments on type definitions. If there's a high degree of regularity in the source that you're trying to expose, there's probably a better way of doing it - extracting out common variables, using a table lookup, adopting acronym conventions for regular parts along any given axis which have the same length…
Edit: Something I hadn't thought of - you mention "rivers of space" - aligning code might make it harder to read for coders with dyslexia: http://uxmovement.com/content/6-surprising-bad-practices-tha...
Regarding your point about unsigned types - If I'm remembering correctly, "Writing Solid Code" had a section about exactly that - nasty bugs caused by things like looping over unsigned integers in reverse.
Re: How to make fewer errors at the stage of code writing
#7Also check for null!!! ;)
Re: How to make fewer errors at the stage of code writing
#8I know that it's still possible to leak memory in C#, like not releasing event handlers, but it's very difficult, if not impossible, to corrupt memory without using unsafe code. In those cases you're probably more likely to be careful with how memory is used or manipulated anyway to avoid causing corruption with the managed code.
Re: How to make fewer errors at the stage of code writing
#9Re: How to make fewer errors at the stage of code writing
#10It seems to me that the most common errors in C/C++ are memory leak and/or corruption issues. Coming from someone who's background is in managed/garbage collected languages, doesn't using a managed language help to avoid these common mistakes? I know that it's still possible to leak memory in C#, like not releasing event handlers, but it's very difficult, if not impossible, to corrupt memory without using unsafe code…