Code readability #1
Replies: 2 comments
-
|
You are right about everything, thanks for making the effort to write this. I should really grow out of my time of having fun with the syntax 😁 Since I'm no longer working on all this alone and people like you actually read my code I think it would be disrespectful not to follow your advice which I understand is the generally accepted standard (for a reason). Also, because the current GetLocalPath function doesn't fit into the StackOverflow answer anyways, I don't have that excuse anymore. Will change stuff as soon as I get to it! |
Beta Was this translation helpful? Give feedback.
-
|
Addressed most of the things you brought up here: 0aae034 If you have additional requests please let me know, otherwise, I'll close the discussion for now :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Making code compact could be a good practice in moderation. However, my opinion is that this repo does this in detriment of readability and maintainability. My hope is that the below will bring solid arguments to back my opinion and possibly determine changes to this repositorya and style of coding.
I will address two important points:
:to put multiple statements in one linein terms of how readability, maintainability and debugging is affected.
I also use the
:to put multiple statements in one line, like in the following scenario. Consider the code:which is the most basic way that most coders will write code. My preference would be to rewrite it like this:
for several reasons:
:.Dimstatements cannot have breakpoints anywayIt is generally an accepted best practice, across most languages, that declaring each variable on it's own separate line is more human readable. Here's why:
Even more so, VBA is not very friendly when it comes to declaring multiple variables on the same line. Most users don't even realize that each variable's type must be declared explicitly and something like
Dim a, b As Stringactually declaresaas Variant. This could be an issue if a different user is changing code at a later stage and starts deleting types because he/she considers that the line should be shorter.Let's consider an actual example from this repo:
Let's rewrite it in the basic way, without any
:statement delimiters:or more compact by using
:statement delimiters:I think both the 2nd and the 3rd form of the function are superior to the 1st when it comes to readability, future maintenance, debugging and source control.
The above example might not be too obvious. Let's dicuss lines 286 to 314 from this gist.
Although I know this last gist example was intended to be written in a single function for portability, I trully believe that end users should make the effort to copy-paste multiple related functions. It almost feels that the end users are indirectly abusing the good will of the author, without knowing. Since copy-paste is involved anyway, it might as well include multiple adjacent methods. So, there's no real reason to have such a long function. On the contrary, the approach forces a very complex method with tens of variables which in turn forces saving a few lines of code space by declaring too many variables on the same line and using
:to put multiple statements on the same line. This leads to tens of minutes of more work in trying to debug an issue (or to add functionality) when if fact the final users might only spend 5 seconds extra to make sure they copied all the needed methods which are anyway adjacent.To make things worse, variables are declared nowhere near where they are actually used which adds to the mental strain of trying to keep the whole function in mind when performing maintenance. Since the number one priority of any programmer should be to manage complexity, compact code like the one above should be avoided.
Control structures should not use the
:statement delimiter.There is no real gain in having this:
as opposed to this:
The space save does not justify the loss of readability.
It can be argued that this:
saves space and is not too long to cause any issues.
However:
is more readable, generally expected and it allows breakpoints on all 3 lines if needed.
Beta Was this translation helpful? Give feedback.
All reactions