Jump to content

Programing Practice

Windows7ge
1 hour ago, Windows7ge said:

I don't think VB is even capable of this.

Yes there's equivalence:

Module Module1
    Private Const SomePrivateConstant = "SomePrivateConstant"

    Sub Main()
        ScopeTest()
        Console.ReadKey()
    End Sub

    Private Sub ScopeTest()
        Console.WriteLine(SomePrivateConstant)
    End Sub
End Module

See Constants Overview (Visual Basic).

1 hour ago, Windows7ge said:

I'm describing the reason for it's existence rather than what the line itself means. Since I don't have any serious plans for the distribution of my programs I kind of have the habit of writing comments in a way that explains to myself what it does because my memory is rather fickle and if I don't write a program at least 2 or 3 weeks apart from each other I can't remember how to do it the next time I want to. Then I can reference past projects and the comments refresh my memory.

Sure then, in this sort of scenario I'll buy that reasoning.

1 hour ago, Windows7ge said:

When I began construction I tried to think of word based names to represent the objects but the names I thought of would have become hard to distinguish from each other and redundant. I know using numbers isn't something I should use but again the mental block I mentioned a while ago, creating names isn't one of my strong suits. It's easy enough when I don't have duplicate buttons that do the exact same thing and in this is where there's more room for improvement.

I understand. It will eventually come with practice, time and experience. Naming things is one of the hardest parts of building software and often those names will end up getting revised and changed a great many times throughout the process... It does seem as though you have a predisposition towards instinctively knowing when something is starting to feel wrong however, and that is a very good quality to have indeed. 

1 hour ago, Windows7ge said:

I do see how it might be possible to use private voids within the tab menu and based on which tab the user is on it could change what the button controls do. This could reduce the amount of code, reduce repetitive buttons, and make the naming scheme easier.

Methods would work but this may prove messy when it comes to the calculation itself. As I mentioned, a better approach may be to consider a Custom User Control, passing whatever setup you may need along with a calculation strategy following the Strategy Design Pattern (which is particularly well suited to calculations). This 'setup' can take the form of a formal model, which simply distils to an aggregation of those aforementioned concerns, as an interface.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, Nuluvius said:

Yes there's equivalence:

I mean't without the use of a module, changing the constants protection level to public, or having to create a dedicated private sub on the main code page. In this regard I can see how C# is already one step better than VB. It has the same level of protection, does the same exact thing, and only uses one line of code.

 

22 hours ago, Nuluvius said:

Methods would work but this may prove messy when it comes to the calculation itself

This is something else I had considered. I imagine it would require multiple variables to pull off and even then it would probably radically increase the complexity of the program for something that should otherwise be simple-ish.

 

If I designed something completely custom my other idea (that I never mentioned) was to use 3 radio buttons inside of a group control. Each radio button could represent a unit of power and could directly control the input boxes and what the calculate button executes. This would also get rid of the need for a tab menu and I think would be a little easier to work with. There would be less objects on the form. However this would require a near complete overhaul of the form and code behind it though if I ever feel like building this again for the third time it would be an educational opportunity.

 

As you mentioned earlier I was still writing what appeared to be repetitive lines. I'll fix those probably tomorrow

 

(Tomorrow) I forgot to click Submit Reply yesterday.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Windows7ge said:

I mean't without the use of a module, changing the constants protection level to public, or having to create a dedicated private sub on the main code page. In this regard I can see how C# is already one step better than VB. It has the same level of protection, does the same exact thing, and only uses one line of code.

I'm not really sure what you mean by 'main code page'. All of the rules on scoping and visibility are essentially the same.

2 hours ago, Windows7ge said:

If I designed something completely custom my other idea (that I never mentioned) was to use 3 radio buttons inside of a group control. Each radio button could represent a unit of power and could directly control the input boxes and what the calculate button executes. This would also get rid of the need for a tab menu and I think would be a little easier to work with. There would be less objects on the form. However this would require a near complete overhaul of the form and code behind it though if I ever feel like building this again for the third time it would be an educational opportunity.

It sounds reasonable, this way you wont event need other User Controls. I suspect that you could still strategize the calculation to make things a bit more concise.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Nuluvius said:

I'm not really sure what you mean by 'main code page'. All of the rules on scoping and visibility are essentially the same.

I just mean the way VS displays the code to you. Imagine a web browser. A module fine would be it's own tab with all of it's code separated from the primary methods. There's also a [Design] page which contains all of the properties for the form that you're looking at but I rarely ever do anything in there. The VS GUI is designed to modefy this page using the form and object properties so you don't have to go in there. Then there's the "tab" which contains all of your object controls which is where you alter what objects do which is what we've been working on the past 3 weeks. This is how VS is set up.

 

As for if the constant could be made private in VB and work like it does in c# may just be my lack of knowledge but from what I do know it would not work unless it was in a module. If I wanted the constant written along side all of my methods it would have to be public for each method to see it. Inside of a module it could be private but would still visible to each method in the form. You said yourself the program isn't complex enough to make a module worth using.

3 hours ago, Nuluvius said:

It sounds reasonable, this way you wont event need other User Controls. I suspect that you could still strategize the calculation to make things a bit more concise.

Most definitely. The more I look back on the project and the things you've pointed out to me I can see places where the program could use improvement. So far this rebuild is significantly improved over my first attempt.

 

I have a similar project in mind where I could incorporate radio buttons instead of using a tab menu. I can use that opportunity to see if it is something that I can make work. Considering the amount of similarity it would have to what you've taught me I could potentially built it without assistance.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Windows7ge said:

I just mean the way VS displays the code to you. Imagine a web browser. A module fine would be it's own tab with all of it's code separated from the primary methods. There's also a [Design] page which contains all of the properties for the form that you're looking at but I rarely ever do anything in there. The VS GUI is designed to modefy this page using the form and object properties so you don't have to go in there. Then there's the "tab" which contains all of your object controls which is where you alter what objects do which is what we've been working on the past 3 weeks. This is how VS is set up.

Indeed, VS can view a UI Control in it's designer view or it can view 'the code' or it can view the 'designer code'. Essentially a UI Control is nothing more than a class with some special characteristics. You seem to have already discovered the Form.Designer.cs (where the designer generated code lives) and the Form.cs (where your code lives). Notice that these are Partial Classes that conform to a templated structure that VS understands how to deal with in a particular way. It's entirely possible to have a project where all UI is instantiated and even generated at runtime i.e. no UI Control files present at all.

 

As a side note, it's worth mentioning that 'Partial Classes' are considered bad practice because they can violate the single responsibility principle and can also break encapsulation. There's only a very small number of acceptable uses for them. In general however, one should always try to avoid them.

11 hours ago, Windows7ge said:

As for if the constant could be made private in VB and work like it does in c# may just be my lack of knowledge but from what I do know it would not work unless it was in a module. If I wanted the constant written along side all of my methods it would have to be public for each method to see it. Inside of a module it could be private but would still visible to each method in the form. You said yourself the program isn't complex enough to make a module worth using.

Yes, if it's private then it's only going to be visible inside the class where it has been declared. If it were public however, then by prefixing it with the name of the class it can be accessed from anywhere. Remember that I mentioned that there must be a very good reason for a constant to exist, this is even more so the case if one is intending to be making it public i.e. thereby potentially exposing an implementation detail to the outside world.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Nuluvius said:

Remember that I mentioned that there must be a very good reason for a constant to exist

For my petty applications it only seems good for resetting some form of counter. Or perhaps if I have an input box with a default dialog (string) inside of it.

Moreover it seems to serve a useful application as a substitute for certain object properties. We discussed before using:

someInputBox.Text = String.Empty()
// as oppose to something like
someInputBox.Text = ""

In the end both would clear the input box but String.Empty is designed for this kind of application while " " is like assigning another string to it just with nothing in it. Not proper.

Since it wouldn't make sense for VS to include a special property option that you could alter which would control what is in the control either during launch or if a specific button is pressed then a constant can be used to change that controls value if the intention is for this value to never be changed when said button is pressed...if that explanation makes any sense.

3 hours ago, Nuluvius said:

this is even more so the case if one is intending to be making it public i.e. thereby potentially exposing an implementation detail to the outside world.

For example if it were public and someone wanted to, oh lets say brute force attack the application (I believe this is when someone throws random code at an application and see's where the application cracks/breaks) then it would be possible for an attacker to alter the constants value because it was made public. Or at least it would be much easier as oppose to if it were private.

 

I've updated the application with your suggestions. I didn't think the code that cleared the fields could be made any shorter:

namespace ConvertVoltsWattsAmps
{
    public partial class ConvertVoltsWattsAmps : Form
    {
        private const decimal ValueDefault = 0.001m;

        public ConvertVoltsWattsAmps()
        {
            InitializeComponent();
        }

        private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => division ? powerInput1 / powerInput2 : powerInput1 * powerInput2;

        private static void Reset(NumericUpDown resetInput1, NumericUpDown resetInput2, Control resetOutput)
        {
            resetInput1.Value = ValueDefault;
            resetInput2.Value = ValueDefault;
            resetOutput.Text = string.Empty;
        }

        private void btnCalculate1_Click(object sender, EventArgs e)
        { 
            var watts = Convert.ToDecimal(nudWatts1.Value);
            var amps = Convert.ToDecimal(nudAmps1.Value);

            lblOutputVolts.Text = PowerResult(watts, amps, true).ToString();
        }

        private void btnClear1_Click(object sender, EventArgs e)
        {
            Reset(nudWatts1, nudAmps1, lblOutputVolts);
        }

        private void btnCalculate2_Click(object sender, EventArgs e)
        {
            var volts = Convert.ToDecimal(nudVolts2.Value);
            var amps = Convert.ToDecimal(nudAmps2.Value);

            lblOutputWatts.Text = PowerResult(volts, amps, false).ToString();
        }

        private void btnClear2_Click(object sender, EventArgs e)
        {
            Reset(nudVolts2, nudAmps2, lblOutputWatts);
        }

        private void btnCalculate3_Click(object sender, EventArgs e)
        {
            var watts = Convert.ToDecimal(nudWatts3.Value);
            var volts = Convert.ToDecimal(nudVolts3.Value);

            lblOutputAmps.Text = PowerResult(watts, volts, true).ToString();
        }

        private void btnClear3_Click(object sender, EventArgs e)
        {
            Reset(nudVolts3, nudWatts3, lblOutputAmps);
        }
    }
}

Out of curiosity since the reset of the inputs have been compressed down to one line would it be possible to write it similarly to the function associated with the methods? If that didn't make sense here's a written example:

// You showed me that it was possible to compress the programs primary function down to one line like this:
private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => division ? powerInput1 / powerInput2 : powerInput1 * powerInput2;
// For each Reset button instead of having written:
private void btnClear1_Click(object sender, EventArgs e)
        {
            Reset(nudWatts1, nudAmps1, lblOutputVolts);
        }
// Would something along the lines of this be possible?:
private void btnClear1_Click(object sender, EventArgs e) = Reset(nudWatts1, nudAmps1, lblOutputVolts);
// Probably wrote it wrong but I think you should get where I'm going with this. I'm thinking it might be worth doing if possible since the method only has one line of code. This IS a method correct? Otherwise I've been calling it the wrong thing for the past few days.

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Windows7ge said:

In the end both would clear the input box but String.Empty is designed for this kind of application while " " is like assigning another string to it just with nothing in it. Not proper.

Remember what I said in the case of a magic literal such as '""'; you are dispersing the implementation detail throughout the code, if one had to change it at any point then one would have a lot of searching, thinking and copy pasting to do. Whereas with a constant there's only a single place to change.

2 hours ago, Windows7ge said:

Since it wouldn't make sense for VS to include a special property option that you could alter which would control what is in the control either during launch or if a specific button is pressed then a constant can be used to change that controls value if the intention is for this value to never be changed when said button is pressed...if that explanation makes any sense.

Not really clear. Are you meaning some kind of default state that a control may return to?

2 hours ago, Windows7ge said:

For example if it were public and someone wanted to, oh lets say brute force attack the application (I believe this is when someone throws random code at an application and see's where the application cracks/breaks) then it would be possible for an attacker to alter the constants value because it was made public. Or at least it would be much easier as oppose to if it were private.

Security in that particular sense was not really what I was meaning. It's more about information hiding and encapsulation of implementation details. In other words hiding how something has been done away behind some kind of abstraction and then coupling to that abstraction instead of the concrete implementation. This is all going back to the theory of Software Engineering again. I can throw a lot of stuff at you to read on these sorts of things...

2 hours ago, Windows7ge said:

Out of curiosity since the reset of the inputs have been compressed down to one line would it be possible to write it similarly to the function associated with the methods?

It most certainly can and in this case should.

// Would something along the lines of this be possible?:
private void btnClear1_Click(object sender, EventArgs e) = Reset(nudWatts1, nudAmps1, lblOutputVolts);
// Probably wrote it wrong but I think you should get where I'm going with this. I'm thinking it might be worth doing if possible since the method only has one line of code. This IS a method correct? Otherwise I've been calling it the wrong thing for the past few days.

Have a read on the => Operator. And to address the question on methods; the general term for all of these is really a subroutine, whereas a method is usually used to refer to a general subroutine within the context of an object. Specifically functions are methods that return something and procedures are methods that do not (those are your 'voids'). Some languages allow the use of 'fee functions'.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Nuluvius said:

Whereas with a constant there's only a single place to change.

That's what you meant. That's understandable. I've thought of posting the applications on some sort of open source platform just so someone can find a use for them because I won't always need them. Then they can modify it as much as they like provided they can read the code which is kind of the whole point you're making.

 

25 minutes ago, Nuluvius said:

Not really clear. Are you meaning some kind of default state that a control may return to?

Yes, like in my instance I have the numericUpDown boxes return to 0.001 when the Clear button is pressed which I would refer to as their "Default Value". Resetting them using a string or saying the property value is equal to that of a another property may make the program harder to read & edit. Therefor creating a constant which can represent a value which you never want to have change can then be used to reset the value of each of the boxes. Makes the code easier to read, edit, and seems like a good reason for the constants existence.

 

I would wonder if a constant is worth using if you only have a single box to reset. Or perhaps multiple boxes but each one requires a different value. To use constants would mean having multiple constants to reset each value. I don't know if this would be good or bad.

 

36 minutes ago, Nuluvius said:

I can throw a lot of stuff at you to read on these sorts of things...

At the moment I think I have enough homework reading everything else you've "thrown" at me. For the time being it should be understood to make everything private if it can be helped. If it can't then it is probably a design issue. It also makes sense to me that if something is made public in an application that not only can the public information be seen by everything within the application but also the system the application is running on. I presume this has the potential to cause system issues if the application became complex enough.

 

46 minutes ago, Nuluvius said:

Have a read on the => Operator.

Alright, I'll read up on it.

 

47 minutes ago, Nuluvius said:

the general term for all of these is really a subroutine, whereas a method is usually used to refer to a general subroutine within the context of an object.

So a method is really used to describe a piece of code that is used to perform a distinctive action or operation like completing a math problem or changing the property value of a object on a form. While a subroutine could be used to describe a void which is perhaps doing some type of conversion or assigning a value to a variable.

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Windows7ge said:

Then they can modify it as much as they like provided they can read the code which is kind of the whole point you're making.

Well the principal of a constant is still essentially the same, it shouldn't be changed but if it really had to be then it saves someone from searching through for all of those literals and having to unravel potentially complicated logic to understand if a specific literal really had to change or not.

 

If you are providing something that has configuration settings that may change then surface those in the form of a formal configuration file. Load these in at run time and potentially make them readonly or use some form of Inversion of Control and Dependency Injection.

 

The point is that the intention is made explicitly clear. The added advantage of a configuration file is that it provides another separation of concerns i.e. you now have a place to couple a set of instructions to.

10 hours ago, Windows7ge said:

Yes, like in my instance I have the numericUpDown boxes return to 0.001 when the Clear button is pressed which I would refer to as their "Default Value". Resetting them using a string or saying the property value is equal to that of a another property may make the program harder to read & edit. Therefor creating a constant which can represent a value which you never want to have change can then be used to reset the value of each of the boxes. Makes the code easier to read, edit, and seems like a good reason for the constants existence.

 

I would wonder if a constant is worth using if you only have a single box to reset. Or perhaps multiple boxes but each one requires a different value. To use constants would mean having multiple constants to reset each value. I don't know if this would be good or bad.

In this sort of scenario it may be reasonable to derive your own control. You may then offer the facility to configure it in whatever way seems appropriate i.e. by passing in this 'default value' or by offering properties. Thus you would have a control that encapsulates your intention.

 

This is exactly what I was saying here:

On 14/08/2017 at 10:43 AM, Nuluvius said:

It may be possible to have a single Form and a single User Control; the User Control could consist of your two inputs and your output and could be instantiated taking whatever names you want to set them up to be. Your Form could then be responsible for instantiating that User Control a number of times and also passing over a calculation strategy. You can host that User Control however you like within your Form

The only difference being that here I was talking about aggregating a number of individuals controls together as one entity.

11 hours ago, Windows7ge said:

It also makes sense to me that if something is made public in an application that not only can the public information be seen by everything within the application but also the system the application is running on. I presume this has the potential to cause system issues if the application became complex enough.

The first part of that is correct but the second part is not. These applications can be decompiled and their contents viewed very easily using readily available tools such as ILSpy and dotPeek for example. Try it out yourself. If it's security that you're concerned about then one solution is obfuscation.

11 hours ago, Windows7ge said:

So a method is really used to describe a piece of code that is used to perform a distinctive action or operation like completing a math problem or changing the property value of a object on a form. While a subroutine could be used to describe a void which is perhaps doing some type of conversion or assigning a value to a variable.

Think of it as a subroutine is the main overarching term, a method is the overarching term for when it's in the context of some object and the specific variants are either a function or a procedure. Free function is another specific overarching term for when they are 'floating about' outside of a specific object's context.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Nuluvius said:

The first part of that is correct but the second part is not.

Bittersweet. I'm surprised you haven't told me I'm wrong more often.

 

11 hours ago, Nuluvius said:

These applications can be decompiled and their contents viewed very easily using readily available tools

I was wondering if there was such a thing as a decompiler. My applications aren't significant enough for me to want to claim the code as my own. If I share the applications I plan to share the code too. However it's worth knowing that I can find tools to make the applications very difficult to read and that there are applications that can decompile applications. In the mean time I'm only writing applications that perform simple math calculations which are not worth protecting.

 

I would find my applications to have a little bit more value once I learn how to use While loops, For loops, & Do loops in C#

 

So I read up on the => operator. Good news is I was actually really close with how I wrote it in my example. Bad news is the web page that explains it is using a lot of programming terms that I still don't understand so I still don't know how it works but it does...something about overloading...I have no idea. I believe it mentioned LINQ. You were having me read on that a few weeks ago.

 

Here's my code now. Because the individual lines actually became hard to spot in between the subroutines I relocated them. I think the code is easier to read this way:

namespace ConvertVoltsWattsAmps
{
    public partial class ConvertVoltsWattsAmps : Form
    {
        private const decimal ValueDefault = 0.001m;

        public ConvertVoltsWattsAmps()
        {
            InitializeComponent();
        }
        
        private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => division ? powerInput1 / powerInput2 : powerInput1 * powerInput2;

        private static void Reset(NumericUpDown resetInput1, NumericUpDown resetInput2, Control resetOutput) 
        {
            resetInput1.Value = ValueDefault;
            resetInput2.Value = ValueDefault;
            resetOutput.Text = string.Empty;
        }

        private void btnClear1_Click(object sender, EventArgs e) => Reset(nudWatts1, nudAmps1, lblOutputVolts);
        private void btnClear2_Click(object sender, EventArgs e) => Reset(nudVolts2, nudAmps2, lblOutputWatts); 
        private void btnClear3_Click(object sender, EventArgs e) => Reset(nudVolts3, nudWatts3, lblOutputAmps);

        private void btnCalculate1_Click(object sender, EventArgs e)
        { 
            var watts = Convert.ToDecimal(nudWatts1.Value);
            var amps = Convert.ToDecimal(nudAmps1.Value);

            lblOutputVolts.Text = PowerResult(watts, amps, true).ToString();
        }

        private void btnCalculate2_Click(object sender, EventArgs e)
        {
            var volts = Convert.ToDecimal(nudVolts2.Value);
            var amps = Convert.ToDecimal(nudAmps2.Value);

            lblOutputWatts.Text = PowerResult(volts, amps, false).ToString();
        }

        private void btnCalculate3_Click(object sender, EventArgs e)
        {
            var watts = Convert.ToDecimal(nudWatts3.Value);
            var volts = Convert.ToDecimal(nudVolts3.Value);

            lblOutputAmps.Text = PowerResult(watts, volts, true).ToString();
        }      
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Windows7ge said:

Bittersweet. I'm surprised you haven't told me I'm wrong more often.

:P

1 hour ago, Windows7ge said:

I would find my applications to have a little bit more value once I learn how to use While loops, For loops, & Do loops in C#

You could have a read of LoopsParallel Loops, break and continue.

1 hour ago, Windows7ge said:

So I read up on the => operator. Good news is I was actually really close with how I wrote it in my example. Bad news is the web page that explains it is using a lot of programming terms that I still don't understand so I still don't know how it works but it does...something about overloading...I have no idea. I believe it mentioned LINQ. You were having me read on that a few weeks ago.

Quintessentially all it does is denote that what follows is the expression body.

public partial class ConvertVoltsWattsAmps : Form
{
  private const decimal ValueDefault = 0.001m;

  public ConvertVoltsWattsAmps()
  {
    InitializeComponent();
  }

  private static void Reset(NumericUpDown resetInput1, NumericUpDown resetInput2, Control resetOutput) 
  {
    resetInput1.Value = ValueDefault;
    resetInput2.Value = ValueDefault;
    resetOutput.Text = string.Empty;
  }

  private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => division ? powerInput1 / powerInput2 : powerInput1 * powerInput2;

  private void btnClear1_Click(object sender, EventArgs e) => Reset(nudWatts1, nudAmps1, lblOutputVolts);

  private void btnClear2_Click(object sender, EventArgs e) => Reset(nudVolts2, nudAmps2, lblOutputWatts); 

  private void btnClear3_Click(object sender, EventArgs e) => Reset(nudVolts3, nudWatts3, lblOutputAmps);

  ...

I'd probably be inclined to group all similar shaped methods together in this particular case.

 

Consider the following:

private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division) => division ? powerInput1 / powerInput2 : powerInput1 * powerInput2;
private void btnCalculate1_Click(object sender, EventArgs e)
{
  var watts = Convert.ToDecimal(nudWatts1.Value);
  var amps = Convert.ToDecimal(nudAmps1.Value);

  lblOutputVolts.Text = PowerResult(watts, amps, true).ToString();
}

...

Now examine the type of NumericUpDown.Value and then have a think about those conversions. I think that you may be able to refactor to the following:

private static string PowerResult(decimal powerInput1, decimal powerInput2, bool division) => (division ? powerInput1 / powerInput2 : powerInput1 * powerInput2).ToString();
private void btnCalculate1_Click(object sender, EventArgs e) => lblOutputVolts.Text = PowerResult(nudWatts1.Value, nudAmps1.Value, true);

...

Which seems a lot more concise.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, Nuluvius said:

You could have a read of LoopsParallel Loops, break and continue.

I'll definitely read into them. Some programs I've written before and programs I may write in the future rely on loops.

 

43 minutes ago, Nuluvius said:

Which seems a lot more concise.

Every time I think the code can't get any shorter you show me another way to do just that. I'll test it out, I trust your judgment over my own but I won't get my hopes up. Convert everything to a string when it goes into the function so I don't have to write it 3 times and see if I can get away without the variables and the convert to decimals. Just send the data from the form objects strait into the function. I don't know if the program will like it but it's worth a shot.

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, edward30 said:

Just be aware that shorter isn't always better. Fewer characters does not necessarily mean something is faster. And remember readability. Even if your code is just for yourself, try to think about your work as if you were writing it for someone else to read. When you come back to it in a years time or more, with no memory of your implementation, you yourself will be that "someone else".

I agree. I see two major things that want to be accounted for when writing an application:

Efficiency & Clarity.

 

If the application is as efficient as it can get then shorter is doing nothing but changing the shape of what's essential to make the program work. If shorter makes it harder to read but making it appear longer without hurting the efficiency makes it clearer then the latter may be the better option.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, edward30 said:

If you're interested in programming exercises, as the title of this thread would suggest, check out www.leetcode.com. Sorry if this has been mentioned already.

I cannot say it has. Programming isn't a core interest of mine but I'm fascinated with the concept of being able to create any application for any specific task. Most of my interest is derived from having tedious tasks or tasks that are repetitive in nature. Well, I can write an application to do it for me. I love that idea so I want to have at least a basic level of programming skills.

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/17/2017 at 2:54 PM, Nuluvius said:

Which seems a lot more concise.

Sorry I kind of disappeared for a bit. Continuing with the lesson.

private static decimal PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => (division ? powerInput1 / powerInput2 : powerInput1 * powerInput2).ToString();

gave me the error "Cannot convert type string to decimal"

private void btnCalculate1_Click(object sender, EventArgs e) => lblOutputVolts.Text = PowerResult(nudWatts1.Value, nudAmps1.Value, true);

gave me the error "Cannot convert type decimal to string"

 

I'm left to assume the code cannot perform a direct conversion without some form of written instruction.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Windows7ge said:

gave me the error "Cannot convert type decimal to string"

 

I'm left to assume the code cannot perform a direct conversion without some form of written instruction.

Change the return type of the function to string.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, Nuluvius said:

Change the return type of the function to string.

That solved the issue immediately.

 

This is where I'm at now:

namespace ConvertVoltsWattsAmps
{
    public partial class ConvertVoltsWattsAmps : Form
    {
        private const decimal ValueDefault = 0.001m;

        public ConvertVoltsWattsAmps()
        {
            InitializeComponent();
        }

        private static void Reset(NumericUpDown resetInput1, NumericUpDown resetInput2, Control resetOutput)
        {
            resetInput1.Value = ValueDefault;
            resetInput2.Value = ValueDefault;
            resetOutput.Text = string.Empty;
        }
        
        private static string PowerResult(decimal powerInput1, decimal powerInput2, bool division = true) => (division ? powerInput1 / powerInput2 : powerInput1 * powerInput2).ToString();

        private void btnClear1_Click(object sender, EventArgs e) => Reset(nudWatts1, nudAmps1, lblOutputVolts);

        private void btnClear2_Click(object sender, EventArgs e) => Reset(nudVolts2, nudAmps2, lblOutputWatts); 

        private void btnClear3_Click(object sender, EventArgs e) => Reset(nudVolts3, nudWatts3, lblOutputAmps);

        private void btnCalculate1_Click(object sender, EventArgs e) => lblOutputVolts.Text = PowerResult(nudWatts1.Value, nudAmps1.Value, true);

        private void btnCalculate2_Click(object sender, EventArgs e) => lblOutputWatts.Text = PowerResult(nudVolts2.Value, nudAmps2.Value, false);

        private void btnCalculate3_Click(object sender, EventArgs e) => lblOutputAmps.Text = PowerResult(nudWatts3.Value, nudVolts3.Value, true);     
    }
}

Constant is at the top, functions, and subroutine controls are all grouped together nicely. There's little if any repetition and it's only 24 lines. I never imagined the code behind this application could be so compressed. I think it looks amazing. It's very clear to me and looks quite easy to understand with just a little bit of analysis.

 

Another application I have in mind is something that would work like a dictionary for commands. Cisco switches & routers are managed through a terminal and you need special commands to make the unit do what you want. I've had thoughts to write a detailed application that could be used to easily look up a particular command with every argument and option which is available pertaining to it. This could drastically increase the ease of use when trying to achieve a particular configuration. This is a application I may see worth obscuring the code deliberately because the amount of work going into it would make me want to claim it as my own without others directly copying it. (Though they could make their own from scratch that looks like it)

Link to comment
Share on other sites

Link to post
Share on other sites

@Nuluvius Thanks for putting so much time into helping me your assistance was instrumental. Should I have questions writing applications on my own in the future I hope you don't mind me consulting you if I cannot find an answer on my own.

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Windows7ge said:

Thanks for putting so much time into helping me your assistance was instrumental. Should I have questions writing applications on my own in the future I hope you don't mind me consulting you if I cannot find an answer on my own.

That's alright, I'm happy that you found things useful. We will all be happy to help out anytime that you may need I'm quite sure xD

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×