Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
sparkyb
Posts: 1091
Joined: Thu Sep 06, 2007 7:30 pm UTC
Location: Camberville proper!
Contact:

Re: Coding: Fleeting Thoughts

Postby sparkyb » Mon Jul 08, 2013 1:07 pm UTC

I just saved you 48 bytes. Now only 107 to go. Good luck.

User avatar
Link
Posts: 1419
Joined: Sat Mar 07, 2009 11:33 am UTC
Location: ᘝᓄᘈᖉᐣ
Contact:

Re: Coding: Fleeting Thoughts

Postby Link » Mon Jul 08, 2013 6:28 pm UTC

I've got it down to 2020 bytes now, even without an optimised divmod10 routine! (It turns out -mcall-prologues, which was previously beneficial, actually increased the binary size after some restructuring.)

Now let's see if I can shave off another 136 bytes so I can re-add a feature which I've had to remove. :)

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Coding: Fleeting Thoughts

Postby jareds » Mon Jul 08, 2013 10:39 pm UTC

Link wrote:
PM 2Ring wrote:
Link wrote:As for the remaining 155 107 bytes, if I can find or invent a space-optimised uint32_t divmod10 routine, I'm probably done. ;)

Well, there's some pretty slick divide by 10 code in this thread, courtesy of jareds, although it's for uint16.

I might be able to turn that into something I can use (my brain doesn't quite work at this moment, so maybe later!), but I also need the modulo 10. Essentially, what I'm trying to implement is this:

Code: Select all

__inline__ uint8_t divmod10 (uint32_t *x)
{
   uint8_t y = *x % 10;
   *x /= 10;
   return y;
}

That causes the generic routine __udivmodsi4 to be pulled in, though, and I was hoping to replace it with something shorter.

I've already cut the binary down to 2116 bytes, so it's only 6% 3.5% too large now. Almost there!

My divide-by-10 code works for 32 bits just by extending it:

Code: Select all

uint32_t div10(uint32_t x)
{
    uint32_t q, a;
    q = a = (x>>1) + (x>>2) + 1;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    q >>= 4;
    q += a;
    return q >> 3;
}
Although at this point you should use a loop especially if you are going for size. To get the remainder, you just multiply and subtract, and you already posted that you can efficiently multiply by 10, so your joint routine would be:

Code: Select all

uint8_t divmod10 (uint32_t *x)
{
    uint32_t q = div10(*x);
    uint8_t r = *x - mul10slow(q);
    *x = q;
    return r;
}

User avatar
Link
Posts: 1419
Joined: Sat Mar 07, 2009 11:33 am UTC
Location: ᘝᓄᘈᖉᐣ
Contact:

Re: Coding: Fleeting Thoughts

Postby Link » Tue Jul 09, 2013 8:48 am UTC

jareds wrote:My divide-by-10 code works for 32 bits just by extending it:
[...]
Although at this point you should use a loop especially if you are going for size. To get the remainder, you just multiply and subtract, and you already posted that you can efficiently multiply by 10, so your joint routine would be:
[...]

Slightly modifying that gives me this:

Code: Select all

uint8_t divmod10 (uint32_t *x)
{
   uint32_t q, a;
   uint8_t r;
   uint8_t i = 8;
   q = a = (*x >> 1) + (*x >> 2) + 1;
   do
   {
      q >>= 4;
      q += a;
   }
   while (--i);
   q >>= 3;
   a = q;
   mul10 (&a);
   r = *x - a;
   *x = q;

   return r;
}

Unfortunately, it gets a whopping 78 bytes larger if I use that, even with a loop. I don't think I'll be able to reduce it that much, so it looks like I'll have to make do with GCC's __udivmodsi4. But since I've already been able to reduce the binary's size to the point where it'll fit on the microcontroller, it's not that big a deal any more. Thanks, though!

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 09, 2013 10:54 am UTC

So this is in our codebase.

Code: Select all

 <div class="triangle" <asp:Literal ID="litFailTriangle" runat="server"></asp:Literal> style="width:465px">

Is that even legal?
I think it's to manipulate some javascript later on.
litFailTriangle has its text property changed in code somewhere.
Image

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Jul 09, 2013 12:19 pm UTC

I've never used ASP, but I've been forced to use JSP on occasion and I think it's pretty similar... and the short version is: yes, it's legal.

The long version: These languages try to embed their serverside code within the HTML you write, so you can easily blur the line between what's running serverside and what's running clientside... because someone actually thought this was a good idea. So you can have

Code: Select all

<sometag><asp:sometag /></sometag>
and the processor will recognise that "<sometag>" is a client-side tag that needs to be sent verbatim in the HTML, while "<asp:sometag>" is a server-side tag that needs to be processed while it's generating the HTML page. And so what will be sent to the browser will be along the lines of

Code: Select all

<sometag>some replacement</sometag>
But the thing you then realise is that the sever doesn't actually care about the "<sometag>" and its meaning... as far as the server's concerned it's just a less than sign, some letters, and a greater than sign. So there's nothing server-side that's enforcing it to actually behave like HTML. And so you end up with the situation that while you can do the usual:

Code: Select all

<h1><%= header_text %></h1>
you can also do the nested:

Code: Select all

<img src="<%= image_uri %>">
or even the absurd:

Code: Select all

<<%= "div" %> class="stupid" <%= greater_than_sign %>
because the server only cares about the bits it recognises, and the rest is just text.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 09, 2013 12:35 pm UTC

Well, I guess that's ok then.
I wasn't certain whether or not it was just a quirk of how it was all implemented or if it's guaranteed by design.

Can I still think it's a terrible idea?
Image

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Tue Jul 09, 2013 2:25 pm UTC

How is that different from PHP?

(Not meaning to say that PHP is sensible. Just asking.)
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 09, 2013 7:14 pm UTC

I have a little open source project I'm writing. Thanks to how horrible all existing crypto libraries are, I have dependencies on two crypto libraries.

I really want to fork gnutls and turn it into a decent crypto library (removing the protocols and stripping down to the algorithms).
Summum ius, summa iniuria.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 09, 2013 10:06 pm UTC

I think I want to make a programming language in which you can name entry points whatever you want, and have multiple of them and shit. The entry point becomes the default name of the executable. Also, sub entry points. Examples:

Code: Select all

[EntryPoint: foo]
void foo(string params[]) {
    return;
}

[EntryPoint: foo.bar]
void bar(string params[]) {
    return;
}

[EntryPoint: baz]
void bar(string params[]) {
    return;
}


Then it creates two executables, foo and baz. The main function in baz is baz, but foo would have three "main" functions. The real main function would inspect the parameters and then call either foo or bar depending on whether your call to the command line was "$ foo" or "$ foo bar"

Anyway, just a thought.
Summum ius, summa iniuria.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Jul 09, 2013 11:32 pm UTC

If I rename the executable, does it fail to run?
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 09, 2013 11:40 pm UTC

That's OS specific, but I'm not aware of any operating systems in which an executable fails to run under a different name.

This is an incredibly useless feature for existing languages, but I'm just thinking of a plan I had in the past for a language framework that didn't require build config.
Summum ius, summa iniuria.

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Wed Jul 10, 2013 12:09 am UTC

Thesh wrote:That's OS specific, but I'm not aware of any operating systems in which an executable fails to run under a different name.
In Unix, at least, the name of the executable (or the symbolic link that refers to it) is passed as the zeroth command-line argument, and some programs behave differently depending on this argument. For instance, /usr/bin/view is a symbolic link to /usr/bin/vim, but running view starts vim in read-only mode. One possible implementation for the multiple-entry-points thing would be to compile your example to something like

Code: Select all

if(argv[0] == "foo")
    if(argv[1] == "bar")
        bar(...)
    else
        foo(...)
else if(argv[0] == "baz")
    baz(...)
in which case it'll fail to run if the executable is renamed (unless there's a default entry point). (Another option would be to generate multiple executables with different code, in which case renaming wouldn't change anything.)
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jul 10, 2013 12:18 am UTC

Multiple executable is what I had in mind. But yeah, the first if is pretty much what I had in mind for the commands.
Summum ius, summa iniuria.

User avatar
Aaeriele
Posts: 2127
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Wed Jul 10, 2013 2:58 am UTC

Thesh wrote:I think I want to make a programming language in which you can name entry points whatever you want, and have multiple of them and shit. The entry point becomes the default name of the executable. Also, sub entry points. Examples:

Code: Select all

[EntryPoint: foo]
void foo(string params[]) {
    return;
}

[EntryPoint: foo.bar]
void bar(string params[]) {
    return;
}

[EntryPoint: baz]
void bar(string params[]) {
    return;
}


Then it creates two executables, foo and baz. The main function in baz is baz, but foo would have three "main" functions. The real main function would inspect the parameters and then call either foo or bar depending on whether your call to the command line was "$ foo" or "$ foo bar"

Anyway, just a thought.


Alternatively, you could just have an executable that takes its first argument and calls the corresponding entry point, and then have shell scripts that provide the first argument if you want different "binaries".
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Wed Jul 10, 2013 7:48 am UTC

Jplus wrote:How is that different from PHP?

PHP only has the special <?php code ?> tags, which is visibly distinct from any actual HTML content because of the question marks. While you do still sometimes get escaping problems trying to embed code in-band like this, particularly with <?xml ?> declarations, those can be worked around thanks to being (a) uncommon, so it's not too bad to have to handle them occasionally, and (b) entirely avoidable if you turn short_tags off (which I really should get in the habit of doing).

ASP/JSP/etc have <% code %> tags, which behave in much the same way, except they also have <tl:sometag />, for "special" server-side tags from various tag libraries, which you can also embed in the code. And these are much less easy to quickly recognise, and make the parsing rules for the language much more complicated - a less than sign followed by a qname could be the start of a preprocessor tag, or just literal text to send to the browser, depending only on whether that qname is recognised. And you can end up with weirdly-nested tags like Xeno's original example because you're actually mixing and matching two different languages that happen to use identical syntax.

I'm OK with the preprocessor model for serverside code in web pages... even if they do blur the line between code and data to an uncomfortable level. But languages where the preprocessor code and the real code aren't easy to separate at a glance, because they're using identical syntax? That's just crazy-pants.

chridd wrote:In Unix, at least, the name of the executable (or the symbolic link that refers to it) is passed as the zeroth command-line argument, and some programs behave differently depending on this argument.

Biggest example of this that I know if is BusyBox, which is designed to be pretty much an entire core system... you can take the one binary file and link it as /bin/sh, /bin/ls, /bin/cp, and a heap of others, and it'll run appropriately.

I don't know if there's anything similar for Windows... you can find out the filename of the exe you're running, but not the shortcut it was launched from (so a BusyBox clone would have to be copied to all those different names, not just linked). On the other hand, NTFS supports proper symlinks (as opposed to shortcut files) to some extent, though the shell doesn't support them very well... I don't know how GetModuleFileName would react if you have a bunch of names symlinked to the same exe.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Wed Jul 10, 2013 8:10 am UTC

phlip wrote:ASP/JSP/etc have <% code %> tags, which behave in much the same way, except they also have <tl:sometag />, for "special" server-side tags from various tag libraries, which you can also embed in the code. And these are much less easy to quickly recognise, and make the parsing rules for the language much more complicated - a less than sign followed by a qname could be the start of a preprocessor tag, or just literal text to send to the browser, depending only on whether that qname is recognised. And you can end up with weirdly-nested tags like Xeno's original example because you're actually mixing and matching two different languages that happen to use identical syntax.


Something like:
<div <%= some_server_side_variable %>> ...</div> I'd be absolutely fine with; it's pretty clear what the intent is. It wouldn't be how I'd achieve what it was being used for, but that's not the point.
<div <asp:tag ...></asp:tag>...</div> just looks wrong; I have to check the code in a couple of places to see that this isn't wrong. At first glance, it's simply a "someone forgot '>' on that div tag".
Image

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Wed Jul 10, 2013 8:37 am UTC

Thesh wrote:I think I want to make a programming language in which you can name entry points whatever you want, and have multiple of them and shit. The entry point becomes the default name of the executable. Also, sub entry points.

What if you define a constant (or a preprocessor alias) to hold a name and jump to the corresponding label? Then you could use a script to replace the constant and build the program with the corresponding name. (That way if you optimized it, it should remove the unreachable code entirely.) eg.:

file: entrypts.c

Code: Select all

#define ENTRY_POINT foo_bar
#include "stdio.h"

int main(int argc, char *argv[])
{
    typedef enum {foo, foo_bar, baz} entry;
   
    switch (ENTRY_POINT) {
    case foo:
        printf("foo\n");
        break;
    case foo_bar:
        printf("foo.bar\n");
        break;
    case baz:
        printf("baz\n");
        break;
    }
   
    return 0;
}


file: mkentrypts

Code: Select all

#!/usr/bin/env bash

TEMPFILE=$(mktemp)
sed "1s/.*/#define ENTRY_POINT $1/" entrypts.c > TEMPFILE
gcc -o "$1" tmp.c    # substitute build instruction

If you wanted to generate all possible executables, you could have another script to scrape the names and generate them. And if you wanted real sub-entry-points (rather than just a naming convention), you could use arrays of enums.

phlip wrote:Biggest example of this that I know if is BusyBox, which is designed to be pretty much an entire core system... you can take the one binary file and link it as /bin/sh, /bin/ls, /bin/cp, and a heap of others, and it'll run appropriately.

I didn't know BusyBox was a single file! I thought it was just a minimal implementation of the most important unix utilities. Why do they do it like this? Do they want to avoid dynamic linking for some reason and do this to save space?

User avatar
Snark
Posts: 425
Joined: Mon Feb 27, 2012 3:22 pm UTC

Re: Coding: Fleeting Thoughts

Postby Snark » Sat Jul 13, 2013 10:15 pm UTC

Java by way of Eclipse question, common enough to not warrant it's own topic:

I'm trying to import project. Get error: Project 'X' is missing required source folder: 'src'

Stackoverflow is not being helpful.

Edit a day later: Solved!
Dashboard Confessional wrote:I want to give you whatever you need. What is it you need? Is it within me?


Avatar by Matt

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Thu Jul 18, 2013 1:04 pm UTC

I constantly have colleagues endorsing my "subversion"-skill on linkedin. Feels like any moment now the government will contact me with a job offer in espionage.

Snark wrote:Solved!


Don't be that guy.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Snark
Posts: 425
Joined: Mon Feb 27, 2012 3:22 pm UTC

Re: Coding: Fleeting Thoughts

Postby Snark » Thu Jul 18, 2013 2:32 pm UTC

It was someone else's code, and they solved it. I'm afraid that's all the wisdom this ancient has to pass down.
Dashboard Confessional wrote:I want to give you whatever you need. What is it you need? Is it within me?


Avatar by Matt

Markus__1
Posts: 36
Joined: Wed Apr 20, 2011 6:42 pm UTC

Re: Coding: Fleeting Thoughts

Postby Markus__1 » Mon Jul 22, 2013 10:32 pm UTC

Snark wrote:Java by way of Eclipse question, common enough to not warrant it's own topic:

I'm trying to import project. Get error: Project 'X' is missing required source folder: 'src'

Stackoverflow is not being helpful.

Edit a day later: Solved!


You, sir, name? wrote:
Snark wrote:Solved!

Don't be that guy.


Well, if it needs a 'src' folder, give it one - make one where it wants it.

How it came to lose its src folder: some tool pruned empty folders (archivers like to do that), and there was nothing yet in that directory.

But yes, in general, ask whoever gave you that project - maybe they forgotten some files.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 30, 2013 4:52 pm UTC

Why do most people (including myself) write

Code: Select all

doubleResult = anInteger / (double) anotherInteger;
and not

Code: Select all

doubleResult = (double) anInteger / anotherInteger;


The two are equivalent statements, and the latter is (IMO) much easier to read.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
cemper93
Posts: 209
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Coding: Fleeting Thoughts

Postby cemper93 » Tue Jul 30, 2013 5:08 pm UTC

Because it might be confused with

Code: Select all

doubleResult = (double)(anInteger / anotherInteger)

If you put the cast on the second operand, the difference is much easier to see (without worrying about operator precedence).

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6598
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 30, 2013 5:10 pm UTC

Order of operations is more clear in the first one. The first definitely says that you do the cast first and then convert to a double, the second one you might have to think "Wait, does casting come before division or after division?"
Summum ius, summa iniuria.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 30, 2013 5:13 pm UTC

Thesh wrote:Order of operations is more clear in the first one. The first definitely says that you do the cast first and then convert to a double, the second one you might have to think "Wait, does casting come before division or after division?"


Shouldn't people know the basic grammar rules of the language they program in?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1456
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 30, 2013 5:20 pm UTC

You're assuming it's being read and analysed in vacuum. This is almost certainly not the case.
When the difference between "(double) (a / b)" and "(double) a / b" is so minor, particularly when the names for a and b are long, you've added another layer of complexity that wasn't necessary and will only serve to trip someone up later on.

Different things should look different.
Image

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Tue Jul 30, 2013 5:53 pm UTC

You, sir, name? wrote:
Thesh wrote:Order of operations is more clear in the first one. The first definitely says that you do the cast first and then convert to a double, the second one you might have to think "Wait, does casting come before division or after division?"


Shouldn't people know the basic grammar rules of the language they program in?

Quick, what will cout << hex << 0xAA & 0x33; print?

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 30, 2013 6:31 pm UTC

EvanED wrote:
You, sir, name? wrote:
Thesh wrote:Order of operations is more clear in the first one. The first definitely says that you do the cast first and then convert to a double, the second one you might have to think "Wait, does casting come before division or after division?"


Shouldn't people know the basic grammar rules of the language they program in?

Quick, what will cout << hex << 0xAA & 0x33; print?


Well, I don't really C++ very often anymore. But if it's anything like C, it should evaluate like ((cout << hex) << 0xAA) & 0x33;

So it would print 0xAA and return a char* pointer into nonsense or something?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Jul 30, 2013 6:45 pm UTC

You, sir, name? wrote:Shouldn't people know the basic grammar rules of the language they program in?
I still don't know the order of && and || without looking it up.

I just always use parens and spacing, because it's easier to read anyway.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 30, 2013 7:09 pm UTC

Xeio wrote:
You, sir, name? wrote:Shouldn't people know the basic grammar rules of the language they program in?
I still don't know the order of && and || without looking it up.

I just always use parens and spacing, because it's easier to read anyway.


|| and && can be implemented with addition and multiplication, and share their relative order of precedence. That's how I remember their order anyway.

Although I'm also perfectly fine with using parens when expressions get really hariy. You shouldn't have to sit and draw up a parsing tree just to make sense of your expression.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Jul 30, 2013 7:52 pm UTC

You, sir, name? wrote:|| and && can be implemented with addition and multiplication
*hissssssssss* Numerical booleans.
You, sir, name? wrote:Although I'm also perfectly fine with using parens when expressions get really hariy. You shouldn't have to sit and draw up a parsing tree just to make sense of your expression.
Yea, I prefer to break out large expressions into smaller components. If you're over 2 nested levels of parens in an if statement, it's probably refactor time.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Tue Jul 30, 2013 8:51 pm UTC

You, sir, name? wrote:Why do most people (including myself) write

Code: Select all

doubleResult = anInteger / (double) anotherInteger;
and not

Code: Select all

doubleResult = (double) anInteger / anotherInteger;


The two are equivalent statements, and the latter is (IMO) much easier to read.


Code: Select all

-- *puts on hipster glasses*
doubleResult :: Double
doubleResult = fromInteger anInteger / fromInteger anotherInteger
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Jul 30, 2013 9:49 pm UTC

You, sir, name? wrote:Why do most people (including myself) write

Code: Select all

doubleResult = anInteger / (double) anotherInteger;
and not

Code: Select all

doubleResult = (double) anInteger / anotherInteger;


The two are equivalent statements, and the latter is (IMO) much easier to read.

Bah, do neither: C style casts are overly powerful. (Although, C-style casts of doubles are surprisingly weak. Slacker doesn't even work on pointers!)

Code: Select all

template<typename U, typename T>
U as( T&& t ) {
  return std::forward<T>(t):
}
doubleResult = anInteger / as<double>(anotherInteger);
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Tue Jul 30, 2013 9:52 pm UTC

Yakk wrote:
You, sir, name? wrote:Why do most people (including myself) write

Code: Select all

doubleResult = anInteger / (double) anotherInteger;
and not

Code: Select all

doubleResult = (double) anInteger / anotherInteger;


The two are equivalent statements, and the latter is (IMO) much easier to read.

Bah, do neither: C style casts are overly powerful. (Although, C-style casts of doubles are surprisingly weak. Slacker doesn't even work on pointers!)

Code: Select all

template<typename U, typename T>
U as( T&& t ) {
  return std::forward<T>(t):
}
doubleResult = anInteger / as<double>(anotherInteger);


In C++, can't you just

Code: Select all

doubleResult = anInteger / double(anotherInteger);


? I seem to vaguely remember such a construct anyway.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
cemper93
Posts: 209
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Coding: Fleeting Thoughts

Postby cemper93 » Tue Jul 30, 2013 9:53 pm UTC

@headprogrammingczar: What are you getting at? I don't find the Haskell any easier to read. I don't even think it's more explicit, mostly because "fromIntegral" is the most terrible magic ever.

@Yakk: Nice. I'm not sure it's a good idea, but nice.

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Coding: Fleeting Thoughts

Postby jareds » Wed Jul 31, 2013 1:09 am UTC

You, sir, name? wrote:In C++, can't you just

Code: Select all

doubleResult = anInteger / double(anotherInteger);


? I seem to vaguely remember such a construct anyway.

You can, but if "T(exp)" is a functional cast of one argument, it is semantically equivalent to "(T)(exp)". Yakk's point is that it's risky to be in the habit of using C-style casts (and, by extension, functional casts of one argument), because they are so powerful. If you use the C++ style casts (static_cast, const_cast, dynamic_cast, reinterpret_cast), you have a better idea of which casts are safe by a local inspection of the code.

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Thu Aug 01, 2013 12:44 am UTC

I don't think using C++ style casts for scalar types is necessary. static_cast<...> is much more verbose without adding any information. Using reinterpret_cast to cast between pointers and integers can be helpful though.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11129
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Aug 01, 2013 1:42 am UTC

C style casts do a static_cast, and if that fails, does a reinterpret_cast.

Every C style cast should be viewed with as much trepidation as a reinterpret_cast. You can feel better after ensuring that it actually reduces to a static_cast, until you page away, come back, and are no longer absolutely certain.

Or you can replace it with an `as` cast, or a `static_cast` if `as` is too weak, and only use a `reinterpret_cast` if you actually need it.

Now your casts stick out, and you won't (through a typo) do arithmetic on pointers with the compiler not warning you a tad, because you cast a pointer-to-double to an int and it went and read the pointer as an int. Or you changed a double to a pointer, found all but 1 use which used a c-style cast, which silently started going random stuff.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
skeptical scientist
closed-minded spiritualist
Posts: 6142
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: San Francisco

Re: Coding: Fleeting Thoughts

Postby skeptical scientist » Thu Aug 01, 2013 2:59 am UTC

Dynamic programming in python how-to:
  • Step 1: write a recursive function
  • Step 2: @memoize it
  • Step 3: profit
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests