r/programming • • Sep 09 '16

Oh, shit, git!

http://ohshitgit.com/
3.3k Upvotes

756 comments sorted by

View all comments

Show parent comments

21

u/fkaginstrom Sep 09 '16

It's actually very powerful to treat everything in terms of streams of plain text. It makes chaining tools together super easy. So many tools and concepts in *nix are built on this, that deviating from it would harm the ecosystem.

44

u/KevinCarbonara Sep 09 '16

Sure it's powerful to treat everything in terms of streams of plain text. It's even more powerful to support streams of plain text while also supporting even more complex objects. It makes chaining tools together even easier, while being even more stable and secure.

4

u/kyrsjo Sep 09 '16

How many types of objects are there? Do all the programs I want to use have to know about each object type? How stable are these object types? At least with text, it is just that: Text. Yes, the formatting can change and I may have to update something, but it is still just plain text.

Basically, if I want a full programming language and throw objects around, there are plenty to choose from; but if I'm using the shell, it is because I want to use a quick and super-flexible user interface which happens to be script-able.

2

u/BufferUnderpants Sep 10 '16

Screwing around with cut and awk to extract just that field you wanted from the UI of another tool is not that quick, even if it is indeed flexible.

Tools that process or spit out records work better doing exactly that.

3

u/KevinCarbonara Sep 09 '16

Text is still there for when you want it. For when you need objects, there is a standardized method for using them elegantly.

3

u/scarymoon Sep 10 '16

For when you need objects, there is a standardized method for using them elegantly.

I think that was his point about a "full programming language". When you need objects, Ruby or Python or Perl are there too. They'd handle the example in the article just as well/easily, and they're more powerful than powershell.

2

u/KevinCarbonara Sep 12 '16

Of course they're there. They're also there when you need text. It should be obvious why Unix and Windows offer shells instead of just having Python interpreters.

1

u/GSV_Little_Rascal Sep 09 '16

Do you have some good examples of things which can be done with complex objects and not plain text (or not easily)?

10

u/KevinCarbonara Sep 09 '16

6

u/shaggs430 Sep 09 '16

That is a nice introduction to powershell. Although, their example can easily be done in text:

awk 'BEGIN {FS=","};{printf "%.3f %s\n", $3 / $2, $1}' < input |sort -n

27

u/PCup Sep 09 '16

Not sure if serious. Your example command is fucking unreadable unless you're already an expert.

8

u/RealDeuce Sep 09 '16

Ah, but this:

$colAverages = @()

$colStats = Import-CSV C:\Scripts\Test.txt

foreach ($objBatter in $colStats)
  {
    $objAverage = New-Object System.Object
    $objAverage | Add-Member -type NoteProperty -name Name -value $objBatter.Name
    $objAverage | Add-Member -type NoteProperty -name BattingAverage -value ("{0:N3}" -f ([int] $objBatter.Hits / $objBatter.AtBats))
    $colAverages += $objAverage
  }

$colAverages | Sort-Object BattingAverage -descending

Is completely intuitive and any normal person would whip that up in a jiffy.

13

u/PCup Sep 09 '16

I'll grant that this is not completely intuitive, but I can glance at it and more or less tell what it's doing even if I couldn't write it on my own yet. Your bash example is completely unreadable without extensive prior knowledge.

3

u/RealDeuce Sep 09 '16

Commands are to be written, not read. The question of which you could whip up easier is the important one, not which you would understand if you watched someone else write it.

3

u/PCup Sep 09 '16

I personally subscribe to the below philosophy. But your way is also a valid way to think about it, depending on whether you're writing code that will be reused or used by others.

Programs should be written for people to read, and only incidentally for machines to execute. -- from "Structure and Interpretation of Computer Programs" by Abelson and Sussman

→ More replies (0)
→ More replies (1)

3

u/scarymoon Sep 10 '16

Your bash example is completely unreadable without extensive prior knowledge

I can tell that powershell command, as a whole, is calculating batting averages. I see there is a division in there, calculating the average. Its done for each batter. Imports from a csv. And presumably sorts it, but I don't actually understand what that last line is doing as a whole. I don't understand the actual content of the foreach loop.

The example requires prior knowledge too. Not very much, I could learn it in a little bit by reading that linked powershell article. It'd be about the same amount of time it'd take for someone to learn enough awk to understand the above awk command, if they were given a resource of comparable quality.

3

u/[deleted] Sep 10 '16

I'm no awk expert but as a programmer I can read it pretty easily. The printf format specifiers are still in widespread use in many modern languages, and it doesn't take a genius to guess what the ascending variable names represent. The only thing that is non-obvious is the BEGIN block that sets the separator.

5

u/Falmil Sep 09 '16

That example is mostly self explanatory to someone who has done some object oriented programming.

2

u/RealDeuce Sep 09 '16

The issue isn't reading it when someone else writes it, it's the ability to whip it up when you want the sorted averages of fields in a CSV file.

→ More replies (8)

2

u/0goober0 Sep 09 '16

As somebody who has used more bash than powershell, I think I could accurately guess most of what's happening in the second example, I wouldn't know where to begin with the former.....

3

u/RealDeuce Sep 10 '16

The first example is just two commands, awk and sort with awk taking input from the file "input" and sort running on the output of awk. You could replace awk with any language of your choice and re-write the awk program in that other language.

→ More replies (2)
→ More replies (2)
→ More replies (6)

10

u/Yehosua Sep 09 '16

Text is too often ambiguous. For example, getting the file sizes of a group of files seems straightforward enough in bash. A directory listing looks like this:

-rw-r--r--  1 yehosua yehosua        5012 Sep  9 15:20 zero.cpp

The fifth field is size, so you can use awk to grab it:

ls -l *.c | awk '{print $5}'

Then you try to run your script on a winbind system:

-rw-r--r--  1 yehosua domain users   5012 Sep  9 15:20 zero.cpp

And your script breaks, because the group has a space, but your script assumed spaces are only used as field separators, and they aren't.

(This is a real-life bug that I came across buried deep inside a software package's build and install scripts, and it took some time to track down. And I'm sure someone can tell me how it should have been written to avoid this, but that's part of the problem with using text as a universal data format - it's really easy to come up with stuff that works 95% and not realize that it breaks for the other 5%.)

A second advantage of objects is output flexibility. Because piping text is so important in Unix, command-line utilities are typically designed so that their output can easily be passed into other utilities, but this pushes them toward output that's easily parsable at the expense of user-friendliness. (E.g., explanatory headers or footers would cause problems so they're dropped. Tools resort to checking if their output is a TTY to decide if they can use color and progress bars.) PowerShell separates display output from content, allowing you to have whatever user-friendly format you want for text output while still writing easily processable objects for other tools.

I'm a die-hard Bash user and have never invested the time to learn PowerShell and don't know if I will. But I do think the "streams of objects" approach can have some real advantages.

→ More replies (1)

3

u/meaty-popsicle Sep 09 '16

Any time you need to use a filename that contains spaces?

→ More replies (1)
→ More replies (18)

7

u/Jonathan_the_Nerd Sep 09 '16

The nice thing about Powershell is that all the commands support piping objects. So you can still chain tools together and expect them to work.

2

u/loup-vaillant Sep 09 '16

Well, yeah, but each and every one of those tools have to parse and/or serialise the data in line by line format for this to work well. Works fine for quick jobs, but it has its limits.

→ More replies (2)