Richard Robertson Posted December 14, 2013 Share Posted December 14, 2013 in C++ constants are converted to numbers? #define Entities created using #define are preprocessor variables, and are inserted as literal copies of the #define line's contents. "#define five 5" will replace every instance of the entity 'five' with the literal digit '5'. There are also constant source level variables. Variables can be declared as const, const pointer, pointer to const, and const pointer to const. czardas 1 Link to comment Share on other sites More sharing options...
Mat Posted December 14, 2013 Share Posted December 14, 2013 And constexpr now as well, which is slightly different and should hopefully be replacing #define for constants. AutoIt Project Listing Link to comment Share on other sites More sharing options...
iamtheky Posted December 14, 2013 Share Posted December 14, 2013 How would you differentiate between a numerical value and one that is a magic number? I dont. I'm trying to use constants with the beta where available now. Czardas, You say you would hammer out a quick 0 for debug, but production would differ... why? ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Richard Robertson Posted December 14, 2013 Share Posted December 14, 2013 constexpr sounds like an optimization I had already done before in an old script language. The final executed version of the script will have already computed all compile-time-known values. Link to comment Share on other sites More sharing options...
czardas Posted December 14, 2013 Author Share Posted December 14, 2013 Czardas, You say you would hammer out a quick 0 for debug, but production would differ... why? Well I wouldn't use the default value of 0 in production because MsgBox is a blocking function. The message must always be dealt with so top-most attribute generally gets set. MsgBox isn't particularly a good example. Some of my programming requires precision timing both in development and in production, leaving very little leeway (speed overriding all other considerations). Finally I use constants sometimes when the binary means little or nothing to me, or if other people are likely to modify what I write. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
iamtheky Posted December 14, 2013 Share Posted December 14, 2013 Thats just the example i use as its the most often questioned in my head, and really the only magic numbers i use frequently. Like the first couple of Guiness' posts with $MB_SYSTEMMODAL and thought "umm thats more typing than the digit and no more useful". czardas 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
guinness Posted December 14, 2013 Share Posted December 14, 2013 (edited) Thats just the example i use as its the most often questioned in my head, and really the only magic numbers i use frequently. Like the first couple of Guiness' posts with $MB_SYSTEMMODAL and thought "umm thats more typing than the digit and no more useful". As demonstrated in your post mistakes can happen, if for example you have a magic number of "42" spread across multiple source files and it should be 43, then you have a lot of work ahead of you than changing typing a variable name. So time of debugging vs typing a couple extra characters, I know which one wins that war. Incidentally my username is guinness! Edit: I changed one word for better clarity in my point. Edited December 14, 2013 by guinness UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
czardas Posted December 14, 2013 Author Share Posted December 14, 2013 (edited) I entirely agree with you guinness. Use of magic numbers should be restricted, but not ruled out. Edited December 14, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
guinness Posted December 14, 2013 Share Posted December 14, 2013 I entirely agree with you guinness. Use of magic numbers should be restrited, but not ruled out. No of course not, but there are limited examples where magic numbers can be deemed acceptable. boththose read this article: http://en.wikipedia.org/wiki/Magic_number_(programming) UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Richard Robertson Posted December 14, 2013 Share Posted December 14, 2013 0xDEADBEEF. Link to comment Share on other sites More sharing options...
Chimaera Posted December 14, 2013 Share Posted December 14, 2013 0xDEADBEEF. The non vegetarian option... If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
AZJIO Posted December 14, 2013 Share Posted December 14, 2013 verbosity in programming. You remember thousands reductions? It is necessary to read the reference to look as it is written. I was also too lazy to write a lot of character Now any example in the Help file requires UDF All examples are not compatible with 3.3.8.1 To replace all the numbers - easy: MsgBox(0, --> MsgBox(43, My other projects or all Link to comment Share on other sites More sharing options...
czardas Posted December 14, 2013 Author Share Posted December 14, 2013 The non vegetarian option... On the the vegetarian menu there is: 0xCABBA6E operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Chimaera Posted December 14, 2013 Share Posted December 14, 2013 thx i was getting hungry If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
czardas Posted December 15, 2013 Author Share Posted December 15, 2013 (edited) Some people have made some very interesting comments in this thread. Mat mentioned earlier about some constants being liable to change so, for the sake of forward compatibility, using globals makes a lot of sense. However this argument seems more a case of damage limitation: because changing constants should really be frowned upon due to backwards incompatibility. What seems to make this so complicated is the number of unrelated consequences of choosing one method over another. I am of the opinion that you learn less if you do not take things apart and try to probe beneath the surface. I like having choices, but I need to know the consequences of the choices I make. Nobody should feel insulted by this. To replace all the numbers - easy: MsgBox(0, --> MsgBox(43, It's easy for a regexp wizzard like you Azjio. Edited December 15, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
DatMCEyeBall Posted December 15, 2013 Share Posted December 15, 2013 I didn't like 0xDEADBEEF so I tried some 0xCABBA6E but it was poisoned so I got a 0xD15EA5E and now I 0xFEE1DEAD. "Just be fred, all we gotta do, just be fred." -Vocaliod "That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha @tabhooked Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation Link to comment Share on other sites More sharing options...
Mat Posted December 15, 2013 Share Posted December 15, 2013 (edited) Mat mentioned earlier about some constants being liable to change so, for the sake of forward compatibility, using globals makes a lot of sense. However this argument seems more a case of damage limitation: because changing constants should really be frowned upon due to backwards incompatibility. Lets say you have the file name of the config file for an application, which is "settings.ini". Since the filename is not going to change, and its pretty obvious what it is, there is the temptation to just use "settings.ini" throughout the code. That's bad practice. Not just because of forward compatibility problems either (which is not likely to be an issue but could be). It's more important that if you make a typo in the string, it's not an error. If you make a typo in a variable it is an error, so you see it straight away. Same goes for magic numbers as well, if you have flags that start from zero then 1 and 2 are both valid flags with keys next to each other on the keyboard. Accidentally hit 2 instead of 1, and don't immediately notice, the problem won't be apparent just by looking at the code, as you'll see 2+16+64 and it looks like your adding flags. In reality you could have completely changed the behaviour of the program (Edit: Or worse - subtly changed it) Edited December 15, 2013 by Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
guinness Posted December 15, 2013 Share Posted December 15, 2013 I said the same Mat. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
czardas Posted December 15, 2013 Author Share Posted December 15, 2013 (edited) Lets say you have the file name of the config file for an application, which is "settings.ini". Since the filename is not going to change, and its pretty obvious what it is, there is the temptation to just use "settings.ini" throughout the code. That's bad practice. Not just because of forward compatibility problems either (which is not likely to be an issue but could be). It's more important that if you make a typo in the string, it's not an error. If you make a typo in a variable it is an error, so you see it straight away. This is a good argument, but it is different from saying you use named constants because their values may be changed in the future. Typos are something else. In any case, I would say "settings.ini" does not qualify as a named constant. Edited December 15, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Mat Posted December 15, 2013 Share Posted December 15, 2013 This is a good argument, but it is different from saying you use named constants because their values may be changed in the future. Typos are something else. In any case, I would say "settings.ini" does not qualify as a named constant. "settings.ini" is a magic string (for want of a better word for it). $SETTINGS_FILE is a named constant. The latter is nearly always better. AutoIt Project Listing Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now