code-elegance

using tab or spaces (indentation for code)

Duplicate of Are spaces preferred over tabs for indentation and almost certainly others For a long time the coding rules and guidelines in my company recommended usage of tabs (of size 4) over spaces (to save valuable storage space), initially I found it tough when I started working for my company, in the long run I got used to it. Now ...

What is the most elegant way to do "foreach x except y" in PHP?

I want to do something like this: foreach ($array as $key=>$value except when $key="id") { // whatever } ... without having to put an "if" clause inside the body of the loop. It is not guaranteed that "id" will the be the first or last element in the array, and I don't really want to unset or slice the array, because that will be expe...

chop unused decimals with javascript

I've got a currency input and need to return only significant digits. The input always has two decimal places, so: 4.00 -> 4 4.10 -> 4.1 4.01 -> 4.01 Here's how I'm currently doing it: // chop off unnecessary decimals if (val.charAt(val.length-1) == '0') { // xx.00 val = val.substr(0, val.length-1); } if (val.charAt(val.length...

If we create code which isn't elegant, have we failed?

The question of 'what is elegance?' has been asked before, and rather than arguing about that, I think we could probably all agree we know it when we see it. But I'd like to know is elegance a goal of programming (an essential aim), or merely a desirable side-effect? (or technique) If we create code which isn't elegant, have we failed...

Elegant code question: How to avoid creating unneeded object

The root of my question is that the C# compiler is too smart. It detects a path via which an object could be undefined, so demands that I fill it. In the code, I look at the tables in a DataSet to see if there is one that I want. If not, I create a new one. I know that dtOut will always be assigned a value, but the the compiler is not ha...

Tips for more elegant code with monads?

Hi. I finally got a hold on how to use monads (don't know if I understand them...), but my code is never very elegant. I guess is from a lack of grip on how all those functions on Control.Monad can really help. So I'd thought it would be nice to ask for tips on this in a particular piece of code using the state monad. The goal of the co...

Efficient way to read a specific line number of a file. (BONUS: Python Manual Misprint)

I have a 100 GB text file, which is a BCP dump from a database. When I try to import it with BULK INSERT, I get a cryptic error on line number 219506324. Before solving this issue I would like to see this line, but alas my favorite method of import linecache print linecache.getline(filename, linenumber) is throwing a MemoryError. Inte...