41

There are a few good practices that will help this code out. Close the HTML tags that you open - generate valid HTML Newer browsers will probably render your content properly, but older browsers may not. You may also get strange formatting errors that are hard to debug. Save yourself the headache and always close your tags in the reverse order that you ...


34

Before dealing with your question, I'd like to point out that you probably want to use empty() instead of comparing with an empty string and that if you really want to compare to an empty string, use the === operator. Anyway, default initialization is a real problem in many languages. C# even has a ?? operator to solve this kind of issue! So, let's try to ...


27

An argument against table-style alignment in code Except when the editor/IDE helps maintain alignment with little work from the programmer, and all those who work with the code have that same facility, horizontal, table-like alignment in code is more trouble than it's worth, for these reasons: Search/replace operations on the entire code-base (as when ...


26

On top of the comments already given : Keep things simple : while (playAgain == true) can be written while (playAgain) if (A) variable=false; else variable=true; can be written variable = !A; Do not repeat yourself : if you deal a lot with user input and checking values, make it a function you can reuse. You do not need static members. Local variable can ...


23

# defining possible lists Don't write comments that describe what the code does; we can see the code, we know what it's doing. If you ever need comments, it's where it's otherwise unclear why the code's doing it. lists = ['cars','favorite foods','family members'] Per the style guide, you should put spaces after the commas: lists = ['cars', 'favorite ...


22

function foobar(n) { var zeroes = "000000"; return zeroes.substr(n.length) + n.toString(); }


19

Some brief comments: Your variable names are quite short (often one letter), which makes the code harder to follow. Longer and more descriptive variable names would make it easier to read and debug (e.g., trg to triangle, r1 to row1). The input to the function printTrg is rows, but we also have variables like r, r1 and r2 which look like rows. Perhaps this ...


19

First of all, the parenthesis around LocalDateTime.now() are useless. Now, let's see what we're really doing here: return LocalDateTime.now().format(DateTimeFormatter.ofPattern(format)); We're creating a unique LocalDateTime each time, but I am assuming that you always use the same format, and therefore the DateTimeFormatter.ofPattern(format) object will ...


18

From here: Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise. So could you write?: $newsItems[0]['image_url'] = $newsItems[0]['image_url'] ?: '/img/cat_placeholder.jpg';


18

That's a big mess of conditionals. Hopefully they all match the comments near them, though given typical code that's likely not the case. You are repeating a lot of HTML code that could be addressed using variables as @V31 demonstrated. If this is the entirety of the code I would probably stop there, but as this system grows you may want to generalize the ...


17

Yes there is: Date Formatting var dateString = DateTime.Now.ToString("yyyyMMdd");


17

Overall it looks pretty good. A few pointers though... Try to indent your code properly. Most of it looks good but on a few places you have missed an indentation. e.g., do { setMax(); setObjective(); userGuess(); playAgain(); } while (playAgain == true); Don't consume nor fail silently on exceptions (except in some rare cases but that's out of scope here) ...


17

General Now that you have such neat postings, the answers are going to need to be neater too. GUI Bugs When I run the GUI, it does not let me select directories from the File Browser. It also starts in the 'Documents' directory, and it would be better to do one of two things: start in the current directory start in the last directory used (use java.util....


16

I would encourage you to produce more explicit output, particularly with the filenames. If I wanted to reverse the process and scrape the code into files on my machine, using a Python script such as the following… import json from lxml import html import re import requests FILENAME_HINT_XPATH = "../preceding-sibling::p[1]/strong/text()" def code_for_post(...


16

Don't reinvent the wheel, this is java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME. long time = System.currentTimeMillis(); ISO_LOCAL_DATE_TIME.format(Instant.ofEpochMilli(time).atZone(ZoneOffset.UTC)) // "2017-03-07T12:09:04.374"


15

Key Points Each Case block implements formatting functionality for a specific format specifier. Goto statements indicate the function wants to be broken down into several smaller functions. Local variables such as alignmentSpecifier, alignmentPadding, precisionString, precisionSpecifier, formatSpecifier and all others, could all be eliminated if there was a ...


15

What you want to do, is use early-returns (FTW). Also, let the formatter do the string work for you. Right tool for the job. Consider your code, rewritten in Ideone...: static string FormatRushTime (TimeSpan span) { if ( span.Days != 0 ) { return String.Format("{0:d}d {1:d}h", span.Days, Math.Abs(span.Hours)); } if ( span.Hours != 0 ...


15

I see some things that could help you improve your program. Learn a recent version of C I still have my K&R first edition from 1978, however it would be a very poor choice to use for learning C today. The version of C it teaches has been obsolete for decades. I don't know which edition you're using (looks like a more recent one by the style of code) ...


15

First of all, in my opinion, both the algorithm is nice and interesting, and the code is really well-written, broken down into easily understandable functions! Well done! The only addition that I can make are corner-cases (and their possible fixes) for some of the helper functions. However, please note, that from the main entry point, I did not find any way ...


15

Thanks for editing the question. I have few ideas how you might want to polish your code. std::string base class It caught my eye that JSONPretify is derived from std::string. Generaly speaking STL containers are not meant for such use case. Specificaly it is a good idea if you are deriving from a base class to check that it has virtual destructor. std::...


15

You can single-quote the 'T' to have it accepted as a String literal private final static String DATE_FORMAT = "yyyy-MM-dd'T'hh:mm:ss.SSS";


14

The first if block can be narrowed by using a color variable; something like this: var color = "red"; // if the person has swiped off but not back on and one hour has elapsed if (Person.DifHours > 1 && (Person.ON === false || Person.OUT !==false)) { color = "red"; // If the person has swiped back on from ...


14

The code you have works, and is very nicely-formatted. There are only two things I would change, stylistically speaking: Explicitly pass input parameters by value. This should have been the default, but it wasn't in classic COM-based VB, so you need to write it out. ByRef is actually a perverse choice for the default, since it is almost never what you want....


13

Lo and behold, the power of PHP 7's null coalesce operator! It is also called the isset ternary operator, for obvious reasons. Here's a passage from php.net's wiki page about it Proposal The coalesce, or ??, operator is added, which returns the result of its first operand if it exists and is not NULL, or else its second operand. This means the $_GET[...


13

It's much more important to convey the code faithfully than to save a few bytes of output by replacing spaces with tabs: line = line.replaceAll(" ", "\t"); // replace four spaces with tabs, since that takes less space In fact, since you used your program to produce this question itself, I suspect that your tool is responsible for introducing the bug in ...


13

Your code looks more complicated than necessary and some of your special cases are really not needed. I will use an incremental development to avoid unnecessary code: The first version is just based on the formula, with no additional prettiness: function printRadians(angle) { return angle + "π" + "/" + 180; } document.write([45, 90, 180, 270, 360, ...


13

Naming Interfaces are usually named starting with a capital I in .NET. ScientificFormatter Here you have public string Format(BigInteger number) and private string FormatNumberString(string numberString) where the second would be easier to read if you would use method overloading. Method overloading is just that you can have a method with the same name ...


12

An opinion question, always tricky. From a DRY perspective, your code should be usable to generate documentation, like 'what use cases does this routine support' or 'What are the mapping values between y and z'. So I definitely align my tables, however I also tend to align the comma's and I tend to align 2 spaces in, not all the way to the right of var. ...


Only top voted, non community-wiki answers of a minimum length are eligible