Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Curly Brace Pairing Identity  (Read 7955 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Curly Brace Pairing Identity
« on: July 10, 2017, 07:58:08 AM »
Working on a large program last night and it compiled and I ran it and it hit an infinite loop at one part so I had to kill it. The problem was difficult to track down opening and closing curly braces since there are so many nested loops etc and no notation as to which are paired for opening and closing. I tried scrolling up and down my code and it was giving me a headache trying to keep track of which opening braces were paired with what closing braces with many while loops and conditional statements that required the use of them.

I ended up having to print out the 22 pages of code and then with a pencil write to the right of opening { and closing } a number like 001 and 002 and so on..... and at 017 I then found the extra closing } curly brace that didnt belong and was the result of me copying and pasting sections of code to not have to retype code for similar but isolated functions that cant reuse the same object and needed to have immunity between each other.

I suppose you could add comment notation to each brace giving it an identity such as {  // 001   and  { // 002 and in the code when coding and fresh on my mind add the closing brace with } // 002 and } // 001 to keep track of them that way with say a while loop with identity 002 nested within while loop 001.

But was wondering if there are any tools or viewers out there that might identify without having to type notation with comments in programming as to which opening and closing curly braces are paired, so that one that is closing too early would stand out when reading my code as to incorrect placement. Such as if I saw the closing curly brace } // 001 mid program it would have stood out right away as the problem because it needed to be moved to the tail end of the program. As for the code will compile because there are the correct number of opening and closing braces, but placement of the closing brace needed to be removed and added at about the 5th to last line of code in the program to operate correctly.

I tried out notepad++ and searched around and couldnt find anything that showed curly brace pairing.... so maybe I just need to add the comment notation to pair the opening and closing curly braces... but figured i'd check here vs assuming since others must have run into the same headache of tracking down misplaced opening or closing braces in code, and maybe someone made a viewer that color codes and shows identity since it seems as though it wouldnt be too hard to parse through code from a code standpoint that will show which { and } are paired to then make it easy for someone to say mouse over the curly braces and see the identity given to them to know which ones are paired with current structure which is sound for compiling with no code issues other than misplacement which leads to the program executing incorrectly to intent.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #1 on: July 10, 2017, 10:40:26 AM »
Here is a not-quick answer.

Break you code into seperate blocks at the source code. Each curly pair is a block. Each block is a different sourc e code file. Each clock of code should made logical sense by itself.You want a modular design where you can edit any module without need to re-write other parts of the whoule project.

Avoid so-called inline code. Don't worry about spped, that can be fixed later. First get the logic, then emply the tools taht improve speed. If somecode gets to be rather large, make it into a thing that is extrernal to what you see.Use whatever tools yur have to hide code from view.  One old school method is to just make anything large a seperate funtion or procedure, even i9t is only used just once.

Some onld0shoolc programers had a rule taht any code fragment had to fit on the screen. If not, it must bebroken up into smaller blocks of code. This is for the benifit of the human, not the computer. The logic has to be in front of your face.

Bookls have been written about this. It is about style. The computer does not care about style. But to use humans, style helps us to understand logic.

Hope this stirs you to re-think your source code style.  ;D

EDIT:
Disclaimaer: Link below is a startng point tath can led in many directions. Be carefull about where it takes you
.

http://www.cspray.net/my.so-archive/100-most-influential-programming-books.html

Look for this one:
The Art of Computer Programming if only for the effort Knuth put into it.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Curly Brace Pairing Identity
« Reply #2 on: July 10, 2017, 01:53:41 PM »
Visual Studio displays the matching starting brace control statement when you hover over a closing brace:



Many text editors will also highlight the matching braces when you are over top of one. usually the editor needs to "know" it is dealing with a source file, doing that varies by the application though.

I think these (and things like regions in C#) are something that really just facilitates what is generally a hard-to-read coding style. If control flow is nested deeply enough that it's hard to keep track of which braces match, it should be refactored into separate functions/methods. Remember that unless you write code in Machine code that it is designed for humans to read first, and computers second.

Notice, for example, in the snippet in the screenshot I took- there are a bunch of function calls describing exactly what is happening- to the point that a lot of it is self documenting even without being very familiar with the codebase.  most of the methods/functions being called all perform a number of looping or other control flow tasks, sometimes based on server configurations as well. If all of that code was in-line within the routine, it would be massive, and it would be difficult to match the braces- but as it is, the tooltip isn't even needed, since it fits on one screen now.

I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #3 on: July 10, 2017, 05:04:49 PM »
Cool Thanks for sharing that info .... I guess I need to break out of my bad habit of having a program all contained in a single cpp file. The program i have has become massive as i had been building on it off and on for the last 3 years picking away at it adding more and more functionality and complexity to it.

I didnt know that Visual Studio had this mouse over feature to show which function it detects is to be closed at the brace location. I have been using Bloodshed Dev C++ 4.9.9.2 IDE because its lightweight and small install. Its obsolete, but works, but perhaps maybe I should move on to Visual Studio C++ Express.

Going to work on splitting this up to make it more manageable. Given that I am the only one that works with my code I generally make it all in a single .cpp etc and once a project is done its almost never revised since it does what was intended and doesnt need to do more, but spitting it up for this project makes sense as for I have more stuff to add to it and I will be able to target functions directly vs having to search for them in 22 or more pages.

Most projects are a small program to do something and there is no revisions, they are just quick console apps. This project though is one that I have been messing with off and on for the last 3 years and I guess should really be treated and structured as a project to make it more manageable even if just my self would ever be looking at the source code. It started off with 2 main features and that has grown to 5 features, soon to have 6 once I can code without distractions and be in the coding mindset as for I need to be free of distractions otherwise I hit walls in programming vs charging through with it to success. Its almost like i have to warm my brain up to programming and then charge full steam ahead when i do it without distractions otherwise I lose track of what is what and why, especially since I use abstract methods that I even confuse myself sometimes when looking back after not looking at my code for a while as to why I did that etc. Perhaps better structuring will remove this problem as for I wont have to think about so much and can focus on just the functions at the function level as blocks vs looking at it as a whole which is massive and can be sort of overwhelming to follow at times.  ;D

Back in college, the professor didnt pick at structure and organization. It was mainly that you know what each thing did, could debug others code as well as write your own, and length of programs in a single .cpp file wasnt a problem. As long as the program did as intended you were scored mainly on it doing as intended. Points were removed if you made redundant functions vs reuse, and commenting was requested but not required. He would test your code to make sure you protected it from say someone entering letters or characters at a input that is requesting a value to teach testing of user input etc. But that was pretty much it. Starting to think maybe my teacher was laid back and i should have had one that focused on better structure and management to make something pretty vs something cluttered.  :P

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #4 on: July 10, 2017, 06:30:24 PM »
Reference:
https://en.wikipedia.org/wiki/Daniel_D._McCracken
Quote
Daniel D. McCracken (July 23, 1930 – July 30, 2011) was a computer scientist in the United States. He was a Professor of Computer Sciences at the City College of New York, and the author of over two dozen textbooks on computer programming. His A Guide to Fortran Programming (Wiley, 1961) and its successors were the standard textbooks on that language for over two decades. His books have been translated into fourteen languages.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #5 on: July 10, 2017, 06:52:33 PM »
Found a youtube video showing "C++ Lesson 04: Making Modular Code" https://www.youtube.com/watch?v=1uqUgMozTXQ

Gonna work on taking what I got and making it more manageable  ;D

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Curly Brace Pairing Identity
« Reply #6 on: July 10, 2017, 07:08:56 PM »
"proper" C++ Would typically compartmentalize things into classes via appropriate OO.

It's all about recognizing what sort of maintenance or improvements are likely in the future- even if you aren't implementing them now- and effectively "laying out the tools", so when it, or perhaps similar changes, come up later you can make the changes easily.

For example the most recent instance where I did this was with some logic intended to delete a particular database object. In order to be allowed to delete, it had to be unused in a number of other tables. Currently, it shows the same message regardless of why it is disallowed, but the function I wrote returns a bitmask flag indicating what other tables contain the item information, so if later it is requested that that information be provided, it will be a matter of adding the appropriate new resource strings, rather than rewriting the function.

of course it is possible to to that too much. I think it is partly something that comes about with experience to get the proper trade off. I've run into situations where something is overarchitected at least as much as underarchitected; though if I had to choose I'd probably go for the former.


I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #7 on: July 10, 2017, 09:10:33 PM »
Years ago when learning use of classes in C++, all examples had the classes in the same .cpp called within the int main { } section of code, and classes listed below that in their own { }. To me seeing it all in the same .cpp which is also how the book had it, I didnt see the benefit of it. To me it felt like GOSUB's of many years earlier when working with BASIC and made the code scattered when following.

So when the professor said you can program a final project any way that you like, I fell back to a very linear ( almost BASIC like ) method which used nested loops but no goto or gosub type of operations in C++ because jumping around seemed too spaghetti code like and I was able to control flow of execution all within loops to pass in and out of loops to reuse functions that way.

 However those programs were maybe 3 pages of code and so easier to scroll up and down the code to follow loop logic. But now that i have this project that has gotten rather large, I see that it will be way better to manage this way with calls to objects vs cramming it all into a massive single .cpp file. So going to break out of my way of clutter programming and try to conform to this other way of better structure for large programs.  :)


The other thing that had most of the class confused was why so many ways of the main of:

int main() {

return(0);
}


int main(void){

}


and

void main(){

}


and there is also

int main(int argc, char* argv[]){

}


as well as

int main(int argc, char** argv){

}



And I stuck with

int main(){

return(0);
}

mainly because I could control where the program ended by where return(0); was placed whereas the other methods you didnt have a return(0); acting as an end of program flag. Reading up on these its a mix of C and C++ and lots of comments on whats better than the other etc.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Curly Brace Pairing Identity
« Reply #8 on: July 11, 2017, 03:35:27 PM »
In addition to The Art of Computer Programming (I have the three-volume set myself, not exactly cheap (nor brief...)) some other good programming books:

Clean Code by Robert C. Martin
Code Complete by Steve McConnell
The Pragmatic Programmer by Andrew Hunt and David Thomas
Refactoring: Improving the design of existing Code by Martin Fowler

Another good one is Programming Pearls by Jon Bentley.

Most of then aren't language specific. They might use one language or another, but the concepts discussed are typically language agnostic.



Also while not directly related to programming I suppose I have to plug Designing Usable Apps by Kevin Matz. It covers things more from a perspective of management but it is still relevant because some of the same considerations make sense in hobby programming as well, and it can be insightful with regards to User Experience. it's sort of like a "The Design of Everyday Things" meets software development.

Has to be said that I might be biased because my copy was free and the author was my Project Manager for a few years.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Curly Brace Pairing Identity
« Reply #9 on: July 12, 2017, 12:32:24 AM »
cool thanks for the book suggestions. Will see if I can get one of them on amazon used or something after looking further into them to see which one I want to get. Just about all book stores dried up which is sad out my way.

Never had any books focusing on clean structure etc. Code examples were clean I suppose with tabs used on lines to indent in stuff in books, with loop or IF logic statements starting far left etc, which I tend to follow but thats all. No code examples ever really had you calling to other code outside of the initial .cpp file etc.