Jump to content

Hello again internet!

My coding has been on and off for quite a long time now as basically I have had quite a struggle with figuring how to code things in general... Let me explain:

 

I've watched tutorials some time ago on how to make basic things; calculators, crude file management apps etc and I feel like I don't know how to apply that knowledge as I tried (and gave up on) a basic file sharing program because the code reminded me more of Tom Scott's video on "the art of the bodge" - describing how taped together solutions can work but are ugly. 

 

Basically the code was a mess and was written wrong from what I could tell (although would love someone to confirm my assumptions) as although it would work it wasn't pretty at all... UI or genuine functionality: would crash for various reasons that meant I couldn't really push it past alpha. 

 

My question is then, tldr, how do you code a program that isn't a taped together solution while actually being creative? I see things made by indie people working on an App or website in awe at how tf they managed for their stuff not to be terrible. Maybe I need a better tutorial?

Please mark as helpful and informative so my profile looks better.

quote or reply to me if you want me to reply to you.

Thanks

Link to post
Share on other sites

1 minute ago, Mad153 said:

Maybe I need a better tutorial

You get better by practice and things learnt over time

 

Do you have programming background? Schooling? Or just individual learning?

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 9600X || GPU: RX 9070 XT|| Memory: 32GB || Cooler: Peerless Assassin || PSU: RM850e|| Case: Lian Li A3

Link to post
Share on other sites

Tom Scott's video on FizzBuzz might interest you:

While it's partially just about creating a simple FizzBuzz program, but it also touches a bit of what you're saying; the whole code for now and the future.

 

In broad terms, you want to be able to quickly and easily change parts of your program, without those parts negatively influencing other parts of your program.

In object oriented programming, that typically means your program consists of classes with well-defined functions of their own.

 

In your calculator program for example, you should make it so your calculator can work without the entire GUI for example. a x+y function should work, regardless of what way the user interacts with it.

So that means you have a class with well-defined functions (the calculator) and then you can add a layer overtop of it that will act like the user interface.

 

That is the sort of way you could think about a program.

It's not a one-and-done thing, so it has to be able to accept changes in the future.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

6 minutes ago, Slottr said:

 

Do you have programming background? Schooling? Or just individual learning?

I'm only <content removed by staff> so considering studying something technology based at uni. (If things go well for me). But for now it's just individual learning off yt and documentation. 

 

Just to clarify though I don't plan to do anything amazing at all with my code, just for it to be a bit cleaner and actually reliable for little utility style programs.

Edited by SansVarnic
Redacted Personal Information

Please mark as helpful and informative so my profile looks better.

quote or reply to me if you want me to reply to you.

Thanks

Link to post
Share on other sites

8 minutes ago, Mad153 said:

I'm only <content removed by staff> so considering studying something technology based at uni. (If things go well for me). But for now it's just individual learning off yt and documentation. 

 

Just to clarify though I don't plan to do anything amazing at all with my code, just for it to be a bit cleaner and actually reliable for little utility style programs.

People can become excellent self taught coders but its pretty objectively harder. Im going to college to learn programing I have a really good understanding and implementation on java. To say I know java would be wrong. Coding isn't something that can be learned without a lot of effort and even if right now your code doesn't look organized I promise you its better to work towards making things you enjoy rather than worry about how it looks. Especially if you're going to college. Simply put make things you're interested in and don't worry about how your code looks. Work on making it pretty after you really understand it.

Link to post
Share on other sites

11 minutes ago, minibois said:

That is the sort of way you could think about a program.

It's not a one-and-done thing, so it has to be able to accept changes in the future.

I think this is what I needed to have realised way too late in the process: my code needed to be a lot more easy to edit and flexible.

 

The question though is how? Draw out how the program should work in a flowchart? What do you recommend?

 

 

Please mark as helpful and informative so my profile looks better.

quote or reply to me if you want me to reply to you.

Thanks

Link to post
Share on other sites

2 minutes ago, Mad153 said:

I think this is what I needed to have realised way too late in the process: my code needed to be a lot more easy to edit and flexible.

The question though is how? Draw out how the program should work in a flowchart? What do you recommend?

Yep, it's important to think about a design of a program before actually going in coding.

Look up UML, it's 'Unified Modeling Language', which is the default in the software industry to model your programs.

 

In this model you can define what parts of your program should be classes, what function and properties they should hold and how they will interact with each other.

The UML is an important part of many software projects, as not only will it give the programmer the confidence to create their software, it also makes future tweaks and additions much easier, especially for people who aren't the original creator. 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

  static void Main(string[] args)
        {
            FizzBuzz(getquestionans("Fizz?"), getquestionans("Buzz?"), getquestionans("start"), getquestionans("end"));


        }

        public static int getquestionans(string question)
        {
            Console.WriteLine(question + " =");
            return Convert.ToInt32(Console.ReadLine());
        }
        public static void FizzBuzz(int fizz, int buzz, int start, int end)
        {
            Console.Clear();
            for (int i = start; i <= end; i++)
            {
                string output = "";
                if (i % fizz == 0) { output += "Fizz"; }
                if (i % buzz == 0) { output += "buzz"; }
                if (output == "") { output = i.ToString(); };
                Console.WriteLine(output);

            }
        }

 

@minibois @Ohsnaps

(and anyone else)

 

Ok so I wrote the following code in C# (should be somewhat easy to follow) and I realised... again that  catch and try clauses are one of the reasons why my code is so messy: 

In this code, if the user enters the answer to a question as blank or not an int, it will obviously give an exception. where is the best place to put a try and catch here?

 

Like this (it just moves the issue further up the chain where i gets 0 instead of an actual inputted  number) ?

 

 public static int getquestionans(string question)
        {
            Console.WriteLine(question + " =");
            try
            {
                return Convert.ToInt32(Console.ReadLine());

            }catch (Exception e)
            {
                Console.WriteLine("error! " + e.ToString());
                return 0;
            }
         
        }

 

thanks again.

Please mark as helpful and informative so my profile looks better.

quote or reply to me if you want me to reply to you.

Thanks

Link to post
Share on other sites

On 6/29/2020 at 12:01 AM, Mad153 said:

Basically the code was a mess and was written wrong from what I could tell (although would love someone to confirm my assumptions) as although it would work it wasn't pretty at all... UI or genuine functionality: would crash for various reasons that meant I couldn't really push it past alpha.

That is pretty normal when you're getting started, and as disheartening as it is, it is valuable experience. When you follow a tutorial you are just doing what they suggest, which makes it more difficult to internalise the lessons that they're teaching. Once you start going out on your own, you make mistakes and end up with ugly code, but then you figure out how to improve it, either on your own or by asking targeted questions as you are doing here, and that's how you improve. Maybe the program that you're working on won't work perfectly in the end, but each change is an improvement both to that program and to your ability to write good code.

 

On 6/29/2020 at 9:26 AM, Mad153 said:

In this code, if the user enters the answer to a question as blank or not an int, it will obviously give an exception. where is the best place to put a try and catch here?

I would suggest that in a case like this, you should first think about what you are hoping will happen from the user perspective if you enter something that isn't a number. To me there are a couple of possible answers - either the program should exit, or it should give them the opportunity to try again. For the first option, it would probably look something like

        static void Main(string[] args)
        {
            try {
                FizzBuzz(getquestionans("Fizz?"), getquestionans("Buzz?"), getquestionans("start"), getquestionans("end"));
            } catch (System.FormatException e) {
                Console.WriteLine("Invalid input: " + e.ToString());
            }
        }

The second option, which is probably more what you want, would look something like

        public static int getquestionans(string question)
        {
            while (true) {
                Console.WriteLine(question + " =");
                try
                {
                    return Convert.ToInt32(Console.ReadLine());
                }catch (System.FormatException e)
                {
                    Console.WriteLine("error! " + e.ToString());
                    // Continue looping to ask the question again
                }
            }
        }

Note that I have basically no C# experience, so the actual code may not be exactly right, but hopefully it is clear enough.

HTTP/2 203

Link to post
Share on other sites

On 6/29/2020 at 3:26 AM, Mad153 said:

In this code, if the user enters the answer to a question as blank or not an int, it will obviously give an exception. where is the best place to put a try and catch here?

4 hours ago, colonel_mortis said:

either the program should exit, or it should give them the opportunity to try again.

 

All of the builtin C# numeric types have a static method TryParse(), with various overloads.
This is true of the following numeric types and their aliases (spoiler alert: It's all of them):

  • SByte
  • Byte
  • Short
  • UShort
  • Int
  • UInt
  • Long
  • ULong
  • Single
  • Double
  • Decimal

The overload of TryParse() that is interesting to us is TryParse(String s, out <NumericType> result), and it functions as follows:

  • Returns True if String s is successfully converted to the type, and False otherwise.
  • On successful conversion, result contains the <NumericType> conversion of the string.

This function eliminates the need for Try-Catch blocks for this type of application:

public void GetUserInteger()
{
  Console.WriteLine("Please enter an integer then press enter:");
  
  int result = 0;
  while(!Int32.TryParse(ConsoleReadLine(), out result))
  {
    Console.Clear();
    Console.WriteLine("An integer is only whitespace, optionally followed by a minus sign (-), followed by demical digits, followed by whitespace.");
    Console.WriteLine("Please enter an integer then press enter:");
  }
  
  Console.WriteLine("The integer you entered was " + result);
  Console.WriteLine("The type of the result is " + result.GetType());
}

 

ENCRYPTION IS NOT A CRIME

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

×