How I write long blocks of code
Another one of my DB redesign led me to create a button that:
1) has 310 lines of code
2) has 24 loops
3) writes to 6 different tables
Trying to keep track of the ends of segments of code this long is impossible with the Ninox editor. I found a program called Sublime Text. They have a version for all OSs. You can load different programming environments so it can color code things properly. Sadly they don’t have one for Ninox, and I don’t know if I’m smart enough to do one for Ninox.
Here is a picture of one of the big advantage of Sublime. You can collapse blocks of code.
You can see the triangles on the left showing collapsed blocks of code. As long as you comment your code correctly you can see what each block is supposed to do. Then you can open a block to edit.
This new pic shows another feature of Sublime. The lines down the left side show you where lines of code are related by indent. For example, line 285 starts a for loop and if you follow the line down we can easily see that line 309 closes this loop. As long as I keep everything inside indented properly, I know that all of the code will run inside this loop.
Of course I didn’t write the code all at once. I wrote each part separately at an appropriate table to make sure it was correct before copying it over to make it all happen in one button.
On line 46, you see the comment to create the ActiveTeams stat records. This block of code starts a line 47 and ends at 171. I can easily find the end just by following the line down until I hit it. Inside this block is a block that writes to the tmStats table. For me to copy this bit (84 lines) and put it inside the ActiveTeam for loop starting at line 46, I just need to make sure all of the code has been indented past the 2nd vertical line.
I’m sure there are other apps that do similar things. This was the one I found that did what I wanted without having to learn how to use it.
It has GIT integration and now Ninox will too so I maybe I’ll be writing/editing code without having to copy and paste. Now that I think about it, I wonder how GIT will be integrated.
What do you do when you are writing long blocks of code? What tips do you have?
3 replies
-
I Fred,
Ninox uses the CodeMirror component to edit its code (https://codemirror.net). However, the latest versions of CodeMirror include most of these functions. The next version of Ninox might allow for code block folding
.
Next, regarding your code, it is extremely rare for a function not to be breakable into reusable sub-functions. In some coding schools, students are forced to limit functions to a maximum of 10 lines to teach them how to logically break down the entire code. But I won’t cast the first stone because I, too, sometimes have functions that are a bit long
.
-
said:
it is extremely rare for a function not to be breakable into reusable sub-functions.Oh, how true that is. That is the one issue with writing the different blocks separately.
The only non-repeatable part, as of this writing, is the create() to the six different tables.
In the second picture you can see that I have two create() commands, there are others with many more. I guess I can figure out a way to create my arrays better so I only need 1.
-
With a bit more work, I'm down to 271 lines. I can probably cut it down even more, but it is taxing on my wee brain.
Content aside
- 1 mth agoLast active
- 3Replies
- 70Views
-
2
Following