5
votes
4answers
2k views

Should you ever use private on fields and methods in C#?

I am somewhat new to C# and just found out that: in C# all of the fields and methods in a class are default private. Meaning that this: class MyClass { string myString } is the same as: class ...
0
votes
1answer
71 views

Inheriting from Abstract class vs Enum Types for custom exceptions

I am creating an interface and would like the implementer(s) of this interface to throw exceptions in the case that something goes wrong. Let's call the implementer a plugin. I have a director which ...
2
votes
1answer
138 views

Is serialization better done in instance methods or static methods

Say I have a class workStockItem, that I wish to serialize. Which is the better style? using a static method or using a non-static method (maybe even implemented as a property as seen below) ...
2
votes
4answers
851 views

Identify this programming style

Some of the legacy code I've inherited uses the fact that C# supports multiple assignment to write code like: void DisableControls() { ddlStore.Enabled = ddlProgram.Enabled ...
1
vote
2answers
389 views

How to set up something like an integration server that measures the quality of code and reject the code if the score is below a certain number?

Even if I don't like enforcing people to do things (and I believe that it may decline the productivity and cause anger), I really want to enforce good coding style. Is there a way to set up ...
0
votes
3answers
166 views

Overloading Methods With Different Behavior - Style

I have a style question about overloading methods/constructors. I have a constructor which does something very simple, and then calls a method with some side effects. Sometimes however I don't want ...
2
votes
4answers
934 views

DTOs matter on ASP.NET MVC JSON Action?

I recently came across an internal project where they were using DTOs throughout the solution, and when I sat down to implement a new MVC controller with a JSON REST endpoint I decided to use an ...
2
votes
3answers
262 views

Should I make a variable readonly when I modify it, but I don't actually set it outside of the constructor?

Background: I was writing some code. It looked something like this: class X { private List<int> _myList = new List<int>(); public void MyMethod(int x) { ...
6
votes
3answers
459 views

Is the usage of internal scope blocks within a function bad style?

There are some (quite rare) cases where there is a risk of: reusing a variable which is not intended to be reused (see example 1), or using a variable instead of another, semantically close (see ...
3
votes
3answers
982 views

When are chained assignments (i.e. a=b=c) bad form?

I'm working on a VB.Net WinForms project and found myself writing code like this: this.Fizz.Enabled = this.Buzz.Enabled = someCondition; I couldn't decide whether that was bad code or not. Are ...
2
votes
4answers
2k views

Microsoft's coding standards for ASP.NET controls

I cannot find any naming standards/conventions in MSDN for naming ASP.NET controls. One of the following standards tends to be used by programmers: lblAddress AddressLabel Address According to ...
7
votes
7answers
1k views

Prevent developers from using constants

I have one one software system which allows developers to specify an ID or name to create NodeReferences. Both work fine, but ID's are not guaranteed to be the same across different environments. I've ...
2
votes
2answers
524 views

coding style for If condition [duplicate]

I came across below style of writing if statements in C#, on msdn code examples. Usually when I write if statements, the conditions I would write `(Customer != null) I want to know if there is any ...
11
votes
4answers
473 views

Always pull out common cases and branch separately? [duplicate]

We had a disagreement in a code review. What I had written: if(unimportantThing().isGood && previouslyCalculatedIndex != -1) { //Stuff } if(otherThing().isBad && ...
2
votes
2answers
338 views

Style for creating IEnumerable unions

There isn't any cool LINQ sugar for creating unions. The Enumerable.Union() method is usually called like this: var bigList = list1.Union(list2); The alternative is to call Enumerable.Union() which ...
3
votes
5answers
288 views

Using 'new' in a projection?

I wish to project a collection from one type (Something) to another type (SomethingElse). Yes, this is a very open-eneded question, but which of the two options below do you prefer? Creating a new ...
6
votes
4answers
1k views

Switch or a Dictionary when assigning to new object

Recently, I've come to prefer mapping 1-1 relationships using Dictionaries instead of Switch statements. I find it to be a little faster to write and easier to mentally process. Unfortunately, when ...
3
votes
6answers
2k views

Using prefix incremented loops in C#

Back when I started programming in college, a friend encouraged me to use the prefix incrementation operator ++i instead of the postfix i++, citing that there was a slight chance of better performance ...
6
votes
3answers
1k views

Should I prefer properties with or without private fields?

The codebase I'm working in now has the convention of using private fields and public properties. For example, most classes have their members defined like this: // Fields private double _foo; ...
1
vote
7answers
10k views

If condition not true: default value or else clause? [closed]

I have searched Programmers and Stackoverflow and was not able to come up with a satisfying answer, even though I'm quite sure it must have been asked many times before. The only question I found has ...
7
votes
4answers
2k views

Are fluent interfaces more flexible than attributes and why?

In a EF 4.1 Code First tutorial the following code is given: public class Department { public int DepartmentId { get; set; } [Required] public string Name { get; set; } public virtual ...
7
votes
4answers
419 views

Warn about 3rd party methods that are forbidden

Note: This question refers to code written in Java or C#. I am managing a couple of large projects where we have discovered issues (not necessarily bugs) with some 3rd party/SDK methods and have ...
9
votes
2answers
1k views

Is this awkward spacing some type of style?

In reading another programmers code, he uses a format I have never seen. E.G. namespace MyNs.HereWeAre {//tab here for some reason public class SomeClass ...
9
votes
4answers
1k views

C# return variables

In a debate regarding return variables, some members of the team prefer a method to return the result directly to the caller, whereas others prefer to declare a return variable that is then returned ...
6
votes
4answers
2k views

How to ensure a single coding standard in .NET with tool support

Probably, the basic situation is familiar to everyone. You have agreed on certain coding standards in your team and now it is time to make sure that everybody follows them. Some do it via heavy ...
3
votes
1answer
491 views

Has anyone thoroughly compared C# common coding standards?

Most of the C# programmers I know embrace one of the common coding standards. However being aware of the standards is one thing, telling the differences is another. Browsing the common coding ...
2
votes
4answers
286 views

Do more object declarations affect the program?

I am programming in Windows Forms and MySQL. If I declare this in the program, I can use the connection and command objects in the whole .cs page: MySqlConnection connection = null; MySqlCommand ...
42
votes
14answers
5k views

Should I encourage junior developers to use explicit or implicit typing?

I am looking to solicit the community's opinion on whether or not it would be advisable to teach junior developers to code with implicit typing (using var) or should I encourage the use of explicit ...
6
votes
3answers
585 views

Programming standards and principles to become better programmer [closed]

I am a c# developer. I have always been interested in increasing my skills and knowledge and trying to pickup new technology. However now I want to enhance my knowledge in Programming standards and ...
6
votes
3answers
692 views

Is it bad idea to use flag variable to search MAX element in array?

Over my programming career I formed a habit to introduce a flag variable that indicates that the first comparison has occured, just like Msft does in its linq Max() extension method implementation ...
16
votes
9answers
6k views

Why are people so strongly opposed to #region tags in methods?

I hear a lot about keeping methods short and I've heard a lot of programmers say that using #region tags within a method is a sure sign that it is too long and should be refactored into multiple ...
4
votes
4answers
277 views

How do you proactively guard against errors of omission?

I'll preface this with I don't know if anyone else who's been programming as long as I have actually has this problem, but at the very least, the answer might help someone with less xp. I just stared ...
10
votes
3answers
4k views

What should I include in XML documentation comments?

I'm trying to make a point of documenting my code better, especially when it comes to the XML comments on class members, but often it just feels silly. In the case of event handlers, the naming ...
15
votes
6answers
1k views

When should I use a 2-property class over a pre-built structure like a KeyValuePair?

When should you put Key/Value type of data in it's own class instead of using a pre-built generic structure, such as a KeyValuePair or a Tuple? For example, most ComboBoxes I create contain a ...
10
votes
9answers
2k views

LINQ Style preference [closed]

I have come to use LINQ in my every day programming a lot. In fact, I rarely, if ever, use an explicit loop. I have, however, found that I don't use the SQL like syntax anymore. I just use the ...
10
votes
5answers
3k views

Why have a method that returns a bool/int and has the actual object as an output parameter?

I see the following code pattern all over the place in my company's codebase (.NET 3.5 application): bool Foo(int barID, out Baz bazObject) { try { // do stuff bazObject ...
13
votes
12answers
1k views

What is the regarded current best practises regarding the “this” keyword in front of field and methods in c#?

Unless it is needed to differentiate between a variable and field with the same name, I never put this. in front of a field or any member access in C#. I see this as no different to m_ prefix that ...
2
votes
3answers
348 views

C# Coding Standard - Position of Attribute in relation to Target

I had thought that this was one of the few solved problems in C# coding standards / style; Attributes always appear on the line above the thing to which they are applied a la [SomeClassAttribute] ...
3
votes
1answer
165 views

FromXYZ vs Overloaded Method

I'm trying to think of the cleanest way to implement a couple of methods that open a file. Consider the following method signatures: public static DomainObject Load(Uri urlToFile) { /* downloads ...