Click here to Skip to main content
15,878,316 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
Generalauto-usings in Visual Studio Pin
Marc Clifton11-Oct-23 4:34
mvaMarc Clifton11-Oct-23 4:34 
GeneralRe: auto-usings in Visual Studio Pin
MarkTJohnson11-Oct-23 4:55
professionalMarkTJohnson11-Oct-23 4:55 
GeneralRe: auto-usings in Visual Studio Pin
Gary Wheeler25-Oct-23 1:16
Gary Wheeler25-Oct-23 1:16 
GeneralRe: auto-usings in Visual Studio Pin
englebart25-Oct-23 13:07
professionalenglebart25-Oct-23 13:07 
GeneralDigital film cartridge adds 20 MP micro 4/3 sensor to any 35 mm film camera Pin
RickZeeland10-Oct-23 0:08
mveRickZeeland10-Oct-23 0:08 
GeneralRe: Digital film cartridge adds 20 MP micro 4/3 sensor to any 35 mm film camera Pin
trønderen10-Oct-23 4:43
trønderen10-Oct-23 4:43 
GeneralRe: Digital film cartridge adds 20 MP micro 4/3 sensor to any 35 mm film camera Pin
RickZeeland10-Oct-23 5:59
mveRickZeeland10-Oct-23 5:59 
GeneralThe changing landscape of OOP (from class to struct) PinPopular
raddevus25-Sep-23 6:04
mvaraddevus25-Sep-23 6:04 
Just started reading this (just released) book, The C# Type System (no starch press)[^] and the first chapter is kind of blowing my mind.

Step 1
Author starts out with the following example and says,
"You must use better named variables so dev users know what they mean."
C#
Displacement(double t, double v, double s)
{
    var x = v * s * Math.Cos(t);
    var y = v * s * Math.Sin(t) - 0.5 * 9.81 * Math.Pow(s, 2);
    return (x, y);
}
Yes, that makes sense.

Step 2
Then he says,
"Oh, you can add meaning with this new idea of named arguments so users don't have to remember order that they should be passed in."
C#
var result = Displacement(angle: .523, speed: 65, elapsedTime: 4);
Ok, yes, that is good advice with the modern capabilities.

Step 3 May Blow Your Mind

He mentions that the code is still confusing because all three arguments are the same primitive type (double) and this leads into...

From the book:
Primitive Obsession code smell, which describes any code that has an overreliance on primitive types—that is, those types that are built into the language, such as int, double, and string.

The solution is...
Wrap All the Primitive Types In Structs 🤯🤯🤯🤯🤯
C#
public struct Angle
{
    public double Size {get; set;}
}
public struct Speed
{
    public double Amount {get; set;}
}


The Paradigm Has Shifted
Now, when the user attempts to call the Displacement method the compiler will know that the argument type is wrong.
Now, there's no way to pass the wrong value into the method, because the compiler will know the type.

Wow, that is a very different paradigm!!

Step 4 Is Immutability
Now, make each struct immutable so that it cannot be altered after construction.

C#
public readonly struct Speed
{
    public Speed(double amount)
        => Amount = amount;
    public double Amount {get;}
}

I began learning OOP back in 1991 or so and it was much different then with a strong focus on Inheritance. Of course we all learned the pain of inheritance and then the Gang Of Four said,
"prefer composition over inheritance" and that changed a lot of thought on OOP.
Design to a interface and not a implementation.

It is interesting now because there seems to be a switch from Class-focus (heap-based objects) to Struct-focus (stack-based objects) at this current time.

Keep in mind that when Java was created that the designers literally made everything a Class.
I mean, basically C# is that way too with Object at the bottom.

In iOS / SwiftUI they have officially said, "If you create a new thing then create a struct not a class" and they explain why.

What Are Your Thoughts?
Anyways, what do you think about this "Primitive Obsession code smell"?

I see the value in it -- and I'm assuming that the people promoting this are saying do this for specific things in the domain and not all of them.

I cannot image wrapping all primitives in structs and having that many extra types. It feels odd but I can definitely see the value / benefit. But it's really odd after all these years.
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Greg Utas25-Sep-23 7:04
professionalGreg Utas25-Sep-23 7:04 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
raddevus25-Sep-23 8:11
mvaraddevus25-Sep-23 8:11 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Greg Utas25-Sep-23 13:24
professionalGreg Utas25-Sep-23 13:24 
GeneralRe: The changing landscape of OOP (from class to struct) PinPopular
Jeremy Falcon25-Sep-23 7:37
professionalJeremy Falcon25-Sep-23 7:37 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
raddevus25-Sep-23 8:06
mvaraddevus25-Sep-23 8:06 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Jeremy Falcon25-Sep-23 8:13
professionalJeremy Falcon25-Sep-23 8:13 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
raddevus25-Sep-23 8:35
mvaraddevus25-Sep-23 8:35 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Jeremy Falcon25-Sep-23 12:26
professionalJeremy Falcon25-Sep-23 12:26 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Nelek27-Sep-23 9:06
protectorNelek27-Sep-23 9:06 
QuestionRe: The changing landscape of OOP (from class to struct) Pin
Jeremy Falcon27-Sep-23 11:59
professionalJeremy Falcon27-Sep-23 11:59 
AnswerRe: The changing landscape of OOP (from class to struct) Pin
Nelek28-Sep-23 10:06
protectorNelek28-Sep-23 10:06 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Matt Bond27-Sep-23 4:37
Matt Bond27-Sep-23 4:37 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
trønderen25-Sep-23 9:59
trønderen25-Sep-23 9:59 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
trønderen25-Sep-23 8:41
trønderen25-Sep-23 8:41 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
raddevus25-Sep-23 10:15
mvaraddevus25-Sep-23 10:15 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Rick York25-Sep-23 10:25
mveRick York25-Sep-23 10:25 
GeneralRe: The changing landscape of OOP (from class to struct) Pin
Nelek27-Sep-23 9:08
protectorNelek27-Sep-23 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.