4 ms·
I never understood why is declaring variables in the middle of the code a "pro" for some. In my experience that's a "con" especially because of maintainability.
by unnouinceput 3y ago
I never understood why is declaring variables in the middle of the code a "pro" for some. In my experience that's a "con" especially because of maintainability. Same way you do a comment before declaring a function to explain its purpose, you do a comment for each variable to declare its goal within said function. That's proper maintainability for a project.
Also Delphi, for past 2 years, has this "middle" variable declaration style, and I hate it, never used it. So your comment missed the mark. If you want to advocate "don't learn Pascal in 2023" please do more research and come up with better argument.
- tnecniv 3y agoLike dynamic typing and other things, declaring things in the middle of your code block seemed like a great idea to young me, but as I’ve gotten older, things like organizing variable definitions, strict use of types (or the kind of sucky type hints in Python), and other kinds of self-documenting declarations have become much more appealing to me. It might save “real work” from happening if you measure by lines of code, but you pay that price later when debugging or reading code you haven’t touched in a long time.
- Al-Khwarizmi 3y agoA clear example of why it is a pro is the index of a for loop. Why would it be more maintainable to declare all for loop indexes at the beginning of a function? They are basically bound variables that are only meaningful within their loop, and they are usually not even worth commenting.
- unnouinceput 3y agoMore often than not, when maintenance really matter and juniors really choke, the algorithms that powers core functionality usually have nested loops and conditionals. So if you start using inline variable declaration you really have a hard time "where did this was declared". Combine that with compiler switches and you have a recipe for disaster when declaring inline variables vs. a clear section of variables right after function header, where you put all comments to specify what those variables do. Remember, initial phase of a project is only 10% of it, the maintenance and expanding its functionality is the rest of 90%. Inline variable declarations are a real PITA for that phase.
- zabzonk 3y agodefining all variables as near to their point of use as possible is a pretty obviously good thing, for readability if nothing else.
- clouddrover 3y ago> I hate it, never used it You should at least use it in for loops. It's a useful improvement, and especially handy in combination with type inference. For example: for var i := 1 to 10 do Inc (Total, i); i is declared inline, it's only in scope inside the for loop, and the type is inferred from the bounds of the loop. You could of course use "for var i : Integer := 1 to 10 do" if you wanted to be explicit about the type. Documentation: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Inline_Variable_Declaration https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Inli...
- unnouinceput 3y agoPlease do another "for" right after this one, identical, and then revisit this comment (hint: you'll get a syntax error).
- clouddrover 3y ago> hint: you'll get a syntax error No. You won't. You didn't even try it, did you. Read the documentation, use inline variables, and be happy.