Skip to content

Instantly share code, notes, and snippets.

@benoitjadinon
Last active August 31, 2017 12:00
Show Gist options
  • Select an option

  • Save benoitjadinon/1e01b31a2423a74c1fab0b0811f5e814 to your computer and use it in GitHub Desktop.

Select an option

Save benoitjadinon/1e01b31a2423a74c1fab0b0811f5e814 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
// http://stackoverflow.com/questions/150479/order-of-items-in-classes-fields-properties-constructors-methods#310967
// https://blogs.msdn.microsoft.com/brada/2005/01/26/internal-coding-guidelines/
public class Program
{
public static void Main()
{
Console.WriteLine(new Guidelines("muh").GetText());
}
}
public interface IGuidelines
{
string GetText();
}
public class Guidelines : IGuidelines
{
// constants
protected const string SomeConstant = "some_constant";
// static fields
private static int SomeCrappyStaticCounter = 0;
// readonly fields
private readonly string _someTextField; // readonly fields before instance fields
// fields
private string _someInstanceField = string.Empty;
// con(de-)structors
public Guidelines(bool someDependency, bool someOtherDependency, bool someOtherOtherDependency, bool someOtherOtherOtherDependency)
: this("blah") // below, so you don't scroll vertically to see it
=> _someInstanceField = "foo";
public Guidelines(string someText)
: base()
{
_someTextField = someText;
SomeCrappyStaticCounter++;
}
~ Guidelines()
=> Console.Error.WriteLine("Finalized!");
// events
protected event Action<bool> Tapped;
// enums
// properties
public bool IsThisAwesome { get; private set; } = true;
public bool IsThisReallyAwesome => true;
private bool isThisLong = true;
public bool IsThisLong
{
get
{
return isThisLong;
}
private set
{
isThisLong = value;
}
}
// C# 7.x
public bool IsThisLessLong
{
get => isThisLong;
private set => isThisLong = value;
}
// indexers
public char this[int index]
=> _someTextField[index];
// methods
public string GetText()
{
var listOfListOfStuff = new List<List<string>>(); // var is more readable
var stringsArray = new string[]{ "yo" }; // it's an array of strings alright
bool isSomething = true; // var is less mandatory, 'bool' is one more char long
bool typeIsUnclear = ShouldIReallyReturnText(); // var would not be super clear without mouse over
return ReturnText(typeIsUnclear);
}
protected bool ShouldIReallyReturnText ()
=> !string.IsNullOrEmpty(_someInstanceField); // smaller method
// private is mandatory
// is right after the one that called it, regardless of accessibility
// Rider would one-line this, but don't do it if it kills readability
private string ReturnText(bool reallyDo = false)
{
// tuple
var hum = (Alpha: "a", Should: false);
// who needs brackets anyway ?
if (hum.Should || reallyDo)
return _someInstanceField;
return "nope.";
}
//tuple C# 7
(string Alpha, string Beta) NamedLetters = ("a", "b");
// internal classes
class SomeInternalClass
{
}
}
@Zilounay
Copy link
Copy Markdown

disagree with lines 87 & 90-91.
Should remove "private" and add brackets. It's true because it's on the internet.
:(

@benoitjadinon
Copy link
Copy Markdown
Author

benoitjadinon commented Jul 7, 2017

the internet will say what you want it to say, like, I want it to say it's better to show privates : https://stackoverflow.com/questions/8479214/any-reason-to-write-the-private-keyword-in-c

@benoitjadinon
Copy link
Copy Markdown
Author

also, Rider said private. and it's really more readable when it's next to privates and protecteds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment