15
u/W2Wizard Aug 17 '20
What's the thing you guys use for these snippets?
28
u/mind_overflow Aug 17 '20
3
Aug 17 '20
Carbon | Create and share beautiful images of your source code
That just looks like a iTerm2 screenshot on macOS like here
1
48
u/thisonehereone Aug 17 '20
You can add ranges to case statements in more modern versions of c#. So you could knock this down to 5 cases in the top section.
case int n when (n < 8 && n >= 3 ):
case int n when (n < 17 && n >= 12 ):
case int n when (n < 26 && n >= 21 ):
case int n when (n < 35 && n >= 30 ):
case int n when (n < 44 && n >= 40 ):I would prefer to know what the ranges mean though. Why are some +1 and others +4? That's what helps it make sense.
15
u/cadatoiva Aug 17 '20
Language is Java, not C#
His other comments show that it's mod 9 and it increments from 3-8 and then the +4 skips 0-2 of the series.
4
u/thisonehereone Aug 17 '20
I see. But still I want a why in my code.
10
u/mind_overflow Aug 17 '20 edited Nov 24 '21
Ok, here goes the explanation.
The software I was working with had a table, however the only way to access data in that table was by the data's index in the table, starting from 0 (much like an Array, visually represented as a Table however). The table had 9 columns, and I had to leave the first 3 empty. example:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |etc. so the only indexes I needed were (3, 4, 5, 6, 7, 8) and a sum of 9 (repeated an increasing number of times) plus the base index number. So the formula is:
[9x + index], where 3 <= index <= 8.I had to leave columns (0, 1, 2) empty because they already had important data.
There you go!
10
u/FrostBite_97 Aug 17 '20 edited Aug 18 '20
Do this
If v%9>=0&&v%9<3 Return 3; Else if v%9 3-7 Return cnt+1 Else return cnt+4I wrote on mobile the formatting sucks I know.
1
u/AutoModerator Aug 17 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/FrostBite_97, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
0
u/AutoModerator Aug 17 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/mind_overflow, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/Mexenstein Aug 17 '20
Java, Microsoft Java, tomatoe, tomato.
0
u/cadatoiva Aug 17 '20
Normally, I would agree. But this feature is not present in both, only in c#.
4
u/Mexenstein Aug 17 '20
It was a joke, but I'm learning that this sub has a low tolerance for those. Every time I make one I get downvoted. Hope this time it's lesson learned.
2
Aug 17 '20
I dont know to much about switches, but whats the point of having all those checks that dont do anything? Like wouldnt you only need to have cases if it does something?
4
u/mind_overflow Aug 17 '20 edited Aug 17 '20
Switches work in a weird way, very different from if statements. Switches (or actually, cases) work until they hit a
break;orreturn;statement. What it means is that:switch(number) { case 1: case 2: case 3: return "AAA"; case 4: return "BBB"; default: return "CCC"; }if the given number is 1, 2 or 3, then the code is executed until
return "AAA";is hit. so, even if directly aftercase 1:I don't write some code, it still executes the first code that it finds, until abreak;orreturn;statement is reached. If no condition is met, thedefaultcode is ran. so:- 1 -> AAA
- 2 -> AAA
- 3 -> AAA
- 4 - > BBB
- 0, 5, 6, 7, ... -> CCC
Hope this helps!
EDIT: formatting
3
Aug 17 '20
Ooooh thats helps alot actually, is there no way to do something like case: <=3 though? Anyways, thanks
2
0
u/AutoModerator Aug 17 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/mind_overflow, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
1
u/-Dueck- Aug 17 '20
What exactly is a Hel position? I assume that's short for something?
2
u/mind_overflow Aug 17 '20 edited Mar 01 '22
correct, that's short for something. I don't remember very well (the code is obviously undocumented), but it was something along the lines of "horizontal elements position", which actually makes the code look even worse. because:
I'm dealing with a table, not anything linear (?)
If I ever had to give it that name, it'd be
incrementHElPos()(with capital E), if notincrementXElPos()at best (orincrementElementPosX()).
1
u/TastyDumplingSoup Aug 17 '20
What the fuck?
3
u/mind_overflow Aug 17 '20
I mean, that's why we are all here, you shouldn't be surprised :lmao:
1
u/TastyDumplingSoup Aug 17 '20
But like, what the hell does it do and why?
2
u/mind_overflow Aug 17 '20
There you go! I just explained it here: https://www.reddit.com/r/badcode/comments/ibajcb/todo_less_dirty/g1w5twn?utm_source=share&utm_medium=web2x
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 06 '20
Image Transcription: Code
public static int incrementHelPost(int currentPos) // todo: less dirty
{
switch (currentPost)
{
case 3:
case 4:
case 5:
case 6:
case 7:
case 12:
case 13:
case 14:
case 15:
case 16:
case 21:
case 22:
case 23:
case 24:
case 25:
case 30:
case 31:
case 32:
case 33:
case 34:
case 39:
case 40:
case 41:
case 41:
case 43:
return currentPos + 1;
case 8:
case 17:
case 26:
case 35:
return currentPos + 4;
default:
return 3;
}
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
3
1
0
Aug 17 '20
[deleted]
1
u/AutoModerator Aug 17 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/mind_overflow, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
132
u/[deleted] Aug 17 '20
I don't think this is bad code. It's very clear what's happening. You could do a bunch of checks, but possibly if the values are within just this range, then I think this is fine.