Hard-to-find Tiny Bugs in Code
Categories: Tech | Pubby Cash Received:
Based on my limited coding experience, I've summarized my top three tiny bugs that have wasted many of my valuable hours. I would like to share with you all so that you can avoid them by all means. No. 1. Punctuation and Spelling. A mistake of missing a comma would either cause an immediate trackback or temporarily got accepted but blow off your code in future. I had a field in a database table storing a list that is dynamically being appended, say [1,2,3,4,5,] and has been converted to string. Note that the list is ended with a comma in the square brackets. That comma costs me hours to make things at other places work as planned. Spelling is another thing that can be easily overlooked. When I was coding to communicate with a database via SQLAlchemy, I typed db.session.commmit(). That triple m drove me crazy when I found out with my old eyes. No. 2. Indentation. Tabs and white spaces are treated differently in Python. This is especially true when you are trying to copy a code snippet from elsewhere and paste it into your program. You thought they are identical and should work flawlessly. However, the cruel reality might be that you just cannot make it work. Tabs and white spaces cannot be distinguished by naked eyes. It takes me hours to find out that the Sublime Text editor has a function of converting all tabs to spaces, and that solves my problem. No. 3. Data Types. Operations with values of different data types will throw out an error. Although it is not difficult to realize that datetime and timedelta are incomparable, and strings are incomparable with other data types, the problem is: sometimes, it is visually impossible to tell the difference between a string and another datatype! Let's say x = 2. Now x is an integer. Then let's say y = str(2), and now y =2, too. But this "2" is different from the previous "2", because this "2" is a string. If you run x + y, it will throw off an error! The recommendation is: always check the data types to make sure you are well informed. If you are trapped in debugging processes, you could try to target on the above-mentioned three hard-to-find bugs. Hope this will help you out!... Read more