{"id":11831,"date":"2016-04-22T09:56:37","date_gmt":"2016-04-22T16:56:37","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=11831"},"modified":"2016-04-23T14:47:39","modified_gmt":"2016-04-23T21:47:39","slug":"calling-a-method-without-parentheses","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/04\/22\/calling-a-method-without-parentheses\/","title":{"rendered":"Calling a method without parentheses"},"content":{"rendered":"<p><strong>Late breaking news from PowerShell Conference Europe 2016!!<\/strong><\/p>\n<p>During PowerShell MVP Chrissy LeMaire&#8217;s (<a href=\"http:\/\/www.twitter.com\/cl\" target=\"_blank\">@cl<\/a>) knockout session at PowerShell Conference Europe 2016, &#8220;My Journey to 200,000 Rows a Second&#8221; (don&#8217;t miss the recording; I&#8217;ll tweet as soon as it&#8217;s available), PowerShell Principal Developer Bruce Payette (<a href=\"https:\/\/twitter.com\/BrucePayette\" target=\"_blank\">@BrucePayette<\/a>) said that you can call the Where method without parentheses.<\/p>\n<div id=\"attachment_11844\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/20160422_185053.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-11844\" class=\"wp-image-11844 size-medium\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/20160422_185053-300x169.jpg\" alt=\"Chrissy LeMaire and Bruce Payette at PowerShell Conference Europe.\" width=\"300\" height=\"169\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/20160422_185053-300x169.jpg 300w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/20160422_185053-768x432.jpg 768w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/20160422_185053-1024x576.jpg 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-11844\" class=\"wp-caption-text\">Chrissy LeMaire and Bruce Payette at PowerShell Conference Europe<\/p><\/div>\n<p>&#8220;It&#8217;s a syntactic twiddle,&#8221; he explained, as only Bruce can.<\/p>\n<p>In an exclusive interview, Bruce clarified that, beginning in PowerShell 4.0, you can call ANY method in PowerShell without parentheses if it meets the specified conditions.<\/p>\n<h1>The Old Rules: Parentheses are required<\/h1>\n<p>Prior to PowerShell 4.0, parentheses were required for any method call, even if the method doesn&#8217;t have any arguments, because the PowerShell parser used the parenthesis to identify a method call and distinguish it from a request for a property value.<\/p>\n<p><code>(Get-Process Notepad).Name\u00a0\u00a0 <span style=\"font-family: sans-serif; color: #000; font-weight: bold;\">\u2190 Property<\/span><\/code><\/p>\n<p><code>(Get-Process Notepad).Kill() <span style=\"font-family: sans-serif; color: #000;\"><strong>\u2190 Method<\/strong> (see the trailing parentheses)<\/span><\/code><\/p>\n<h1>The New Rules<\/h1>\n<p>Beginning in PowerShell 4.0, you can call ANY method in PowerShell without parentheses if the method meets the following conditions.<\/p>\n<ul>\n<li>The method takes only one argument<\/li>\n<li>That argument is a scriptblock<\/li>\n<\/ul>\n<p>There&#8217;s no particular advantage to this other than style, but it&#8217;s an interesting stylistic alternative.<\/p>\n<p>This &#8220;syntactic twiddle&#8221; was designed for the Where and ForEach methods, also introduced in PowerShell 4.0. These magic methods are alternatives to the Where-Object and ForEach-Object cmdlets. They&#8217;re faster and have several cool options that make them very valuable. You can read about these new methods <a href=\"http:\/\/www.powershellmagazine.com\/2014\/10\/22\/foreach-and-where-magic-methods\/\" target=\"_blank\">here<\/a> and <a href=\"https:\/\/mcpmag.com\/articles\/2015\/12\/02\/where-method-in-powershell.aspx\" target=\"_blank\">here<\/a>.<\/p>\n<p>For example, here is a typical call to the Where method:<\/p>\n<p><code>$services.Where( {$_.Status -eq 'Running'} )<\/code><\/p>\n<p>But, you can also write it like this &#8212; without the parentheses. Note that there are no spaces between the &#8220;Where&#8221; method name and the first curly brace.<\/p>\n<p><code>$services.Where{$_.Status -eq 'Running'}<\/code><\/p>\n<p>Of course, just because you can doesn&#8217;t mean that you should. I wouldn&#8217;t teach this to beginners, and I probably won&#8217;t use it in shared code, but because it&#8217;s shown in blogs and you might find it in shared code, it&#8217;s important to understand it.<\/p>\n<p>&nbsp;<\/p>\n<h1>In Other News: Why is Out-Null slow?<\/h1>\n<p>Bruce also mentioned that, while piping is always slower than the alternatives&#8230;<\/p>\n<p><code>\"Hello\" | Write-Output<\/code><\/p>\n<p>&#8211; is slower than &#8211;<\/p>\n<p><code>Write-Output -InputObject \"Hello\"<\/code><\/p>\n<p>&#8230; piping to Out-Null should be as quick as redirecting.<\/p>\n<p>However, in Chrissy&#8217;s tests, it was much slower.<\/p>\n<p><code>$(1..1000) &gt; $null\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"font-family: sans-serif; color: #000; font-weight: bold;\">00.21 ms<\/span><\/code><\/p>\n<p><code>$(1..1000) | Out-Null\u00a0\u00a0\u00a0 <span style=\"font-family: sans-serif; color: #000; font-weight: bold;\">76.21 ms<\/span><\/code><\/p>\n<p>&#8220;That&#8217;s a bug,&#8221; Bruce explained. &#8220;Under the covers, these two expressions are identical, so they should perform the same. When Jason optimized the first form, he probably forgot to optimize the second form.&#8221;<\/p>\n<p>You heard it here! Stay tuned for breaking news on the SAPIEN Technologies blog. <span style=\"font-size: 16px;\">\u263a<\/span><\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at <\/em><a href=\"mailto:juneb@sapien.com\"><em>juneb@sapien.com <\/em><\/a><em>or follow her on Twitter at <\/em><a href=\"http:\/\/www.twitter.com\/juneb_get_help\" target=\"_blank\"><em>@juneb_get_help<\/em><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Late breaking news from PowerShell Conference Europe 2016!! During PowerShell MVP Chrissy LeMaire&#8217;s (@cl) knockout session at PowerShell Conference Europe 2016, &#8220;My Journey to 200,000 Rows a Second&#8221; (don&#8217;t miss the recording; I&#8217;ll tweet as soon as it&#8217;s available), PowerShell Principal Developer Bruce Payette (@BrucePayette) said that you can call the Where method without parentheses. [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,1055,303],"tags":[682,1100,1073,1099],"class_list":["post-11831","post","type-post","status-publish","format-standard","hentry","category-ask-the-experts","category-scripting","category-tip","tag-methods","tag-out-null","tag-powershell-4-0","tag-powershell-conference-europe-2016"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=11831"}],"version-history":[{"count":21,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11831\/revisions"}],"predecessor-version":[{"id":11855,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11831\/revisions\/11855"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=11831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=11831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=11831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}