15
Heliarco
58d

In C# you can open as many scopes wherever you want at whatever time you please. Nested within themselves, doesn't matter.

Use this to fuck with the other developers on your team. Fence off their evil code behind a thousand curly braces!

Or maybe jazz up your indentation, give the code a nice and bouncy flow!

Comments
  • 4
    For extra points, since this changes the timing of built in object disposal, make logic which depends on this feature
  • 5
    That compiles ? Lol
  • 1
    @MadMadMadMrMim it very much does!
    C# Actually has tons of ways you can subtly punish good coding practice like you wouldn't believe it. Such as raw pointers etc. but somehow people look at you funny when you use them :(
  • 0
    @Heliarco lol whyyyyyyyyyyyyyyyyyyy??!??!?? Lol
  • 10
    Scopenhagen, capital of Antagonizia
  • 0
    And now what you are all amased by this, go read this if you want to REALLY make things impossible to track :
    https://medium.com/@trapdoorlabs/...
  • 0
    Here you go with a real life code illustratiuon.
  • 3
    Does C# have a concept of a stack vs heap?

    In C++ scoping is useful if you want to allocate an object on the stack, but when it leaves the scope it is destroyed.
  • 0
    @NoToJavaScript

    Technically your casing is off and you’re just overriding the String class inside then namespace creating ambiguity

    string s1 will fix that
  • 1
    @MadMadMadMrMim

    "string" is just a keyword. Not a class definition. Which always target System.String.

    But there are cases you don't want System.String.

    Yes.

    But not String.Empty. And you can relocate the String "overide" in another part of the project.

    You can redifine String.IsNullOrEmpty. And just watch the whole project burn haha
  • 4
    I use narrow scoping in C (but not pointless empty scope levels) because it doesn't just clarify that some stuff is only used very locally - it also can gain speed because the compiler knows that it can discard the variable after the scope.

    You can get like 1% of speed-up in tight loops just by narrow scoping. Yes, I write code with such small performance gains in mind because shit adds up.
  • 1
    @Fast-Nop

    Can't talk about C, but in C#

    if (condition)
    DoStuf();

    and

    if (condition)
    {
    DoStuf();
    }

    Doesn't change anything.

    But there is a "new" (well, couple of years now) syntax for using.

    using (var j = new service()) {
    code
    }
    code1
    Will dispose j before code1 part 9Well, it also depends on plenty of factors, but usually it does).

    new syntax is
    using var j = new service();
    code;
    code1;

    j will not be disposed until code1 run.

    But you won 1 encapsulation level and code is more clear. At the price of disposing your ressource later.

    I take this aproach everytime.
  • 0
    @NoToJavaScript point is just spelling out system.string would solve part of your problem there lol
  • 1
    @NoToJavaScript Yeah it hits in only when you declare (stack) variables in the small scope instead of a larger one, basically because the compiler can reason better about register allocation for that part, and because it won't pointlessly push the value back into memory.
  • 0
    @Demolishun It has! But all classes and arrays are heap allocated. You can opt out of that by using structs and stackalloc arrays.

    Primitives are also on the stack.

    C# calls this reference types and value types.
    String is the only exception. It's heap allocated, but acts as a value type.
  • 3
    {
    you
    }
    {
    {
    are
    }
    }
    {
    {
    {
    fired.
    }
    }
    }
  • 1
    You can do the same in C++ actually. Scopes are pretty useful for automated memory management
  • 1
    @Demolishun even with heap allocated items using smart pointers with scopes can guarantee cleanup before the end of function execution.
  • 1
    @iiii Though I wouldn't scope heap allocation or object instantiation to tight loops.
  • 1
    @Fast-Nop fair enough. Frequent allocations are very costly
Add Comment