Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: A question about coding practices.

  1. #1
    Join Date
    Dec 2008
    Location
    Montana, USA
    Posts
    676

    Default A question about coding practices.

    Years ago, I created a proprietary commercial program that was character based and strictly modal. Now, no one saw my code but me, and so I pretty much developed my own coding style. One of the standards was that every function have one entry and only one exit. I never used a return statement anywhere except at the very bottom of a function. My usual practice was to start every block of code with a "while true", write the code and if it needed to stop due to an error or user pressing the Oh S key or whatever, I could issue a break statement that would drop me to the bottom of the while loop and allow me to clean up any things that needed to be done before the return. Of course the last statement in the loop was a break, so the code wouldn't loop. The loop was strictly to allow me to have one exit point.

    I am now creating a GUI application in C++ and am up to my ears in How To books. Nowhere do I see an example of using a single exit point for a function. There seems to be no concern for cleaning up, and that worries me especially using a language with no garbage collection.

    So, I'm asking: What is/are the accepted methods of writing functions that will allow me to keep things clean?

    Bart

  2. #2
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: A question about coding practices.

    Perhaps unsurprisingly,
    What you describe sounds very much like Functional Programming

    https://en.wikipedia.org/wiki/Functional_programming

    In other words, a style where you minimize or eliminate changing variables with the expectation that the result is highly predictable.

    I don't know that there is necessarily only one way to write code...
    As long as you don't violate fundamental practices like writing structured, easily understood code.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  3. #3
    Join Date
    Dec 2008
    Location
    Montana, USA
    Posts
    676

    Default Re: A question about coding practices.

    You may be correct. That's not an easy document to grasp at first read. One of the reasons I coded the way I did was when I took a job supporting a project written by another. Each afternoon, around 2:00, the whole system came crashing down. I started looking at the code to see what time functions were being used only to find many, many instances where the original programmer simply used block copy instead of creating functions. He also left in the middle of procedures and functions creating a memory leak situation. It was sickening to read and obviously didn't work.

    I have to wonder if using a goto and label at the end of each code block wouldn't be a better method. Guess I'll have to try it and see what it looks like. I just can't seem to be willing to quit halfway through.

    Bart

  4. #4
    Join Date
    Sep 2012
    Posts
    7,093

    Default Re: A question about coding practices.

    Quote Originally Posted by montana_suse_user View Post
    creating a memory leak situation.
    Finally you explained what "cleanup" you mean.
    I have to wonder if using a goto and label at the end of each code block wouldn't be a better method.
    If you are after memory leak, GCC "cleanup" attribute may result in more readable code.

  5. #5
    Join Date
    Dec 2008
    Location
    Montana, USA
    Posts
    676

    Default Re: A question about coding practices.

    Well memory leaks are a large part of it, but, resetting labels, colors, menu items, all sorts of thingies that change when you add or remove messages and titles and the like. That's really what I meant. Going in to a user input function or procedure I can save titles, positions of things, which procedure called this function, which files are open (depends on the database manager) and a lot of other things. If the user decides he's in the wrong area or just changes his mind and hits the esc key part way through the entry process, I need to drop through the entry screen and reset all the things I changed when I entered. That's what I meant by cleanup.

    Bart

  6. #6

    Default Re: A question about coding practices.

    In opensuse.org.help.programming-scripting, you wrote:
    > Well memory leaks are a large part of it, but, resetting labels, colors,
    > menu items, all sorts of thingies that change when you add or remove
    > messages and titles and the like. <SNIP> I need to drop through
    > the entry screen and reset all the things I changed when I entered.
    > That's what I meant by cleanup.


    I don't think a functional programming paradigm will work well for this kind of cleanup. Since you're coding in C++, I
    think you're best bet for avoiding memory leaks by inheriting your GUI objects and coding explicit destructors or using
    smart pointers: https://en.wikipedia.org/wiki/Smart_pointer . If you need to GUI objects to reset, I can't see any
    reason why you code this.reset() or this.cleanup() member functions for each of your objects.

    I also can't see much advantage of avoiding mid-function return statements. In C++, it certainly doesn't improve code
    readability since it necessitates introducing additional but avoidable branch statements. It also makes the code less
    robust and vulnerable to silent bugs if the memory contents of the pointer/reference arguments are unintentionally
    modified in the code following where the return statement should be. But I think the worst problem about avoiding
    mid-function returns in C++ is that it makes returns from base class member functions far less readable compared to the
    intuitive version e.g:

    Code:
    int InheritingObject::memberFunction(bool useBaseFunction  = false) {
    if (useBaseFunction) {
    return BaseObject::memberFunction()
    }
    
    // InheritingObject version of the function begins here
    }
    vs the version avoiding a mid-function return:

    Code:
    int InheritingObject::memberFunction(bool useBaseFunction  = false) {
    int returnFlag;
    if (useBaseFunction) {
    returnFlag = BaseObject::memberFunction()
    }
    else {
    // InheritingObject version of the function begins here
    }
    
    return (returnFlag);
    }
    Which is more readable would you say?


  7. #7
    Join Date
    Dec 2008
    Location
    Montana, USA
    Posts
    676

    Default Re: A question about coding practices.

    Point taken! Never had access to .reset objects before! I've got to completely change my old ways! This has put the fun back into programming. I've missed that. Thanks for you reply.

    Bart

  8. #8
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,559

    Cool Re: A question about coding practices.

    Object-oriented code; readability of code; execution stability.

    IMHO the (formal) bottom line is: "Code inspection; Module Test; Integration Test; System Test."

    Given that, the testing environment includes tools such as "Valgrind" -- see "Dynamic program analysis" in Wikipedia.

    BTW, by "Code inspection", I mean the formal process first suggested by Michael E. Fagan and further pursued by Tom Gilb and Dorothy Graham.

    Is "Agile development" better?
    • IMHO if and only if "Agile testing" is used to confirm that "done is done" but, even then I'm not convinced -- even if a portion is "done", how does that portion interact with all the other portions which are also "done".
    • The "daily crash at 2 pm" is a good example -- something happens at 2 pm every working day which causes the system to misbehave despite, every portion of the system behaving perfectly, individually, at 2 pm.


    Ask people who work in the automotive industry: the code executes perfectly in the software laboratory -- under the bonnet it falls apart …

    Someone said: "Everything should be as simple as possible but, no more simple than that … "
    Someone else said: "Software development is the most complex and difficult task that mankind has foisted on itself … "

  9. #9
    Join Date
    Dec 2008
    Location
    Montana, USA
    Posts
    676

    Default Re: A question about coding practices.

    Quote Originally Posted by dcurtisfra View Post
    <snip>
    Ask people who work in the automotive industry: the code executes perfectly in the software laboratory -- under the bonnet it falls apart …

    "
    I was poking around, I think youtube, and in the comment section of a video clip about how to do something, one of the commenters remarked that the author went to excess with the code. Her reply was that she worked in the automotive industry and they had a hard fast rule about having only one exit point to any block of code. Then, I see your remark. Perhaps I should do a 6 month stint for one of the big three, I'll bet I'd be good when I came out!

  10. #10
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: A question about coding practices.

    Quote Originally Posted by montana_suse_user View Post
    Point taken! Never had access to .reset objects before! I've got to completely change my old ways! This has put the fun back into programming. I've missed that. Thanks for you reply.

    Bart

    You give in too easily.
    There are many acceptable styles of programming and Functional Programming definitely has its points.
    It might not be the best for all scenarios, but in the hands of a real Code Warrior, even this can be pretty impressive coding (I've seen some).

    And, in some situations like inaccessible situations you want the code to be as bullet-proof as possible.
    Or, you want each code block to never or hardly ever need to ever be modified after creation.
    Or, you'd be surprised how clear the code can be if the code is uncomplex and certain.

    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •