C
C#5mo 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
MKP5mo 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
mtreit5mo 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
MKP5mo 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
mellowzippy5mo 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
MKP5mo ago
$patterns
mtreit
mtreit5mo ago
That's the general idea behind formalizing design patterns, yes.
mellowzippy
mellowzippy5mo ago
Do you recommend reading the book in that case?
mtreit
mtreit5mo ago
That said, I never read the design patterns book.
MKP
MKP5mo ago
Design patterns come after learning algorithms tbh
mtreit
mtreit5mo ago
And I don't generally think in terms of design patterns when I write software. Although I probably use those patterns organically.
mellowzippy
mellowzippy5mo ago
Does it come naturally then or is it not really something to keep in mind when programming? Ah okay
mtreit
mtreit5mo 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
MKP5mo ago
Naturally most of the time. Like my themes example is an actual deployment of singleton I used.
mtreit
mtreit5mo 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
mellowzippy5mo ago
brooo what lmfao Okok
MKP
MKP5mo ago
No description
MKP
MKP5mo ago
Wtf Reee line breaks
mtreit
mtreit5mo ago
In case it's not obvious, that's a parody of how programming in the enterprise world works.
MKP
MKP5mo ago
Going from reading C# to reading Java is so jarring
mellowzippy
mellowzippy5mo 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
mtreit5mo 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
mellowzippy5mo ago
Okay okay Thanks for the advice
MKP
MKP5mo ago
inb4 mtreit loses his job You're more likely to get a job knowing OOP and Algorithms than Design patterns
mellowzippy
mellowzippy5mo ago
Yeah I figured but I was just wondering if design patterns are like a golden standard
MKP
MKP5mo ago
Definitely not, and if you see people pushing them they're often a corporate shill
mellowzippy
mellowzippy5mo ago
What about principles? Does your code need to follow like every SOLID principle or is it the same story as design patterns?
mtreit
mtreit5mo ago
Same thing That's another thing that people who are way into OOP get all excited about.
mellowzippy
mellowzippy5mo ago
What is the alternative to OOP then?
mtreit
mtreit5mo ago
Procedural and functional approaches mostly Composition using interfaces Instead of inheritance
MKP
MKP5mo ago
Procedural is usually for scripting purposes, functional is more for math and data science
mtreit
mtreit5mo ago
Some of the more modern languages (Rust is a good example) don't really even have OOP support.
MKP
MKP5mo 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
mellowzippy5mo 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
mtreit5mo ago
Rust has types. They can have state. But it doesn't really support inheritance hierarchies
mellowzippy
mellowzippy5mo ago
What is a state?
mtreit
mtreit5mo ago
Data associated with a type.
MKP
MKP5mo ago
OOP is mostly about stitching functions that work on state with the state itself (encapsulation) the fields and properties in a class
mtreit
mtreit5mo 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
mellowzippy5mo ago
So a state is just a type with it's properties?
MKP
MKP5mo ago
new object(); <- state
mellowzippy
mellowzippy5mo 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
MKP5mo ago
In reverse, stateless would be without data
mellowzippy
mellowzippy5mo ago
I'm hearing a lot of big words
MKP
MKP5mo 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
mtreit5mo ago
That's not a good definition of functional programming.
MKP
MKP5mo ago
Functional programming is not a good definition of functional programming.
mellowzippy
mellowzippy5mo 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
mtreit5mo ago
That's like the opposite of functional programming
MKP
MKP5mo ago
Side effects REEE
mellowzippy
mellowzippy5mo ago
😭
mtreit
mtreit5mo 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
MKP5mo ago
Functional revolves around 'pure' functions in which there is an input and an output
mellowzippy
mellowzippy5mo ago
I mean this function has an input and output
mtreit
mtreit5mo ago
Often data is immutable when you follow a functional approach.
MKP
MKP5mo ago
its not pure, cuz it mutates state something.property += 5; is a mutation
mtreit
mtreit5mo ago
When you 'mutate' an object in a functional approach you return a copy (a new object) with the updated state.
mellowzippy
mellowzippy5mo ago
So then you'd copy the "something", mutate it return the new thing or ? ohh
MKP
MKP5mo ago
yup
mellowzippy
mellowzippy5mo ago
but why
MKP
MKP5mo ago
Because
mtreit
mtreit5mo 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
MKP5mo ago
You still have thread safety issues, theyre just contained in a single area
mtreit
mtreit5mo 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
MKP5mo ago
immutable by default is a bad idea when youre oop
mtreit
mtreit5mo ago
Immutability also allows for performance optimizations.
mellowzippy
mellowzippy5mo 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
MKP5mo ago
void
mellowzippy
mellowzippy5mo ago
yeah whatever you get the idea Why is this better
MKP
MKP5mo ago
missing semi colon when
mellowzippy
mellowzippy5mo 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
MKP5mo ago
Typically you arent really lugging around state like that change something to an integer
mtreit
mtreit5mo 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
MKP5mo ago
You should actually preach haskell mtreit
mellowzippy
mellowzippy5mo ago
Born to haskell, forced to C#
mtreit
mtreit5mo ago
I'm too dumb for Haskell
MKP
MKP5mo ago
its not better, its less efficient, as a trade off
mellowzippy
mellowzippy5mo ago
so the trade off is immutability/less bugs for less efficient code
MKP
MKP5mo ago
$itdepends
mtreit
mtreit5mo 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
MKP5mo ago
C# leverages spans in this style
mtreit
mtreit5mo ago
Span is a good example
mellowzippy
mellowzippy5mo ago
what is span
MKP
MKP5mo ago
A view of an array
mtreit
mtreit5mo 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
mellowzippy5mo ago
I don't know what this means
MKP
MKP5mo 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
mtreit5mo 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
mellowzippy5mo 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
mtreit5mo ago
Stephen gives a bunch of examples
MKP
MKP5mo ago
'arbitrary memory' is technically true, but you will usually see span wrapping a subset of an array or list, more so ReadOnlySpans
mellowzippy
mellowzippy5mo ago
I dont know what arbitrary memory and arbitrary pointers are
mtreit
mtreit5mo ago
It's used with stackalloc quite a lot. At least that's one of my main uses of it.
MKP
MKP5mo ago
Think of memory as state
mtreit
mtreit5mo ago
Or NativeMemory.Alloc Do you know what a pointer is?
mellowzippy
mellowzippy5mo ago
Not really
MKP
MKP5mo ago
Pointers are indexes into memory
mellowzippy
mellowzippy5mo ago
I just know that it points somewhere and thats how you know where something is stored
MKP
MKP5mo ago
memory[n] where n is the pointer
mellowzippy
mellowzippy5mo ago
I haven't really been taught or have read into low-level things like pointers
mtreit
mtreit5mo ago
A pointer is a variable that holds an address to some type.
mellowzippy
mellowzippy5mo ago
Okok
mtreit
mtreit5mo 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
MKP5mo ago
(registers are not memory, registers are not memory)
mtreit
mtreit5mo ago
Registers are memory. Just a really fast form of memory that is directly part of the CPU.
MKP
MKP5mo ago
👍 Beware of non internal working ram
mellowzippy
mellowzippy5mo ago
So
int number = 9;
int number = 9;
Then somewhere on my pc theres a pointer from my variable number to 9?
MKP
MKP5mo ago
likely on the stack yes
mtreit
mtreit5mo 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
MKP5mo ago
mtreit you're confusing pointers with pointer variables
mellowzippy
mellowzippy5mo ago
so memory[number] = 9 ? no 9 isnt a pointer?
mtreit
mtreit5mo ago
It's not, no.
mellowzippy
mellowzippy5mo ago
OhNo
MKP
MKP5mo ago
9 can be a pointer if you want to handle a probable segfault
mellowzippy
mellowzippy5mo ago
so memory[number] = pointer => 9
MKP
MKP5mo 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
mtreit5mo ago
I would say memory[address] = value
MKP
MKP5mo ago
true
mellowzippy
mellowzippy5mo ago
okay so value = 9 what is the address?
MKP
MKP5mo 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
mellowzippy5mo ago
is that C# ??
MKP
MKP5mo ago
yes
mellowzippy
mellowzippy5mo ago
madness
MKP
MKP5mo ago
My favorite feature
mtreit
mtreit5mo 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
mellowzippy5mo ago
oo What would be a better example
MKP
MKP5mo 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
mtreit5mo 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
MKP5mo ago
ok thats enough of that
mtreit
mtreit5mo 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
mellowzippy5mo ago
Where are resources to learn that? I'm not learning it anywhere in the near future at school Oh okay Thank you
MKP
MKP5mo ago
The way I learned pointers is by suffering through coding the gameboy lol
mtreit
mtreit5mo 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
MKP5mo 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
mellowzippy5mo 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
MKP5mo ago
read it twice one for reading one for comprehension In my experience learning programming is being exposed, then understanding
mtreit
mtreit5mo ago
I would just read through them and make notes on things you find interesting. Keep them on your bookshelf for reference.
mtreit
mtreit5mo 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
MKP5mo 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
mellowzippy5mo ago
Have you read them? Or do you recommend them because you've heard great things about them?
mtreit
mtreit5mo ago
I've read them
mellowzippy
mellowzippy5mo ago
okok
MKP
MKP5mo ago
^ in this style
mellowzippy
mellowzippy5mo 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
MKP5mo ago
No you should definitely touch your computer and study at the same time Concepts are nothing without implementation
mtreit
mtreit5mo 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
MKP5mo ago
Which is again why paradigms like Singletons often go misused
mellowzippy
mellowzippy5mo 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
MKP5mo 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
mtreit5mo 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
mellowzippy5mo ago
okok head hurts pointers bytes
MKP
MKP5mo ago
mtreit convince ImageSharp to give image loading a reasonable interface
mellowzippy
mellowzippy5mo ago
came for factory pattern
MKP
MKP5mo ago
gonna finish that phrase lol
mellowzippy
mellowzippy5mo ago
that was it sorry to dissapoint
MKP
MKP5mo ago
lol
mellowzippy
mellowzippy5mo ago
But I learned a lot at least
JBSP
JBSP5mo ago
What’s wrong with how it works now?
MKP
MKP5mo ago
Images you load have to be flipped