{"id":200,"date":"2018-09-15T18:00:59","date_gmt":"2018-09-15T18:00:59","guid":{"rendered":"https:\/\/datablog.roman-halliday.com\/?p=200"},"modified":"2018-07-14T17:27:37","modified_gmt":"2018-07-14T17:27:37","slug":"replacing-repeated-magic-numbers","status":"publish","type":"post","link":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/","title":{"rendered":"Replacing Repeated Magic Numbers"},"content":{"rendered":"<p>If an ID or reference is used more than once in a process, it&#8217;s best to store it in a variable and reference that. This can slow down debugging, but makes a process far more portable. It also reduces the number of magic numbers in a process (or at least times they are defined).<\/p>\n<p>I started writing another post on magic numbers and this bit jumped out to be useful, but not what I wanted that post to focus on.<\/p>\n<h1>The problem<\/h1>\n<p>This is often seen in the wild in processes which produce an output\/modify values for an individual customer. This customer is referenced by an ID (or name) in the code. In the future we want to repeat this process (or a variation on it) for another customer.<\/p>\n<p>This becomes especially useful in processes where the ID is referenced many times (human error\/forgetting to change all the references).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-highlight=\"3,9\">SELECT ...\r\n  FROM customers c\r\n WHERE c.customer_id = 1\r\n;\r\n\r\nUPDATE c\r\n   SET c.text_attribute = 'new value'\r\n  FROM customers c\r\n WHERE c.customer_id\u00a0=\u00a01\r\n;<\/pre>\n<p>In the above example, lines 3 and 9 both have a hard coded value, this can be replaced with a variable (which in the future could be given as an argument to a procedure).<\/p>\n<h1>The solution<\/h1>\n<p>Best practices suggest any ID\/reference used should be declared as a variable at the start of a script\/process. I do appreciate that this can increase debugging time (if a developer has to declare a variable for each statement they test as they test it).<\/p>\n<p>This is a very quick and easy change to make to SQL code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-highlight=\"1,5,11\">DECLARE @CustomerID INT = 1;\r\n\r\nSELECT ...\r\n  FROM customers c\r\n WHERE c.customer_id = @CustomerID\r\n;\r\n\r\nUPDATE c\r\n   SET c.text_attribute = 'new value'\r\n  FROM customers c\r\n WHERE c.customer_id\u00a0=\u00a0@CustomerID\r\n;<\/pre>\n<p>The variable is defined on line 1, then referenced on 5 and 11.<\/p>\n<h2>Examples<\/h2>\n<p>Here are a couple of working examples, based on the code for my other &#8220;Magic Numbers&#8221; post.<\/p>\n<ul>\n<li><a href=\"http:\/\/sqlfiddle.com\/#!18\/9e7c3\/1\">Problem SQL Fiddle<\/a><\/li>\n<li><a href=\"http:\/\/rextester.com\/edit\/IIGE73928\">Solution\u00a0rextester<\/a>\u00a0(because SQL Fiddle does funny things with variables)<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If an ID or reference is used more than once in a process, it&#8217;s best to store it in a variable and reference that. This can slow down debugging, but makes a process far more portable. It also reduces the number of magic numbers in a process (or at least times they are defined). I&hellip;<\/p>\n<p class=\"read-more\"><a class=\"readmore-btn\" href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/\">Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[18,19],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-databases","category-sql-server","tag-magic-numbers","tag-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Replacing Repeated Magic Numbers - Rows Across The Lake<\/title>\n<meta name=\"description\" content=\"If an ID or reference is used more than once in a process, it&#039;s best to store it in a variable and reference that.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Replacing Repeated Magic Numbers - Rows Across The Lake\" \/>\n<meta property=\"og:description\" content=\"If an ID or reference is used more than once in a process, it&#039;s best to store it in a variable and reference that.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/\" \/>\n<meta property=\"og:site_name\" content=\"Rows Across The Lake\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-15T18:00:59+00:00\" \/>\n<meta name=\"author\" content=\"david\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:site\" content=\"@d_roman_h\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"david\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/\"},\"author\":{\"name\":\"david\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"headline\":\"Replacing Repeated Magic Numbers\",\"datePublished\":\"2018-09-15T18:00:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/\"},\"wordCount\":291,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"keywords\":[\"Magic Numbers\",\"variables\"],\"articleSection\":[\"Databases\",\"SQL Server\"],\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/\",\"name\":\"Replacing Repeated Magic Numbers - Rows Across The Lake\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\"},\"datePublished\":\"2018-09-15T18:00:59+00:00\",\"description\":\"If an ID or reference is used more than once in a process, it's best to store it in a variable and reference that.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/index.php\\\/2018\\\/09\\\/15\\\/replacing-repeated-magic-numbers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Replacing Repeated Magic Numbers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#website\",\"url\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/\",\"name\":\"Rows Across The Lake\",\"description\":\"Data &amp; Databases\",\"publisher\":{\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/datablog.roman-halliday.com\\\/#\\\/schema\\\/person\\\/575f96d2590c3085923ff9e1b565748b\",\"name\":\"david\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\",\"caption\":\"david\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Replacing Repeated Magic Numbers - Rows Across The Lake","description":"If an ID or reference is used more than once in a process, it's best to store it in a variable and reference that.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/","og_locale":"en_GB","og_type":"article","og_title":"Replacing Repeated Magic Numbers - Rows Across The Lake","og_description":"If an ID or reference is used more than once in a process, it's best to store it in a variable and reference that.","og_url":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/","og_site_name":"Rows Across The Lake","article_published_time":"2018-09-15T18:00:59+00:00","author":"david","twitter_card":"summary_large_image","twitter_creator":"@d_roman_h","twitter_site":"@d_roman_h","twitter_misc":{"Written by":"david","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/#article","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/"},"author":{"name":"david","@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"headline":"Replacing Repeated Magic Numbers","datePublished":"2018-09-15T18:00:59+00:00","mainEntityOfPage":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/"},"wordCount":291,"commentCount":0,"publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"keywords":["Magic Numbers","variables"],"articleSection":["Databases","SQL Server"],"inLanguage":"en-GB","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/","url":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/","name":"Replacing Repeated Magic Numbers - Rows Across The Lake","isPartOf":{"@id":"https:\/\/datablog.roman-halliday.com\/#website"},"datePublished":"2018-09-15T18:00:59+00:00","description":"If an ID or reference is used more than once in a process, it's best to store it in a variable and reference that.","breadcrumb":{"@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/datablog.roman-halliday.com\/index.php\/2018\/09\/15\/replacing-repeated-magic-numbers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/datablog.roman-halliday.com\/"},{"@type":"ListItem","position":2,"name":"Replacing Repeated Magic Numbers"}]},{"@type":"WebSite","@id":"https:\/\/datablog.roman-halliday.com\/#website","url":"https:\/\/datablog.roman-halliday.com\/","name":"Rows Across The Lake","description":"Data &amp; Databases","publisher":{"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/datablog.roman-halliday.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/datablog.roman-halliday.com\/#\/schema\/person\/575f96d2590c3085923ff9e1b565748b","name":"david","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g","caption":"david"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/acddbc676a1d5c73795edcf0627ee39e5aa947da9033b58373e03d93122cb3b7?s=96&d=mm&r=g"}}]}},"_links":{"self":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":2,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/posts\/200\/revisions\/202"}],"wp:attachment":[{"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datablog.roman-halliday.com\/index.php\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}