Author Topic: My first programming exercise for college  (Read 14656 times)

0 Members and 1 Guest are viewing this topic.

kaefer

  • Guest
Re: My first programming exercise for college
« Reply #45 on: February 09, 2011, 01:53:34 PM »
if (File.Exists(f))
   File.Open(f)

Would you recommend this instead?
Code: [Select]
    static StreamReader TryOpen (string name)
    {
      if (File.Exists (name))
        return new StreamReader (name, Encoding.ASCII);
      return null;
    }

There's a lot more to worry about, considering that only reads/writes of primitive types T where sizeof<T> <= sizeof<nativeint> are guaranteed to be atomic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: My first programming exercise for college
« Reply #46 on: February 09, 2011, 02:04:31 PM »
More along the lines of don't have redundant code. In the provided example, try/catch is the proper way to convert the input to an int. Why fight the language and API?


I see your point about not having redundant code, I just don't accept that IsNumeric(TextBox1.Text) is any less a valid way to catch user input error than a Try/Catch block.

I just did a simple test to see the difference in execution between a Try/Catch block and an If/Then block using IsNumeric and the results pretty well dispel any notion that one is introducing redundancy, at least in the execution of the code ...

My timing test reveals that in debug mode the execution time is always longer in a Try/Catch block than an If/Then, however, in a release build, the number of ticks for the Try/Catch block is only sometimes longer, but when it is, it is significantly longer. The If/Then is always equal to or less than the time ... at least in this test. YMMV
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

dan.glassman

  • Guest
Re: My first programming exercise for college
« Reply #47 on: February 09, 2011, 02:14:12 PM »
Quote
Would you recommend this instead?

Sorry, I wasn't trying to exemplify "the Right Thing" in terms of how to open a file in a particular framework.  I was trying to point out that {pseudocode}

if file.exists(f)
   file.open(f)

is a bug anywhere because the file may disappear between the .exists() and .open() calls -- it's not in the programmer's control.  And that I wasn't sure whether a textbox's value was [similarly] vulnerable to changes outside the programmer's control between the IsNumeric() and CInt() calls.

In the file case,

try
   file.open(f)
catch IOError

Is the only proper thing to do -- no pre-checking for existence, permissions, lock state, etc. -- they're all liable to change at _just_ the wrong time.

-drg

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: My first programming exercise for college
« Reply #48 on: February 09, 2011, 02:19:22 PM »
I didn't mean to suggest that using a Try/Catch block was wrong, but rather than using it as an end-all bug catcher, the programmer should spend a little time thinking through the requirements of the code, the effects of user input and possible scenarios.

Certainly no programmer is capable of recognizing ALL potential pitfalls and as the previous post points out, there are some things that are simply not within control of the code, but my assertion is that when known possible conditions exist, they should be provided for by code.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Jeff H

  • Needs a day job
  • Posts: 6144
Re: My first programming exercise for college
« Reply #49 on: February 09, 2011, 07:24:30 PM »
I typically have used a textbox's Validating Event Handler with tryparse



It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: My first programming exercise for college
« Reply #50 on: February 09, 2011, 08:33:40 PM »
IMHO,  using try catch to validate a textbox is a bit heavy handed, but not unreasonable.  I certainly would use try/catch/finally  when doing file io operations,  as one may need to perform cleanup when the exceptional happens.  but this is comparing apples and oranges. 
The correct approach if you're doing more than one textbox for numeric's would be to subclass textbox as shown here http://msdn.microsoft.com/en-us/library/ms229644.aspx   

Jeff H

  • Needs a day job
  • Posts: 6144
Re: My first programming exercise for college
« Reply #51 on: February 09, 2011, 10:03:08 PM »
Or override OnVaildating event

From here http://www.theswamp.org/index.php?topic=36832.msg418314#msg418314



Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace WindowsFormsControlLibrary1
{

    public class IntegerRedTextBox : TextBox
    {
        public IntegerRedTextBox()
        {
            OriginalColor = this.BackColor;
        }

        public Color ChangeColor { get; set; }
        private Color OriginalColor { get; set; }

        private bool ChangeBackColor()
        {
           char[] chars = this.Text.ToCharArray();

            foreach (char c in chars)
            {
                if (!Char.IsNumber(c))
                {
                    this.BackColor = ChangeColor;
                    return true;
                }
           
            }
            this.BackColor = OriginalColor;
            return false;
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);

            e.Cancel = ChangeBackColor();
        }

    }
}


StefanDidak

  • Guest
Re: My first programming exercise for college
« Reply #52 on: February 10, 2011, 07:44:20 AM »
I am seriously questioning the ineptitude and ignorance of the instructors for this class.

I've just read this thread and I must say, the hair at the back of my neck is standing up and my toes are curled into position in a way that I wonder how long it'll take to uncurl! I'm not surprised, though, I hear things like this quite often. Education isn't what it used to be!

I must say you're quite courageous to enter into a CS "education" (edutainment?) when you're already far ahead of the pack that the curriculum is aimed at. I've seen it over the past 20 years with some other folks and it usually ends up being somewhat frustrating to jump through hoops that you could've designed yourself. The whole "we require a degree" sounds like a brainless robo-parrot policy and indicates that too many companies are no longer interested in real experience and skill but rather want worthless pieces of paper that in the real world mean nothing. Being a rebel at heart I spent much of the 90's hiring and contracting folks that only had proven experience and actively ignored anyone with "papers". If more companies did that and knew how to properly assess and judge someone's abilities they'd be so much better off.

The one thing that keeps coming to mind here is a question, though. Have you ever considered going into business for yourself? It may be easier to contract for those many companies than it is to get a job there and while there are certain risks that always come up when being self employed there's also a lot of benefits. Just a thought...

Cheers,
Stefan.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: My first programming exercise for college
« Reply #53 on: February 10, 2011, 10:15:47 AM »

The one thing that keeps coming to mind here is a question, though. Have you ever considered going into business for yourself? It may be easier to contract for those many companies than it is to get a job there and while there are certain risks that always come up when being self employed there's also a lot of benefits. Just a thought...


Actually I have, but if I do in fact become self-employed, I will lose whatever unemployment I have, regardless of the amount of income I generate. In fact, I had to fight with them to get it re-instated after I reported having made $100 doing odd jobs. I was told that I couldn't get unemployment if I earned wages from a self-employment source.

I have a conference call on Wednesday with a marketing research firm out of Olathe, Kansas. I have done freelance work for them in the past and we have been in negotiations for a while over getting an open end contract. If that works out, I'll be travelling to Kansas in March and making recommendations for setting up a data collection center. Fun stuff -NOT - but it WILL be a paycheck.

Plus, there is the chance that I'll have to go to Honolulu in the fall - now that would be terrible, wouldn't it.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie