Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Coding Horror

1252628303137

Comments

  • Moderators, Society & Culture Moderators Posts: 15,685 Mod ✭✭✭✭smacl


    It's a permanent problem with coders though. "good enough, don't have the time /energy /knowledge to fix it, the hardware will pick up the slack". We've all done it!

    Agreed, but I often wonder how many have spent significant time looking at orders of complexity of what their coding prior to implementation and spent the same time again in front of a profiler post implementation? Believing that the language, framework, SDK or hardware will do this for you is a fallacy. I've seen a lot of well structured, nice looking C++ with absolutely appalling performance characteristics over the last few years.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 79,829 Mod ✭✭✭✭Sephiroth_dude


    Why is it not ok to use try catch as controlling the flow of a program? I hear a lot of people giving out about this.


  • Technology & Internet Moderators Posts: 28,780 Mod ✭✭✭✭oscarBravo


    Why is it not ok to use try catch as controlling the flow of a program? I hear a lot of people giving out about this.

    It's a matter of opinion. In some languages exception handling is expensive, and should only be used to catch actual errors. In others, it's a perfectly valid flow control mechanism.


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Why is it not ok to use try catch as controlling the flow of a program? I hear a lot of people giving out about this.

    Mainly because if you do things correctly then Exceptions should only happen in exceptional circumstances.

    Realistically you always control the flow of a program using exceptions but what you are supposed to do is recover from the exceptional state, log and report the problem to the user etc.

    For example if you are connecting to an endpoint using a TCPClient which accepts the hostname and port number from the user. You dont just accept what the user gives you and try and connect, catch the exception when it fails and ask the user to try again. That would be bad usage of exception flow control.

    You would check the port number for a valid range and check the hostname to make sure it is a valid hostname and doesnt contain spaces and if they are wrong you will report the error to the user. The exception should only catch the exceptions caused by connection failures which should only happen in exceptional circumstances.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 79,829 Mod ✭✭✭✭Sephiroth_dude


    Mainly because if you do things correctly then Exceptions should only happen in exceptional circumstances.

    Realistically you always control the flow of a program using exceptions but what you are supposed to do is recover from the exceptional state, log and report the problem to the user etc.

    For example if you are connecting to an endpoint using a TCPClient which accepts the hostname and port number from the user. You dont just accept what the user gives you and try and connect, catch the exception when it fails and ask the user to try again. That would be bad usage of exception flow control.

    You would check the port number for a valid range and check the hostname to make sure it is a valid hostname and doesnt contain spaces and if they are wrong you will report the error to the user. The exception should only catch the exceptions caused by connection failures which should only happen in exceptional circumstances.

    If you had a program where you take in userInput and lets say it you want them to enter their name, so a string only, rather user try catch, you would be better off using if statement to validate it?


  • Advertisement
  • Registered Users Posts: 768 ✭✭✭14ned


    If you had a program where you take in userInput and lets say it you want them to enter their name, so a string only, rather user try catch, you would be better off using if statement to validate it?

    Ah, that's the narrow vs wide contract API design debate. See https://blog.regehr.org/archives/1436 for a flavour.

    (tldr; "it depends")

    Niall


  • Technology & Internet Moderators Posts: 28,780 Mod ✭✭✭✭oscarBravo


    If you had a program where you take in userInput and lets say it you want them to enter their name, so a string only, rather user try catch, you would be better off using if statement to validate it?

    14ned's "it depends" is the right answer.

    Coming at it from a Python perspective, which is where I mostly live: if I need to validate a string input that's supposed to contain an integer value, the easiest way to do it is:
    try:
        number = int(value)
    except ValueError:
        [handle the exception]
    

    If I need a floating point value:
    try:
        number = float(value)
    except ValueError:
        [handle the exception]
    

    There simply isn't a simpler way to do it. There may be better approaches in other languages, which is fine: use what makes sense.


  • Registered Users Posts: 768 ✭✭✭14ned


    oscarBravo wrote: »
    There simply isn't a simpler way to do it. There may be better approaches in other languages, which is fine: use what makes sense.

    So, putting this into the wide vs narrow contract terminology I linked to earlier, in Python exception raises are considered part of a function's wide contract with their caller i.e. if the function doesn't like its inputs, it can say "I refuse to complete due to reason X".

    In other languages such as C++, exception throws are considered either wide or narrow i.e. if the function doesn't like its inputs, it panics i.e. aborts the program. In C++, "exception safe" code treats exception throws as wide. In "exception unsafe" code, exception throws are narrow.

    In Rust, all functions default to narrow contract. If you call them wrong, the program aborts. However countervailing that, it's much harder to write code which calls a function wrong because the borrow checker will refuse to compile most instances. You then opt-in to wide contracts by returning a Result<T, E> on a function by function basis.

    So you can see there is a pattern here. The higher level the language, the more wide contracts you tend to see. The lower level the language, the more narrow contracts you tend to see, at least in new code. Future C++ 26 is expected to have far more narrow contracts again, and become much more like Rust in this regard. We effectively are formalising the low level trueism that "use me wrong, I kill you".

    Niall


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 90,538 Mod ✭✭✭✭Capt'n Midnight


    7309_9e93.png


  • Registered Users Posts: 2,781 ✭✭✭amen


    I'm robbing that Safety Pig. I have a home for it.


  • Advertisement
  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 79,829 Mod ✭✭✭✭Sephiroth_dude


    oscarBravo wrote: »
    14ned's "it depends" is the right answer.

    Coming at it from a Python perspective, which is where I mostly live: if I need to validate a string input that's supposed to contain an integer value, the easiest way to do it is:
    try:
        number = int(value)
    except ValueError:
        [handle the exception]
    

    If I need a floating point value:
    try:
        number = float(value)
    except ValueError:
        [handle the exception]
    

    There simply isn't a simpler way to do it. There may be better approaches in other languages, which is fine: use what makes sense.

    The reason I asked is because all the C# and Java heads tell me otherwise so was a bit confused about the whole thing :s


  • Registered Users Posts: 3,945 ✭✭✭Anima


    In C# and Java, they're probably right. I think it does very much depend. You see them being used in non-exceptional circumstances all the time so hard to form a set of rules. Probably experience will teach you when to use them.

    If you're a beginner, I'd just say if you can't return a value from a method that is suitable in an error condition, just throw the exception and deal with it that way. Likewise if you have a method/function that doesn't return something and has errored, then throw an exception.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 90,538 Mod ✭✭✭✭Capt'n Midnight


    https://asset-1.soup.io/asset/14391/0449_1622_849.gif

    I thought this was a blast from the past. It's not.


  • Registered Users Posts: 1,459 ✭✭✭Anesthetize


    https://asset-1.soup.io/asset/14391/0449_1622_849.gif

    I thought this was a blast from the past. It's not.
    Looks like he's still on JDK 1.1


  • Registered Users Posts: 4,757 ✭✭✭cython


    Coding horror might be a bit strong, but didn't know where else to put this: https://github.com/auchenberg/volkswagen
    Volkswagen detects when your tests are being run in a CI server, and makes them pass.


  • Registered Users Posts: 768 ✭✭✭14ned


    cython wrote: »
    Coding horror might be a bit strong, but didn't know where else to put this: https://github.com/auchenberg/volkswagen

    Funnily enough, I've written code many times in my career which detects when we are running inside a CI (usually by inspecting environment variables), and early outs some tests with a pass.

    As a contractor, you do exactly what your client tells you to do :(

    Niall


  • Registered Users Posts: 1,275 ✭✭✭tobsey




  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    tobsey wrote: »

    horror but also genius


  • Registered Users Posts: 4,325 ✭✭✭iLikeWaffles


    horror but also genius

    It's vary hard to follow, is it Python? From the looks its waiting for numbers.


  • Registered Users Posts: 2,213 ✭✭✭MajesticDonkey


    horror but also genius

    It's vary hard to follow, is it Python? From the looks its waiting for numbers.
    It's a regular expression, not a programming language per se...


  • Advertisement
  • Technology & Internet Moderators Posts: 28,780 Mod ✭✭✭✭oscarBravo


    tobsey wrote: »

    That's about the scariest thing I've ever seen.


  • Registered Users Posts: 12,793 ✭✭✭✭Igotadose


    oscarBravo wrote: »
    That's about the scariest thing I've ever seen.

    Great comment:
    So this is what programming with Satan is like


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Found a beauty today.

    So ive setup a Jenkins build server for our team and added the SonarQube plugin https://www.sonarqube.org/. This scans the code base and uploads the results to our sonar server. It reports on issues with the code, potential bugs and vulnerabilities and general ****ty code.

    I added some of our older projects to it as a test and this issue got reported.
    private void WriteBump()
    {
        Height = Height;
        Width = Width;
        BackgroundColor = BackgroundColor;
        StartDay = StartDay;
    }
    

    Why would you create a method to set each property to itself?

    Then I have a look at each of the properties and see in the property the Get and Set.
    public int Width
    {
        get { return GetValue("Width"); }
        set { SetValue("Width"); }
    }
    

    In the Get and Set value he is maintaining a Dictionary with a bunch of different properties data.

    WTF. WHY?


  • Closed Accounts Posts: 9,046 ✭✭✭Berserker


    Drafted onto a new project today, which is running behind schedule and the code is full of Christmas crackers. No unit tests in it and no documentation either.
    public string CurrencyMoneyStripper(string Currency)
    {
    	if (Currency.Contains(".") == false)
    		Currency = Currency + ".00";
    
    	if (String.IsNullOrEmpty(Currency))
    		return String.Empty;
    	else
    		return Currency.Substring(0, Currency.IndexOf('.'));
    }
    


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    You know you are probably in for some **** coding when you come across this. Yes he called the static class StuffManager and the method doStuff.

    He must have been having a bad day.
        class Program
        {
            static void Main(string[] args)
            {
                StuffManager.doStuff(args);
            }
        }
    
    


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    What the **** are these response enums supposed to represent ffs.
    public enum ResponseCodeEnum
        {
            A = 0,
            N = 1,
            E = 2,
            D = 3
        }
    


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    What the **** are these response enums supposed to represent ffs.
    public enum ResponseCodeEnum
        {
            A = 0,
            N = 1,
            E = 2,
            D = 3
        }
    
    A = A-OK!
    N = No Dice
    E = Ehhh...WTF
    D = Do it yourself, I'm out


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Im starting to think i work with a bunch of cowboys with the amount i post in this thread. haha.


  • Closed Accounts Posts: 9,046 ✭✭✭Berserker


    Im starting to think i work with a bunch of cowboys with the amount i post in this thread. haha.

    Oh, I'd wager that I could give you a very good run for your money. I'm cautious about posting code in case someone from the company spots it.

    I worked my way through thousands of line of utter sh1te last week. One aspx page had 3,000+ lines of javascript in the header tag. This code was written by very senior devs, not juniors. They are all permies with lead level salaries. How the fcuk do these people get these jobs!!!


  • Advertisement
  • Registered Users Posts: 4,325 ✭✭✭iLikeWaffles


    Could they be index positions?


Advertisement