Jump to content

Programing Practice

Windows7ge
5 hours ago, Windows7ge said:

According to Nuluvius even my VB code isn't well written. If I convert to C# out of habit I'd still write my programs similarly.

 

The only solution I have to that is to post the code for a program on here that I wrote and ask for criticism. If there's one thing I know the forum is good for it's receiving criticism but so long as it's constructive and intended to help then I won't mind.

It's not my intention to shred you :P I'm just evangelistic of good practice and design.

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

8 hours ago, ElfFriend said:

I'd also suggest looking at design patterns and the like but if you're just writing code for yourself and without the intention of modifying it in a few years, it's quality doesn't really matter that much.

 

Anyway here's a good resource for learning about code quality. It's the course website for the software engineering class I took during winter. https://hcitang.github.io/seng301/?_escaped_fragment_=a1.md#!index.md

(Hopefully Tony's fine with me linking to it. After all it can be found via google, although the search terms have to be rather specific. Anyway Tony's an awesome prof so I really can't see why he wouldn't be fine with his notes being used for "the greater good")

The programs that I've made thus far are too small to really require any kind of overhaul in their redesign. Most changes I make post production are either bug fixes or small add-on's to the application in terms of features. In terms of code that the program actually uses to process information most of my programs are only around 100 or 200 lines in length. I'm not making large complicated applications. Mostly little stuff for repetitive tedious tasks. I'm open to writing applications for people if I know how to write what they want but I'd do it for free. I don't know nearly enough to want to charge money.

 

Also I'll look into your prof notes. I just hope they don't go over my head.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Nuluvius said:

It's not my intention to shred you :P I'm just evangelistic of good practice and design.

I understand. It makes sense that you want your code to be as legible to others as possible and to also be efficient. As the program grows in size with more features and higher resource requirements you want the program to be more efficient so it can run on as many differently built systems as possible. A poorly written program may work but it may be difficult for others to read and also be more work for the system hardware than it needs to be.

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/25/2017 at 0:06 PM, Windows7ge said:

If anybody thought TL:DR:

I'm looking for ideas of small programs I can write to practice Visual Basic. It's just for fun and the tools (programs) can be useful from time to time but I've run out of my own ideas.

If you don't feel like writing programs, you can always use your VB knowledge to write Word and Excel Macros!

That said, since when was VB considered unpopular? Doesn't Facebook use it for their apps/games?

CPU - Ryzen 7 3700X | RAM - 64 GB DDR4 3200MHz | GPU - Nvidia GTX 1660 ti | MOBO -  MSI B550 Gaming Plus

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, PocketNerd said:

If you don't feel like writing programs, you can always use your VB knowledge to write Word and Excel Macros!

That said, since when was VB considered unpopular? Doesn't Facebook use it for their apps/games?

From what I've heard among the programming community it's a lesser language. I was told it was good when it first came out but since then better languages have been released.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, PocketNerd said:

If you don't feel like writing programs, you can always use your VB knowledge to write Word and Excel Macros!

That said, since when was VB considered unpopular? Doesn't Facebook use it for their apps/games?

No. Facebook apps are either Java, Flash, or some other web app language/frame work. VB does not have a web app framework (as far as I know).

 

VB also wasn't exactly mega popular to begin with, but if I have two main reasons why I don't like it: it's verbose (every variable must have a dim before it for no reason other than tradition) and it's case insensitive.

Link to comment
Share on other sites

Link to post
Share on other sites

Some beginner programming ideas:

 

allow the user user to input 5 numbers, and then order them in an increasing order, and then show (print) the numbers again in ascending order. 

 

To to improve this, allow the user to determine how many numbers to input. 

 

Final challenge: 

allow the user to choose whether the numbers should ascend OR descend. 

 

All those problems can be solved a few ways. 

 

Program #2:

scan a .txt file, and print replace every "the" with another phrase. 

Watch out... words like "them" can cause bugs. Also "the" is NOT equal to "The"

 

to improve/alter: make a program that alphabetized every word in the document. 

Ie:

dogs say bark.

to 

bark dogs say. 

 

I am not smart enough at programming to say how to automate, nor give a beginner's challenge. 

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, Windows7ge said:

From what I've heard among the programming community it's a lesser language. I was told it was good when it first came out but since then better languages have been released.

Yes VB quite a popular language, so was Delphi (similar syntax but with mostly deterministic destruction). However these days these languages have taken a legacy support role. No one really chooses them to write new software in and their features have lagged behind their more modern counterparts. But because of the large amount of legacy applications that were written in them 'back in their day' they will certainly not be going anywhere anytime soon.

 

I say it a lot, pick the 'right tool for the job'. In this particular context that's not really something that's primary role is for legacy support.

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

7 hours ago, Nuluvius said:

Yes VB quite a popular language, so was Delphi (similar syntax but with mostly deterministic destruction). However these days these languages have taken a legacy support role. No one really chooses them to write new software in and their features have lagged behind their more modern counterparts. But because of the large amount of legacy applications that were written in them 'back in their day' they will certainly not be going anywhere anytime soon.

 

I say it a lot, pick the 'right tool for the job'. In this particular context that's not really something that's primary role is for legacy support.

Something interesting that I learned is that even though Windows XP is still actively used on many many computers. As it turns out programs written in VB will not run on XP machines. At least from my experience.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Windows7ge said:

Something interesting that I learned is that even though Windows XP is still actively used on many many computers. As it turns out programs written in VB will not run on XP machines. At least from my experience.

That's strange, what flavour of VB are we talking about i.e. .NET or VB6 - if it's .NET than you'd need the appropriate framework installed.

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

Just now, Nuluvius said:

That's strange, what flavour of VB are we talking about i.e. .NET or VB6 - if it's .NET than you'd need the appropriate framework installed.

I think it's .NET, I don't recognize VB6. It has to do with a missing framework? When I tried to run the program I believe it threw an error code but I was just trying it for the heck of it so I didn't bother looking up the error code to see if it was something I could fix with a driver.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Windows7ge said:

I think it's .NET, I don't recognize VB6. It has to do with a missing framework? When I tried to run the program I believe it threw an error code but I was just trying it for the heck of it so I didn't bother looking up the error code to see if it was something I could fix with a driver.

XP does not support the most recent versions of the .NET framework, see this matrix. Therefore it depends on what framework the application was build using.

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

26 minutes ago, Nuluvius said:

XP does not support the most recent versions of the .NET framework, see this matrix. Therefore it depends on what framework the application was build using.

The programming software is Microsoft Visual Studios. I wonder if there's an option when building the program to use an older version of frameworks for better backwards compatibility.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Windows7ge said:

The programming software is Microsoft Visual Studios. I wonder if there's an option when building the program to use an older version of frameworks for better backwards compatibility.

Yes you do this from the target frameworks drop down box available from project setup and also from project options. See here.

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

12 hours ago, Nuluvius said:

Yes you do this from the target frameworks drop down box available from project setup and also from project options. See here.

This is useful information to know. In the event I have an XP machine I want to run my own custom software on. Although I haven't tested it I have some belief that using the default frameworks option should work in Vista. Though I haven't been exposed to many machines with that installed. I used Vista on another students laptop and within 10 minutes I hated it (at the time I was comparing it to 7)

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Windows7ge said:

This is useful information to know. In the event I have an XP machine I want to run my own custom software on. Although I haven't tested it I have some belief that using the default frameworks option should work in Vista. Though I haven't been exposed to many machines with that installed. I used Vista on another students laptop and within 10 minutes I hated it (at the time I was comparing it to 7)

Why bother with all of that legacy crap anyway. Again you're moving away from current spec and features to instead go after legacy standards.

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

8 hours ago, Nuluvius said:

Why bother with all of that legacy crap anyway. Again you're moving away from current spec and features to instead go after legacy standards.

I know, at the same time though if you plan to distribute your own software you want it to be as backwards compatible as it can be. Not everyone is running the latest version of windows. Although I will try to take up C#. I plan to start by re-writing programs I've made before in VB.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Windows7ge said:

I know, at the same time though if you plan to distribute your own software you want it to be as backwards compatible as it can be. Not everyone is running the latest version of windows. Although I will try to take up C#. I plan to start by re-writing programs I've made before in VB.

Of course and you can usually find a happy middle grounds between latest features and being good enough for compatibility. Have a look at WIX when your ready to distribute. It also integrates right into VS.

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

7 minutes ago, Nuluvius said:

Of course and you can usually find a happy middle grounds between latest features and being good enough for compatibility. Have a look at WIX when your ready to distribute. It also integrates right into VS.

I always wondered how installers were made. Thanks. I may ask you for criticism about my code in the future if that's alright. Right now I'm thinking about writing a very small program that lets you convert °F to °C and vise versa.

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Windows7ge said:

I may ask you for criticism about my code in the future if that's alright.

Of course, we are all here to help.

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

7 hours ago, Nuluvius said:

Of course, we are all here to help.

That's good to know. You, vorticalbox, and M.Yurizaki seem like good people to ask. I'll probably start a new topic and @ you all when the time comes.

 

In the meantime I do have one immediate question. In my example code you pointed out how the If, ElseIf statement was either being dragged out for too long or was poorly constructed. How else might I check that a field or input (for one or more than one input) is NotEmpty, IsNumeric, and is within a certain range (0 - 100, -50 - 150, -75 - (-25))? Would I still use a series of If statements but write them differently? Check one field at a time? Nest the if statements inside of each other and verify if it's correct with Booleans? (This looks like four questions but I'm only looking for one answer)

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, Windows7ge said:

In my example code you pointed out how the If, ElseIf statement was either being dragged out for too long or was poorly constructed.

In the general sense if one ends up writing out lots and lots of if else statements then it's a clear indication that something is going wrong.

16 hours ago, Windows7ge said:

Nest the if statements inside of each other and verify if it's correct with Booleans? (This looks like four questions but I'm only looking for one answer)

And nesting them is even worse, adding Boolean conditionals is worse still. Topologically you have just made a mess that's going to be very difficult to understand. Both to yourself in a few months time and worse for anyone else who has the misfortune to have to deal with it. See Cyclomatic Complexity.

16 hours ago, Windows7ge said:

How else might I check that a field or input (for one or more than one input) is NotEmpty, IsNumeric

In general you want these conditions to come first, they are known as Guard Clauses. If they fail then you simply exit that particular route of execution, for instance:

private void SomeProcedure(string someParameter)
{
  if (string.IsNullOrEmpty(someParameter))
  {
    return;
  }
  
  // Else do something with it
}

Not only is this far more simple and readable but it's the accepted standard when writing classical and iterative recursion; it is a best practice is to have one's termination clauses at the start.

 

When dealing with a number of conditions then one can obviously combine then into a single logical condition so long as it is not overly convoluted - this is likely trivial when dealing with two concerns such as two inputs... If it does end up being convoluted then consider more than one of such clauses or, once again, consider that something is wrong with the design.

16 hours ago, Windows7ge said:

and is within a certain range

You have identified a thing here: Range. If it were me then I'd probably lift this out and make it a part of my narrative:

class Range
{
  public int Minimum { get; set; }

  public int Maximum { get; set; }

  public bool ContainsValue(int value)
  {
    return value >= Minimum && value <= Maximum;
  }
}

You even seem to have some data?

16 hours ago, Windows7ge said:

(0 - 100, -50 - 150, -75 - (-25))

In general declaring any kind of literal in one's code i.e. a string or a number for example is considered very bad; these are known as magic values. They add to the overall entropy of the code and therefore damage the integrity of the design. The solution for magic values is to use a constant with the caveat being that there must be a clear reason for it's existence and that it fits into your narrative in some meaningful way.

 

In this particular case you may have a few values. Therefore the solution here may be to either declare them statically or load them in dynamically at run time from some kind of data source such as a file for example.

 

I've chosen to hard code the values for this example:

class Program
{
  class Range
  {
    public int Minimum { get; set; }

    public int Maximum { get; set; }

    public bool ContainsValue(int value)
    {
      return value >= Minimum && value <= Maximum;
    }
  }

  private static readonly List<Range> Ranges = new List<Range>
  {
    new Range { Minimum = 0, Maximum = 10 },
    new Range { Minimum = 60, Maximum = 70 }
  };

  static void Main(string[] args)
  {
    ProcessSomeNumber(100);
    ProcessSomeNumber(5);

    Console.ReadKey();
  }

  private static void ProcessSomeNumber(int someNumber)
  {
    if (!IsInRange(someNumber))
    {
      return;
    }

    Console.WriteLine($"Do something with {someNumber}");
  }

  private static bool IsInRange(int someNumber)
  {
    return Ranges.Any(r => r.ContainsValue(someNumber));
  }
}

Again, if it were me, then I'd probably seek to further encapsulate this behaviour inside of a class that was concerned with holding this list of Ranges and checking at the highest topological level whether a value was within any of them or not (this can still be accomplished in a single readable line by using Linq).

 

You want to be moving away from spaghetti implementations and towards thinking about and identifying different concerns that occur in your design or emerge and evolve within your code. Remember SOLID that I linked you earlier.

 

In object oriented software design we seek to decouple and encapsulate implementation detail. We achieve this by using clearly defined and readable abstract interfaces. The concept of 'is something within a range' is an abstract concern for an interface and the mechanics of how that check is accomplished is an implementation detail and a concern for the concrete implementation of that interface. We also try to favour Composition over Inheritance.

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

7 hours ago, Nuluvius said:

In general you want these conditions to come first, they are known as Guard Clauses. If they fail then you simply exit that particular route of execution, for instance:

Would I still put "return;" if I want to tell the user what they did wrong? For example:

messagebox.show("This field cannot be left empty.");
return;

or
  
return; 
messagebox.show("This field cannot be left empty.");

 

7 hours ago, Nuluvius said:

When dealing with a number of conditions then one can obviously combine then into a single logical condition

Not entirely sure what you mean by "single logical condition" but might you mean if the first condition is passed then when testing the same input could be it be tested against another condition under the else statement? Example:

if (string.IsNullOrEmpty(inputWatts)) // Testing volt calculator which requires two inputs. Watts & Amps
  {
    return;
  }
  
Else;
  if (condition(inputWatts)) // Test if it is a number
  {
    // What will happen.
  }

  Else;
    // Perhaps a third condition.

I just noticed a big issue. In VB "IsNumeric" is used to test if a value is a number or not. A quick Google search didn't give me good results for C# It directed me towards using a Boolean inside of it's own public static...and using Doubles...seems more complicated than it needs to be to test an input for if it is a number or not. Would you happen to have a solution for that?

 

7 hours ago, Nuluvius said:

I've chosen to hard code the values for this example: -- code --

Compared to VB, how the professor taught us, and the fact I understand little C# code that is the most complicated mass of code I've ever seen to determine if an input is between two numbers. In VB he taught us a way to sum up all that code into one single line:

if input < min or input > max then

There is a slightly more complicated way to write it if I wanted it to be global (So that more than one field could reference it and more than one input could be compared to it without having to write it multiple times.) but it would not reach the length of your C# example. I looked at all the hyperlinks you linked and LINQ abbreviated your code quite a bit but I still didn't understand where I would insert the min & max value and where I would put the variable that represented the user input.

 

I'm interested in being able to use a min max input system because I wouldn't want users to be able to input numbers like 9,999,999 * 9,999,999 because they wouldn't even fit on the page or the output box. Plus for the application it's a grossly exaggerated number that is well out of range for practical use. Same goes for negative numbers.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Windows7ge said:

Would I still put "return;" if I want to tell the user what they did wrong?

No, it'd likely be better to return some result and then decide what to do with it somewhere else in order to keep good adherence to the Single Responsibility principal (you don't want to be doing more than one thing in a given method or class - they should only have a single concern).

3 hours ago, Windows7ge said:

Not entirely sure what you mean by "single logical condition" but might you mean if the first condition is passed then when testing the same input could be it be tested against another condition under the else statement?

Yes precisely and nothing is stopping you from using operators to combine checks so long as the result is still simple and readable.

3 hours ago, Windows7ge said:

I just noticed a big issue. In VB "IsNumeric" is used to test if a value is a number or not. A quick Google search didn't give me good results for C# It directed me towards using a Boolean inside of it's own public static...and using Doubles...seems more complicated than it needs to be to test an input for if it is a number or not. Would you happen to have a solution for that?

Don't ever use static for a variable if you can help it, this is very bad as it essentially makes a singleton global instance of the thing. This is simply a no no or a cardinal sin.

3 hours ago, Windows7ge said:

Would you happen to have a solution for that?

Yes, one would likely use int.TryParse - though again, maybe you'd like to consider encapsulating this behaviour in some abstraction in order to make it fit your narrative.

3 hours ago, Windows7ge said:

Compared to VB, how the professor taught us, and the fact I understand little C# code that is the most complicated mass of code I've ever seen to determine if an input is between two numbers. In VB he taught us a way to sum up all that code into one single line

As I said, academia does a rather piss poor job of teaching how to do something the right way. Follow it if you will but you'll inevitably end up in a tangled mess and be very sad for your efforts. I took the information that you had described and turned into a very simple example of how one may encapsulate such behaviour in a set of abstractions (using a console application template).

3 hours ago, Windows7ge said:

There is a slightly more complicated way to write it if I wanted it to be global (So that more than one field could reference it and more than one input could be compared to it without having to write it multiple times.) but it would not reach the length of your C# example.

In the classical sense your View would be completely decoupled from all of this 'business logic' or ViewModel. Those inputs would be bound to properties in some ViewModel class and the 'checking' would occur once a bound command had been fired. This is how things are done correctly when one has to build this sort of application (it's the same in quite a few technologies). Though I do appreciate that you are simply trying to get something to just work so that you can learn in this case.

3 hours ago, Windows7ge said:

I looked at all the hyperlinks you linked and LINQ abbreviated your code quite a bit but I still didn't understand where I would insert the min & max value and where I would put the variable that represented the user input.

Put simply, you wouldn't, not in my example at least. I'm not doing your homework for you ^_^ My purpose is to furnish you with the tools and appreciation that you need in order to accomplish that yourself and I expect for you to meet me at least halfway.

3 hours ago, Windows7ge said:

I'm interested in being able to use a min max input system because I wouldn't want users to be able to input numbers like 9,999,999 * 9,999,999 because they wouldn't even fit on the page or the output box. Plus for the application it's a grossly exaggerated number that is well out of range for practical use. Same goes for negative numbers.

This is a specific concern that has absolutely no place or purpose in the ViewModel or business logic, it is a concern of the View - the 'input box' should be capable of providing this kind of filtering. What I'm trying to say is that once the 'input' reaches the processing method it should already have been shaped correctly - at least for the most part.

 

In your simple context I'd probably be looking for a masked input control - there should be one already included in the framework. A mask will likely take care of the scenario that you want without needing validation bindings (thus inferring a transformation to a full MVVM or MVC pattern).

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


×