What is good practice
-
I have written a program the has numerous methods in form1.cs file and it has become somewhat difficult to manage because of its length. Is there a way to move some of these methods to a separate file? If so, what are the mechanics of linking the together? Or is there another approach to manage long programs?
JBD
-
I have written a program the has numerous methods in form1.cs file and it has become somewhat difficult to manage because of its length. Is there a way to move some of these methods to a separate file? If so, what are the mechanics of linking the together? Or is there another approach to manage long programs?
JBD
To me a good sign of "good practice" would be that someone else reading your code would have some clear ideas about its structure, and what it does, just by looking at the use of namespaces, classes, method names. variable names. This is a very broad question, and it may well relate to bigger-picture issues in object-oriented design (OOD). Do you know about SOLID, a coherent set of ideas for OOD [^] ? In WinForms, you can declare a Class as 'partial to get around the restriction on having a single Class definition across multiple files [^]. Other ways commonly used to group Methods, where the Method has no dependencies, include writing Extension Methods in a static Class [^]. Of course, you can also group Methods into a library, compile that to a .dll, load that into your project, and reference the assembly in your Class' code ... as long as those methods have no innate dependency on your app's run-time objects, declarations, etc.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
To me a good sign of "good practice" would be that someone else reading your code would have some clear ideas about its structure, and what it does, just by looking at the use of namespaces, classes, method names. variable names. This is a very broad question, and it may well relate to bigger-picture issues in object-oriented design (OOD). Do you know about SOLID, a coherent set of ideas for OOD [^] ? In WinForms, you can declare a Class as 'partial to get around the restriction on having a single Class definition across multiple files [^]. Other ways commonly used to group Methods, where the Method has no dependencies, include writing Extension Methods in a static Class [^]. Of course, you can also group Methods into a library, compile that to a .dll, load that into your project, and reference the assembly in your Class' code ... as long as those methods have no innate dependency on your app's run-time objects, declarations, etc.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
just a quick correction: It's not WinForms that defines partial classes, but the languages of C# and VB do. You don't need a WinForms project to make use of the feature.
Josh Davis
This is what plays in my head when I finish projects. -
just a quick correction: It's not WinForms that defines partial classes, but the languages of C# and VB do. You don't need a WinForms project to make use of the feature.
Josh Davis
This is what plays in my head when I finish projects.Thanks, Josh !
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
I have written a program the has numerous methods in form1.cs file and it has become somewhat difficult to manage because of its length. Is there a way to move some of these methods to a separate file? If so, what are the mechanics of linking the together? Or is there another approach to manage long programs?
JBD
When someone starts programming, the first concepts he's learning are "procedural" concepts, i.e. how to control the program flow (if...else, for, do...while etc). Another concept is "object orientation". Now you have to think of "encapsulation of data" (how to access them, and more important how to change them safely), and eventually of components and their interactions. Bill mentioned SOLID: those are 5 ways for creating components and having them interact. "S" stands for "single responsibility" and is a good point to start with: a class takes care of one thing only, a function in that class of one action only. This change in paradigm from mere procedural code to object oriented code is a big step.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!