# Tag Info

## New answers tagged formatting

0

return "page.aspx?id={id}&ref={ref}".Replace("{id}", id) .Replace("{referrer}", referrer); The above can be written since C# 6 and ECMAScript 6 as.. C# return $"page.aspx?id=${id}&ref=${referrer}"; Javascript return page.aspx?id=${id}&ref=\${referrer};

2

A view more suggestions with explanation to the current source code (first BlackRedBST). I deleted public static final NodeRB NILL = null; because null can be used instead of NILL for less code. I also deleted public static final boolean RED = true; and public static final boolean BLACK = false; because color is never used (in the given code). There is no ...

6

To truncate a double to 3 decimal places and remove the decimal separator, is I think more easily done by multiplying by 1000 and casting to an int. Padding the result to 9 digits is easily done with a format specifier in the ToString method. It could look something like this: static string FormatNum(double num) => ((int)(num * 1000)).ToString("D9"); ...

1

STYLING According to PEP-8, there are a few improvements to be made to your code: Imports: Your imports should go in alphabetical order Variable/Function Names: You should use snake_case instead of camelCase Parameter Names: This may just be how I view your code, but how your parameters are named look like your taking in objects. You should use lowercase ...

-1

It seems a main thing you need to learn about is f-strings These are an update to the old format method. print "Rule " + str (numberofrows) becomes print(f"Rule str(numberofrows)") and "name " + '"{}"'.format(CellContent) becomes f"name {CellContent}" NOTE Python 3 uses () for print statements It is also worth using a PEP8 checker (such as black - ...

1

Building on @RickDavin. VB.Net also has a robust If() function that shortcuts the checks (unlike VBA's IIf() function) If hours = 1 Then hourString = Cstr(hours) & " hour" Else If hours > 1 Then hourString = Cstr(hours) & " hours" End If Becomes hourstring = If(hours = 1, "hour", "hours") @Heslacher's function can then be: Private ...

2

As @Hesclacher has given a fantastic answer, which is not only one that I have upvoted but I would personally mark it as the correct answer if I had the power, I am hesitant to improve upon it. But I would like to offer constructive feedback on your original code. I have a personal distinction between being a VB Coder and a .NET Developer. I think you can ...

6

You should look into the TimeSpan struct, which provides a nice method TimeSpan.FromSeconds(). Having filled a TimeSpan struct by e.g calling the FromSeconds() method, you can just access its properties Seconds, Minutes and Hours. Instead of concating strings like you do, you should consider to use a StringBuilder. To get rid of the If ... Else If you ...

1

To help with readability, I'd just import datetime.datetime and then alias it, that way you aren't typing datetime.datetime all over the place: from datetime import datetime as dt def time_converter(time): midday_dt = dt.strptime('12:00','%H:%M') time_dt = dt.strptime(time, '%H:%M') if time_dt >= midday_dt: if time_dt >= dt....

2

Pedantically, if (snprintf(buff, sizeof(buff), format, ...) >= SSIZEOF(buff)) goto err; is an insufficient test. Test for < 0 is also needed. The snprintf function returns the number of characters that would have been written had n been sufficiently large, not counting the terminating null character, or a negative value if an encoding error ...

0

This is the code that Toby Speight suggested in his answer: /* * int sbprintf(char buff[restrict], int *restrict written, * const char *restrict format, ...); */ #define sbprintf(buff, written, format, ...) ( \ { \ int ...

1

This is a good and useful idea. The name is intuitive and memorable. My first thought was that if a pointer were passed, then we'd use the size of the pointer, but you've found a good way to ensure only arrays are passed. Obviously, this means that we can't use this for those occasions where we build up the content in pieces (with several prints into a ...

3

The API you expose is very weird. Consider the following call: padString("foo", true, '\t', 5); The result being "" is not really obvious. Let's just rewrite this from scratch and then compare it to the existing code. First we're going to have some requirements: padString should not make the input smaller. The code you present correctly deals with that,...

10

You should generally try to avoid global variables (like tableSize). Since they can be used everywhere ("globally"), at the worst case you need to read all of the code to figure out where they come from and how they're used. In this case, tableSize could become a parameter of multTable. While it will probably work on the compilers you'll come across, void ...

2

Good start mate. Few items for you to consider: Things tend to get complicated by law of entropy all by them self - your help is not needed (aka: KISS) Use natural names so your code reads as close to English as possible. Learn to be a good listener - if an experienced user tells you huge files, like pictures are not appreciated needlessly, take pain and ...

3

Is the graphic representation of a data structure an integral part of that data structure (val gString = myGrid.asText) or separate and independent from the data structure (val gString = asText(myGrid))? I tend to favor the former, but if the Grid API is solid and supplies everything needed for one or more graphic representations, then the latter is ...

2

General Feedback The library looks useful and the code appears to adhere to style guides like PSR-2 for the most part. I don't see docblocks in the code. Each method should have a description of what it does, along with any parameters and the return type. Then anyone reading or contributing will be able to know how methods are implemented without having ...

2

Time will tell if your question is considered a worthy Code Review question, but till then I'ld like you to give a short review on your code nevertheless. Format function You could reduce the code duplication in the format function and make use of .format or f-strings (from Python 3.6 onwards). def sizeof_fmt_rev(num, suffix='B'): for unit in ['', 'Ki'...

Top 50 recent answers are included