Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

one liners considered good?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-26-2007   #1 (permalink)
Mike G.
Guest


 

one liners considered good?

On a regular basis on the powershell team blog, a "one liner" is published.
It is usually does something quite impressive, cool, and awe-inspiring. And,
it is usually unreadable at first glance.

Any time I'm working with code, whether it's vbscript, vb.net, c#, SQL, etc,
one primary goal is readability / maintainability. If the code does amazing
things, but I can't read it 6 months later or figure out how to make
changes: it's bad code.

So why do we celebrate confusing one liners in PowerShell when a confusing
one liner in most other languages is criticized?

Along those lines, in any other language, I could create a function that
calls a built in function. For example:

public function l(s as string) as string
return len(s)
end function

I could then use the "l" function, instead of len. This is usually called
obfuscation. It makes the code more difficult to read for other developers
and provides no benefits. Of course, the developer calling the custom
function saves 2 keystrokes, but saving keystrokes at the cost of
readability is considered an amateur mistake.

In PowerShell, renaming built-in functions is called creating an alias and
is touted as an advantage.

Why?

Mike G.




My System SpecsSystem Spec
Old 09-26-2007   #2 (permalink)
Brandon Shell
Guest


 

Re: one liners considered good?

I think the idea of one liners is showing the amazing power of Powershell...
not to promote that as a general practice.

I personally don't like them because they are normally convoluted and
require as much troubleshooting as a full fledge script.

One liners make sense for day to day task you do, but not for complex task.

"Mike G." <m4053946_noSpam_@xxxxxx> wrote in message
news:ujTyG3HAIHA.5160@xxxxxx
Quote:

> On a regular basis on the powershell team blog, a "one liner" is
> published. It is usually does something quite impressive, cool, and
> awe-inspiring. And, it is usually unreadable at first glance.
>
> Any time I'm working with code, whether it's vbscript, vb.net, c#, SQL,
> etc, one primary goal is readability / maintainability. If the code does
> amazing things, but I can't read it 6 months later or figure out how to
> make changes: it's bad code.
>
> So why do we celebrate confusing one liners in PowerShell when a confusing
> one liner in most other languages is criticized?
>
> Along those lines, in any other language, I could create a function that
> calls a built in function. For example:
>
> public function l(s as string) as string
> return len(s)
> end function
>
> I could then use the "l" function, instead of len. This is usually called
> obfuscation. It makes the code more difficult to read for other developers
> and provides no benefits. Of course, the developer calling the custom
> function saves 2 keystrokes, but saving keystrokes at the cost of
> readability is considered an amateur mistake.
>
> In PowerShell, renaming built-in functions is called creating an alias and
> is touted as an advantage.
>
> Why?
>
> Mike G.
>
>
>
My System SpecsSystem Spec
Old 09-26-2007   #3 (permalink)
Karl Mitschke
Guest


 

Re: one liners considered good?

Hello Mike G.,
Quote:

> On a regular basis on the powershell team blog, a "one liner" is
> published. It is usually does something quite impressive, cool, and
> awe-inspiring. And, it is usually unreadable at first glance.
>
> Any time I'm working with code, whether it's vbscript, vb.net, c#,
> SQL, etc, one primary goal is readability / maintainability. If the
> code does amazing things, but I can't read it 6 months later or figure
> out how to make changes: it's bad code.
>
> So why do we celebrate confusing one liners in PowerShell when a
> confusing one liner in most other languages is criticized?
>
> Along those lines, in any other language, I could create a function
> that calls a built in function. For example:
>
> public function l(s as string) as string
> return len(s)
> end function
> I could then use the "l" function, instead of len. This is usually
> called obfuscation. It makes the code more difficult to read for other
> developers and provides no benefits. Of course, the developer calling
> the custom function saves 2 keystrokes, but saving keystrokes at the
> cost of readability is considered an amateur mistake.
>
> In PowerShell, renaming built-in functions is called creating an alias
> and is touted as an advantage.
>
> Why?
>
> Mike G.
Mike - I think I can answer this

The main use of powershell is within the console, entering commands by typing,
and getting immediate results.

One liners and aliases are a LOT easier to type than multiple lines with
long commands, so in the console, "one liner’s” and aliases are considered
a "good thing"

Since I am creating applications that run powershell commands, and my code
is (fairly) static, I use the whole command, and one command per line. -
As you say, it's easier to read.

Karl


My System SpecsSystem Spec
Old 09-26-2007   #4 (permalink)
Hal Rottenberg
Guest


 

Re: one liners considered good?

On Sep 26, 4:42 pm, "Mike G." <m4053946_noSp...@xxxxxx> wrote:
Quote:

> So why do we celebrate confusing one liners in PowerShell when a confusing
> one liner in most other languages is criticized?
>
> In PowerShell, renaming built-in functions is called creating an alias and
> is touted as an advantage.
Heck yeah they are! It's all about the console. These other
languages (generally speaking) don't have to deal with the dichotomy
of a scipting host that's also an interactive console. Powershell
handles it very well by giving us these shortcuts. When I'm writing
full-fledged scripts, I write things longhand or let my IDE do the
auto-complete to get the same effect.

All the best practices you stated above are still in effect for
PowerShell.



--
Blog: http://halr9000.com
Webmaster, Psi (http://psi-im.org)
Co-host, PowerScripting Podcast (http://powerscripting.net)

My System SpecsSystem Spec
Old 09-26-2007   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: one liners considered good?

"Karl Mitschke" <kmitschke@xxxxxx> wrote in message
news:7063857f9ab68c9ce64edbcac9e@xxxxxx
Quote:

> The main use of powershell is within the console, entering commands by
> typing, and getting immediate results.
>
> One liners and aliases are a LOT easier to type than multiple lines with
> long commands, so in the console, "one liner’s” and aliases are considered
> a "good thing"
+1 : my wrists really appreciate these one-liners
Quote:

>
> Since I am creating applications that run powershell commands, and my code
> is (fairly) static, I use the whole command, and one command per line. -
> As you say, it's easier to read.
and +1

--
Keith


My System SpecsSystem Spec
Old 09-27-2007   #6 (permalink)
RichS
Guest


 

Re: one liners considered good?

I don't like the over aliased one liners that we keep seeing & tab completion
while not perfect does take a way a lot of the typing
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Hal Rottenberg" wrote:
Quote:

> On Sep 26, 4:42 pm, "Mike G." <m4053946_noSp...@xxxxxx> wrote:
Quote:

> > So why do we celebrate confusing one liners in PowerShell when a confusing
> > one liner in most other languages is criticized?
> >
> > In PowerShell, renaming built-in functions is called creating an alias and
> > is touted as an advantage.
>
> Heck yeah they are! It's all about the console. These other
> languages (generally speaking) don't have to deal with the dichotomy
> of a scipting host that's also an interactive console. Powershell
> handles it very well by giving us these shortcuts. When I'm writing
> full-fledged scripts, I write things longhand or let my IDE do the
> auto-complete to get the same effect.
>
> All the best practices you stated above are still in effect for
> PowerShell.
>
>
>
> --
> Blog: http://halr9000.com
> Webmaster, Psi (http://psi-im.org)
> Co-host, PowerScripting Podcast (http://powerscripting.net)
>
>
My System SpecsSystem Spec
Old 09-27-2007   #7 (permalink)
Karl Mitschke
Guest


 

Re: one liners considered good?

Hello RichS,
Quote:

> I don't like the over aliased one liners that we keep seeing & tab
> completion while not perfect does take a way a lot of the typing

I'd forgotten about tab completion in the console! That rocks, usually!

Karl


My System SpecsSystem Spec
Old 09-28-2007   #8 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: one liners considered good?

As an aside, with some of the new stuff coming in V2, we should be able to
offer both better tab completion (for everyone, not just the in default
host) as well as tools for analyzing and canonicalizing scripts. This will
allow you to write your scripts the way you want and then convert them into
canonical form for redistribution. We're also working on a style guide/best
practise document that (hopefully) we'll release for review soon...

-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
My Book: http://manning.com/powershell

"Karl Mitschke" <kmitschke@xxxxxx> wrote in message
news:7063857f9d478c9cef3410f97ec@xxxxxx
Quote:

> Hello RichS,
>
Quote:

>> I don't like the over aliased one liners that we keep seeing & tab
>> completion while not perfect does take a way a lot of the typing
>
>
> I'd forgotten about tab completion in the console! That rocks, usually!
>
> Karl
>
>

My System SpecsSystem Spec
Old 09-30-2007   #9 (permalink)
Jon
Guest


 

Re: one liners considered good?

"Bruce Payette [MSFT]" <brucepay@xxxxxx> wrote in message
news:eScHMDaAIHA.4712@xxxxxx
Quote:

> As an aside, with some of the new stuff coming in V2, we should be able to
> offer both better tab completion (for everyone, not just the in default
> host) as well as tools for analyzing and canonicalizing scripts. This will
> allow you to write your scripts the way you want and then convert them
> into canonical form for redistribution. We're also working on a style
> guide/best practise document that (hopefully) we'll release for review
> soon...
>
> -bruce
> --
> Bruce Payette [MSFT]
> Windows PowerShell Technical Lead
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
> Visit the Windows PowerShell Team blog at:
> http://blogs.msdn.com/PowerShell
> Visit the Windows PowerShell ScriptCenter at:
> http://www.microsoft.com/technet/scr.../hubs/msh.mspx
> My Book: http://manning.com/powershell
>


The best practice guide sounds like a good idea.

I'm not normally a fan of these, but in the case of Powershell there often
seems to be a large number of ways that you could achieve exactly the same
end, and it's not always obvious which is the best way to achieve it.

While freedom is good thing (no-one likes to be over-prescribed to), it 's
also good to have guidance as to what you guys at MS would consider the best
usage of the different technological areas

eg when it would be considered best practice to

1. Put code into the main body of the script
2. Put code into a function
3. Put code into a custom cmdlet
4. 'Dot source' an external script
5. Run an external .ps1 script
6. Use 'inline VB / C#'
7. Create a custom .net class
8. Script existing /downloadable COM objects or create one's own
9. Run external executables, either existing or one's own
etc

This is an area that I currently struggle with, so anything in your document
that helps with this would certainly be of value.

--
Jon



My System SpecsSystem Spec
Old 10-01-2007   #10 (permalink)
Gunilla
Guest


 

RE: one liners considered good?

I use PowerShell commands in MsBuild files to get extended functionality. In
an MsBuild file, I like one-liners, preferrable short one-liners. That gives
a better overview of the MsBuild file. A short one-liner accompanied with a
good comment could be considered as an idiom.

"Mike G." wrote:
Quote:

> On a regular basis on the powershell team blog, a "one liner" is published.
> It is usually does something quite impressive, cool, and awe-inspiring. And,
> it is usually unreadable at first glance.
>
> Any time I'm working with code, whether it's vbscript, vb.net, c#, SQL, etc,
> one primary goal is readability / maintainability. If the code does amazing
> things, but I can't read it 6 months later or figure out how to make
> changes: it's bad code.
>
> So why do we celebrate confusing one liners in PowerShell when a confusing
> one liner in most other languages is criticized?
>
> Along those lines, in any other language, I could create a function that
> calls a built in function. For example:
>
> public function l(s as string) as string
> return len(s)
> end function
>
> I could then use the "l" function, instead of len. This is usually called
> obfuscation. It makes the code more difficult to read for other developers
> and provides no benefits. Of course, the developer calling the custom
> function saves 2 keystrokes, but saving keystrokes at the cost of
> readability is considered an amateur mistake.
>
> In PowerShell, renaming built-in functions is called creating an alias and
> is touted as an advantage.
>
> Why?
>
> Mike G.
>
>
>
>
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
emails FROM me are considered Junk FireBrick Vista mail 2 08-18-2008 02:32 PM
Would This Be Considered Good Performance In Vista? Little Billy Vista General 1 05-01-2008 06:25 PM
SP1 Good romanom Vista General 11 02-08-2008 10:32 PM
Workgroup Network is considered insecure in Vista/Live One Care J. Resperger Vista networking & sharing 1 05-25-2007 05:19 AM
My downloaded Vista with OEM emulation workaround, passed WGA and is considered "legit" !! Aek from Thailand Vista General 0 03-28-2007 01:29 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51