Talk:Magic number (programming)
![]() | This article is rated C-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||
|
Programming Example
I think that better programming examples are needed. Currently, the examples encourage another poor programming practice that could lead to buffer overruns if the array is shorter than deckSize. For example:
function shuffle (int deckSize) for i from 1 to deckSize j := i + randomInt(deckSize + 1 - i) - 1 a.swapEntries(i, j)
Would be better written as
function shuffle () for i from 1 to a.length j := i + randomInt(a.length + 1 - i) - 1 a.swapEntries(i, j)
63.87.189.17 (talk) 16:23, 28 April 2010 (UTC)
- What you say would be true, except that your second example has no realistic place where a new 'magic number' would normally appear (only a dozo would hard-code a.length so that wouldn't be a magic number problem but a dozo's error). We're not teaching programming here, but giving an example where a problem-domain fact (i.e. 52) is hard-coded implicitly (without declaration and naming), twice (once as 53). If you can think of a better magic number example, or better still, find one in a reliable source, please let's hear about it. Improving the same example further, as you suggest, actually doesn't make the point at all. --Nigelj (talk) 17:31, 28 April 2010 (UTC)
- Maybe in the third example, where the code has been extracted into a parameterised function, we could add the following line as the first line in the function? Is this adding unnecessary complexity?
if (deckSize > a.length) throw new Exception("Deck array is shorter than deckSize")
- --Nigelj (talk) 17:37, 28 April 2010 (UTC)
- Hey, how about we leave it alone, and just assume that all the necessary protection is built into the unspecified swapEntries() function? --Nigelj (talk) 17:40, 28 April 2010 (UTC)
- I think the idea is that some unmanaged languages (such as C) have neither built-in protection against undefined behavior nor an easily accessible .length property on arrays passed as arguments. Nor does it have .swapEntries(). I'd recommend adding a
swap()
global function to the pseudocode standard library that takes two lvalues by reference and swaps their values:swap(a[i], a[j])
--Damian Yerrick (talk | stalk) 04:56, 13 March 2011 (UTC)
- I think the idea is that some unmanaged languages (such as C) have neither built-in protection against undefined behavior nor an easily accessible .length property on arrays passed as arguments. Nor does it have .swapEntries(). I'd recommend adding a
- While everybody stumbles over the code example being good or not, I've noticed the following:
- It makes the code more complex, adding 25% to the LOC in this example --> Going from 3 to 4 lines adds 25%? Is 25 perhaps a magic number here? :-)
- --212.123.21.39 (talk) 15:00, 4 February 2011 (UTC)
Not playing with a full deck
Magic number (programming)#Unnamed numerical constants states:
- An increase in complexity may be justified if there is some likelihood of confusion about the constant, or if there is a likelihood the constant may need to be changed. Neither is likely for a deck of playing cards, which has been well-known to be 52 cards for several hundred years.
I don't think this is the best example we could give. The size of the playing card deck was changed when Euchre (24 cards), Pinochle (48 cards), Uno (108 cards IIRC), Mahjong and Mahjong solitaire (144 tiles), and casino Blackjack (208+ cards) were invented. I see a substantial likelihood that the number of cards will change, even if only to facilitate code reuse in a program implementing other card games. --Damian Yerrick (talk | stalk) 23:18, 12 March 2011 (UTC)
Changing "MZ" to "ZM" in a PE32 causes problems
Magic number (programming)#Examples says the following:
- MS-DOS EXE files and the EXE stub of the Microsoft Windows PE (Portable Executable) files start with the characters "MZ" (
4D
5A
), the initials of the designer of the file format, Mark Zbikowski. The definition allows "ZM" (5A
4D
) as well, but this is quite uncommon.
- MS-DOS EXE files and the EXE stub of the Microsoft Windows PE (Portable Executable) files start with the characters "MZ" (
However, changing "MZ" to "ZM" in a PE32 causes modern versions of Windows to puke. This is independently verifiable with a hex editor. Maybe this information is old/out of date? 174.55.150.167 (talk) 18:12, 2 November 2012 (UTC)
- Don't know about Windows, but MS-DOS, PC DOS and DR-DOS actually support both, 5A4Dh (4Dh 5Ah = "MZ") and 4D5Ah (5Ah 4Dh = "ZM"), whereby the second one appears to be the older variant. --Matthiaspaul (talk) 18:53, 2 November 2012 (UTC)
CEFAEDFE
I removed this entry from the 'Magic debug values' table: "CEFAEDFE
|| "Feed face", Seen in Intel Mach-O binaries on Apple Inc.'s Mac OS X platform (see FEEDFACE
)". I see that CEFAEDFE does say FEEDFACE if you read the 2-hex-digit bytes backwards, but I'm still not happy with the explanation we had. The Apple PowerPC hardware booted up in big endian mode, but could switch to little endian at any point. Mach-O object files could contain either PowerPC or x86 code - presumably with either endianness. Also what is the word Intel doing in that sentence? Weren't the chips in big endian Apple machines made by Motorola? What debugger would it be, running on what hardware, that would display FEEDFACE as CEFAEDFE? And what kind of Mach-O file would have to be being displayed? One meant for the other kind of hardware? Why did the developer type the magic number in in such a way that it would display garbled in his/her debugger? I'm sorry, this entry raises far more questions than it answers. If anyone can explain what it's meant to illustrate here, then at the very least we need to rewrite the entry so that we can all share the joke. --Nigelj (talk) 20:54, 18 January 2013 (UTC)
IDEs and named constants
Regarding this edit, I just want to say that some IDEs will show the value of a named constant, for example by hovering the mouse over a mention of it, or looking in an 'immediate' window, even if it is defined in a completely different file elsewhere in the project. Just saying. I don't know about the mention - it didn't make that clear, and I'm not sure it matters. But it wasn't completely insane by any means. --Nigelj (talk) 22:12, 29 July 2013 (UTC)
"Bjarne Stroustrup on Educating Software Developers" reference in Unnamed numerical constants section
65.78.114.251 has removed a reference to http://www.datamation.com/columns/article.php/3789981/Bjarne-Stroustrup-on-Educating-Software-Developers.htm on the basis that it does not support the point in the text, namely that use of magic numbers "makes it more difficult for the program to be adapted and extended in the future". From the second page of the link:
- Take a simple example: A friend of mine looked at the final projects of a class of third-year CS students from a famous university. Essentially all had their code littered with “magic constants.” They had never been taught that was bad style – in fact they had never been taught about programming style because the department “taught computer science; not programming.” That is, programming was seen as a lowly skill that students either did not need or could easily pick up on their own.
- I have seen the result of that attitude in new graduate students: It is rare that anyone thinks about the structure of their code or the implications for scaling and maintenance – those are not academic subjects. Students are taught good practical and essential topics, such as algorithms, data structures, machine architecture, programming languages, and “systems,” but only rarely do they “connect the dots” to see how it all fits together in a maintainable program.
and:
- The “magic constant” example is indicative. Few students see code as anything but a disposable entity needed to complete the next project and get good grades. Much of the emphasis in teaching encourages that view.
both address the impact of poor practices, specifically citing the use of "magic constants" as an example poor practice, on code maintenance and longevity, which is just another way of saying "adapted and extended in the future". Rwessel (talk) 05:54, 8 February 2014 (UTC)
Unnamed numerical constants
'It helps detect typos' is disingenuous. Obviously in the example cited the names variable will need to be initialized, and that initial value may just as easily be mistyped. It could be argued that you now have the opportunity to mistype the initial value of the named variable and the variable name itself when referenced. 81.108.216.18 (talk) 15:53, 30 June 2014 (UTC)Phil Short
- Not really. It's reasonably likely that mistyping the variable name will lead to a compilation error. And while the actual definition can certainly be pooched, writing the definition, where only the definition is in play, likely concentrates the mind on that task, leading to a higher chances of being correct (IOW, when you're defining the constant pi, you're just doing that, not also trying to work the formula for the volume of a sphere into your application at the same time). Ad if you do manage to use the constant more than once, you've only got once chance to mess it up rather than several. Finally, having the constant clearly separated makes it easier to verify later. Rwessel (talk) 16:18, 30 June 2014 (UTC)
Bad coding practices 101
This article needs a link to another article listing bad coding practices. — Preceding unsigned comment added by 206.132.109.103 (talk) 16:13, 8 June 2017 (UTC)
External links modified
Hello fellow Wikipedians,
I have just modified 2 external links on Magic number (programming). Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
- Added archive https://web.archive.org/web/20061104034450/http://cm.bell-labs.com/cm/cs/who/dmr/odd.html to http://cm.bell-labs.com/cm/cs/who/dmr/odd.html
- Added archive https://web.archive.org/web/20110718163417/http://cataclysm.cx/random/amiga/reference/AmigaMail_Vol2_guide/node0053.html to http://cataclysm.cx/random/amiga/reference/AmigaMail_Vol2_guide/node0053.html
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
This message was posted before February 2018. After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than regular verification using the archive tool instructions below. Editors have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the RfC before doing mass systematic removals. This message is updated dynamically through the template {{source check}}
(last update: 5 June 2024).
- If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
- If you found an error with any archives or the URLs themselves, you can fix them with this tool.
Cheers.—InternetArchiveBot (Report bug) 14:46, 10 December 2017 (UTC)
Extended Justification for Most Recent Edit
Some four years ago I added a note to 0xFDFDFDFD that certain debug versions of Win32 functions will flood fill 0xFD during their operation. I chose to add this note to Wikipedia because I had spent significant time looking for what function was responsible for flood filling this magic number in an application I was working on; it is not particularly easy to discover this fact as it is buried halfway through a very large article on this function and does not get mentioned in most places that discuss magic numbers, and I wanted to spare the next poor individual the same agony.
Today I was trying to remember what functions those were, and when I went to the article expecting to see my edit, I found out that it had been removed within about 10 minutes after having been made. I will assume on good faith that the person who removed my edit thought I was being unconstructive. The person in question does not appear to have programming experience (based on reviewing their contributions), so I do not know what criteria that user judged my edit by. The function I listed does not allocate or deallocate memory, so it is entirely separate from the malloc() comment for the same entry. My static IP has made other edits that were fixing typos and had no prior evidence of vandalism. I added a specific citation to MSDN regarding the function in question in the hopes that it isn't reverted again, but I note that most entries do not have any citations and do not end up reverted.
Furthermore, I noticed the entry for D15EA5E had a comment to the effect "Should this be D15EA5ED "Diseased"" It just so happens that I have extensive experience with the Nintendo Wii hardware platform. I know for a fact that the entry is 0D15EA5E; there is no D at the end, and it begins with 0. Look at address 32 (hex 0x20) of any Wii or GameCube ROM with a hex editor.
Finally, I noticed that one entry referred to "uninitialised". While this is British spelling, Wikipedia itself has an article Uninitialized variable but lacks Uninitialised variable. Therefore, I felt justified changing the word. Cheers 98.111.204.114 (talk) 22:11, 16 January 2019 (UTC)
Magic Number for PDF
Not sure how to change this because so many sources on this conflict each other. However, in the PDF Reference edition 2, it mentions that for many viewers, the magic number only has to appear somewhere in the first 1024 bytes, and not necessarily the first 4 bytes. Skyturnrouge (talk) 22:36, 14 November 2019 (UTC)
Data type limits
Does § Data type limits really belong in this article? I'm not saying it doesn't belong somewhere, but I am not sure that it belongs here. These limits are not really magic numbers, they are fundamental. The article Integer (computer science), to which Integer type redirects, has them, but only in decimal (and in a sense they are "magic" in decimal as seemingly arbitrary, whereas in hex they are plainly not). I am tempted to add the hex into the Integer article and delete this section: there are no redirects to it, but of course other articles might link to it, and I would check thoroughly they are retargeted if consensus were to make that move.
For that matter, the section should probably be renamed "Integer type limits". 94.21.38.126 (talk) 10:17, 29 November 2019 (UTC)
- I had the same remark. The only justification to keep these limits here would be to provide the name to use instead of writing these magic numbers in code, example
INT_MAX
in C/C++, however that would become a list of names specific to each language. A reference to headers like<limits.h>
in the section "Unnamed numerical constants" should be enough for that article. Teuxe (talk) 13:19, 3 August 2023 (UTC)
PowerPC Mach-O binaries on Apple Inc.'s macOS platform?!?!?
Fromerly the FEEDFACE Entry read:
| FEEDFACE
|| "Feed face", Seen in PowerPC Mach-O binaries on Apple Inc.'s macOS platform. On Sun Microsystems' Solaris, marks the red zone
Now, I'm very puzzled by this because "MacOS" has never run on PowerPC. OS X 10.5, Leopard (2007) was the last OS to support PowerPC architecture. Perhaps they didn't purge all the PowerPC code, although much of this would also be 32-bit rather than the 64-bit... It just doesn't make much sense that the code would remained buried in there. I replaced it with "Mac OS X" unlinked, as I'm not entirely certain that this doesn't extend past OS X Leopard 10.5 and into Snow Leopard 10.6, for example. Also, linking "Mac OS X" and "OS X" and "OSX" just redirects to "MacOS", so that wouldn't clarify the issue, and just lead to the same confusion. 75.71.166.197 (talk) 01:57, 16 February 2022 (UTC)
DEC PDP 11/x
DEC PDPs have some affinity to 8x1777777 ( its an octal number ). I will find out, and document it here. — Preceding unsigned comment added by 2600:1700:87D3:22E0:1008:10E6:55:AF7B (talk) 03:36, 3 September 2022 (UTC)
Advantages of naming magic constants
The second and last item in the list of advantages,
- It is easier to alter the value of the number, as it is not duplicated. [...]
- It facilitates parameterization. [...]
strike me as saying essentially the same thing. I would suggest merging the former into the latter. Mjaðveig (talk) 09:30, 25 September 2023 (UTC)