Saturday, May 18, 2024
HomeProgrammingA Method to Examine Advanced Code With Thoughts Maps | by Jonathan...

A Method to Examine Advanced Code With Thoughts Maps | by Jonathan Bispo | Sep, 2022


Via Freemind

Picture by Natasha Connell on Unsplash

Studying and understanding advanced code is a really exhausting process. Not all the time the code has good variable names, concise and pure capabilities, checks, and so forth. Sadly rewriting the code is never an possibility.

In my day-to-day, I have to learn and perceive previous code — written years in the past, earlier than the nice practices in software program improvement had been created. Not solely this sort of code, however even current code, written in new languages, might be very exhausting to grasp on the first time.

I’ve tried to discover a option to analyze this sort of code with much less effort and discover out that through the use of thoughts maps, the code might be seen in a greater means. I all the time favored thoughts maps. Since 2008 I used it to prepare a number of info hierarchically. Thoughts maps are probably the greatest (or the most effective) methods to review and retailer info, in my view.

On this article, I’ll use the Freemind thoughts mapping instrument.

To elucidate the method, I’ll use one instance in Python. I’m a computing professor and after I train programming languages I all the time create with my college students a Tic Tac Toe recreation. To detect if participant 1 or participant 2 wins in every spherical, run this algorithm:

Begin a 3×3 matrix stuffed with zeroes.

When participant 1 makes a transfer, I write 1 within the cell. When participant 2 makes a transfer, I write 2 within the cell. So that is an instance that participant 1 wins in the primary diagonal:

To detect that participant 1 wins, do the product on the strains, columns, and diagonals. If the result’s 1, participant 1 wins; if the result’s 8, participant 2 wins; if the result’s 0, 2 or 4, no one wins on this line, column, or diagonal.

That’s it, easy.

That is the Python perform with the algorithm applied:

This perform is written in a means that I take into account enough: the search in strains, columns, and diagonals is splitted into nested capabilities inside the primary perform, and it makes use of enums to explain gamers and winners. As well as, the early returns (that may be thought-about a foul factor) on this algorithm will not be an issue — on condition that the primary movement is straightforward to see and perceive.

Even the method described on this article is targeted on advanced algorithms, not properly written like this. I selected this instance as a result of is extra easy to elucidate and perceive.

Now, with the code and Freemind opened I copy and paste the code, and voilà! The magic is finished.

Freemind acknowledge tabs as youngster nodes routinely, so the code and not using a tab is positioned on the primary node slice, with one tab on the second node slice, and so forth.

With Python, because the indentation is a part of the supply code, it’s even simpler. However in languages like C#, for default the braces are positioned in a single line beneath the assertion, the thoughts map might be tough to learn, with a number of braces.

Instruments like this https://formatter.org/csharp-formatter with formatter set to Chromium and line width set to a excessive worth, might help.

One other instance: if the code has an uncommon indentation (like in Visible FoxPro when an if assertion may be very lengthy), it’s common for builders to interrupt the if situation, put semicolon, go to the road beneath add use tabs to place the code aligned with unique if. On this state of affairs, it’s worthwhile to modify manually the nodes in a thoughts map.

As stated earlier than, this instance is straightforward — with few capabilities, well-written code. However think about a code with if inside else, inside for, inside whereas, inside attempt, and a number of these statements, with the mutability of variables, and so forth.

Not all the time the define of IDE might help to visualise the code. When you’re studying the content material of an if assertion, it’s possible you’ll neglect during which construction this if assertion was.

With the code positioned in a thoughts map construction, you’ll be able to visualize this extra simply, and hierarchically, which is a pure option to learn and write code.

Moreover, Freemind provides a number of instruments: write notes, colorize, create hyperlinks between the nodes, draw clouds across the nodes, and so forth. All of those can be utilized to assist perceive advanced codes.

I hope this trace might help you to grasp advanced (and sadly a number of instances, unhealthy) codes. Builders spend way more time studying than writing code. So enhancing readability is all the time a goal in software program improvement.

In a super world, this sort of method wouldn’t be vital! All the time attempt to write good code. If it’s worthwhile to use this system, is usually a clue that refactoring is critical. Clear Code e book from Robert Martin and understanding ideas of useful programming are good begin factors to writing higher code.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments