C
C#10mo ago
mellowzippy

Having trouble understanding patterns

Hello, I`m a second year student and I'm reading about patterns like a factory pattern. I have trouble understanding why I should use patterns and when to use which pattern.
162 Replies
Mkp
Mkp10mo ago
Patterns usage is learned by example. For example, Singletons. The purpose of Singletons is to have one instance of an object. The reason to use Singletons comes from the explicit choice to disallow other parts of the program tracking their own version of the object. For example, you want one single place for your theme configuration, instead of loading it from disk throughout the program.
mtreit
mtreit10mo ago
Most of the ones you hear about originally come from a classic software engineering book called Design Patterns. If you really want the background on those, read that book. I personally think design patterns are over-used as a fancy way of saying what should generally be simple concepts. The whole point of patterns is that the same recurring patterns keep coming up in software design and it's an attempt to formalize and name what those patterns are.
Mkp
Mkp10mo ago
Factories are a bit more complex where the object creation is abstracted away. The purpose of doing so is sometimes because people want a Example ex = new ExampleBuilder().SetFieldA(1).SetFieldB(2).Build(); type of architecture. Which is bad design cuz we have Example ex = new Example { A = 1, B = 2 } syntax. The true purpose of the Factory pattern is to alleviate downstream changes, in which if you change something it should be ABI compliant unless its a function sig change. I would focus on terminology and implementation over the need to actually use these patterns, as mtreit is right in that patterns are overused for a mispurpose like corporate buzzwords. I would assume modix has a $factory Nope?
mellowzippy
mellowzippyOP10mo ago
Would at least having a basic understanding of those patterns make me a better programmer because then I know how I should/could write code in a more "consistent" way?
Mkp
Mkp10mo ago
$patterns
mtreit
mtreit10mo ago
That's the general idea behind formalizing design patterns, yes.
mellowzippy
mellowzippyOP10mo ago
Do you recommend reading the book in that case?
mtreit
mtreit10mo ago
That said, I never read the design patterns book.
Mkp
Mkp10mo ago
Design patterns come after learning algorithms tbh
mtreit
mtreit10mo ago
And I don't generally think in terms of design patterns when I write software. Although I probably use those patterns organically.
mellowzippy
mellowzippyOP10mo ago
Does it come naturally then or is it not really something to keep in mind when programming? Ah okay
mtreit
mtreit10mo ago
It's like anything, some people find it really helpful to try and study the patterns in a formal way, other people don't.
Mkp
Mkp10mo ago
Naturally most of the time. Like my themes example is an actual deployment of singleton I used.
mtreit
mtreit10mo ago
That said, without things like the factory pattern we would not have the absolutely priceless work of art known as FizzBuzz Enterprise Edition... https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition/tree/uinverse/src/main/java/com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/factories
mellowzippy
mellowzippyOP10mo ago
brooo what lmfao Okok
Mkp
Mkp10mo ago
No description
Mkp
Mkp10mo ago
Wtf Reee line breaks
mtreit
mtreit10mo ago
In case it's not obvious, that's a parody of how programming in the enterprise world works.
Mkp
Mkp10mo ago
Going from reading C# to reading Java is so jarring
mellowzippy
mellowzippyOP10mo ago
yeye i know Did you ever look into them or did you just learn what is "wrong" and "right" after years of experience?
mtreit
mtreit10mo ago
I've leafed through a colleagues copy of the book and if you work in software long enough you'll just see plenty of explanations of different patterns. But I couldn't tell you what most of the formal design patterns actually are. I use singletons a lot. I know that's a design pattern. Factory pattern, I use that sometimes. Observer, Adapater, Bridge, Mediator, Decorator, Facade, Visitor <- I see these terms regularly and have no real idea what they are. A lot of the classic design patterns are very much associated with "object oriented programming" and some programmers (myself, for instance) are not a fan of object oriented programming and thus don't really feel like these classic design patterns are all that important. but you should read up on them and decide for yourself. Reading the classic book certainly wouldn't hurt. It's a classic for a reason 🙂
mellowzippy
mellowzippyOP10mo ago
Okay okay Thanks for the advice
Mkp
Mkp10mo ago
inb4 mtreit loses his job You're more likely to get a job knowing OOP and Algorithms than Design patterns
mellowzippy
mellowzippyOP10mo ago
Yeah I figured but I was just wondering if design patterns are like a golden standard
Mkp
Mkp10mo ago
Definitely not, and if you see people pushing them they're often a corporate shill
mellowzippy
mellowzippyOP10mo ago
What about principles? Does your code need to follow like every SOLID principle or is it the same story as design patterns?
mtreit
mtreit10mo ago
Same thing That's another thing that people who are way into OOP get all excited about.
mellowzippy
mellowzippyOP10mo ago
What is the alternative to OOP then?
mtreit
mtreit10mo ago
Procedural and functional approaches mostly Composition using interfaces Instead of inheritance
Mkp
Mkp10mo ago
Procedural is usually for scripting purposes, functional is more for math and data science
mtreit
mtreit10mo ago
Some of the more modern languages (Rust is a good example) don't really even have OOP support.
Mkp
Mkp10mo ago
Composition using interfaces makes me shutter when I think about partial classes and code generation In which you lug around a state object from method call to method call
mellowzippy
mellowzippyOP10mo ago
I feel pretty lost hearing this Does this mean that like rust has no classes? Or is OOP not just classes, inheritance, encapsulation etc?
mtreit
mtreit10mo ago
Rust has types. They can have state. But it doesn't really support inheritance hierarchies
mellowzippy
mellowzippyOP10mo ago
What is a state?
mtreit
mtreit10mo ago
Data associated with a type.
Mkp
Mkp10mo ago
OOP is mostly about stitching functions that work on state with the state itself (encapsulation) the fields and properties in a class
mtreit
mtreit10mo ago
Like if you have a file type, state associated with an instance of that type would be things like the file name, the file size, the security descriptor, etc.
mellowzippy
mellowzippyOP10mo ago
So a state is just a type with it's properties?
Mkp
Mkp10mo ago
new object(); <- state
mellowzippy
mellowzippyOP10mo ago
oh okay so functional is that you take the state and then adjust it and then OOP would be adjusting the state using methods within the class?
Mkp
Mkp10mo ago
In reverse, stateless would be without data
mellowzippy
mellowzippyOP10mo ago
I'm hearing a lot of big words
Mkp
Mkp10mo ago
'adjust it' is typically said as mutate Functional programming is where you take arrays of data and mutate it to a final result using functions
mtreit
mtreit10mo ago
That's not a good definition of functional programming.
Mkp
Mkp10mo ago
Functional programming is not a good definition of functional programming.
mellowzippy
mellowzippyOP10mo ago
public static void Functional(object something)
{
something.property += 5;
return something;
}
public static void Functional(object something)
{
something.property += 5;
return something;
}
From what im understanding this is functional programming Do you guys maybe have like any sources where I could find more about this so i can see it visually in code or something
mtreit
mtreit10mo ago
That's like the opposite of functional programming
Mkp
Mkp10mo ago
Side effects REEE
mellowzippy
mellowzippyOP10mo ago
😭
mtreit
mtreit10mo ago
Calling that function has the side effect of mutating a property of the input. Functional programming doesn't have (or generally avoids) side effects.
Mkp
Mkp10mo ago
Functional revolves around 'pure' functions in which there is an input and an output
mellowzippy
mellowzippyOP10mo ago
I mean this function has an input and output
mtreit
mtreit10mo ago
Often data is immutable when you follow a functional approach.
Mkp
Mkp10mo ago
its not pure, cuz it mutates state something.property += 5; is a mutation
mtreit
mtreit10mo ago
When you 'mutate' an object in a functional approach you return a copy (a new object) with the updated state.
mellowzippy
mellowzippyOP10mo ago
So then you'd copy the "something", mutate it return the new thing or ? ohh
Mkp
Mkp10mo ago
yup
mellowzippy
mellowzippyOP10mo ago
but why
Mkp
Mkp10mo ago
Because
mtreit
mtreit10mo ago
It eliminates entire classes of bugs. Like you don't have thread safety issues when objects are immutable. In some languages (Rust, F#) everything is immutable by default and you have to opt-in to mutability using things like the mut keyword.
Mkp
Mkp10mo ago
You still have thread safety issues, theyre just contained in a single area
mtreit
mtreit10mo ago
I wish C# was immutable by default but it was designed before that feature of functional programming kind of became popular in the industry (i.e., outside of academic circles.)
Mkp
Mkp10mo ago
immutable by default is a bad idea when youre oop
mtreit
mtreit10mo ago
Immutability also allows for performance optimizations.
mellowzippy
mellowzippyOP10mo ago
so
public static object Functional(object something)
{
object somethingElse = something.Clone(); // whatever method makes it copy
somethingElse.property += 5;
return somethingElse;
}
public static object Functional(object something)
{
object somethingElse = something.Clone(); // whatever method makes it copy
somethingElse.property += 5;
return somethingElse;
}
Is a better representation
Mkp
Mkp10mo ago
void
mellowzippy
mellowzippyOP10mo ago
yeah whatever you get the idea Why is this better
Mkp
Mkp10mo ago
missing semi colon when
mellowzippy
mellowzippyOP10mo ago
ok man think ur so funny but I just dont get how this is better than just changing the object that gets passed in the input you'd spend extra time cloning it then, no?
Mkp
Mkp10mo ago
Typically you arent really lugging around state like that change something to an integer
mtreit
mtreit10mo ago
If you want to deep dive on why this kind of thing is useful, Eric Lippert wrote a great series of articles on his blog about immutability.
Mkp
Mkp10mo ago
You should actually preach haskell mtreit
mellowzippy
mellowzippyOP10mo ago
Born to haskell, forced to C#
mtreit
mtreit10mo ago
I'm too dumb for Haskell
Mkp
Mkp10mo ago
its not better, its less efficient, as a trade off
mellowzippy
mellowzippyOP10mo ago
so the trade off is immutability/less bugs for less efficient code
Mkp
Mkp10mo ago
$itdepends
mtreit
mtreit10mo ago
It's not necessarily less efficient. In some cases immutability leads to performance improvements. Like if you know a type is immutable you can eliminate certain kinds of checks (bounds checks for instance.)
Mkp
Mkp10mo ago
C# leverages spans in this style
mtreit
mtreit10mo ago
Span is a good example
mellowzippy
mellowzippyOP10mo ago
what is span
Mkp
Mkp10mo ago
A view of an array
mtreit
mtreit10mo ago
In a nutshell, a pointer and a length Representing a slice (span) of some arbitrary memory. Not necessarily an array. They're very useful for doing high performance stuff when you're working with low level constructs like raw bytes.
mellowzippy
mellowzippyOP10mo ago
I don't know what this means
Mkp
Mkp10mo ago
They reduce the difficulty of passing around lengths as well Where in a low level language like C, you supply the length EVERYWHERE, or its in its own state in a struct
mtreit
mtreit10mo ago
To quote the article I linked:
System.Span<T> is a new value type at the heart of .NET. It enables the representation of contiguous regions of arbitrary memory, regardless of whether that memory is associated with a managed object, is provided by native code via interop, or is on the stack. And it does so while still providing safe access with performance characteristics like that of arrays.
mellowzippy
mellowzippyOP10mo ago
Yeah I'm reading it and its just words man I cant help it Im reading it like now but I don't get what it's for
mtreit
mtreit10mo ago
Stephen gives a bunch of examples
Mkp
Mkp10mo ago
'arbitrary memory' is technically true, but you will usually see span wrapping a subset of an array or list, more so ReadOnlySpans
mellowzippy
mellowzippyOP10mo ago
I dont know what arbitrary memory and arbitrary pointers are
mtreit
mtreit10mo ago
It's used with stackalloc quite a lot. At least that's one of my main uses of it.
Mkp
Mkp10mo ago
Think of memory as state
mtreit
mtreit10mo ago
Or NativeMemory.Alloc Do you know what a pointer is?
mellowzippy
mellowzippyOP10mo ago
Not really
Mkp
Mkp10mo ago
Pointers are indexes into memory
mellowzippy
mellowzippyOP10mo ago
I just know that it points somewhere and thats how you know where something is stored
Mkp
Mkp10mo ago
memory[n] where n is the pointer
mellowzippy
mellowzippyOP10mo ago
I haven't really been taught or have read into low-level things like pointers
mtreit
mtreit10mo ago
A pointer is a variable that holds an address to some type.
mellowzippy
mellowzippyOP10mo ago
Okok
mtreit
mtreit10mo ago
Every byte in your computer's memory has an address. (There's a physical address and a virtual address but that's probably diving a bit too deep...)
Mkp
Mkp10mo ago
(registers are not memory, registers are not memory)
mtreit
mtreit10mo ago
Registers are memory. Just a really fast form of memory that is directly part of the CPU.
Mkp
Mkp10mo ago
👍 Beware of non internal working ram
mellowzippy
mellowzippyOP10mo ago
So
int number = 9;
int number = 9;
Then somewhere on my pc theres a pointer from my variable number to 9?
Mkp
Mkp10mo ago
likely on the stack yes
mtreit
mtreit10mo ago
In that case no, the memory location referenced by 'number' directly contains the value 9. There is no pointer there. A variable is a "named memory location". In this case the name is 'number'
Mkp
Mkp10mo ago
mtreit you're confusing pointers with pointer variables
mellowzippy
mellowzippyOP10mo ago
so memory[number] = 9 ? no 9 isnt a pointer?
mtreit
mtreit10mo ago
It's not, no.
mellowzippy
mellowzippyOP10mo ago
OhNo
Mkp
Mkp10mo ago
9 can be a pointer if you want to handle a probable segfault
mellowzippy
mellowzippyOP10mo ago
so memory[number] = pointer => 9
Mkp
Mkp10mo ago
memory[address] = value where memory is type of unsigned byte and pointer is type of DWORD (64 bit number on a x64 machine)
mtreit
mtreit10mo ago
I would say memory[address] = value
Mkp
Mkp10mo ago
true
mellowzippy
mellowzippyOP10mo ago
okay so value = 9 what is the address?
Mkp
Mkp10mo ago
&value is the address Nobody knows Well I know its greater than 0x800000 you'd have to
unsafe
{
int value = 9;
int* ptr = &value;
Console.WriteLine(ptr);
}
unsafe
{
int value = 9;
int* ptr = &value;
Console.WriteLine(ptr);
}
To find it out Which changes every time you run the program
mellowzippy
mellowzippyOP10mo ago
is that C# ??
Mkp
Mkp10mo ago
yes
mellowzippy
mellowzippyOP10mo ago
madness
Mkp
Mkp10mo ago
My favorite feature
mtreit
mtreit10mo ago
value = 9 is a tricky example because the compiler could decide not to store that value in the computer's main memory (RAM) at all. It could use a CPU register for instance. Depending on what you then do with the value variable.
mellowzippy
mellowzippyOP10mo ago
oo What would be a better example
Mkp
Mkp10mo ago
Nothing The theory is there The intrinsics of the compiler are not important The result is the same irregardless mtreit does C# have an unsafe everywhere option
mtreit
mtreit10mo ago
It really helps to learn a little bit about how computers actually work down at the lower levels. You don't have to be writing code in assembly, but knowing the very basics of things like assembly language and computer memory and CPU registers and stack vs. heap and that kind of things goes a LONG way to understanding programming. Not that I'm aware of.
Mkp
Mkp10mo ago
ok thats enough of that
mtreit
mtreit10mo ago
@mellowzippy I have a couple of books I'd recommend if you want to learn more: https://codehiddenlanguage.com/ https://www.amazon.com/Secret-Life-Programs-Understand-Computers/dp/1593279701
“Code” by Charles Petzold
A few interactive circuits from “Code” 2nd edition
The Secret Life of Programs: Understand Computers -- Craft Better Code
The Secret Life of Programs: Understand Computers -- Craft Better Code
mellowzippy
mellowzippyOP10mo ago
Where are resources to learn that? I'm not learning it anywhere in the near future at school Oh okay Thank you
Mkp
Mkp10mo ago
The way I learned pointers is by suffering through coding the gameboy lol
mtreit
mtreit10mo ago
Notes to self
Fundamentals I wish you knew: bits and bytes. Part 1.
The scene: a programming interview The interview candidate is staring at me, frowning. Behind them is the whiteboard, where they have tentatively sketched out a rudimentary function prototype. They are trying to understand more about what exactly they need to do to implement the function, and it’s going poorly.
Notes to self
Fundamentals I wish you knew: bits and bytes. Part 2.
Here’s a file. It contains a series of integer values, stored as binary. I’d like you to do some processing of each value. How would you do it?
Mkp
Mkp10mo ago
Ah yes "the bits and the bytes" the coming of age tale Getting comfortable with binary data (with help from PowerShell) this scares me tho
mellowzippy
mellowzippyOP10mo ago
How would I read these books? Do I read a chapter and then do an exercise with it? Do I make notes? How do I make reading this worth my time instead of forgetting it after a week?
Mkp
Mkp10mo ago
read it twice one for reading one for comprehension In my experience learning programming is being exposed, then understanding
mtreit
mtreit10mo ago
I would just read through them and make notes on things you find interesting. Keep them on your bookshelf for reference.
mtreit
mtreit10mo ago
Hey that blog post has my proudest (and practically only) UI achievement: figuring out how to write the HTML for this table 😄
No description
Mkp
Mkp10mo ago
My experience learning spans is "What the fuck is a span" followed by "oh I can pass an array or list thru it" ending with "damn this saves a headache"
mellowzippy
mellowzippyOP10mo ago
Have you read them? Or do you recommend them because you've heard great things about them?
mtreit
mtreit10mo ago
I've read them
mellowzippy
mellowzippyOP10mo ago
okok
Mkp
Mkp10mo ago
^ in this style
mellowzippy
mellowzippyOP10mo ago
I'll read your blog posts too If you have anything else I should learn before touching my computer again please tell me :) I want to learn but I just didn't really know about this stuff until now
Mkp
Mkp10mo ago
No you should definitely touch your computer and study at the same time Concepts are nothing without implementation
mtreit
mtreit10mo ago
Don't try to learn everything at once. This stuff takes a lot of time to master. (And I use the term master very loosely, I've been doing this for many years and I am constantly learning new things every day)
Mkp
Mkp10mo ago
Which is again why paradigms like Singletons often go misused
mellowzippy
mellowzippyOP10mo ago
Sure but I would like to at least have a basic foundation of this so that I at least know about it How are they misused?
Mkp
Mkp10mo ago
Oh mtreit another thing, people will often try to learn singletons or factory patterns so they use it for the next thing they are working on until it works and that invariably gets pushed Messing with memory and pointers is a realtively optional task, I wouldn't really waste life on it early in your learning career. its a more advanced topic Knowledge about them is good though
mtreit
mtreit10mo ago
I'm entirely self taught. I read a lot of books. One of the most influential books I read was a somewhat obscure title called "The most complex machine" that taught how computers work from the ground up. The two books I linked earlier do much the same thing. Well worth reading to get sort of a foundation on what's going on under the hood. And yeah most of the time you don't need that low level knowledge. But when you do need it, it's super helpful.
mellowzippy
mellowzippyOP10mo ago
okok head hurts pointers bytes
Mkp
Mkp10mo ago
mtreit convince ImageSharp to give image loading a reasonable interface
mellowzippy
mellowzippyOP10mo ago
came for factory pattern
Mkp
Mkp10mo ago
gonna finish that phrase lol
mellowzippy
mellowzippyOP10mo ago
that was it sorry to dissapoint
Mkp
Mkp10mo ago
lol
mellowzippy
mellowzippyOP10mo ago
But I learned a lot at least
JBSP
JBSP9mo ago
What’s wrong with how it works now?
Mkp
Mkp9mo ago
Images you load have to be flipped
Want results from more Discord servers?
Add your server