-3

This question already has an answer here:

I am fluent in most of Java and have thought about moving on to a .Net language. Obviously C# and Java are very similar so I have chosen to learn C#. When programming in Java I would always use Eclipse as my IDE, which doesn't have a Drag and Drop system for creating the UI. I noticed, however, that Visual Studio does for all .Net languages. I love to keep my code clean and format it in my own away and do not like how Visual Studio formats it. I have looked for tutorials on how to program the GUI instead of using Drag and drop but cannot find any tutorials. Any help? Do I have to use the Drag and Drop system. (By "the Drag and Drop System" I meant he form creator.)

marked as duplicate by GlenH7, user40980, Dan Pichelman, Kilian Foth, gnat Aug 8 '14 at 13:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    WinForms, WPF, web apps, ...? – Kate Gregory Aug 6 '14 at 14:50
  • @Kate winforms. – Harry Kitchener Aug 6 '14 at 14:53
  • 1
    You can design anything in C# without an editor. If you want to do everything by hand, feel free, add a single control then investigate what modifications VS actually makes to understand what modifications are required. – Ramhound Aug 6 '14 at 15:04
  • Why would you want to? Writing raw winforms is only slightly nicer than raw SWT. At very least do WPF, which is reasonably nice to use without editors. – Phoshi Aug 6 '14 at 15:34
  • The WinForms designer creates a pair of files. One is where you put the handlers and you can format it to your heart's desire and the other is where it configures the widgets and you don't ever need to look in it and therefore shouldn't mind how it is formatted. – Jan Hudec Aug 6 '14 at 16:45
6

Code generators increase productivity...

Code generators available in Visual Studio are here to make your life easier. This includes the one which generates code for Windows Forms.

A graphical tool like that is particularly useful when you need to communicate that this button is large and is positioned here, while that one is small and is just in this corner, but not too close to the border: having to position the elements through code would be much harder. If you're not convinced, try to do both techniques while sitting near a stakeholder or a designer who actually takes decisions about the look of a form; what would be more productive: to have an immediate visual feedback after every change, or to have to recompile and rerun the app every time, trying to guess whether "close enough to the border" should be 11, 12, 13, 14 or 15 pixels?

... so be productive

As a programmer or a developer, you're expected to write clean code when it is beneficial for the project. Following the clean code dogma in every case makes you unproductive: in other words, you become a burden for the project.

It's your job to determine right where clean code matters, and where anybody couldn't care less.

A piece of business-critical code which will be read thousands of times by you and your colleagues over ten years period and frequently modified has every reason to be as clean as possible. On the other hand, boilerplate code nobody will read doesn't have to be well commented or nicely indented.

Code generators also have a benefit compared to a human: machines have less chances to introduce a subtle bug. Guess how much bugs I've found in code produced by code generators for the last five years? Zero. On the other hand, when repetitive and monotonous tasks are performed by humans, errors have a great risk of being introduced. A bug at this level would only decrease your productivity.

Side note: as a recruiter, I'll probably reject a candidature of a .NET WinForms programmer who claims that he don't use WinForms code generator because he prefers writing clean code by hand. While I favor persons who are able to write clean code, I don't need a person who is wasting my money typing by hand boilerplate code.

Code generators don't necessarily produce ugly code...

Talking about clean code, Windows Forms code is not the ugliest one. It could certainly be better (and at least be compliant with the official style guidelines enforced through StyleCop and official recommendations enforced through Code analysis), but in all cases, the usage of partial classes with two SuppressMessage and a GeneratedCode similarly to this example makes it possible to abstract completely this part of the code and prevent it from spawning useless warnings during static checking.

... but not all code generators are equal

Code generator for ASP.NET (WebForms) is a very different beast. It's not particularly useful: unless a programmer doesn't know HTML, he'll be as efficient (if not more) writing code directly. My recommendation is to never use this code generator.

Code generator for WPF is quite useful when you don't know the exact names. On the other hand, it often produces code which is much more complicated than it should be. My recommendation is to use this code generator selectively, and try to write most XAML code by hand¹.

Code generator for LINQ to SQL and for Entity Framework is a must-have. There is no need to write boilerplate code by hand. T4 is an excellent feature which reduces even more the necessity to write repetitive code.

Conclusion

This being said, nothing forces you to use those code generators. You can create a form directly in code, including through Visual Studio.


¹ I have used WPF in Visual Studio 2010 only. Things may have changed since then, and if I remember well, next versions of Visual Studio were advertised for having a much better WPF editor.

  • 1
    In the earlier days (Visual Studio 2003 to, perhaps, 2008) it was also recommended to avoid the WinForms designer for things intended to run on Windows CE, because the generated code was suboptimal (used setters for things that were default, could be passed to constructor or otherwise set using fewer calls) and it made a difference on the slower devices of the day. – Jan Hudec Aug 6 '14 at 15:49
  • @JanHudec: great point. I didn't know that. – Arseni Mourzenko Aug 6 '14 at 15:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.