|
Can you spot the bug? |
torhu
Member #2,727
September 2002
|
Just spent twenty minutes debugging this function before I realized why it seems to always return zero int ServerData::maxClients() const { const QString& s = server[ServerColumn::PLAYERS]; int slash = s.indexOf('/'); return (slash =! -1) ? s.midRef(slash + 1).toInt() : 0; }
|
torhu
Member #2,727
September 2002
|
Yeah... |
RPG Hacker
Member #12,492
January 2011
|
Nice one! I think we all had small mistakes like that already, that threw our whole application over. For example: I once accidentally did something like this ((a, b, c, d), e) instead of this ((a, b, c, d, e)) which in that special instance still compiled fine (the comma at the end there was interpreted as the comma operator), but made everything behave in a completely different and unexpected way. It took me a good while to find that error. At that time, I didn't even know a comma operator exists.
|
bamccaig
Member #7,536
July 2006
|
I may well be 5 AM, but I didn't spot that. That's a bitch. It's times like this that you need a linter! -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Niunio
Member #1,975
March 2002
|
That never happens in Pascal... ----------------- |
Bruce Perry
Member #270
April 2000
|
I spotted it The best example I can think of for myself is trying to write ~/.ssh and ending up with ~./ssh or something. I don't often trip up over syntax in languages I use often RPG Hacker, how come you needed double brackets? -- |
RPG Hacker
Member #12,492
January 2011
|
Bruce Perry said: RPG Hacker, how come you needed double brackets? This was a bad example, because I just didn't remember the exact situation back then, but basically I think that I had some kind of construct with a number of brackets and needed an ", argument" inside one of the brackets, but accidentally placed it outside that bracket because I got confused by all the brackets. It compiled fine, but behaved completely differently.
|
beoran
Member #12,636
March 2011
|
That's why in C it's better to write the integer literal first, like (-1 != slash), etc. This is sometimes called "Yoda conditions". |
torhu
Member #2,727
September 2002
|
I don't think that would help, but it would prevent you from writing = instead of ==. APPEND: Oh wait... |
bamccaig
Member #7,536
July 2006
|
I feel like <literal> == <variable> just reads wrong. It's like maths written like . I don't know. I imagine there are cultures and classes and groups where that's normal. I just find it weird. In particular, I have never ever been stung by this particular bug (that I can remember). It's the sort of problem that should be recognizable by a linter if you do regularly. I personally find I look sideways at the aforementioned pattern because it reads unnaturally. Arguably it's a good pattern because it can eliminate a source of bugs, but I think you need to rewire your brain to read it, and I don't think the investment pays off. I could be wrong. Thus far, I haven't found that I appreciate it. One or two colleagues has tried this once in a while, and I always found it less readable and never found it added value. At the same time, I didn't notice the bug in the OP so maybe I'm getting old. Or maybe I was just tired. Either way, meh. Append: Perhaps what we should really take away from this is that the language should impose a particular order on us... Or use separate, unrelated operators or functions for assignment and comparison rather than trying to rearrange the operands to catch errors. One option could be =/ from Lisp land. That won't compile if you swap the characters. Alternatively, instead of ! which a lot of whiners recently complain about, use an explicit not keyword instead. *shrug* It isn't really a problem for me... At 30. Maybe 40? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
torhu
Member #2,727
September 2002
|
C could be improved but the cost would be in backwards compatibility, and that's an issue for the most important programming language in the world for the last 40 years or so. You could change the rules for what counts as a token in C to not include =!, I guess... |
bamccaig
Member #7,536
July 2006
|
That would turn something like x=!y; into invalid code. While in principle it's OK, it's not exactly a consistent rule, and I wouldn't want that applied to all operators. It's best just to test your code... Just because it compiles doesn't mean it's correct... Ideally, you should be able to test a unit a time and verify that what you just wrote is correct... None of that 3 days coding and then commit and compile cycles. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Bruce Perry
Member #270
April 2000
|
Bob
Free Market Evangelist
September 2000
|
As it happens, this was in the news recently. Static code analysis tools are awesome. -- |
RPG Hacker
Member #12,492
January 2011
|
WOW, that seems quite useful and powerful!
|
|