|
I write docs and grab screenshots and paste them into Google Docs.
Sometimes those documents end up being articles I post to CodeProject.
Images
However, CP wants the images to be uploaded to CP servers as separate image files.
Google Doc HTML Creator (Save as...)
You can download the document as a zipped HTML and Google Docs will pull out every image from your document and save them in a \images directory. That's very cool.
That's very nice because now you have all of the images saved as PNGs and you didn't have to do any work.
The Problem With The Images
The problem is that the images are all out of order.
They're named like: imageXXX.png in consecutive order (image1.png, image2.png).
Not Same Order As Your Document
However, the order that they are numbered in is not the order they appear in your Google Docs document. I have no idea why that would be true. What is wrong with some devs' minds?
Finally, i haz teh Codz!!
Here are the steps.
1. Download your Google Doc as a zip html
2. unzip it to a local directory
3. Open in your favorite browser (It's FireFox, right? Mine too.)
4. Hit F12 to open dev tools / console.
5. Run the following script**:
document.querySelectorAll("img").forEach((s, counter=1) => (console.log("mv " + s.src.substring(s.src.lastIndexOf("/")+1,s.src.length) + " 0"+counter++ + ".png")))
This will generate output on your console that looks like the following:*
mv image38.png 00.png
mv image3.png 01.png
mv image36.png 02.png
mv image28.png 03.png
mv image9.png 04.png
mv image29.png 05.png
mv image11.png 06.png
mv image37.png 07.png
etc...
6. Copy all of those lines, go to your images folder and run it.
Now all of your images are renamed in order that they appear in the Document.
This will make it far easier when you copy your Google document to CP and upload the images.
You can insert them back into your document in order. It's much easier.
* If you are on a Windows machine (sorry for you ) then simply use the following script (changes mv to ren).
Windows Version
document.querySelectorAll("img").forEach((s, counter=1) => (console.log("ren " + s.src.substring(s.src.lastIndexOf("/")+1,s.src.length) + " 0"+counter++ + ".png")))
** Yes, you can make the script even better by cleaning up some things, but this gets you there and is good enough for what I need.
|
|
|
|
|
Writing a simple console app.
I wrote this to insure there is at least one argument provided by user.
if (args.Length < 1){
Console.WriteLine("Need at least one arg.");
return;
}
Interesting thing is that Visual Studio Code has these little helpers that pop up at various times which state [Show fixes]. This one says, "invert if"[^].
If you click, it changes the code to:
if (args.Length >= 1){
return;
}
Console.WriteLine("Need at least one arg.");
return;
Do you find that clearer?
I don't.
In my case, the if statement occurs at the top and if it is not fulfilled then the app exits.
In that case there is no need to think about other code.
Plus, the code that executes normally will not be wrapped in any outer if statement, instead it will simply following the if statement in a normal reading flow.
Inverted Case
In the inverted if then when there is at least one argument then all of your base code is now wrapped in the if statement and you have to think backwards. It's weird.
AI you have failed me.
|
|
|
|
|
I use yoda conditionals all the time (due to cutting my teeth on C++ back before the compiler was smart enough to warn on accidental assignment). I stuck with it due to the fact that it's a habit I spent so long at that it would take me at least as long to unlearn. At least I'm consistent about it.
So I can see it being at least a little bit helpful sometimes for a confusing condition, but that's me.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: I use yoda conditionals all the time
Haven't heard that term before, but seems to apply, it does.
|
|
|
|
|
This is not a true case of yoda conditionals or at least not in the sense I've heard the term.
For me, yoda conditionals are written like:
if (1 >= args.Length)
{
Console.Write ("Not enough arguments");
return;
}
The inversion refers to the order of terms in the if and the reason for it is that a construct like:
if (1 = args.Length)
(note the missing equal sign)
will get you slapped with a big fat error message ('1' is not a l-value). The "normal" order might or might not produce a warning depending on compiler's whims.
In your case it's just a case of compiler being annoying. Sometime I feel it's becoming almost like Clippy: "It seems you want to write an if statemenet. Do you need help with that?".
Here is a random example taken straight out of some code:
assert (str);
while (*str > 0 && *str <= ' ')
str++;
And it flags the while statement with an IntelliSense warning: "Dereferencing NULL pointer". Heck no! That's why I put the assert right before it.
Mircea
|
|
|
|
|
I use the "first the constant" rule (I didn't know it was called "yoda conditionals", nice name hehehe) in comparisons for equality.
But for greater / lower than I don't use it for readability.I find it easier to read when it is "var <= value " than "value >= var " in a check_lower_limit()
Same thing I try to make my conditions to be read as if (true)
I mean, I don't like "if (!var) ", I prefer to write "if (false == var) " this way is 100% clear on the first sight.
Or naming the variables in a way that they meaning is "true". This is something that comes from working in industry PLCs. I have had sensors called "part_exist" where the "1" was meaning "empty" (security against cable breaks), to look an "if (part_exist_x == true) PutPartInPlace(x); "
I always renamed such sensors to follow the rule "name means true" so that the same condition check as above would read "if (place_empty_x == true) PutPartInPlace(x); "
Just personal taste...
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
If there's a var called part_exist, and 1 (or true) means the part does not exist, then that's not personal taste, it's just plain bad programming! The entire reason for selecting a variable name is to accurately convey the data stored within...Some day I imagine AIs will be smart enough to warn us for those type of code smells
|
|
|
|
|
When the electronic planer does the eplan in the office and never goes to the production hall, and just copy paste things from other projects that never saw in person... it happens more often than it should.
At the end I had an agreement (after several discussion he logically didn't win). Once I got to the machine and did my IO-Tests... I would give him my variables list and he would accept it and correct the E-Plan without complaining.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
asserts( ) can be removed entirely based on compiler settings. That would explain the warning.
|
|
|
|
|
I know, but I didn't have NDEBUG defined in that configuration so assert was active.
Don't get me wrong, I agree with having Intellisense looking over my shoulder and flagging potential issues but sometimes it gets annoying just like sometimes a coworker can be annoying when you do pair programming. And just as with a coworker, it feels good to bitch from time to time
Mircea
|
|
|
|
|
They are hints. (resharper C++ also do that)
Sometimes they work, sometimes they don't.
In that case, it is not useful
I'd rather be phishing!
|
|
|
|
|
Maximilien wrote: They are hints.
I agree. Just suggestions.
Maximilien wrote: In that case, it is not useful
Thanks for the confirmation. That's what I thought, but was wondering if I missed something.
Also, it really is just a matter of opinion on this one since either would work.
|
|
|
|
|
raddevus wrote: AI you have failed me. Problem found.
BTW... I am with you in this case.
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Nelek wrote: Problem found.
Nelek wrote: BTW... I am with you in this case.
Thanks. I thought all sane devs would be. The rest are not devs or are not sane or both.
|
|
|
|
|
I'm curious if this error still pops up if you write more code below the if statement. I've never seen this before. My initial hunch is it might think you're doing the "if-good-else-bad" pattern since the only active code is in the if statement currently, and trying to move it around to the "early-exit/pre-condition" pattern which I personally do think is way cleaner. Or it may be attempting some pointless hyper-optimization?
|
|
|
|
|
Jon McKee wrote: I'm curious if this error still pops up if you write more code below the if statement.
That's a very good question.
Here's the updated code that contains more code now. Note: this is simple code that is going in a book.
static void Main(string[] args)
{
if (args.Length < 1){
Console.WriteLine("Please provide 1 argument to indicate the command you want to run.\nUsage: getInfo <command-name>");
return;
}
switch (args[0].ToLower()){
case "os":{
Console.WriteLine($"OS : {Environment.OSVersion}");
break;
}
case "pwd":{
Console.WriteLine($"The current directory is: {Environment.CurrentDirectory}");
break;
}
case "cl":{
Console.WriteLine($"Command line was: {Environment.CommandLine}");
break;
}
case "sysdir":{
Console.WriteLine($"System dir: {Environment.SystemDirectory}");
break;
}
case "mname":{
Console.WriteLine($"Machine name: {Environment.MachineName}");
break;
}
}
}
Here's a snapshot of the invert if[^] that still shows up in VSC as a hint.
Here's the code you get if you invert now:
static void Main(string[] args)
{
if (args.Length >= 1)
{
switch (args[0].ToLower())
{
case "os":
{
Console.WriteLine($"OS : {Environment.OSVersion}");
break;
}
case "pwd":
{
Console.WriteLine($"The current directory is: {Environment.CurrentDirectory}");
break;
}
case "cl":
{
Console.WriteLine($"Command line was: {Environment.CommandLine}");
break;
}
case "sysdir":
{
Console.WriteLine($"System dir: {Environment.SystemDirectory}");
break;
}
case "mname":
{
Console.WriteLine($"Machine name: {Environment.MachineName}");
break;
}
}
}
else
{
Console.WriteLine("Please provide 1 argument to indicate the command you want to run.\nUsage: getInfo <command-name>");
return;
}
}
|
|
|
|
|
That's so bizarre. It seems like the purpose of this rule (from some short googling) is to reduce conditional nesting and/or cyclomatic complexity. But in this example it actually increases nesting by changing away from an early-exit guard clause and doesn't change the CC at all. It's like it's doing the exact opposite of what it should be doing
|
|
|
|
|
Jon McKee wrote: seems like the purpose of this rule (from some short googling) is to reduce conditional nesting and/or cyclomatic complexity.
Thanks for researching that and confirming that you find it odd too. And for putting some data behind it. I really thought it was strange but hadn't defined why very well.
Great info.
|
|
|
|
|
I don't like the args.Length < 1 test - it's either going to be greater than or equal to 1 , or equal to 0 .
Just for fun, you could also introduce some C# 8 into the mix, and support multiple commands:
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please provide 1 argument to indicate the command you want to run.\nUsage: getInfo <command-name>");
return;
}
foreach (string arg in args)
{
Console.WriteLine(arg.ToLowerInvariant() switch
{
"os" => $"OS : {Environment.OSVersion}",
"pwd" => $"The current directory is: {Environment.CurrentDirectory}",
"cl" => $"Command line was: {Environment.CommandLine}",
"sysdir" => $"System dir: {Environment.SystemDirectory}",
"mname" => $"Machine name: {Environment.MachineName}",
_ => $"Unknown command: '{arg}'",
});
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: I don't like the args.Length < 1 test
Yeah, admittedly this is just shorthand for "I only want to know if it is 0" otherwise keep on running. I talk about that in relation to the code however -- since this is really just part of a beginning code sample that is part of a larger context of explaining how to get arguments from command-line. I talk about this being a cheat that will simply ignore any arguments that are after the first one.
This is all part of a book (Programming Linux With .NET Core) and part of the beginning stages of teaching an early beginner.
The foreach code is a really good example of things that will come in later explanations and is much more elegant. Thanks
|
|
|
|
|
Whoa! Hold the phone. I hadn't really dived into the C# 8 stuff yet. So a switch supports an expression syntax now? That's clean.
|
|
|
|
|
|
That's a tool, not even a suggestion. And tools might be used when they are useful.
So: when?
E.g. you decided to add an else clause, and you prefer the order the other way round.
E.g. you have both the normal and the else clause, and decide the other way round looks better.
Then, the new code is just a mouse click away.
A tool. To be used when appropriate.
Not:
Here's a hammer. Now you have to use it, even when there's no nail.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Bernhard Hiller wrote: Here's a hammer. Now you have to use it, even when there's no nail. But if you only have a hammer... would not come the moment when all what you see is a nail?
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
I agree. It's just that any time a tool attempts to have some "intelligence" about it then you begin to think it may have more context to the situation. But, this probably really is more like pop up code snippets and you take them or leave them. Also, because it has the initial pop up that states, "Show fixes (Ctrl+.)" it leads you to believe that something is wrong.
|
|
|
|