PeterPanettone 157 Posted December 19, 2019 (edited) Put the caret on a source code line containing the URL of a web page: Then press Alt+T -> U to run this IDE tool Open URL From Current Line: Here is the source code: program OpenUrlFromCurrentLine; {$APPTYPE CONSOLE} {$R *.res} uses Winapi.Windows, Winapi.ShellAPI, System.Classes, System.RegularExpressions, System.SysUtils; var ThisLine: Integer; ThisLineStr, ThisURL: string; ThisSource: TStringList; begin // This is explained at: https://en.delphipraxis.net/ try if ParamCount > 0 then begin if ParamCount = 2 then begin if FileExists(ParamStr(1)) then begin if System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then begin ThisSource := TStringList.Create; try ThisSource.LoadFromFile(ParamStr(1)); if ThisSource.Count >= (ThisLine) then begin ThisLineStr := ThisSource[ThisLine - 1]; ThisURL := TRegEx.Match(ThisLineStr, '\b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]', [roIgnoreCase]).Value; if ThisURL <> '' then Winapi.ShellAPI.ShellExecute(0, 'open', PChar(ThisURL), nil, nil, SW_SHOW); end; finally ThisSource.Free; end; end; end; end; end; //Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. Build it. Then create a new IDE tool and configure it like this: Now you are ready to have fun. Merry Christmas! Edited December 19, 2019 by PeterPanettone 1 Share this post Link to post
Anders Melander 1782 Posted December 19, 2019 Nice. But: Early Return ... if ParamCount < 2 then Exit; if not FileExists(ParamStr(1)) then Exit; if not System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then Exit; ... 1 1 Share this post Link to post
PeterPanettone 157 Posted December 19, 2019 4 minutes ago, Anders Melander said: Nice. But: Early Return ... if ParamCount < 2 then Exit; if not FileExists(ParamStr(1)) then Exit; if not System.SysUtils.TryStrToInt(ParamStr(2), ThisLine) then Exit; ... Thanks. But how could this improve the tool? Share this post Link to post
Anders Melander 1782 Posted December 19, 2019 17 minutes ago, PeterPanettone said: But how could this improve the tool? It's a refactoring; It improves the readability of the code without changing the functionality. 1 1 Share this post Link to post
PeterPanettone 157 Posted December 19, 2019 (edited) 12 minutes ago, Anders Melander said: It's a refactoring; It improves the readability of the code without changing the functionality. IMO it complicates the readability. But that's a personal view. Did you get this from a book or is this your own idea? BTW, Pascal Expert found 0 issues: Edited December 19, 2019 by PeterPanettone 1 Share this post Link to post
Anders Melander 1782 Posted December 19, 2019 2 hours ago, PeterPanettone said: Did you get this from a book or is this your own idea? That's a strange question. It's based on the experience that unnecessary nesting makes the code harder to read. Sure it's a personal preference, some like it, some don't (just google Early Return - I'm sure you will find one or two that doesn't advocate it), but at least one of your "if" blocks are complete superfluous and would be removed by the optimizer if it was worth anything. 2 hours ago, PeterPanettone said: BTW, Pascal Expert found 0 issues What's the relevance? Are you saying that because some simple rule based validator doesn't flag your code then it can't be improved? - Because I can spot several goofs in it. 1 1 Share this post Link to post
PeterPanettone 157 Posted December 19, 2019 (edited) Anders, I love you, Merry Christmas! Edited December 19, 2019 by PeterPanettone 1 Share this post Link to post
PeterPanettone 157 Posted December 19, 2019 Nesting shows the BEAUTY OF LOGIC. 1 Share this post Link to post
aehimself 396 Posted December 19, 2019 5 hours ago, Anders Melander said: It's a refactoring; It improves the readability of the code without changing the functionality. I'm a fan of early returns, I use them all the times. For me it does improve readability but my colleagues complain that it makes it harder for them. This is a matter of preference IMO, nothing else. 1 Share this post Link to post
PeterPanettone 157 Posted December 19, 2019 1 minute ago, aehimself said: I'm a fan of early returns I too, if they are LOGICAL. But the above are not. 1 1 Share this post Link to post
aehimself 396 Posted December 19, 2019 11 minutes ago, PeterPanettone said: I too, if they are LOGICAL. But the above are not. For me the changes make sense what @Anders Melander mentioned. But please - do not be OFFENDED. We are NOT saying it's bad code. As stated above - it's only PREFERENCE. 🙂 Share this post Link to post
David Heffernan 2345 Posted December 20, 2019 7 hours ago, PeterPanettone said: Did you get this from a book or is this your own idea? I doub you'll find a single book written in the past 25 years that would advocate nesting over early return. All the most respected experts agree on this and have done for years. Which books are you reading? 1 1 Share this post Link to post
dummzeuch 1505 Posted December 20, 2019 7 hours ago, David Heffernan said: Which books are you reading? Science Fiction 😉 SCNR 1 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 (edited) 1 hour ago, dummzeuch said: Science Fiction Thomas, didn't you say you are a fan of MANGA PORN COMICS? Edited December 20, 2019 by PeterPanettone Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 (edited) 13 hours ago, Anders Melander said: It's based on the experience that unnecessary nesting makes the code harder to read. That's not an "experience" but a FALSE ASSUMPTION (like e.g. climate change caused by cow farts). Edited December 20, 2019 by PeterPanettone 1 Share this post Link to post
Anders Melander 1782 Posted December 20, 2019 30 minutes ago, PeterPanettone said: MANGA PORN Code style to tentacles in three posts. I didn't see that coming. 1 Share this post Link to post
aehimself 396 Posted December 20, 2019 1 minute ago, Anders Melander said: Code style to tentacles in three posts. I didn't see that coming. There's always a connection. It's not spaghetti, it's tentacle code! 1 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 I have found a new DEFINITION of "Early Return": "STUPIDITY TRYING TO LOOK SMART" 1 Share this post Link to post
aehimself 396 Posted December 20, 2019 4 minutes ago, PeterPanettone said: That's not an "experience" but a FALSE ASSUMPTION. No, it's not. @Anders Melander said: "It's based on the experience that unnecessary nesting makes the code harder to read." which is not false and not an assumption. It's a general statement. I only can quote myself. Don't get offended. We are talking about personal preferences here. 1 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 1 minute ago, aehimself said: unnecessary nesting Define "unnecessary nesting". Share this post Link to post
aehimself 396 Posted December 20, 2019 1 minute ago, PeterPanettone said: Define "unnecessary nesting". I'm no expert, but I'd say it's... nesting, which is... unnecessary? 😐 2 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 1 minute ago, aehimself said: I'm no expert, but I'd say it's... nesting, which is... unnecessary? 😐 That's not a definition. Try harder. Share this post Link to post
aehimself 396 Posted December 20, 2019 1 minute ago, PeterPanettone said: That's not a definition. Try harder. There you go, I found it: https://dictionary.cambridge.org/us/dictionary/english/sarcasm I'll try to act like an adult and stop this discussion right here. If you would like to learn more about coding styles, Google is - as always - your friend. In the mean time, please learn how not to get offended when people say their favorite color is not the same as yours. 2 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 1 minute ago, aehimself said: https://dictionary.cambridge.org/us/dictionary/english/sarcasm That's the proof, thank you: There is no such thing as "unnecessary nesting"! 1 Share this post Link to post
PeterPanettone 157 Posted December 20, 2019 (edited) Let's make an intelligent conclusion and let's agree on it: Good code styles are very important and we should follow them. But do not try to apply code styles to your home-baked Christmas cookies! Edited December 20, 2019 by PeterPanettone Share this post Link to post