44

I'm a Python programmer learning C# who is trying to stop worrying and just love C# for what it is, rather than constantly comparing it back to Python.

I'm caught up on one point: the lack of explicitness about where things are defined, as detailed in this Stack Overflow question. In short: in C#, using foo doesn't tell you what names from foo are being made available, which is analogous to from foo import * in Python -- a form that is discouraged within Python coding culture for being implicit rather than the more explicit approach of from foo import bar.

I was rather struck by the Stack Overflow answers to this point from C# programmers, which was that in practice this lack of explicitness doesn't really matter because in your IDE (presumably Visual Studio) you can just hover over a name and be told by the system where the name is coming from. E.g.:

Now, in theory I realise this means when you're looking with a text editor, you can't tell where the types come from in C#... but in practice, I don't find that to be a problem. How often are you actually looking at code and can't use Visual Studio?

This is revelatory to me. Many Python programmers prefer a text editor approach to coding, using something like Sublime Text 2 or vim, where it's all about the code, plus command line tools and direct access and manipulation of folders and files. The idea of being dependent on an IDE to understand code at such a basic level seems anathema. It seems C# culture is radically different on this point. And I wonder if I just need to accept and embrace that as part of my learning of C#.

Which leads me to my question here: is C# development effectively inseparable from the IDE you use?

Ghopper21
  • 872
  • 1
  • 7
  • 15
  • 7
    Python is a dynamic language, C# statically typed (like Java), so how you code in either is _very_ different. – Oded Oct 15 '12 at 11:00
  • @Oded: Except that many Java devs also frown on wildcard imports: http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad – pdr Oct 15 '12 at 11:03
  • @pdr - Possibly, but C# doesn't have this distinction. There is no way to selectively import types from an assembly/namespace. – Oded Oct 15 '12 at 11:04
  • 6
    @Oded: `using MyType = MyNamespace.MyType;`? – pdr Oct 15 '12 at 11:05
  • 1
    @pdr - Nope. That is a way to give a type an _alias_, the reference to the assembly already imported it. – Oded Oct 15 '12 at 11:06
  • 1
    @Oded - sure, but to pdr's point, it's not clear to me why being statically typed leads to lack of explicitness about imports. In any case, are you effectively saying "yes" to my question? I think if I know the answer is "yes", it will help me accept and embrace this different way of coding. – Ghopper21 Oct 15 '12 at 11:09
  • @Oded: That's how we USE it, but it is also a way of explicitly importing one type from a namespace. ie. if I say `using Regex = System.Text.RegularExpressions.Regex;` then my code has access to the Regex class (because I'm essentially fully-qualifying it every time I use it) but not the MatchCollection class, which is in the same namespace. However, Ghopper21 is right that my initial point was that the dynamic/static typing is irrelevant. – pdr Oct 15 '12 at 11:13
  • @Ghopper21 - Fair point. And yes, I am saying the answer is "yes". When you reference an assembly, all its types will get loaded. When you use a `using` declaration, that simply helps with exposing the types on that code file as a way to write less code (note that the types always can be accessed using their fully qualified name). – Oded Oct 15 '12 at 11:14
  • @pdr - Yes, but my understanding of `import` in .NET is that of `reference`, not of a `using` declaration (which is just syntactic sugar, when all is said and done). – Oded Oct 15 '12 at 11:15
  • 1
    @Oded: No, I think the same argument applies to using in C# as applies to import in Java. IntelliSense would be much more selective about the classes it offered me if I didn't open up entire namespaces to my code. However, I've worked in Java a bit and I think that argument is heavily offset by the massive collection of imports in every file. Intelligent namespacing is a much better solution to the same problem. – pdr Oct 15 '12 at 11:21
  • 4
    I don't know what it's like in Python, but in C# you can always start at `global::` and work your way from there. `using` doesn't make anything available that wasn't before; it only makes it easier to access (as in less typing needed to use a particular class). – user Oct 15 '12 at 12:27
  • @MichaelKjörling - thanks. In Python you have to import stuff to use it -- that's definitely a difference. But my hangup is about reading not writing code. When you see a reference to a name, how do you know where it's from? The Pythonic way is: look for the import (which should be explicit by convention). The C# way, I'm coming to understand, is: use the IDE features and/or Google the docs. – Ghopper21 Oct 15 '12 at 12:57
  • I think it's a matter of preference, really. I tend to most often use the IDE features (usually mouse hover to see the fully qualified name, sometimes Ctrl+click for jump to definition) when for some reason I need to know the full name of the type. Any non-trivial functionality will usually make the file so long that the `using` directives at the top are well out of view, anyway, unless you are working with a split screen view which has its own pros and cons. – user Oct 15 '12 at 13:34
  • 5
    "Many Python programmers prefer a text editor approach to coding, using something like Sublime Text 2 or vim, where it's all about the code, plus command line tools and direct access and manipulation of folders and files." - Which sounds horribly inefficent to me. An IDE is a tool which helps you be more productive. Sure you can build a house with a hammer and hand saw and cut down the trees yourself, but I think you'd have a much faster time of it with some power tools and buying some pre-cut lumber. – Andy Oct 15 '12 at 13:57
  • 2
    @Andy -- I can see it sounds that way. But these text editors are actually very powerful, offering a ton of tooling and programmer efficiency features. It's just a radically different way of getting at and using those tools and features than an IDE (and one that may be more or less appropriate for certain languages/programming cultures). – Ghopper21 Oct 15 '12 at 14:05
  • 1
    If it offers integrated development features on some level, then by definition it is an IDE (Integrated Development Environment). An IDE can be lightweight or heavyweight but still be an IDE. – user Oct 15 '12 at 14:49
  • 1
    For those who missed the pop culture reference in the first sentence of the question... http://en.wikipedia.org/wiki/Dr._Strangelove – JoelFan Oct 15 '12 at 15:00
  • 1
    I'm familiar with vim; I don't think it offers near the functionality that Visaul Studio does, even before you add something like R#. Unless there's a phython add-in that adds a lot of functionality i'm not aware of. – Andy Oct 15 '12 at 15:14
  • @Ghopper21 So where did your C# adventures take you? Did you stick to the editor+terminal combo, or did you submit to the dark side, or...? – Alec Mev Jun 29 '17 at 15:24

11 Answers11

34

The idea of being dependent on an IDE to understand code at such a basic level seems anathema.

It is not a question of understanding your code: given sufficient time, you can always locate the right variable with a basic text editor or even in a printout. As far as understanding the code goes, the IDE dependency absolutely does not exist.

Locating your references efficiently is an entirely different subject: I love the ability to find usages of Java variables in Eclipse as much as I love finding declaration points in Visual Studio, for both C# and C++. I prefer spending my time coding, rather than looking for declaration points manually. This is similar to doing math: I can multiply multidigit numbers on a piece of paper, but I prefer using calculator to save myself a minute or two.

Starting at a certain "critical size" of the code, a good IDE becomes very useful regardless of the programming language. The size may vary language to language, but once you cross several thousand lines, having an IDE helps regardless of your language. This has more to do with limitations of a human mind than with a particular programming language: at some point, your short-term memory is bound to "overflow".

There are tricks letting you increase that critical size where IDE becomes useful. For example, you could follow a naming convention (Hungarian names were big in the C++ world at some point, especially among the Windows practitioners). Another common trick is qualifying instance variables with this. even in contexts where such qualification is not required.

These tricks come with trade-offs: almost inevitably, they make your program less readable by obscuring names, or inserting the explicit references the encapsulation was intended to hide. Faced with the choice, I pick clean-looking code plus an IDE over a less-clean-looking code minus an IDE. I fully recognize, however, that other people's choices may differ from mine.

Sergey Kalinichenko
  • 17,393
  • 4
  • 57
  • 73
  • 1
    I should probably have said "quickly read code"... It's quite a mental shift for me to not have all references right there in the same source file, and instead to use tools to locate those references. – Ghopper21 Oct 15 '12 at 11:33
  • @Ghopper21 I added a point about beating at the need to use an IDE, take a look at the edit. – Sergey Kalinichenko Oct 15 '12 at 12:40
  • Thanks for the additional thoughts. By the way, funny you mention `this`. The (enforced) use of `self` in Python (to the point of even having to include `self` as the first parameter for a method) is one of the most distinctive features of the language -- and a good example of its philosophy of "explicit is better than implicit". It's another thing that will require a mental shift for me with C# culture. [continued in next comment] – Ghopper21 Oct 15 '12 at 12:50
  • 5
    Not that I couldn't choose to use `this` idiosyncratically -- but coding isn't an isolated art and I think it's best to follow (or at least start with) the norms of a coding culture -- i.e. be "Pythonic" with Python and whatever the equivalent "C# way" is with C#. And it's clear from the answers and comments here that using a good IDE like Visual Studio is central to the "C# way" (if that's the right way to put it). – Ghopper21 Oct 15 '12 at 12:50
  • 3
    @Ghopper21 Thing you'll run into with C# isn't that you can't write it without an IDE, but that the vast majority of C# code was written in an IDE and all the developers use that IDE. This fact combined with intellisense causes developers to fall into the google memory trap as studies have spoke about http://www.huffingtonpost.com/2011/07/15/google-memory-study-columbia-university_n_899730.html "According to the studies' findings, people with access to Google's massive web index are less likely to recall a fact when prompted; however, they remember where and how to locate that fact online" – Jimmy Hoffa Oct 15 '12 at 13:51
  • 2
    @Ghopper21 I'm not saying this memory trap is *bad*, it's a source of extremely increased productivity for most which is why only those with long memories of the days when they could memorize the entire API they worked with really complain about it. I am merely pointing this out because this fact directly affects the C# code you'll read and the style and culture of C# development. – Jimmy Hoffa Oct 15 '12 at 13:52
  • @JimmyHoffa - thanks, that's helpful for understanding the C# culture. – Ghopper21 Oct 15 '12 at 13:57
  • Here's to hoping very few MS-DOS programmers (even out of the subset who worked in assembly) committed the entire INT 21h API (let alone the other parts of the MS-DOS and IBM BIOS API) to memory... :) – user Oct 15 '12 at 14:52
  • @MichaelKjörling You know many of them did, and to be sure it probably wasn't that hard. What an interesting time to be a developer that must have been. – Jimmy Hoffa Oct 15 '12 at 15:36
  • 1
    I never quite went down to bios/low level dos APIs; but my first language/environment was Borland TurboPascal; and I probably had ~90% of the language/standard library memorized at one point. The primary exceptions being related to completely failing to figure out what OO was supposed to be about. – Dan Is Fiddling By Firelight Oct 15 '12 at 15:56
27

Visual Studio is so convenient that after working with it for a while it is difficult to use a different IDE. It has a lot of handy tools and a bunch of plugins available, so practically it has every feature you would need.

On the other hand, whatever language you learn, it is recommended to use command line at the beginning, so you can better understand how it works. C# isn't an exception.

is C# development effectively inseparable from the IDE you use?

Theoretically no, but practically yes. It is possible to write in C# using a text editor and command line, but if you have Visual Studio, you'd never do this. In fact very few programmers have ever executed C# code from command line.

BTW If you feel inconvenient with using foo, you can use the whole path when using a type.

superM
  • 7,363
  • 4
  • 29
  • 38
  • 9
    SharpDevelop and MonoDevelop are alternative IDEs to Visual Studio. – Oded Oct 15 '12 at 11:11
  • @Oded, I've never used them but its interesting to take a look. Thanks )) – superM Oct 15 '12 at 11:12
  • Thanks superM -- can you give a flavor of the kinds of features that make Visual Studio so indispensable? I know it has very good code completion, but what else? Is it more-or-less like Eclipse for C#, or is there something more specific about it that C# developers rely on? – Ghopper21 Oct 15 '12 at 11:14
  • @Ghopper21, I've found this, it's a tiny overview of VS features: http://ianquigley.com/A75_New_features_of_VS2012_revealed.html and also a little about what's new VS 2012: http://www.microsoft.com/visualstudio/eng/whats-new – superM Oct 15 '12 at 11:19
  • 2
    @Ghopper21: I would suggest it's more comparable to IntelliJ than Eclipse (especially when you include Resharper), except that, in the .NET world, community plugins are generally written for Visual Studio. – pdr Oct 15 '12 at 11:27
  • 5
    +1: I agree, while you can write code at the command line with notepad, you'd be an idiot to ignore the tooling that VS or Sharp Develop brings to the table. – Binary Worrier Oct 15 '12 at 11:55
  • @BinaryWorrier -- Python folks actually use a ton of tooling also; it just often via command-line tools and visually-minimalist-but-feature-rich-text-editors-with-lots-of-keyboard-shortcuts-or-commands like Sublime Text 2 and vim. The end result in terms of getting lots of coding conveniences may not be all that different, but the way you get and think about those conveniences sure is. – Ghopper21 Oct 15 '12 at 12:16
  • A co-worker of mine uses a vim plugin for Visual Studio, but he will occasionally open up stand-alone vim as well. – Dan Lyons Oct 15 '12 at 18:55
  • Lots of helpful insights from the various answers given (thanks to all!). Selected this one for answering the question explicitly: "Theoretically no, but practically yes." – Ghopper21 Oct 15 '12 at 19:23
8

Many Python programmers prefer a text editor approach to coding, using something like Sublime Text 2 or vim, where it's all about the code, plus command line tools and direct access and manipulation of folders and files.

That's great, but it misses the point of the VS IDE. The point of an IDE like VS is rapid development support via strong code tools like refactoring and intellisense. VS is a very very good editor for C# code.

Now C# lets you code in a style that depends on its IDE to a greater extent (you can use lots of var keywords and the like). Some people prefer to be more explicit, for instance by using namespace aliases to be clear on which namespace a class belongs to (like import in Java or Python). That's more of a coding style choice than a feature of the language.

As C# is statically typed (although with some dynamic extensions, as of v4) it's always fairly easy to find out what types are being referred to - if they're wrong the code won't compile, and VS isn't the only IDE with support for C# intellisense. It's probably the best though.

Developing C# without a powerful IDE (like VS) is rather like hammering in nails by hand when you already have a top of the range nailgun - there might be the odd time you need to do it, but professionals use the right tool for the job.

I'd say the same is probably true of Java too. If there's a powerful IDE with intellisense and code refactor tools out there you should probably be using it.

However, look at it the other way round - if you don't want intellisense, compile time code checking and code-analysis/refactoring then a bloated IDE is not the way to go, and neither is a statically typed language. I think it's the other way round:

Many programmers that prefer a text editor approach to coding don't gain as much from statically typed languages (like C# and Java) and so could be better off if they stick to dynamic ones like Python and Javascript.

I think:

  • Dynamic languages suit lightweight tools (heavyweight IDEs confer less benefit here)
  • Static languages suit powerful IDEs (tools can help with the code at the cost of flexibility)
Keith
  • 854
  • 6
  • 10
5

To answer your question: although it's slowly changing, the Microsoft development environment has largely been a monoculture.

This approach has many positives and negatives, which could be argued at length (e.g. consider the pros and cons of open and closed platforms, such as PCs vs an Xbox), but at the end of the day, the tooling from Microsoft is what most people use. The company has also shown that their decision making is often a kind of "give the most value to the majority of our users" process, always looking for practical compromises (most recently - consider Typescript). So basically, I wouldn't be surprised to find that the development of C# was / is done with the tooling (VS) in mind.

Daniel B
  • 6,184
  • 1
  • 22
  • 30
  • By the way, it could be argued that Python is also its own monoculture, given then strong adherence to what is considered "Pythonic" in coding style and approach, and the language's core design principle of having one obvious right way to do things. I tend to think programming monocultures in this sense can be very good, as it creates a coherent community and shareable code. It's just (too bad? and/or an opportunity to expand my mind?) that the Python and C# cultures are so... different! – Ghopper21 Oct 15 '12 at 11:46
  • 2
    @Ghopper21 That's a very good point about Python. I think MS's version of "there should be a single, obvious way to do it" extends to the choice of IDE :) – Daniel B Oct 15 '12 at 11:56
5

Anyone bother to read way down here??

I summarize by saying the massively complex IDE functionality is indispensable and it will (should) evolve to the Zen of Sublime VimNess some day....

Our software is 129 projects of about 2M LOC. Add in the massiveness of the .NET framework and given this all I can say is the IDE is vital, transcending the motivations of this thread's question.

Insight into the Code Base

Period. You know the kinds of features we're talking about; except that its convenience becomes indispensable and essential with the kind of code base I deal with.

I write better code because of the IDE. I always add custom messages to my Nunit tests because it's easy, fast and accurate. I favor enumerations over strings due in large part to intellisense. I do not hesitate to use descriptive/long naming - a multi-line statement is composed fast and clean.

But even this smartness is too much at times. I often use good-old "find in files" text searching.

Coding help

Here is where I oft cry "enough!". Imagine a screen-full with a dozen colors of mostly obscureatta, some particular variable highlighted everywhere, brace highlighting obscuring what the brace actually is, squiggly underlining everywhere because "it" wants me to write literature not code, icons for Resharper context menus (you just gotta click it! then ignore it most of the time), a signature help popup spanning 2/3 of the screen horizontally, vertically displaying several overloads, a popup because of where you just happened to leave the mouse cursor.... I cant even seen the &^!% line*s* of code I'm working on!

Vis.Stud. needs to embrace minimalism so I can focus on coding and not go through hundreds (thousands if you count every color coding setting and all those plugins) of settings in a losing battle to reclaim sanity. A "Pareto" key would be great.

radarbob
  • 5,808
  • 18
  • 31
  • 1
    Really appreciated your thoughts here, as you seem to really "get it" in terms of both the IDE and "Sublime VimNess" approaches to things. I'm with you in all you say here. Here's hoping it comes to pass. – Ghopper21 Dec 06 '12 at 13:20
4

For one, C# is not Python.. There are different design methodologies.

Now, to answer your question, it's completely possible to use your Python-esque using statements.

using FooBar=MyName.Foo.FooBar; 

It's just it is definitely not the norm because it's not nearly so easy. However, I think you should worry less about knowing exactly where exactly each class is coming from. I don't understand the entire point of doing it this way in Python though.

And also, C# is a language that lends itself very well to using IDEs to make it easier. Intellisense is amazingly simple to implement, especially compared to dynamic languages such as Ruby and Python. However, you don't have to be stuck to your IDE. I've heard of people using Eclipse. There also is of course MonoDevelop(which I use quite a lot), and you can even work from a command line. On my server sometimes, I'll be editing C# files with vi and then using xbuild to rebuild it... It's just that using an IDE makes things much easier compared to the command line for typical cases.

Earlz
  • 22,658
  • 7
  • 46
  • 60
1

My understanding was that in Python, "everything's public" or something to that effect. In C#, the module designer decides what is public and what isn't, so when you do an import you only get the public API anyway. That could be a reason for the difference you're describing.

Scott Whitlock
  • 21,874
  • 5
  • 60
  • 88
  • That's definitely a big difference between Python and C#, but it doesn't really help me understand why there is such lack of explicitness in imports. As @pbr noted in his comment, Java (which also has the public/private distinction) is similar to Python in its cultural preference for explicit imports. – Ghopper21 Oct 15 '12 at 11:36
  • 5
    @Ghopper21 - The only reason to prefer explicit imports in Java (and C++ for example) is to prevent name collisions. C# made the design decision to deal with this problem via import/type aliases, since they better handle when you actually _have_ a name collision along with the benefit of not having a bunch of boilerplate imports cluttering your source. – Telastyn Oct 15 '12 at 13:25
1

is C# development effectively inseparable from the IDE you use?

At my previous job I was mostly using vim to code in languages such as C#, JavaScript, Powershell, Perl, C++, and quite a few developers used to do something similar. The project was just too big for Visual Studio.

That said, most C# developers deal with much smaller projects and are quite happy to use VS.

Nemanja Trifunovic
  • 6,815
  • 1
  • 26
  • 34
0

This is an interesting view on C# development. What you are asking goes beyond C# though, if you're asking about the IDE.

Like you say, in Python, you can use various editors to write your code. You can do that with the .NET framework too. There are also other IDE tools you can use such as SharpDevelop. Visual Studio is very tightly co-developed with the .NET framework and is relatively expensive. Tools such as SharpDevelop and the "Express" versions of VS exist to entice more developers to use .NET. Essentially all an IDE does for you is provide an organised environment, usually with intellisense, add-ons for productivity and a "helper" to assemble what can end up being a very scary looking command-line to pass to your compiler. The same is true for Java, tools such as Eclipse just provide the organisation and productivity enhancements for us. Under the hood, nothing magical happens until you build or compile your project and that respect, all IDE's are just pretty faces to the unfriendly compiler.

When you talk about the "using" statement in C# and compare it to Python, it's not really the same thing going on under the hood. The using statements are used by the compiler to aid it's effort in transforming the C# code into MSIL. In MSIL, there is no "import all these classes from this namespace" directive. By the time code is at MSIL level, all the classes have been tagged with their fully qualified names. These "using" and "import" statements are to aid human readability. They are not compiler optimisation commands. After all this initial "tagging of fully qualified names" has gone on, there might be a kind of low-level "minify" to the FQNs but this is to aid the compiler/interpreter to execute faster. This is the optimisation - but there is no control of this offered to the developer, unless you are writing your own custom version of the compiler!

One final fundamental difference between all these languages mentioned on here are that some of them are interpreted by VMs, some are interpreted JIT style and some are fully compiled. How these using directives are implemented across those compilers and interpreters differs greatly.

HTH.

Lee James
  • 9
  • 1
  • "These 'using' and 'import' statements are to aid human readability." -- yes, that's a key philosophical difference at work here. Readability at a textual level is a key first-order Python design principle. C# don't strictly need, but practically rely on a good IDE like VS to be "readable". Incidentally, "import" in Python is also more than about readability, unlike "using" in C#. – Ghopper21 Dec 06 '12 at 13:17
0

I think that historically the answer is pretty much, yes - getting C# development up and running effectively in anything outside of Visual Studio, Xamarin, or SharpDevelop was just too much an unpleasant experience.

But recently a lot of projects have emerged that makes it easier, e.g.:

OmniSharp, provides a foundation for intellisense and refactoring, along with vim, emacs, atom, and sublime plugins. I haven't actually used it, so I don't know how well it works, but it does look promising.

Yeoman ASP.NET MVC generators, helps bootstrap a new MVC project (maybe other generators exist).

paket, an alternative to NuGet where you can actually add NuGet packages to your project from the command line, and have your .csproj files updated (although there was a NuGet command line tool, actually updating projects to use the packages was part of the IDE integration, not command line tools).

Paket has the implication that you have to adapt your source code to using it - so if you sit as the single member in a team and wants to use a text editor for coding, and everybody else uses Visual Studio, you would have to convince everybody else to adapt the solution to your needs :(

So you should be able to get up and running, but there is comparably more work to do to get your toolchain up and running efficiently.

Pete
  • 8,916
  • 3
  • 41
  • 53
-1

Not anymore:

http://www.omnisharp.net/

OmniSharp is a family of Open Source projects, each with one goal - To enable great .NET development in YOUR editor of choice.

It’s fun to say Cross Platform .NET. But is it reasonable for someone to develop .NET without Visual Studio and Windows?

Is it fun to do .NET on a Mac in Sublime? Ubuntu and Emacs? Windows and Atom? You can use your editor PLUS get to use great features like Intellisense (not just Autocomplete), Add Reference, Format Document, and lots more. Develop anywhere, deploy anywhere (and to Azure!)

I have tested this with subime3 and osx

More samples at

http://blog.jonathanchannon.com/2014/11/12/csharp-first-class-citizen-sublime-text/

mamcx
  • 412
  • 3
  • 8
  • Omnisharp make possible to use C# with text editors, like sublime and emacs. So why is downvoted? – mamcx Apr 10 '15 at 21:34